Skip to contentSkip to conversation

Introduce MediaTek frequency hopping driver

RFC PATCH In review Johnson Wang 6 participants 18 messages Jun 12, 2022 – Jul 28, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Introduce MediaTek frequency hopping driver

This serie aims to add support for MediaTek frequency hopping and spread spectrum clocking (SSC) control on MT8186 SoC.

Johnson Wang (2):
dt-bindings: arm: mediatek: Add new bindings of MediaTek frequency
hopping
clk: mediatek: Add frequency hopping support

 .../bindings/arm/mediatek/mediatek,fhctl.yaml | 149 ++++++++
 drivers/clk/mediatek/Kconfig                  |   8 +
 drivers/clk/mediatek/Makefile                 |   2 +
 drivers/clk/mediatek/clk-fhctl-ap.c           | 347 ++++++++++++++++++
 drivers/clk/mediatek/clk-fhctl-pll.c          | 209 +++++++++++
 drivers/clk/mediatek/clk-fhctl-pll.h          |  74 ++++
 drivers/clk/mediatek/clk-fhctl-util.h         |  24 ++
 drivers/clk/mediatek/clk-fhctl.c              | 191 ++++++++++
 drivers/clk/mediatek/clk-fhctl.h              |  45 +++
 drivers/clk/mediatek/clk-pll.c                |   5 +-
 drivers/clk/mediatek/clk-pll.h                |   5 +
 11 files changed, 1058 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml
 create mode 100644 drivers/clk/mediatek/clk-fhctl-ap.c
 create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.c
 create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.h
 create mode 100644 drivers/clk/mediatek/clk-fhctl-util.h
 create mode 100644 drivers/clk/mediatek/clk-fhctl.c
 create mode 100644 drivers/clk/mediatek/clk-fhctl.h
Signature · 1 line
2.18.0
#1 lore raw

RFC PATCH 1/2 dt-bindings: arm: mediatek: Add new bindings of MediaTek frequency hopping

This patch adds the new binding documentation for frequency hopping and spread spectrum clocking control on MT8186.

---
 .../bindings/arm/mediatek/mediatek,fhctl.yaml | 149 ++++++++++++++++++
 1 file changed, 149 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +149 -0
new file mode 100644
index 000000000000..9cb04bed9e86
@@ -0,0 +1,149 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/arm/mediatek/mediatek,fhctl.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek frequency hopping and spread spectrum clocking control
+
+maintainers:
+  - Edward-JW Yang <[email protected]>
+
+description: |
+  Frequency hopping control (FHCTL) is a piece of hardware that control
+  some PLLs to adopt "hopping" mechanism to adjust their frequency.
+  Spread spectrum clocking (SSC) is another function provided by this hardware.
+
+properties:
+  compatible:
+    const: mediatek,mt8186-fhctl
+
+  reg:
+    maxItems: 2
+
+patternProperties:
+  "^map[0-9]$":
+    type: object
+    description:
+      Represents a group of PLLs controlled by the same domain and method.
+
+    properties:
+      domain:
+        description: The position of this hardware block in the chip.
+        $ref: /schemas/types.yaml#/definitions/string-array
+
+      method:
+        description: Determine which method to control this FHCTL hardware.
+        $ref: /schemas/types.yaml#/definitions/string-array
+
+    patternProperties:
+      "^armpll_(ll|bl)|(cci|main|mm|tvd|m|adsp|mfg|nna|nna2|msdc)pll$":
+        type: object
+        description:
+          A subnode represents one PLL clock. The properties it contains are
+          used to determine whether this PLL enable frequency hopping mechanism           and spread spectrum clocking (SSC) control.
+
+        properties:
+          fh-id:
+            description: The index of the PLL clocks.
+            $ref: /schemas/types.yaml#/definitions/uint32
+
+          perms:
+            description:
+              This is a bit field used to manage the permission of frequency
+              hopping function and spread spectrum clocking control.
+            $ref: /schemas/types.yaml#/definitions/uint32
+
+          ssc-rate:
+            description: The percentage of the spectrum spreading.
+            $ref: /schemas/types.yaml#/definitions/uint32
+            minimum: 0
+            maximum: 8
+
+        required:
+          - fh-id
+          - perms
+
+    required:
+      - domain
+      - method
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/mt8186-clk.h>
+    fhctl: fhctl@1000ce00 {
+        compatible = "mediatek,mt8186-fhctl";
+        reg = <0x1000ce00 0x200>,
+              <0x1000c000 0xe00>;
+        map0 {
+            domain = "top";
+            method = "fhctl-ap";
+
+            armpll_ll {
+                fh-id = <0>;
+                perms = <0x10>;
+            };
+
+            armpll_bl {
+                fh-id = <1>;
+                perms = <0x10>;
+            };
+
+            ccipll {
+                fh-id = <2>;
+                perms = <0x10>;
+            };
+
+            mainpll {
+                fh-id = <3>;
+                perms = <0x1c>;
+            };
+
+            mmpll {
+                fh-id = <4>;
+                perms = <0x1c>;
+            };
+
+            tvdpll {
+                fh-id = <5>;
+                perms = <0x1c>;
+            };
+
+            mpll {
+                fh-id = <6>;
+                perms = <0x10>;
+            };
+
+            adsppll {
+                fh-id = <7>;
+                perms = <0x1c>;
+            };
+
+            mfgpll {
+                fh-id = <8>;
+                perms = <0x1c>;
+            };
+
+            nnapll {
+                fh-id = <9>;
+                perms = <0x1c>;
+            };
+
+            nna2pll {
+                fh-id = <10>;
+                perms = <0x1c>;
+            };
+
+            msdcpll {
+                fh-id = <11>;
+                perms = <0x1c>;
+                ssc-rate = <3>;
+            };
+        };
+    };
Signature · 1 line
2.18.0
#4 lore raw

RFC PATCH 2/2 clk: mediatek: Add frequency hopping support

In reply to Johnson Wang

Add frequency hopping support and spread spectrum clocking control for MT8186.

---
 drivers/clk/mediatek/Kconfig          |   8 +
 drivers/clk/mediatek/Makefile         |   2 +
 drivers/clk/mediatek/clk-fhctl-ap.c   | 347 ++++++++++++++++++++++++++
 drivers/clk/mediatek/clk-fhctl-pll.c  | 209 ++++++++++++++++
 drivers/clk/mediatek/clk-fhctl-pll.h  |  74 ++++++
 drivers/clk/mediatek/clk-fhctl-util.h |  24 ++
 drivers/clk/mediatek/clk-fhctl.c      | 191 ++++++++++++++
 drivers/clk/mediatek/clk-fhctl.h      |  45 ++++
 drivers/clk/mediatek/clk-pll.c        |   5 +-
 drivers/clk/mediatek/clk-pll.h        |   5 +
 10 files changed, 909 insertions(+), 1 deletion(-)
 create mode 100644 drivers/clk/mediatek/clk-fhctl-ap.c
 create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.c
 create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.h
 create mode 100644 drivers/clk/mediatek/clk-fhctl-util.h
 create mode 100644 drivers/clk/mediatek/clk-fhctl.c
 create mode 100644 drivers/clk/mediatek/clk-fhctl.h
drivers/clk/mediatek/Kconfig +8 -0
index d5936cfb3bee..fd887c537a91 100644
@@ -622,4 +622,12 @@ config COMMON_CLK_MT8516_AUDSYS
 	help
 	  This driver supports MediaTek MT8516 audsys clocks.
 
+config COMMON_CLK_MTK_FREQ_HOPPING
+	tristate "MediaTek frequency hopping driver"
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	select COMMON_CLK_MEDIATEK
+	help
+	  This driver supports frequency hopping and spread spectrum clocking
+	  control for some MediaTek SoCs.
+
 endmenu
drivers/clk/mediatek/Makefile +2 -0
index caf2ce93d666..3c0e9bd3978b 100644
@@ -99,3 +99,5 @@ obj-$(CONFIG_COMMON_CLK_MT8195) += clk-mt8195-apmixedsys.o clk-mt8195-topckgen.o
 				   clk-mt8195-apusys_pll.o
 obj-$(CONFIG_COMMON_CLK_MT8516) += clk-mt8516.o
 obj-$(CONFIG_COMMON_CLK_MT8516_AUDSYS) += clk-mt8516-aud.o
+obj-$(CONFIG_COMMON_CLK_MTK_FREQ_HOPPING) += fhctl.o
+fhctl-objs += clk-fhctl.o clk-fhctl-ap.o clk-fhctl-pll.o
drivers/clk/mediatek/clk-fhctl-ap.c new file +347 -0

347 lines not on this page · raw message

drivers/clk/mediatek/clk-fhctl-pll.c new file +209 -0

209 lines not on this page · raw message

drivers/clk/mediatek/clk-fhctl-pll.h new file +74 -0

74 lines not on this page · raw message

drivers/clk/mediatek/clk-fhctl-util.h new file +24 -0
new file mode 100644
index 000000000000..824ed94b9f79
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020 MediaTek Inc.
+ * Author: Yu-Chang Wang <[email protected]>
+ */
+
+#ifndef __CLK_FHCTL_UTIL_H
+#define __CLK_FHCTL_UTIL_H
+
+#define fh_set_field(reg, field, val) \
+do { \
+	unsigned int tv = readl(reg); \
+	tv &= ~(field); \
+	tv |= ((val) << (ffs(field) - 1)); \
+	writel(tv, reg); \
+} while (0)
+
+#define fh_get_field(reg, field, val) \
+do { \
+	unsigned int tv = readl(reg); \
+	val = ((tv & (field)) >> (ffs(field) - 1)); \
+} while (0)
+
+#endif
drivers/clk/mediatek/clk-fhctl.c new file +191 -0

191 lines not on this page · raw message

drivers/clk/mediatek/clk-fhctl.h new file +45 -0

45 lines not on this page · raw message

drivers/clk/mediatek/clk-pll.c +4 -1
index 54e6cfd29dfc..1acd21ca4b93 100644
@@ -206,7 +206,10 @@ static int mtk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
 	u32 postdiv;
 
 	mtk_pll_calc_values(pll, &pcw, &postdiv, rate, parent_rate);
-	mtk_pll_set_rate_regs(pll, pcw, postdiv);
+#ifdef CONFIG_COMMON_CLK_MTK_FREQ_HOPPING
+	if (!mtk_clk_try_freq_hopping(pll->data->name, pcw, postdiv))
+#endif
+		mtk_pll_set_rate_regs(pll, pcw, postdiv);
 
 	return 0;
 }
drivers/clk/mediatek/clk-pll.h +5 -0
index fe3199715688..e95f5f48f308 100644
@@ -54,4 +54,9 @@ int mtk_clk_register_plls(struct device_node *node,
 void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
 			     struct clk_hw_onecell_data *clk_data);
 
+#ifdef CONFIG_COMMON_CLK_MTK_FREQ_HOPPING
+extern bool mtk_clk_try_freq_hopping(const char *pll_name, unsigned long dds,
+				     int postdiv);
+#endif
+
 #endif /* __DRV_CLK_MTK_PLL_H */
Signature · 1 line
2.18.0
maintainer
#2 lore raw

Re: [RFC PATCH 1/2] dt-bindings: arm: mediatek: Add new bindings of MediaTek frequency hopping

In reply to Johnson Wang
Quoted 10 lines · Johnson Wang

This patch adds the new binding documentation for frequency hopping and spread spectrum clocking control on MT8186.

---
 .../bindings/arm/mediatek/mediatek,fhctl.yaml | 149 ++++++++++++++++++
 1 file changed, 149 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:
./Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml:44:111: [warning] line too long (133 > 110 characters) (line-length)

dtschema/dtc warnings/errors:

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/patch/

This check can fail if there are any dependencies. The base for a patch series is generally the most recent rc1.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit.

#5 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

In reply to Johnson Wang

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

	PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

	    PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

In any case, to speed up development (I believe that transferring this in clk-pll means that the code will still be more or less the same), I've performed a review on the code; check below.

Quoted 24 lines
---
  drivers/clk/mediatek/Kconfig          |   8 +
  drivers/clk/mediatek/Makefile         |   2 +
  drivers/clk/mediatek/clk-fhctl-ap.c   | 347 ++++++++++++++++++++++++++
  drivers/clk/mediatek/clk-fhctl-pll.c  | 209 ++++++++++++++++
  drivers/clk/mediatek/clk-fhctl-pll.h  |  74 ++++++
  drivers/clk/mediatek/clk-fhctl-util.h |  24 ++
  drivers/clk/mediatek/clk-fhctl.c      | 191 ++++++++++++++
  drivers/clk/mediatek/clk-fhctl.h      |  45 ++++
  drivers/clk/mediatek/clk-pll.c        |   5 +-
  drivers/clk/mediatek/clk-pll.h        |   5 +
  10 files changed, 909 insertions(+), 1 deletion(-)

create mode 100644 drivers/clk/mediatek/clk-fhctl-ap.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.h create mode 100644 drivers/clk/mediatek/clk-fhctl-util.h create mode 100644 drivers/clk/mediatek/clk-fhctl.c create mode 100644 drivers/clk/mediatek/clk-fhctl.h

drivers/clk/mediatek/Kconfig +0 -0
index d5936cfb3bee..fd887c537a91 100644

drivers/clk/mediatek/Kconfig config COMMON_CLK_MT8516_AUDSYS · line 626

Quoted 5 lines
drivers/clk/mediatek/Kconfig +2 -0
@@ -622,4 +622,12 @@ config COMMON_CLK_MT8516_AUDSYS
  	help
  	  This driver supports MediaTek MT8516 audsys clocks.
 
+config COMMON_CLK_MTK_FREQ_HOPPING
+	tristate "MediaTek frequency hopping driver"

If this goes inside of clk-pll, this configuration option can be safely removed.

Quoted 57 lines
drivers/clk/mediatek/Kconfig +6 -0
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	select COMMON_CLK_MEDIATEK
+	help
+	  This driver supports frequency hopping and spread spectrum clocking
+	  control for some MediaTek SoCs.
+
  endmenu
drivers/clk/mediatek/Makefile +2 -0
index caf2ce93d666..3c0e9bd3978b 100644
@@ -99,3 +99,5 @@ obj-$(CONFIG_COMMON_CLK_MT8195) += clk-mt8195-apmixedsys.o clk-mt8195-topckgen.o
  				   clk-mt8195-apusys_pll.o
  obj-$(CONFIG_COMMON_CLK_MT8516) += clk-mt8516.o
  obj-$(CONFIG_COMMON_CLK_MT8516_AUDSYS) += clk-mt8516-aud.o
+obj-$(CONFIG_COMMON_CLK_MTK_FREQ_HOPPING) += fhctl.o
+fhctl-objs += clk-fhctl.o clk-fhctl-ap.o clk-fhctl-pll.o
drivers/clk/mediatek/clk-fhctl-ap.c new file +34 -0
new file mode 100644
index 000000000000..9e3226a9c1ca
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include "clk-fhctl.h"
+#include "clk-fhctl-pll.h"
+#include "clk-fhctl-util.h"
+
+#define FHCTL_TARGET FHCTL_AP
+
+#define PERCENT_TO_DDSLMT(dds, percent_m10) \
+	((((dds) * (percent_m10)) >> 5) / 100)
+
+struct fh_ap_match {
+	char *name;
+	struct fh_hdlr *hdlr;
+	int (*init)(struct pll_dts *array, struct fh_ap_match *match);
+};
+
+struct hdlr_data {
+	struct pll_dts *array;
+	struct fh_pll_domain *domain;
+	spinlock_t *lock;
+};
+

drivers/clk/mediatek/clk-fhctl-ap.c line 44

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+static int fhctl_set_ssc_regs(struct fh_pll_regs *regs,
+			      struct fh_pll_data *data,
+			      int fh_id, int rate)
+{
+	unsigned int updnlmt_val;
+
+	if (rate > 0) {
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+		fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);

Are all of these writes to be performed with a barrier?
Can't we use writel_relaxed() for some, with a "final" writel() where ordering *really* matters?

Also, at least these three field settings are common between (rate > 0) and (rate <= 0), so they can go outside of the conditional.

Quoted 44 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +44 -0
+
+		/* Set the relative parameter registers (dt/df/upbnd/downbnd) */
+		fh_set_field(regs->reg_cfg, data->msk_frddsx_dys, data->df_val);
+		fh_set_field(regs->reg_cfg, data->msk_frddsx_dts, data->dt_val);
+
+		writel((readl(regs->reg_con_pcw) & data->dds_mask) |
+			data->tgl_org, regs->reg_dds);
+
+		/* Calculate UPDNLMT */
+		updnlmt_val = PERCENT_TO_DDSLMT((readl(regs->reg_dds) &
+						 data->dds_mask), rate) <<
+						 data->updnlmt_shft;
+
+		writel(updnlmt_val, regs->reg_updnlmt);
+
+		fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
+
+		/* Enable SSC */
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 1);
+		/* Enable Hopping control */
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+
+	} else {
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+		fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);
+
+		/* Switch to APMIXEDSYS control */
+		fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+		/* Wait for DDS to be stable */
+		udelay(30);
+	}
+
+	return 0;
+}
+
+static int hopping_hw_flow(void *priv_data, char *domain_name, int fh_id,
+			   unsigned int new_dds, int postdiv)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	unsigned int dds_mask;

drivers/clk/mediatek/clk-fhctl-ap.c line 98

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+	unsigned int mon_dds = 0;
+	int ret = 0;
+	unsigned int con_pcw_tmp;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	struct pll_dts *array = d->array;
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+	dds_mask = data->dds_mask;

Just perform these assignments in the variable declarations... with some reordering as well, and drop the zero assignment to ret.

In few words:

	struct hdlr_data *d = (struct hdlr_data *)priv_data;

	struct fh_pll_domain *domain = d->domain;

	struct fh_pll_regs *regs = &domain->regs[fh_id];

	struct fh_pll_data *data = &domain->data[fh_id];

	struct pll_dts *array = d->array;

	u32 con_pcw_tmp, dds_mask;

	u32 mon_dds = 0;

	int ret;

This comment is valid for some other functions as well - I won't repeat this for every instance... :-)

Quoted 8 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +8 -0
+
+	if (array->ssc_rate)
+		fhctl_set_ssc_regs(regs, data, fh_id, 0);
+
+	writel((readl(regs->reg_con_pcw) & dds_mask) |
+		data->tgl_org, regs->reg_dds);
+
+	fh_set_field(regs->reg_cfg, data->sfstrx_en, 1);

drivers/clk/mediatek/clk-fhctl-ap.c line 116

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+	fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+	writel(data->slope0_value, regs->reg_slope0);
+	writel(data->slope1_value, regs->reg_slope1);
+
+	fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
+	writel((new_dds) | (data->dvfs_tri), regs->reg_dvfs);
+
+	/* Wait 1000 us until DDS stable */
+	ret = readl_poll_timeout_atomic(regs->reg_mon, mon_dds,
+				(mon_dds & dds_mask) == new_dds, 10, 1000);

Why are you writing to CON_PCW even when this returns en error? Please add a comment explaining the reasons.

Quoted 14 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +14 -0
+
+	con_pcw_tmp = readl(regs->reg_con_pcw) & (~dds_mask);
+	con_pcw_tmp = (con_pcw_tmp | (readl(regs->reg_mon) & dds_mask) |
+		       data->pcwchg);
+
+	writel(con_pcw_tmp, regs->reg_con_pcw);
+
+	fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+	if (array->ssc_rate)
+		fhctl_set_ssc_regs(regs, data, fh_id, array->ssc_rate);
+
+	return ret;
+}

drivers/clk/mediatek/clk-fhctl-ap.c line 140

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+
+static unsigned int __get_postdiv(struct fh_pll_regs *regs,
+				  struct fh_pll_data *data)
+{
+	unsigned int regval;
+
+	regval = (readl(regs->reg_con_postdiv) & data->postdiv_mask)
+		  >> data->postdiv_offset;
+
+	return data->postdiv_table[regval];

Can we instead simply reuse struct clk_div_table from clk-provider.h?

Quoted 96 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +96 -0
+}
+
+static void __set_postdiv(struct fh_pll_regs *regs, struct fh_pll_data *data,
+			  int postdiv)
+{
+	unsigned int regval, temp;
+
+	for (regval = 0 ; regval < data->postdiv_table_size ; regval++) {
+		if (data->postdiv_table[regval] > postdiv) {
+			regval--;
+			break;
+		}
+	}
+
+	temp = (readl(regs->reg_con_postdiv)) & ~(data->postdiv_mask);
+	temp |= regval << data->postdiv_offset;
+	writel(temp, regs->reg_con_postdiv);
+}
+
+static int fhctl_ap_hopping(void *priv_data, char *domain_name, int fh_id,
+			    unsigned int new_dds, int postdiv)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	int ret = 0;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	spinlock_t *lock = d->lock;
+	unsigned long flags = 0;
+	unsigned int pll_postdiv;
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+
+	if (postdiv > 0) {
+		pll_postdiv = __get_postdiv(regs, data);
+
+		if (postdiv > pll_postdiv)
+			__set_postdiv(regs, data, postdiv);
+	}
+
+	spin_lock_irqsave(lock, flags);
+
+	ret = hopping_hw_flow(priv_data, domain_name, fh_id, new_dds, postdiv);
+
+	spin_unlock_irqrestore(lock, flags);
+
+	if (postdiv > 0) {
+		if (postdiv < pll_postdiv)
+			__set_postdiv(regs, data, postdiv);
+	}
+
+	return ret;
+}
+
+static int fhctl_ap_ssc_enable(void *priv_data, char *domain_name,
+			       int fh_id, int rate)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	spinlock_t *lock = d->lock;
+	struct pll_dts *array = d->array;
+	unsigned long flags = 0;
+
+	spin_lock_irqsave(lock, flags);
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+
+	fhctl_set_ssc_regs(regs, data, fh_id, rate);
+
+	array->ssc_rate = rate;
+
+	spin_unlock_irqrestore(lock, flags);
+
+	return 0;
+}
+
+static int fhctl_ap_ssc_disable(void *priv_data, char *domain_name, int fh_id)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	spinlock_t *lock = d->lock;
+	struct pll_dts *array = d->array;
+	unsigned long flags = 0;
+
+	spin_lock_irqsave(lock, flags);
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];

drivers/clk/mediatek/clk-fhctl-ap.c line 246

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+	data = &domain->data[fh_id];
+
+	fhctl_set_ssc_regs(regs, data, fh_id, 0);
+
+	array->ssc_rate = 0;
+
+	spin_unlock_irqrestore(lock, flags);
+
+	return 0;
+}

Just commonize these two...

static int __fhctl_ap_ssc_enable(struct hdlr_data *d, int fh_id, int rate)

{
	struct fh_pll_domain *domain = d->domain;

	struct fh_pll_regs *regs = &domain->regs[fh_id];

	struct fh_pll_data *data = &domain->data[fh_id];

	struct pll_dts *array = d->array;

	spinlock_t *lock = d->lock;

	unsigned long flags = 0;



	spin_lock_irqsave(lock, flags);



	fhctl_set_ssc_regs(regs, data, fh_id, rate);



	array->ssc_rate = rate;



	spin_unlock_irqrestore(lock, flags);



	return 0;
}

static int fhctl_ap_ssc_enable(void *priv_data, char *domain_name, int fh_id, int rate)

{
	return __fhctl_ap_ssc_enable((struct hdlr_data *)priv_data, domain_name,
				     fh_id, rate);
}

static int fhctl_ap_ssc_disable(void *priv_data, char *domain_name, int fh_id)

{
	return __fhctl_ap_ssc_enable((struct hdlr_data *)priv_data, domain_name,
				     fh_id, 0);
}
Quoted 28 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +28 -0
+
+static int fhctl_ap_hw_init(struct pll_dts *array, struct fh_ap_match *match)
+{
+	static DEFINE_SPINLOCK(lock);
+	struct hdlr_data *priv_data;
+	struct fh_hdlr *hdlr;
+	struct fh_pll_domain *domain;
+	int fh_id = array->fh_id;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	int mask = BIT(fh_id);
+
+	priv_data = kzalloc(sizeof(*priv_data), GFP_KERNEL);
+	hdlr = kzalloc(sizeof(*hdlr), GFP_KERNEL);
+	init_fh_domain(array->domain, array->comp, array->fhctl_base,
+		       array->apmixed_base);
+
+	priv_data->array = array;
+	priv_data->lock = &lock;
+	priv_data->domain = get_fh_domain(array->domain);
+
+	/* do HW init */
+	domain = priv_data->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+
+	fh_set_field(regs->reg_clk_con, mask, 1);
+	fh_set_field(regs->reg_rst_con, mask, 0);

drivers/clk/mediatek/clk-fhctl-ap.c line 284

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+	fh_set_field(regs->reg_rst_con, mask, 1);
+	writel(0x0, regs->reg_cfg);
+	writel(0x0, regs->reg_updnlmt);
+	writel(0x0, regs->reg_dds);
+
+	/* hook to array */
+	hdlr->data = priv_data;
+	hdlr->ops = match->hdlr->ops;
+	/* hook hdlr to array is the last step */
+	mb();

I really don't think that you need this barrier here - if there's something that I have misunderstood about that, please provide an extensive explanation.

Quoted 128 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +63 -0
+	array->hdlr = hdlr;
+
+	/* do SSC */
+	if (array->ssc_rate) {
+		struct fh_hdlr *hdlr = array->hdlr;
+
+		hdlr->ops->ssc_enable(hdlr->data, array->domain, array->fh_id,
+				      array->ssc_rate);
+	}
+
+	return 0;
+}
+
+static struct fh_operation fhctl_ap_ops = {
+	.hopping = fhctl_ap_hopping,
+	.ssc_enable = fhctl_ap_ssc_enable,
+	.ssc_disable = fhctl_ap_ssc_disable,
+};
+
+static struct fh_hdlr mt8186_hdlr = {
+	.ops = &fhctl_ap_ops,
+};
+
+static struct fh_ap_match mt8186_match = {
+	.name = "mediatek,mt8186-fhctl",
+	.hdlr = &mt8186_hdlr,
+	.init = &fhctl_ap_hw_init,
+};
+
+static struct fh_ap_match *matches[] = {
+	&mt8186_match,
+	NULL,
+};
+
+int fhctl_ap_init(struct pll_dts *array)
+{
+	int i;
+	int num_pll = array->num_pll;
+	struct fh_ap_match **match = matches;
+
+	/* find match by compatible */
+	for (i = 0; i < ARRAY_SIZE(matches); i++) {
+		char *comp = (*match)->name;
+		char *target = array->comp;
+
+		if (!strcmp(comp, target))
+			break;
+		match++;
+	}
+
+	if (*match == NULL)
+		return -1;
+
+	/* init flow for every pll */
+	for (i = 0; i < num_pll; i++, array++) {
+		char *method = array->method;
+
+		if (!strcmp(method, FHCTL_TARGET))
+			(*match)->init(array, *match);
+	}
+
+	return 0;
+}
drivers/clk/mediatek/clk-fhctl-pll.c new file +59 -0
new file mode 100644
index 000000000000..b3ccbbd04e1b
@@ -0,0 +1,209 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include "clk-fhctl-pll.h"
+#include "clk-fhctl-util.h"
+
+#define REG_ADDR(base, x) ((void __iomem *)((unsigned long)base + (x)))
+
+struct fh_pll_match {
+	char *compatible;
+	struct fh_pll_domain **domain_list;
+};
+
+static int fhctl_pll_init(struct fh_pll_domain *d, void __iomem *fhctl_base,
+			  void __iomem *apmixed_base)
+{
+	struct fh_pll_data *data = d->data;
+	struct fh_pll_offset *offset = d->offset;
+	struct fh_pll_regs *regs = d->regs;
+
+	if (regs->reg_hp_en)
+		return 0;
+
+	while (data->dds_mask != 0) {
+		int regs_offset;
+
+		/* fhctl common part */
+		regs->reg_hp_en = REG_ADDR(fhctl_base, offset->offset_hp_en);
+		regs->reg_clk_con = REG_ADDR(fhctl_base,
+					     offset->offset_clk_con);
+		regs->reg_rst_con = REG_ADDR(fhctl_base,
+					     offset->offset_rst_con);
+		regs->reg_slope0 = REG_ADDR(fhctl_base, offset->offset_slope0);
+		regs->reg_slope1 = REG_ADDR(fhctl_base, offset->offset_slope1);
+
+		/* fhctl pll part */
+		regs_offset = offset->offset_fhctl + offset->offset_cfg;
+		regs->reg_cfg = REG_ADDR(fhctl_base, regs_offset);
+		regs->reg_updnlmt = REG_ADDR(regs->reg_cfg,
+					     offset->offset_updnlmt);
+		regs->reg_dds = REG_ADDR(regs->reg_cfg, offset->offset_dds);
+		regs->reg_dvfs = REG_ADDR(regs->reg_cfg, offset->offset_dvfs);
+		regs->reg_mon = REG_ADDR(regs->reg_cfg, offset->offset_mon);
+
+		/* apmixed part */
+		regs->reg_con_pcw = REG_ADDR(apmixed_base,
+					     offset->offset_con_pcw);
+		regs->reg_con_postdiv = REG_ADDR(apmixed_base,
+						 offset->offset_con_postdiv);
+
+		data++;
+		offset++;
+		regs++;
+	}
+

drivers/clk/mediatek/clk-fhctl-pll.c line 69

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-pll.c new file +10 -0
+	return 0;
+}
+
+static unsigned int __postdiv_pow_tbl[8] = {1, 2, 4, 8, 16, 1, 1, 1};
+#define POSTDIV_TABLE_SIZE (sizeof(__postdiv_pow_tbl)\
+	/sizeof(unsigned int))
+
+#define SIZE_8186_TOP (sizeof(mt8186_top_data)\
+	/sizeof(struct fh_pll_data))
+#define DATA_8186_TOP(_name) {						\

Is it just only about the name?
I mean, are there other SoCs for which each PLL needs different parameters?

If not, there are many ways to commonize these params instead of repeating them over and over again...

Quoted 167 lines
drivers/clk/mediatek/clk-fhctl-pll.c new file +140 -0
+		.name = _name,						\
+		.dds_mask = GENMASK(21, 0),				\
+		.postdiv_mask = GENMASK(26, 24),			\
+		.postdiv_offset = 24,					\
+		.postdiv_table = __postdiv_pow_tbl,			\
+		.postdiv_table_size = POSTDIV_TABLE_SIZE,		\
+		.slope0_value = 0x6003c97,				\
+		.slope1_value = 0x6003c97,				\
+		.sfstrx_en = BIT(2),					\
+		.frddsx_en = BIT(1),					\
+		.fhctlx_en = BIT(0),					\
+		.tgl_org = BIT(31),					\
+		.dvfs_tri = BIT(31),					\
+		.pcwchg = BIT(31),					\
+		.dt_val = 0x0,						\
+		.df_val = 0x9,						\
+		.updnlmt_shft = 16,					\
+		.msk_frddsx_dys = GENMASK(23, 20),			\
+		.msk_frddsx_dts = GENMASK(19, 16),			\
+	}
+#define OFFSET_8186_TOP(_fhctl, _con_pcw) {				\
+		.offset_fhctl = _fhctl,					\
+		.offset_con_pcw = _con_pcw,				\
+		.offset_con_postdiv = _con_pcw,				\
+		.offset_hp_en = 0x0,					\
+		.offset_clk_con = 0x8,					\
+		.offset_rst_con = 0xc,					\
+		.offset_slope0 = 0x10,					\
+		.offset_slope1 = 0x14,					\
+		.offset_cfg = 0x0,					\
+		.offset_updnlmt = 0x4,					\
+		.offset_dds = 0x8,					\
+		.offset_dvfs = 0xc,					\
+		.offset_mon = 0x10,					\
+	}
+static struct fh_pll_data mt8186_top_data[] = {
+	DATA_8186_TOP("armpll_ll"),
+	DATA_8186_TOP("armpll_bl"),
+	DATA_8186_TOP("ccipll"),
+	DATA_8186_TOP("mainpll"),
+	DATA_8186_TOP("mmpll"),
+	DATA_8186_TOP("tvdpll"),
+	DATA_8186_TOP("mpll"),
+	DATA_8186_TOP("adsppll"),
+	DATA_8186_TOP("mfgpll"),
+	DATA_8186_TOP("nnapll"),
+	DATA_8186_TOP("nna2pll"),
+	DATA_8186_TOP("msdcpll"),
+	DATA_8186_TOP("mempll"),
+	{}
+};
+static struct fh_pll_offset mt8186_top_offset[] = {
+	OFFSET_8186_TOP(0x003C, 0x0208),
+	OFFSET_8186_TOP(0x0050, 0x0218),
+	OFFSET_8186_TOP(0x0064, 0x0228),
+	OFFSET_8186_TOP(0x0078, 0x0248),
+	OFFSET_8186_TOP(0x008C, 0x0258),
+	OFFSET_8186_TOP(0x00A0, 0x0268),
+	OFFSET_8186_TOP(0x00B4, 0x0278),
+	OFFSET_8186_TOP(0x00C8, 0x0308),
+	OFFSET_8186_TOP(0x00DC, 0x0318),
+	OFFSET_8186_TOP(0x00F0, 0x0360),
+	OFFSET_8186_TOP(0x0104, 0x0370),
+	OFFSET_8186_TOP(0x0118, 0x0390),
+	OFFSET_8186_TOP(0x012c, 0xdeb1),
+	{}
+};
+static struct fh_pll_regs mt8186_top_regs[SIZE_8186_TOP];
+static struct fh_pll_domain mt8186_top = {
+	.name = "top",
+	.data = (struct fh_pll_data *)&mt8186_top_data,
+	.offset = (struct fh_pll_offset *)&mt8186_top_offset,
+	.regs = (struct fh_pll_regs *)&mt8186_top_regs,
+	.init = &fhctl_pll_init,
+};
+static struct fh_pll_domain *mt8186_domain[] = {
+	&mt8186_top,
+	NULL,
+};
+static struct fh_pll_match mt8186_match = {
+	.compatible = "mediatek,mt8186-fhctl",
+	.domain_list = (struct fh_pll_domain **)mt8186_domain,
+};
+
+static const struct fh_pll_match *matches[] = {
+	&mt8186_match,
+	NULL
+};
+
+
+static struct fh_pll_domain **get_list(char *comp)
+{
+	struct fh_pll_match **match;
+	static struct fh_pll_domain **list;
+	int i;
+
+	match = (struct fh_pll_match **)matches;
+
+	/* name used only if !list */
+	if (!list) {
+		for (i = 0; i < ARRAY_SIZE(matches); i++) {
+			if (!strcmp(comp, (*match)->compatible)) {
+				list = (*match)->domain_list;
+				break;
+			}
+			match++;
+		}
+	}
+	return list;
+}
+void init_fh_domain(const char *domain, char *comp, void __iomem *fhctl_base,
+		    void __iomem *apmixed_base)
+{
+	struct fh_pll_domain **list;
+
+	list = get_list(comp);
+
+	while (*list != NULL) {
+		if (!strcmp(domain, (*list)->name)) {
+			(*list)->init(*list, fhctl_base, apmixed_base);
+			return;
+		}
+		list++;
+	}
+}
+
+struct fh_pll_domain *get_fh_domain(const char *domain)
+{
+	struct fh_pll_domain **list;
+
+	list = get_list(NULL);
+
+	/* find instance */
+	while (*list != NULL) {
+		if (!strcmp(domain, (*list)->name))
+			return *list;
+		list++;
+	}
+	return NULL;
+}
drivers/clk/mediatek/clk-fhctl-pll.h new file +21 -0
new file mode 100644
index 000000000000..7f0f7577f7a5
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#ifndef __CLK_FHCTL_PLL_H
+#define __CLK_FHCTL_PLL_H
+#include <linux/types.h>
+#include <linux/bitops.h>
+#include <linux/iopoll.h>
+
+struct fh_pll_data {
+	char *name;
+	unsigned int dds_mask;
+	unsigned int postdiv_mask;
+	unsigned int postdiv_offset;
+	unsigned int *postdiv_table;
+	unsigned int postdiv_table_size;
+	unsigned int slope0_value;
+	unsigned int slope1_value;
+	unsigned int sfstrx_en;

drivers/clk/mediatek/clk-fhctl-pll.h line 31

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +10 -0
+	unsigned int frddsx_en;
+	unsigned int fhctlx_en;
+	unsigned int tgl_org;
+	unsigned int dvfs_tri;
+	unsigned int pcwchg;
+	unsigned int dt_val;
+	unsigned int df_val;
+	unsigned int updnlmt_shft;
+	unsigned int msk_frddsx_dys;
+	unsigned int msk_frddsx_dts;

That's describing some hardware, so... you should really use u32 here as that would be more descriptive.

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +5 -0
+};
+struct fh_pll_offset {
+	int offset_fhctl;
+	int offset_con_pcw;
+	int offset_con_postdiv;

drivers/clk/mediatek/clk-fhctl-pll.h line 46

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +10 -0
+	int offset_hp_en;
+	int offset_clk_con;
+	int offset_rst_con;
+	int offset_slope0;
+	int offset_slope1;
+	int offset_cfg;
+	int offset_updnlmt;
+	int offset_dds;
+	int offset_dvfs;
+	int offset_mon;

These also seem to be describing HW registers, and I don't think that we'll ever see a negative offset for any of these?

Quoted 18 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +18 -0
+};
+struct fh_pll_regs {
+	void __iomem *reg_hp_en;
+	void __iomem *reg_clk_con;
+	void __iomem *reg_rst_con;
+	void __iomem *reg_slope0;
+	void __iomem *reg_slope1;
+	void __iomem *reg_cfg;
+	void __iomem *reg_updnlmt;
+	void __iomem *reg_dds;
+	void __iomem *reg_dvfs;
+	void __iomem *reg_mon;
+	void __iomem *reg_con_pcw;
+	void __iomem *reg_con_postdiv;
+};
+struct fh_pll_domain {
+	char *name;
+	struct fh_pll_data *data;

drivers/clk/mediatek/clk-fhctl-pll.h line 74

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +10 -0
+	struct fh_pll_offset *offset;
+	struct fh_pll_regs *regs;
+	int (*init)(struct fh_pll_domain *d, void __iomem *fhctl_base,
+		    void __iomem *apmixed_base);
+};
+extern struct fh_pll_domain *get_fh_domain(const char *name);
+extern void init_fh_domain(const char *domain_name, char *comp_name,
+			   void __iomem *fhctl_base,
+			   void __iomem *apmixed_base);
+#endif

...and I think this covers enough of the code that may be transferred to clk-pll.

Excited to see the next iteration of this one!

Cheers,
Angelo

maintainer
#3 lore raw

Re: [RFC PATCH 1/2] dt-bindings: arm: mediatek: Add new bindings of MediaTek frequency hopping

In reply to Johnson Wang
Quoted 22 lines · Johnson Wang

This patch adds the new binding documentation for frequency hopping and spread spectrum clocking control on MT8186.

---
 .../bindings/arm/mediatek/mediatek,fhctl.yaml | 149 ++++++++++++++++++
 1 file changed, 149 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +6 -0
new file mode 100644
index 000000000000..9cb04bed9e86
@@ -0,0 +1,149 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/arm/mediatek/mediatek,fhctl.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 16

Quoted 10 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +10 -0
+title: MediaTek frequency hopping and spread spectrum clocking control
+
+maintainers:
+  - Edward-JW Yang <[email protected]>
+
+description: |
+  Frequency hopping control (FHCTL) is a piece of hardware that control
+  some PLLs to adopt "hopping" mechanism to adjust their frequency.
+  Spread spectrum clocking (SSC) is another function provided by this hardware.
+

Are all these settings per board?

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 22

Quoted 6 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +6 -0
+properties:
+  compatible:
+    const: mediatek,mt8186-fhctl
+
+  reg:
+    maxItems: 2

Need to define what each entry is.

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 26

Quoted 4 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +4 -0
+
+patternProperties:
+  "^map[0-9]$":
+    type: object

additionalProperties: false

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 31

Quoted 5 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +5 -0
+    description:
+      Represents a group of PLLs controlled by the same domain and method.
+
+    properties:
+      domain:

Custom properties need a vendor prefix.

'domain' is a pretty generic term.

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 33

Quoted 2 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +2 -0
+        description: The position of this hardware block in the chip.
+        $ref: /schemas/types.yaml#/definitions/string-array

You have to define possible values.

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 41

Quoted 8 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +8 -0
+
+      method:
+        description: Determine which method to control this FHCTL hardware.
+        $ref: /schemas/types.yaml#/definitions/string-array
+
+    patternProperties:
+      "^armpll_(ll|bl)|(cci|main|mm|tvd|m|adsp|mfg|nna|nna2|msdc)pll$":
+        type: object

additionalProperties: false

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 48

Quoted 7 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +7 -0
+        description:
+          A subnode represents one PLL clock. The properties it contains are
+          used to determine whether this PLL enable frequency hopping mechanism           and spread spectrum clocking (SSC) control.
+
+        properties:
+          fh-id:
+            description: The index of the PLL clocks.

index is based on what?

Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml line 58

Quoted 10 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +10 -0
+            $ref: /schemas/types.yaml#/definitions/uint32
+
+          perms:
+            description:
+              This is a bit field used to manage the permission of frequency
+              hopping function and spread spectrum clocking control.
+            $ref: /schemas/types.yaml#/definitions/uint32
+
+          ssc-rate:
+            description: The percentage of the spectrum spreading.

Use '-percent' suffix then.

Quoted 95 lines
Documentation/devicetree/bindings/arm/mediatek/mediatek,fhctl.yaml new file +91 -0
+            $ref: /schemas/types.yaml#/definitions/uint32
+            minimum: 0
+            maximum: 8
+
+        required:
+          - fh-id
+          - perms
+
+    required:
+      - domain
+      - method
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/mt8186-clk.h>
+    fhctl: fhctl@1000ce00 {
+        compatible = "mediatek,mt8186-fhctl";
+        reg = <0x1000ce00 0x200>,
+              <0x1000c000 0xe00>;
+        map0 {
+            domain = "top";
+            method = "fhctl-ap";
+
+            armpll_ll {
+                fh-id = <0>;
+                perms = <0x10>;
+            };
+
+            armpll_bl {
+                fh-id = <1>;
+                perms = <0x10>;
+            };
+
+            ccipll {
+                fh-id = <2>;
+                perms = <0x10>;
+            };
+
+            mainpll {
+                fh-id = <3>;
+                perms = <0x1c>;
+            };
+
+            mmpll {
+                fh-id = <4>;
+                perms = <0x1c>;
+            };
+
+            tvdpll {
+                fh-id = <5>;
+                perms = <0x1c>;
+            };
+
+            mpll {
+                fh-id = <6>;
+                perms = <0x10>;
+            };
+
+            adsppll {
+                fh-id = <7>;
+                perms = <0x1c>;
+            };
+
+            mfgpll {
+                fh-id = <8>;
+                perms = <0x1c>;
+            };
+
+            nnapll {
+                fh-id = <9>;
+                perms = <0x1c>;
+            };
+
+            nna2pll {
+                fh-id = <10>;
+                perms = <0x1c>;
+            };
+
+            msdcpll {
+                fh-id = <11>;
+                perms = <0x1c>;
+                ssc-rate = <3>;
+            };
+        };
+    };
Signature · 1 line
2.18.0
#6 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

In reply to AngeloGioacchino Del Regno

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

	PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

	    PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.
Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate. We will set some PLL's frequency be controlled by clk-pll and some are controlled by FHCTL. And use perms param to decide whether a PLL is using FHCTL to change its frequency.

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Quoted 32 lines

In any case, to speed up development (I believe that transferring this in clk-pll means that the code will still be more or less the same), I've performed a review on the code; check below.

Quoted 27 lines
---
  drivers/clk/mediatek/Kconfig          |   8 +
  drivers/clk/mediatek/Makefile         |   2 +
  drivers/clk/mediatek/clk-fhctl-ap.c   | 347 ++++++++++++++++++++++++++
  drivers/clk/mediatek/clk-fhctl-pll.c  | 209 ++++++++++++++++
  drivers/clk/mediatek/clk-fhctl-pll.h  |  74 ++++++
  drivers/clk/mediatek/clk-fhctl-util.h |  24 ++
  drivers/clk/mediatek/clk-fhctl.c      | 191 ++++++++++++++
  drivers/clk/mediatek/clk-fhctl.h      |  45 ++++
  drivers/clk/mediatek/clk-pll.c        |   5 +-
  drivers/clk/mediatek/clk-pll.h        |   5 +
  10 files changed, 909 insertions(+), 1 deletion(-)

create mode 100644 drivers/clk/mediatek/clk-fhctl-ap.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.h create mode 100644 drivers/clk/mediatek/clk-fhctl-util.h create mode 100644 drivers/clk/mediatek/clk-fhctl.c create mode 100644 drivers/clk/mediatek/clk-fhctl.h

drivers/clk/mediatek/Kconfig +0 -0
index d5936cfb3bee..fd887c537a91 100644
@@ -622,4 +622,12 @@ config COMMON_CLK_MT8516_AUDSYS
  	help
  	  This driver supports MediaTek MT8516 audsys clocks.
 
Quoted 4 lines

drivers/clk/mediatek/Kconfig config COMMON_CLK_MT8516_AUDSYS · line 626

Quoted 2 lines
drivers/clk/mediatek/Kconfig +2 -0
+config COMMON_CLK_MTK_FREQ_HOPPING
+	tristate "MediaTek frequency hopping driver"

If this goes inside of clk-pll, this configuration option can be safely removed.

I think we should keep this for clk-fhctl* files.

Quoted 64 lines
Quoted 64 lines
drivers/clk/mediatek/Kconfig +6 -0
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	select COMMON_CLK_MEDIATEK
+	help
+	  This driver supports frequency hopping and spread spectrum clocking
+	  control for some MediaTek SoCs.
+
  endmenu
drivers/clk/mediatek/Makefile +2 -0
index caf2ce93d666..3c0e9bd3978b 100644
@@ -99,3 +99,5 @@ obj-$(CONFIG_COMMON_CLK_MT8195) += clk-mt8195-apmixedsys.o clk-mt8195-topckgen.o
  				   clk-mt8195-apusys_pll.o
  obj-$(CONFIG_COMMON_CLK_MT8516) += clk-mt8516.o
  obj-$(CONFIG_COMMON_CLK_MT8516_AUDSYS) += clk-mt8516-aud.o
+obj-$(CONFIG_COMMON_CLK_MTK_FREQ_HOPPING) += fhctl.o
+fhctl-objs += clk-fhctl.o clk-fhctl-ap.o clk-fhctl-pll.o
drivers/clk/mediatek/clk-fhctl-ap.c new file +40 -0
new file mode 100644
index 000000000000..9e3226a9c1ca
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include "clk-fhctl.h"
+#include "clk-fhctl-pll.h"
+#include "clk-fhctl-util.h"
+
+#define FHCTL_TARGET FHCTL_AP
+
+#define PERCENT_TO_DDSLMT(dds, percent_m10) \
+	((((dds) * (percent_m10)) >> 5) / 100)
+
+struct fh_ap_match {
+	char *name;
+	struct fh_hdlr *hdlr;
+	int (*init)(struct pll_dts *array, struct fh_ap_match *match);
+};
+
+struct hdlr_data {
+	struct pll_dts *array;
+	struct fh_pll_domain *domain;
+	spinlock_t *lock;
+};
+
+static int fhctl_set_ssc_regs(struct fh_pll_regs *regs,
+			      struct fh_pll_data *data,
+			      int fh_id, int rate)
+{
+	unsigned int updnlmt_val;
+
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 44

Quoted 4 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +4 -0
+	if (rate > 0) {
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+		fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);

Are all of these writes to be performed with a barrier?
Can't we use writel_relaxed() for some, with a "final" writel() where ordering *really* matters?

Do this mean use writel_relaxed() on the first two and writel() on the thrid?

Quoted 3 lines

Also, at least these three field settings are common between (rate > 0) and (rate <= 0), so they can go outside of the conditional.

OK, we will move them to outside of conditional. Thanks.

Quoted 77 lines
Quoted 45 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +44 -0
+
+		/* Set the relative parameter registers (dt/df/upbnd/downbnd) */
+		fh_set_field(regs->reg_cfg, data->msk_frddsx_dys, data->df_val);
+		fh_set_field(regs->reg_cfg, data->msk_frddsx_dts, data->dt_val);
+
+		writel((readl(regs->reg_con_pcw) & data->dds_mask) |
+			data->tgl_org, regs->reg_dds);
+
+		/* Calculate UPDNLMT */
+		updnlmt_val = PERCENT_TO_DDSLMT((readl(regs->reg_dds) &
+						 data->dds_mask), rate) <<
+						 data->updnlmt_shft;
+
+		writel(updnlmt_val, regs->reg_updnlmt);
+
+		fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
+
+		/* Enable SSC */
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 1);
+		/* Enable Hopping control */
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+
+	} else {
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+		fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);
+
+		/* Switch to APMIXEDSYS control */
+		fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+		/* Wait for DDS to be stable */
+		udelay(30);
+	}
+
+	return 0;
+}
+
+static int hopping_hw_flow(void *priv_data, char *domain_name, int fh_id,
+			   unsigned int new_dds, int postdiv)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	unsigned int dds_mask;

drivers/clk/mediatek/clk-fhctl-ap.c line 98

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+	unsigned int mon_dds = 0;
+	int ret = 0;
+	unsigned int con_pcw_tmp;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	struct pll_dts *array = d->array;
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+	dds_mask = data->dds_mask;

Just perform these assignments in the variable declarations... with some reordering as well, and drop the zero assignment to ret.

In few words:

	struct hdlr_data *d = (struct hdlr_data *)priv_data;

	struct fh_pll_domain *domain = d->domain;

	struct fh_pll_regs *regs = &domain->regs[fh_id];

	struct fh_pll_data *data = &domain->data[fh_id];

	struct pll_dts *array = d->array;

	u32 con_pcw_tmp, dds_mask;

	u32 mon_dds = 0;

	int ret;
Quoted 2 lines

This comment is valid for some other functions as well - I won't repeat this for every instance... :-)

OK, we will merge them. Thanks.

Quoted 14 lines
Quoted 14 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +13 -0
+
+	if (array->ssc_rate)
+		fhctl_set_ssc_regs(regs, data, fh_id, 0);
+
+	writel((readl(regs->reg_con_pcw) & dds_mask) |
+		data->tgl_org, regs->reg_dds);
+
+	fh_set_field(regs->reg_cfg, data->sfstrx_en, 1);
+	fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+	writel(data->slope0_value, regs->reg_slope0);
+	writel(data->slope1_value, regs->reg_slope1);
+
+	fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 116

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +5 -0
+	writel((new_dds) | (data->dvfs_tri), regs->reg_dvfs);
+
+	/* Wait 1000 us until DDS stable */
+	ret = readl_poll_timeout_atomic(regs->reg_mon, mon_dds,
+				(mon_dds & dds_mask) == new_dds, 10, 1000);

Why are you writing to CON_PCW even when this returns en error? Please add a comment explaining the reasons.

Oh, we will add a warning log and dump HW register when this returns an error The reg_mon is a register reflects the current frequency rate. So, it's fine to write the current rate back to CON_PCW. We will also add a comment on it. Thanks

Quoted 19 lines
Quoted 19 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +18 -0
+
+	con_pcw_tmp = readl(regs->reg_con_pcw) & (~dds_mask);
+	con_pcw_tmp = (con_pcw_tmp | (readl(regs->reg_mon) & dds_mask) |
+		       data->pcwchg);
+
+	writel(con_pcw_tmp, regs->reg_con_pcw);
+
+	fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+	if (array->ssc_rate)
+		fhctl_set_ssc_regs(regs, data, fh_id, array->ssc_rate);
+
+	return ret;
+}
+
+static unsigned int __get_postdiv(struct fh_pll_regs *regs,
+				  struct fh_pll_data *data)
+{
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 140

Quoted 6 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +6 -0
+	unsigned int regval;
+
+	regval = (readl(regs->reg_con_postdiv) & data->postdiv_mask)
+		  >> data->postdiv_offset;
+
+	return data->postdiv_table[regval];

Can we instead simply reuse struct clk_div_table from clk-provider.h?

"postdiv" is part of setting in PCW_CON, not a individual clk divider. I think it's not suitable to use struct clk_div_table here.

Quoted 152 lines
Quoted 97 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +96 -0
+}
+
+static void __set_postdiv(struct fh_pll_regs *regs, struct fh_pll_data *data,
+			  int postdiv)
+{
+	unsigned int regval, temp;
+
+	for (regval = 0 ; regval < data->postdiv_table_size ; regval++) {
+		if (data->postdiv_table[regval] > postdiv) {
+			regval--;
+			break;
+		}
+	}
+
+	temp = (readl(regs->reg_con_postdiv)) & ~(data->postdiv_mask);
+	temp |= regval << data->postdiv_offset;
+	writel(temp, regs->reg_con_postdiv);
+}
+
+static int fhctl_ap_hopping(void *priv_data, char *domain_name, int fh_id,
+			    unsigned int new_dds, int postdiv)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	int ret = 0;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	spinlock_t *lock = d->lock;
+	unsigned long flags = 0;
+	unsigned int pll_postdiv;
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+
+	if (postdiv > 0) {
+		pll_postdiv = __get_postdiv(regs, data);
+
+		if (postdiv > pll_postdiv)
+			__set_postdiv(regs, data, postdiv);
+	}
+
+	spin_lock_irqsave(lock, flags);
+
+	ret = hopping_hw_flow(priv_data, domain_name, fh_id, new_dds, postdiv);
+
+	spin_unlock_irqrestore(lock, flags);
+
+	if (postdiv > 0) {
+		if (postdiv < pll_postdiv)
+			__set_postdiv(regs, data, postdiv);
+	}
+
+	return ret;
+}
+
+static int fhctl_ap_ssc_enable(void *priv_data, char *domain_name,
+			       int fh_id, int rate)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	spinlock_t *lock = d->lock;
+	struct pll_dts *array = d->array;
+	unsigned long flags = 0;
+
+	spin_lock_irqsave(lock, flags);
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+
+	fhctl_set_ssc_regs(regs, data, fh_id, rate);
+
+	array->ssc_rate = rate;
+
+	spin_unlock_irqrestore(lock, flags);
+
+	return 0;
+}
+
+static int fhctl_ap_ssc_disable(void *priv_data, char *domain_name, int fh_id)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	spinlock_t *lock = d->lock;
+	struct pll_dts *array = d->array;
+	unsigned long flags = 0;
+
+	spin_lock_irqsave(lock, flags);
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];

drivers/clk/mediatek/clk-fhctl-ap.c line 246

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+	data = &domain->data[fh_id];
+
+	fhctl_set_ssc_regs(regs, data, fh_id, 0);
+
+	array->ssc_rate = 0;
+
+	spin_unlock_irqrestore(lock, flags);
+
+	return 0;
+}

Just commonize these two...

static int __fhctl_ap_ssc_enable(struct hdlr_data *d, int fh_id, int rate)

{
	struct fh_pll_domain *domain = d->domain;

	struct fh_pll_regs *regs = &domain->regs[fh_id];

	struct fh_pll_data *data = &domain->data[fh_id];

	struct pll_dts *array = d->array;

	spinlock_t *lock = d->lock;

	unsigned long flags = 0;



	spin_lock_irqsave(lock, flags);



	fhctl_set_ssc_regs(regs, data, fh_id, rate);



	array->ssc_rate = rate;



	spin_unlock_irqrestore(lock, flags);



	return 0;
}

static int fhctl_ap_ssc_enable(void *priv_data, char *domain_name, int fh_id, int rate)

{
	return __fhctl_ap_ssc_enable((struct hdlr_data *)priv_data, domain_name,
				     fh_id, rate);
}
Quoted 5 lines

static int fhctl_ap_ssc_disable(void *priv_data, char *domain_name, int fh_id)

{
	return __fhctl_ap_ssc_enable((struct hdlr_data *)priv_data, domain_name,
				     fh_id, 0);
}

OK, we will commonize these. Thanks.

Quoted 35 lines
Quoted 35 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +33 -0
+
+static int fhctl_ap_hw_init(struct pll_dts *array, struct fh_ap_match *match)
+{
+	static DEFINE_SPINLOCK(lock);
+	struct hdlr_data *priv_data;
+	struct fh_hdlr *hdlr;
+	struct fh_pll_domain *domain;
+	int fh_id = array->fh_id;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	int mask = BIT(fh_id);
+
+	priv_data = kzalloc(sizeof(*priv_data), GFP_KERNEL);
+	hdlr = kzalloc(sizeof(*hdlr), GFP_KERNEL);
+	init_fh_domain(array->domain, array->comp, array->fhctl_base,
+		       array->apmixed_base);
+
+	priv_data->array = array;
+	priv_data->lock = &lock;
+	priv_data->domain = get_fh_domain(array->domain);
+
+	/* do HW init */
+	domain = priv_data->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+
+	fh_set_field(regs->reg_clk_con, mask, 1);
+	fh_set_field(regs->reg_rst_con, mask, 0);
+	fh_set_field(regs->reg_rst_con, mask, 1);
+	writel(0x0, regs->reg_cfg);
+	writel(0x0, regs->reg_updnlmt);
+	writel(0x0, regs->reg_dds);
+
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 284

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +5 -0
+	/* hook to array */
+	hdlr->data = priv_data;
+	hdlr->ops = match->hdlr->ops;
+	/* hook hdlr to array is the last step */
+	mb();

I really don't think that you need this barrier here - if there's something that I have misunderstood about that, please provide an extensive explanation.

Actually we can remove this mb(), the original thought is that make sure once hdlr is used, hdlr->data and hdlr->ops is prepared.
After review, hdlr won't be used during fhctl_ap_hw_init, so it can be safely removed. Thanks.

Quoted 143 lines
Quoted 129 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +63 -0
+	array->hdlr = hdlr;
+
+	/* do SSC */
+	if (array->ssc_rate) {
+		struct fh_hdlr *hdlr = array->hdlr;
+
+		hdlr->ops->ssc_enable(hdlr->data, array->domain, array->fh_id,
+				      array->ssc_rate);
+	}
+
+	return 0;
+}
+
+static struct fh_operation fhctl_ap_ops = {
+	.hopping = fhctl_ap_hopping,
+	.ssc_enable = fhctl_ap_ssc_enable,
+	.ssc_disable = fhctl_ap_ssc_disable,
+};
+
+static struct fh_hdlr mt8186_hdlr = {
+	.ops = &fhctl_ap_ops,
+};
+
+static struct fh_ap_match mt8186_match = {
+	.name = "mediatek,mt8186-fhctl",
+	.hdlr = &mt8186_hdlr,
+	.init = &fhctl_ap_hw_init,
+};
+
+static struct fh_ap_match *matches[] = {
+	&mt8186_match,
+	NULL,
+};
+
+int fhctl_ap_init(struct pll_dts *array)
+{
+	int i;
+	int num_pll = array->num_pll;
+	struct fh_ap_match **match = matches;
+
+	/* find match by compatible */
+	for (i = 0; i < ARRAY_SIZE(matches); i++) {
+		char *comp = (*match)->name;
+		char *target = array->comp;
+
+		if (!strcmp(comp, target))
+			break;
+		match++;
+	}
+
+	if (*match == NULL)
+		return -1;
+
+	/* init flow for every pll */
+	for (i = 0; i < num_pll; i++, array++) {
+		char *method = array->method;
+
+		if (!strcmp(method, FHCTL_TARGET))
+			(*match)->init(array, *match);
+	}
+
+	return 0;
+}
drivers/clk/mediatek/clk-fhctl-pll.c new file +59 -0
new file mode 100644
index 000000000000..b3ccbbd04e1b
@@ -0,0 +1,209 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include "clk-fhctl-pll.h"
+#include "clk-fhctl-util.h"
+
+#define REG_ADDR(base, x) ((void __iomem *)((unsigned long)base + (x)))
+
+struct fh_pll_match {
+	char *compatible;
+	struct fh_pll_domain **domain_list;
+};
+
+static int fhctl_pll_init(struct fh_pll_domain *d, void __iomem *fhctl_base,
+			  void __iomem *apmixed_base)
+{
+	struct fh_pll_data *data = d->data;
+	struct fh_pll_offset *offset = d->offset;
+	struct fh_pll_regs *regs = d->regs;
+
+	if (regs->reg_hp_en)
+		return 0;
+
+	while (data->dds_mask != 0) {
+		int regs_offset;
+
+		/* fhctl common part */
+		regs->reg_hp_en = REG_ADDR(fhctl_base, offset->offset_hp_en);
+		regs->reg_clk_con = REG_ADDR(fhctl_base,
+					     offset->offset_clk_con);
+		regs->reg_rst_con = REG_ADDR(fhctl_base,
+					     offset->offset_rst_con);
+		regs->reg_slope0 = REG_ADDR(fhctl_base, offset->offset_slope0);
+		regs->reg_slope1 = REG_ADDR(fhctl_base, offset->offset_slope1);
+
+		/* fhctl pll part */
+		regs_offset = offset->offset_fhctl + offset->offset_cfg;
+		regs->reg_cfg = REG_ADDR(fhctl_base, regs_offset);
+		regs->reg_updnlmt = REG_ADDR(regs->reg_cfg,
+					     offset->offset_updnlmt);
+		regs->reg_dds = REG_ADDR(regs->reg_cfg, offset->offset_dds);
+		regs->reg_dvfs = REG_ADDR(regs->reg_cfg, offset->offset_dvfs);
+		regs->reg_mon = REG_ADDR(regs->reg_cfg, offset->offset_mon);
+
+		/* apmixed part */
+		regs->reg_con_pcw = REG_ADDR(apmixed_base,
+					     offset->offset_con_pcw);
+		regs->reg_con_postdiv = REG_ADDR(apmixed_base,
+						 offset->offset_con_postdiv);
+
+		data++;
+		offset++;
+		regs++;
+	}
+

drivers/clk/mediatek/clk-fhctl-pll.c line 69

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-pll.c new file +10 -0
+	return 0;
+}
+
+static unsigned int __postdiv_pow_tbl[8] = {1, 2, 4, 8, 16, 1, 1, 1};
+#define POSTDIV_TABLE_SIZE (sizeof(__postdiv_pow_tbl)\
+	/sizeof(unsigned int))
+
+#define SIZE_8186_TOP (sizeof(mt8186_top_data)\
+	/sizeof(struct fh_pll_data))
+#define DATA_8186_TOP(_name) {						\

Is it just only about the name?
I mean, are there other SoCs for which each PLL needs different parameters?

Quoted 2 lines

If not, there are many ways to commonize these params instead of repeating them over and over again...

Some parameters are difference on other SoCs. We will find a way to commonize these. Thanks.

Quoted 173 lines
Quoted 173 lines
drivers/clk/mediatek/clk-fhctl-pll.c new file +140 -0
+		.name = _name,						\
+		.dds_mask = GENMASK(21, 0),				\
+		.postdiv_mask = GENMASK(26, 24),			\
+		.postdiv_offset = 24,					\
+		.postdiv_table = __postdiv_pow_tbl,			\
+		.postdiv_table_size = POSTDIV_TABLE_SIZE,		\
+		.slope0_value = 0x6003c97,				\
+		.slope1_value = 0x6003c97,				\
+		.sfstrx_en = BIT(2),					\
+		.frddsx_en = BIT(1),					\
+		.fhctlx_en = BIT(0),					\
+		.tgl_org = BIT(31),					\
+		.dvfs_tri = BIT(31),					\
+		.pcwchg = BIT(31),					\
+		.dt_val = 0x0,						\
+		.df_val = 0x9,						\
+		.updnlmt_shft = 16,					\
+		.msk_frddsx_dys = GENMASK(23, 20),			\
+		.msk_frddsx_dts = GENMASK(19, 16),			\
+	}
+#define OFFSET_8186_TOP(_fhctl, _con_pcw) {				\
+		.offset_fhctl = _fhctl,					\
+		.offset_con_pcw = _con_pcw,				\
+		.offset_con_postdiv = _con_pcw,				\
+		.offset_hp_en = 0x0,					\
+		.offset_clk_con = 0x8,					\
+		.offset_rst_con = 0xc,					\
+		.offset_slope0 = 0x10,					\
+		.offset_slope1 = 0x14,					\
+		.offset_cfg = 0x0,					\
+		.offset_updnlmt = 0x4,					\
+		.offset_dds = 0x8,					\
+		.offset_dvfs = 0xc,					\
+		.offset_mon = 0x10,					\
+	}
+static struct fh_pll_data mt8186_top_data[] = {
+	DATA_8186_TOP("armpll_ll"),
+	DATA_8186_TOP("armpll_bl"),
+	DATA_8186_TOP("ccipll"),
+	DATA_8186_TOP("mainpll"),
+	DATA_8186_TOP("mmpll"),
+	DATA_8186_TOP("tvdpll"),
+	DATA_8186_TOP("mpll"),
+	DATA_8186_TOP("adsppll"),
+	DATA_8186_TOP("mfgpll"),
+	DATA_8186_TOP("nnapll"),
+	DATA_8186_TOP("nna2pll"),
+	DATA_8186_TOP("msdcpll"),
+	DATA_8186_TOP("mempll"),
+	{}
+};
+static struct fh_pll_offset mt8186_top_offset[] = {
+	OFFSET_8186_TOP(0x003C, 0x0208),
+	OFFSET_8186_TOP(0x0050, 0x0218),
+	OFFSET_8186_TOP(0x0064, 0x0228),
+	OFFSET_8186_TOP(0x0078, 0x0248),
+	OFFSET_8186_TOP(0x008C, 0x0258),
+	OFFSET_8186_TOP(0x00A0, 0x0268),
+	OFFSET_8186_TOP(0x00B4, 0x0278),
+	OFFSET_8186_TOP(0x00C8, 0x0308),
+	OFFSET_8186_TOP(0x00DC, 0x0318),
+	OFFSET_8186_TOP(0x00F0, 0x0360),
+	OFFSET_8186_TOP(0x0104, 0x0370),
+	OFFSET_8186_TOP(0x0118, 0x0390),
+	OFFSET_8186_TOP(0x012c, 0xdeb1),
+	{}
+};
+static struct fh_pll_regs mt8186_top_regs[SIZE_8186_TOP];
+static struct fh_pll_domain mt8186_top = {
+	.name = "top",
+	.data = (struct fh_pll_data *)&mt8186_top_data,
+	.offset = (struct fh_pll_offset *)&mt8186_top_offset,
+	.regs = (struct fh_pll_regs *)&mt8186_top_regs,
+	.init = &fhctl_pll_init,
+};
+static struct fh_pll_domain *mt8186_domain[] = {
+	&mt8186_top,
+	NULL,
+};
+static struct fh_pll_match mt8186_match = {
+	.compatible = "mediatek,mt8186-fhctl",
+	.domain_list = (struct fh_pll_domain **)mt8186_domain,
+};
+
+static const struct fh_pll_match *matches[] = {
+	&mt8186_match,
+	NULL
+};
+
+
+static struct fh_pll_domain **get_list(char *comp)
+{
+	struct fh_pll_match **match;
+	static struct fh_pll_domain **list;
+	int i;
+
+	match = (struct fh_pll_match **)matches;
+
+	/* name used only if !list */
+	if (!list) {
+		for (i = 0; i < ARRAY_SIZE(matches); i++) {
+			if (!strcmp(comp, (*match)->compatible)) {
+				list = (*match)->domain_list;
+				break;
+			}
+			match++;
+		}
+	}
+	return list;
+}
+void init_fh_domain(const char *domain, char *comp, void __iomem *fhctl_base,
+		    void __iomem *apmixed_base)
+{
+	struct fh_pll_domain **list;
+
+	list = get_list(comp);
+
+	while (*list != NULL) {
+		if (!strcmp(domain, (*list)->name)) {
+			(*list)->init(*list, fhctl_base, apmixed_base);
+			return;
+		}
+		list++;
+	}
+}
+
+struct fh_pll_domain *get_fh_domain(const char *domain)
+{
+	struct fh_pll_domain **list;
+
+	list = get_list(NULL);
+
+	/* find instance */
+	while (*list != NULL) {
+		if (!strcmp(domain, (*list)->name))
+			return *list;
+		list++;
+	}
+	return NULL;
+}
drivers/clk/mediatek/clk-fhctl-pll.h new file +26 -0
new file mode 100644
index 000000000000..7f0f7577f7a5
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#ifndef __CLK_FHCTL_PLL_H
+#define __CLK_FHCTL_PLL_H
+#include <linux/types.h>
+#include <linux/bitops.h>
+#include <linux/iopoll.h>
+
+struct fh_pll_data {
+	char *name;
+	unsigned int dds_mask;
+	unsigned int postdiv_mask;
+	unsigned int postdiv_offset;
+	unsigned int *postdiv_table;
+	unsigned int postdiv_table_size;
+	unsigned int slope0_value;
+	unsigned int slope1_value;
+	unsigned int sfstrx_en;
+	unsigned int frddsx_en;
+	unsigned int fhctlx_en;
+	unsigned int tgl_org;
+	unsigned int dvfs_tri;
+	unsigned int pcwchg;
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-pll.h line 31

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +5 -0
+	unsigned int dt_val;
+	unsigned int df_val;
+	unsigned int updnlmt_shft;
+	unsigned int msk_frddsx_dys;
+	unsigned int msk_frddsx_dts;

That's describing some hardware, so... you should really use u32 here as that would be more descriptive.

OK, we will fix it. Thanks.

Quoted 11 lines
Quoted 11 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +10 -0
+};
+struct fh_pll_offset {
+	int offset_fhctl;
+	int offset_con_pcw;
+	int offset_con_postdiv;
+	int offset_hp_en;
+	int offset_clk_con;
+	int offset_rst_con;
+	int offset_slope0;
+	int offset_slope1;
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-pll.h line 46

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +5 -0
+	int offset_cfg;
+	int offset_updnlmt;
+	int offset_dds;
+	int offset_dvfs;
+	int offset_mon;

These also seem to be describing HW registers, and I don't think that we'll ever see a negative offset for any of these?

Oh, you are right, these are positive value. We will fix it. Thanks.

Quoted 36 lines
Quoted 19 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +18 -0
+};
+struct fh_pll_regs {
+	void __iomem *reg_hp_en;
+	void __iomem *reg_clk_con;
+	void __iomem *reg_rst_con;
+	void __iomem *reg_slope0;
+	void __iomem *reg_slope1;
+	void __iomem *reg_cfg;
+	void __iomem *reg_updnlmt;
+	void __iomem *reg_dds;
+	void __iomem *reg_dvfs;
+	void __iomem *reg_mon;
+	void __iomem *reg_con_pcw;
+	void __iomem *reg_con_postdiv;
+};
+struct fh_pll_domain {
+	char *name;
+	struct fh_pll_data *data;

drivers/clk/mediatek/clk-fhctl-pll.h line 74

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-pll.h new file +10 -0
+	struct fh_pll_offset *offset;
+	struct fh_pll_regs *regs;
+	int (*init)(struct fh_pll_domain *d, void __iomem *fhctl_base,
+		    void __iomem *apmixed_base);
+};
+extern struct fh_pll_domain *get_fh_domain(const char *name);
+extern void init_fh_domain(const char *domain_name, char *comp_name,
+			   void __iomem *fhctl_base,
+			   void __iomem *apmixed_base);
+#endif

...and I think this covers enough of the code that may be transferred to clk-pll.

Excited to see the next iteration of this one!

Cheers,
Angelo

#7 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

	PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

	    PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Quoted 2 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 2 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

Quoted 2 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)
* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)
* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Quoted 7 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 112 lines
Quoted 32 lines

In any case, to speed up development (I believe that transferring this in clk-pll means that the code will still be more or less the same), I've performed a review on the code; check below.

Quoted 27 lines
---
   drivers/clk/mediatek/Kconfig          |   8 +
   drivers/clk/mediatek/Makefile         |   2 +
   drivers/clk/mediatek/clk-fhctl-ap.c   | 347 ++++++++++++++++++++++++++
   drivers/clk/mediatek/clk-fhctl-pll.c  | 209 ++++++++++++++++
   drivers/clk/mediatek/clk-fhctl-pll.h  |  74 ++++++
   drivers/clk/mediatek/clk-fhctl-util.h |  24 ++
   drivers/clk/mediatek/clk-fhctl.c      | 191 ++++++++++++++
   drivers/clk/mediatek/clk-fhctl.h      |  45 ++++
   drivers/clk/mediatek/clk-pll.c        |   5 +-
   drivers/clk/mediatek/clk-pll.h        |   5 +
   10 files changed, 909 insertions(+), 1 deletion(-)

create mode 100644 drivers/clk/mediatek/clk-fhctl-ap.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.h create mode 100644 drivers/clk/mediatek/clk-fhctl-util.h create mode 100644 drivers/clk/mediatek/clk-fhctl.c create mode 100644 drivers/clk/mediatek/clk-fhctl.h

drivers/clk/mediatek/Kconfig +0 -0
index d5936cfb3bee..fd887c537a91 100644
@@ -622,4 +622,12 @@ config COMMON_CLK_MT8516_AUDSYS
   	help
   	  This driver supports MediaTek MT8516 audsys clocks.
 
Quoted 4 lines

drivers/clk/mediatek/Kconfig config COMMON_CLK_MT8516_AUDSYS · line 626

Quoted 2 lines
drivers/clk/mediatek/Kconfig +2 -0
+config COMMON_CLK_MTK_FREQ_HOPPING
+	tristate "MediaTek frequency hopping driver"

If this goes inside of clk-pll, this configuration option can be safely removed.

I think we should keep this for clk-fhctl* files.

Quoted 64 lines
Quoted 64 lines
drivers/clk/mediatek/Kconfig +6 -0
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	select COMMON_CLK_MEDIATEK
+	help
+	  This driver supports frequency hopping and spread spectrum clocking
+	  control for some MediaTek SoCs.
+
   endmenu
drivers/clk/mediatek/Makefile +2 -0
index caf2ce93d666..3c0e9bd3978b 100644
@@ -99,3 +99,5 @@ obj-$(CONFIG_COMMON_CLK_MT8195) += clk-mt8195-apmixedsys.o clk-mt8195-topckgen.o
   				   clk-mt8195-apusys_pll.o
   obj-$(CONFIG_COMMON_CLK_MT8516) += clk-mt8516.o
   obj-$(CONFIG_COMMON_CLK_MT8516_AUDSYS) += clk-mt8516-aud.o
+obj-$(CONFIG_COMMON_CLK_MTK_FREQ_HOPPING) += fhctl.o
+fhctl-objs += clk-fhctl.o clk-fhctl-ap.o clk-fhctl-pll.o
drivers/clk/mediatek/clk-fhctl-ap.c new file +40 -0
new file mode 100644
index 000000000000..9e3226a9c1ca
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include "clk-fhctl.h"
+#include "clk-fhctl-pll.h"
+#include "clk-fhctl-util.h"
+
+#define FHCTL_TARGET FHCTL_AP
+
+#define PERCENT_TO_DDSLMT(dds, percent_m10) \
+	((((dds) * (percent_m10)) >> 5) / 100)
+
+struct fh_ap_match {
+	char *name;
+	struct fh_hdlr *hdlr;
+	int (*init)(struct pll_dts *array, struct fh_ap_match *match);
+};
+
+struct hdlr_data {
+	struct pll_dts *array;
+	struct fh_pll_domain *domain;
+	spinlock_t *lock;
+};
+
+static int fhctl_set_ssc_regs(struct fh_pll_regs *regs,
+			      struct fh_pll_data *data,
+			      int fh_id, int rate)
+{
+	unsigned int updnlmt_val;
+
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 44

Quoted 4 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +4 -0
+	if (rate > 0) {
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+		fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);

Are all of these writes to be performed with a barrier?
Can't we use writel_relaxed() for some, with a "final" writel() where ordering *really* matters?

Quoted 2 lines

Do this mean use writel_relaxed() on the first two and writel() on the thrid?

If it's important that the hardware has frddsx_en, sfstrx_en, fhctlx_en *before* programming df_val/dt_val, then yes.... otherwise, just use writel_relaxed() everywhere until the last write has to happen.

writel_relaxed(something, a)
writel_relaxed(something_else, b)
writel_relaxed(something_more, a)
writel_relaxed(another_one, c)
writel_relaxed(blah, b)
writel(reg_cfg, X)

....but having a second look at it, this doesn't make a lot of sense because you are anyway performing multiple writes to the same reg_cfg, so I think that you can as well aggregate the writes in one and reduce the barriers like that, so we'd have something like:

u32 pcw_val, val;

/* Important: This assumes that the contents of reg_cfg never change during the
execution of this programming sequence. */
val = readl_relaxed(regs->reg_cfg);
pcw_val = readl_relaxed(regs->reg_con_pcw) & data->dds_mask;

/* Pause/disable Frequency Hopping controller for reconfiguration */

/* P.S.: can't we use FHCTLx_PAUSE instead of turning off?? */

/* SSC: Disable free-run mode */
val &= ~data->frddsx_en;

/* Disable Soft-start mode */
val &= ~data->sfstrx_en;

/* Disable Frequency Hopping controller */
val &= ~data->fhctlx_en;

writel(val, regs->reg_cfg);

/* **** warning: I'm covering only the enablement flow, not the disablement **** */

/* SSC Slope: Set delta frequency, delta time (df/dt) */ val |= data->df_val & data->msk_frddsx_dys;
val |= data->dt_val & data->msk_frddsx_dts;

/* is it important to write these before DDS?
* no -> writel_relaxed; yes -> writel
*/
writel_relaxed(val, regs->reg_cfg);

/* Update PLL Toggle value */ writel_relaxed(pcw_val | data->tgl_org, regs->reg_dds);

/* SSC Swing: Calculate upper/lower limits */
updnlmt_val = PERCENT_TO_DDSLMT((readl_relaxed(regs->reg_dds) & data->dds_mask),

				rate << data->updnlmt_shft);

writel_relaxed(updnlmt_val, regs->reg_updnlmt);

/* Hand over PLL control to FHCTL */
fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);

/* Re-Enable SSC and Hopping control */
val |= data->frddsx_en | data->fhctlx_en;
writel(val, data->reg_cfg);

Roughly, that's the idea.
Also, keep in mind that aggregating the writes when possible is already improving the flow... relaxing R/W is another improvement though... but beware that technically this is important only in performance paths (so if this function gets called only very few times in a kernel life, it's not really important to use _relaxed accessors).

Besides... I don't *really* like seeing the fh_{set, get}_field helpers... they're confusing at best, and open-coding the R/W makes you able to aggregate fields in one write without impacting on human readability.

Quoted 143 lines
Quoted 3 lines

Also, at least these three field settings are common between (rate > 0) and (rate <= 0), so they can go outside of the conditional.

OK, we will move them to outside of conditional. Thanks.

Quoted 77 lines
Quoted 45 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +44 -0
+
+		/* Set the relative parameter registers (dt/df/upbnd/downbnd) */
+		fh_set_field(regs->reg_cfg, data->msk_frddsx_dys, data->df_val);
+		fh_set_field(regs->reg_cfg, data->msk_frddsx_dts, data->dt_val);
+
+		writel((readl(regs->reg_con_pcw) & data->dds_mask) |
+			data->tgl_org, regs->reg_dds);
+
+		/* Calculate UPDNLMT */
+		updnlmt_val = PERCENT_TO_DDSLMT((readl(regs->reg_dds) &
+						 data->dds_mask), rate) <<
+						 data->updnlmt_shft;
+
+		writel(updnlmt_val, regs->reg_updnlmt);
+
+		fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
+
+		/* Enable SSC */
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 1);
+		/* Enable Hopping control */
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+
+	} else {
+		fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+		fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+		fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);
+
+		/* Switch to APMIXEDSYS control */
+		fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+		/* Wait for DDS to be stable */
+		udelay(30);
+	}
+
+	return 0;
+}
+
+static int hopping_hw_flow(void *priv_data, char *domain_name, int fh_id,
+			   unsigned int new_dds, int postdiv)
+{
+	struct fh_pll_domain *domain;
+	struct fh_pll_regs *regs;
+	struct fh_pll_data *data;
+	unsigned int dds_mask;

drivers/clk/mediatek/clk-fhctl-ap.c line 98

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+	unsigned int mon_dds = 0;
+	int ret = 0;
+	unsigned int con_pcw_tmp;
+	struct hdlr_data *d = (struct hdlr_data *)priv_data;
+	struct pll_dts *array = d->array;
+
+	domain = d->domain;
+	regs = &domain->regs[fh_id];
+	data = &domain->data[fh_id];
+	dds_mask = data->dds_mask;

Just perform these assignments in the variable declarations... with some reordering as well, and drop the zero assignment to ret.

In few words:

	struct hdlr_data *d = (struct hdlr_data *)priv_data;

	struct fh_pll_domain *domain = d->domain;

	struct fh_pll_regs *regs = &domain->regs[fh_id];

	struct fh_pll_data *data = &domain->data[fh_id];

	struct pll_dts *array = d->array;

	u32 con_pcw_tmp, dds_mask;

	u32 mon_dds = 0;

	int ret;
Quoted 2 lines

This comment is valid for some other functions as well - I won't repeat this for every instance... :-)

OK, we will merge them. Thanks.

Quoted 14 lines
Quoted 14 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +13 -0
+
+	if (array->ssc_rate)
+		fhctl_set_ssc_regs(regs, data, fh_id, 0);
+
+	writel((readl(regs->reg_con_pcw) & dds_mask) |
+		data->tgl_org, regs->reg_dds);
+
+	fh_set_field(regs->reg_cfg, data->sfstrx_en, 1);
+	fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+	writel(data->slope0_value, regs->reg_slope0);
+	writel(data->slope1_value, regs->reg_slope1);
+
+	fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 116

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +5 -0
+	writel((new_dds) | (data->dvfs_tri), regs->reg_dvfs);
+
+	/* Wait 1000 us until DDS stable */
+	ret = readl_poll_timeout_atomic(regs->reg_mon, mon_dds,
+				(mon_dds & dds_mask) == new_dds, 10, 1000);

Why are you writing to CON_PCW even when this returns en error? Please add a comment explaining the reasons.

Oh, we will add a warning log and dump HW register when this returns an error The reg_mon is a register reflects the current frequency rate. So, it's fine to write the current rate back to CON_PCW. We will also add a comment on it. Thanks

Quoted 19 lines
Quoted 19 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +18 -0
+
+	con_pcw_tmp = readl(regs->reg_con_pcw) & (~dds_mask);
+	con_pcw_tmp = (con_pcw_tmp | (readl(regs->reg_mon) & dds_mask) |
+		       data->pcwchg);
+
+	writel(con_pcw_tmp, regs->reg_con_pcw);
+
+	fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+	if (array->ssc_rate)
+		fhctl_set_ssc_regs(regs, data, fh_id, array->ssc_rate);
+
+	return ret;
+}
+
+static unsigned int __get_postdiv(struct fh_pll_regs *regs,
+				  struct fh_pll_data *data)
+{
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 140

Quoted 6 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +6 -0
+	unsigned int regval;
+
+	regval = (readl(regs->reg_con_postdiv) & data->postdiv_mask)
+		  >> data->postdiv_offset;
+
+	return data->postdiv_table[regval];

Can we instead simply reuse struct clk_div_table from clk-provider.h?

Quoted 3 lines

"postdiv" is part of setting in PCW_CON, not a individual clk divider. I think it's not suitable to use struct clk_div_table here.

Uhm, I don't think that struct clk_div_table is tied to individual clk dividers in its definition... I mean, it shouldn't be a problem to reuse it in this case... The advantage of it is that we are able to set a clear idx<->divider relation.

Anyway, if you have strong feelings about not using clk_div_table, it's ok, unless anyone else has considerations about that.

#8 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Quoted 251 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

     PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

         PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Quoted 2 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 2 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

Quoted 2 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)
* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)
* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Quoted 7 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 112 lines
Quoted 32 lines

In any case, to speed up development (I believe that transferring this in clk-pll means that the code will still be more or less the same), I've performed a review on the code; check below.

Quoted 27 lines
---
   drivers/clk/mediatek/Kconfig          |   8 +
   drivers/clk/mediatek/Makefile         |   2 +
   drivers/clk/mediatek/clk-fhctl-ap.c   | 347 ++++++++++++++++++++++++++
   drivers/clk/mediatek/clk-fhctl-pll.c  | 209 ++++++++++++++++
   drivers/clk/mediatek/clk-fhctl-pll.h  |  74 ++++++
   drivers/clk/mediatek/clk-fhctl-util.h |  24 ++
   drivers/clk/mediatek/clk-fhctl.c      | 191 ++++++++++++++
   drivers/clk/mediatek/clk-fhctl.h      |  45 ++++
   drivers/clk/mediatek/clk-pll.c        |   5 +-
   drivers/clk/mediatek/clk-pll.h        |   5 +
   10 files changed, 909 insertions(+), 1 deletion(-)

create mode 100644 drivers/clk/mediatek/clk-fhctl-ap.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.h create mode 100644 drivers/clk/mediatek/clk-fhctl-util.h create mode 100644 drivers/clk/mediatek/clk-fhctl.c create mode 100644 drivers/clk/mediatek/clk-fhctl.h

drivers/clk/mediatek/Kconfig +0 -0
index d5936cfb3bee..fd887c537a91 100644
@@ -622,4 +622,12 @@ config COMMON_CLK_MT8516_AUDSYS
    help
      This driver supports MediaTek MT8516 audsys clocks.
 
Quoted 4 lines

drivers/clk/mediatek/Kconfig config COMMON_CLK_MT8516_AUDSYS · line 626

Quoted 2 lines
drivers/clk/mediatek/Kconfig +2 -0
+config COMMON_CLK_MTK_FREQ_HOPPING
+   tristate "MediaTek frequency hopping driver"

If this goes inside of clk-pll, this configuration option can be safely removed.

I think we should keep this for clk-fhctl* files.

Quoted 64 lines
Quoted 64 lines
drivers/clk/mediatek/Kconfig +6 -0
+   depends on ARCH_MEDIATEK || COMPILE_TEST
+   select COMMON_CLK_MEDIATEK
+   help
+     This driver supports frequency hopping and spread spectrum clocking
+     control for some MediaTek SoCs.
+
   endmenu
drivers/clk/mediatek/Makefile +2 -0
index caf2ce93d666..3c0e9bd3978b 100644
@@ -99,3 +99,5 @@ obj-$(CONFIG_COMMON_CLK_MT8195) += clk-mt8195-apmixedsys.o clk-mt8195-topckgen.o
                               clk-mt8195-apusys_pll.o
   obj-$(CONFIG_COMMON_CLK_MT8516) += clk-mt8516.o
   obj-$(CONFIG_COMMON_CLK_MT8516_AUDSYS) += clk-mt8516-aud.o
+obj-$(CONFIG_COMMON_CLK_MTK_FREQ_HOPPING) += fhctl.o
+fhctl-objs += clk-fhctl.o clk-fhctl-ap.o clk-fhctl-pll.o
drivers/clk/mediatek/clk-fhctl-ap.c new file +40 -0
new file mode 100644
index 000000000000..9e3226a9c1ca
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include "clk-fhctl.h"
+#include "clk-fhctl-pll.h"
+#include "clk-fhctl-util.h"
+
+#define FHCTL_TARGET FHCTL_AP
+
+#define PERCENT_TO_DDSLMT(dds, percent_m10) \
+   ((((dds) * (percent_m10)) >> 5) / 100)
+
+struct fh_ap_match {
+   char *name;
+   struct fh_hdlr *hdlr;
+   int (*init)(struct pll_dts *array, struct fh_ap_match *match);
+};
+
+struct hdlr_data {
+   struct pll_dts *array;
+   struct fh_pll_domain *domain;
+   spinlock_t *lock;
+};
+
+static int fhctl_set_ssc_regs(struct fh_pll_regs *regs,
+                         struct fh_pll_data *data,
+                         int fh_id, int rate)
+{
+   unsigned int updnlmt_val;
+
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 44

Quoted 4 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +4 -0
+   if (rate > 0) {
+           fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+           fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+           fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);

Are all of these writes to be performed with a barrier?
Can't we use writel_relaxed() for some, with a "final" writel() where ordering *really* matters?

Quoted 2 lines

Do this mean use writel_relaxed() on the first two and writel() on the thrid?

If it's important that the hardware has frddsx_en, sfstrx_en, fhctlx_en *before* programming df_val/dt_val, then yes.... otherwise, just use writel_relaxed() everywhere until the last write has to happen.

writel_relaxed(something,       a)
writel_relaxed(something_else,  b)
writel_relaxed(something_more,  a)
writel_relaxed(another_one,     c)
writel_relaxed(blah,            b)
writel(reg_cfg,                 X)
Quoted 3 lines

....but having a second look at it, this doesn't make a lot of sense because you are anyway performing multiple writes to the same reg_cfg, so I think that you can as well aggregate the writes in one and reduce the barriers like that, so

Writes to the same endpoint are always ordered. The barrier gives you ordering against writes to memory _only_.

MMIO addresses are mapped as Device-nGnRE. nR specifically means non-reordering.

If you want to make sure writes hit the hardware, instead of getting queued up in the interconnects, you need to read back the register from the hardware.

For reference, see this talk: https://youtu.be/i6DayghhA8Q
Note: I banged my head against this more than a couple times.

ChenYu

Quoted 221 lines

we'd have something like:

u32 pcw_val, val;

/* Important: This assumes that the contents of reg_cfg never change during the
execution of this programming sequence. */
val = readl_relaxed(regs->reg_cfg);
pcw_val = readl_relaxed(regs->reg_con_pcw) & data->dds_mask;

/* Pause/disable Frequency Hopping controller for reconfiguration */

/* P.S.: can't we use FHCTLx_PAUSE instead of turning off?? */

/* SSC: Disable free-run mode */
val &= ~data->frddsx_en;

/* Disable Soft-start mode */
val &= ~data->sfstrx_en;

/* Disable Frequency Hopping controller */
val &= ~data->fhctlx_en;

writel(val, regs->reg_cfg);

/* **** warning: I'm covering only the enablement flow, not the disablement **** */

/* SSC Slope: Set delta frequency, delta time (df/dt) */ val |= data->df_val & data->msk_frddsx_dys;
val |= data->dt_val & data->msk_frddsx_dts;

/* is it important to write these before DDS?
* no -> writel_relaxed; yes -> writel
*/
writel_relaxed(val, regs->reg_cfg);

/* Update PLL Toggle value */ writel_relaxed(pcw_val | data->tgl_org, regs->reg_dds);

/* SSC Swing: Calculate upper/lower limits */
updnlmt_val = PERCENT_TO_DDSLMT((readl_relaxed(regs->reg_dds) & data->dds_mask),

                                rate << data->updnlmt_shft);

writel_relaxed(updnlmt_val, regs->reg_updnlmt);

/* Hand over PLL control to FHCTL */
fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);

/* Re-Enable SSC and Hopping control */
val |= data->frddsx_en | data->fhctlx_en;
writel(val, data->reg_cfg);

Roughly, that's the idea.
Also, keep in mind that aggregating the writes when possible is already improving the flow... relaxing R/W is another improvement though... but beware that technically this is important only in performance paths (so if this function gets called only very few times in a kernel life, it's not really important to use _relaxed accessors).

Besides... I don't *really* like seeing the fh_{set, get}_field helpers... they're confusing at best, and open-coding the R/W makes you able to aggregate fields in one write without impacting on human readability.

Quoted 143 lines
Quoted 3 lines

Also, at least these three field settings are common between (rate > 0) and (rate <= 0), so they can go outside of the conditional.

OK, we will move them to outside of conditional. Thanks.

Quoted 77 lines
Quoted 45 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +44 -0
+
+           /* Set the relative parameter registers (dt/df/upbnd/downbnd) */
+           fh_set_field(regs->reg_cfg, data->msk_frddsx_dys, data->df_val);
+           fh_set_field(regs->reg_cfg, data->msk_frddsx_dts, data->dt_val);
+
+           writel((readl(regs->reg_con_pcw) & data->dds_mask) |
+                   data->tgl_org, regs->reg_dds);
+
+           /* Calculate UPDNLMT */
+           updnlmt_val = PERCENT_TO_DDSLMT((readl(regs->reg_dds) &
+                                            data->dds_mask), rate) <<
+                                            data->updnlmt_shft;
+
+           writel(updnlmt_val, regs->reg_updnlmt);
+
+           fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
+
+           /* Enable SSC */
+           fh_set_field(regs->reg_cfg, data->frddsx_en, 1);
+           /* Enable Hopping control */
+           fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+
+   } else {
+           fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+           fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+           fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);
+
+           /* Switch to APMIXEDSYS control */
+           fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+           /* Wait for DDS to be stable */
+           udelay(30);
+   }
+
+   return 0;
+}
+
+static int hopping_hw_flow(void *priv_data, char *domain_name, int fh_id,
+                      unsigned int new_dds, int postdiv)
+{
+   struct fh_pll_domain *domain;
+   struct fh_pll_regs *regs;
+   struct fh_pll_data *data;
+   unsigned int dds_mask;

drivers/clk/mediatek/clk-fhctl-ap.c line 98

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+   unsigned int mon_dds = 0;
+   int ret = 0;
+   unsigned int con_pcw_tmp;
+   struct hdlr_data *d = (struct hdlr_data *)priv_data;
+   struct pll_dts *array = d->array;
+
+   domain = d->domain;
+   regs = &domain->regs[fh_id];
+   data = &domain->data[fh_id];
+   dds_mask = data->dds_mask;

Just perform these assignments in the variable declarations... with some reordering as well, and drop the zero assignment to ret.

In few words:

     struct hdlr_data *d = (struct hdlr_data *)priv_data;

     struct fh_pll_domain *domain = d->domain;

     struct fh_pll_regs *regs = &domain->regs[fh_id];

     struct fh_pll_data *data = &domain->data[fh_id];

     struct pll_dts *array = d->array;

     u32 con_pcw_tmp, dds_mask;

     u32 mon_dds = 0;

     int ret;
Quoted 2 lines

This comment is valid for some other functions as well - I won't repeat this for every instance... :-)

OK, we will merge them. Thanks.

Quoted 14 lines
Quoted 14 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +13 -0
+
+   if (array->ssc_rate)
+           fhctl_set_ssc_regs(regs, data, fh_id, 0);
+
+   writel((readl(regs->reg_con_pcw) & dds_mask) |
+           data->tgl_org, regs->reg_dds);
+
+   fh_set_field(regs->reg_cfg, data->sfstrx_en, 1);
+   fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+   writel(data->slope0_value, regs->reg_slope0);
+   writel(data->slope1_value, regs->reg_slope1);
+
+   fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 116

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +5 -0
+   writel((new_dds) | (data->dvfs_tri), regs->reg_dvfs);
+
+   /* Wait 1000 us until DDS stable */
+   ret = readl_poll_timeout_atomic(regs->reg_mon, mon_dds,
+                           (mon_dds & dds_mask) == new_dds, 10, 1000);

Why are you writing to CON_PCW even when this returns en error? Please add a comment explaining the reasons.

Oh, we will add a warning log and dump HW register when this returns an error The reg_mon is a register reflects the current frequency rate. So, it's fine to write the current rate back to CON_PCW. We will also add a comment on it. Thanks

Quoted 19 lines
Quoted 19 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +18 -0
+
+   con_pcw_tmp = readl(regs->reg_con_pcw) & (~dds_mask);
+   con_pcw_tmp = (con_pcw_tmp | (readl(regs->reg_mon) & dds_mask) |
+                  data->pcwchg);
+
+   writel(con_pcw_tmp, regs->reg_con_pcw);
+
+   fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+   if (array->ssc_rate)
+           fhctl_set_ssc_regs(regs, data, fh_id, array->ssc_rate);
+
+   return ret;
+}
+
+static unsigned int __get_postdiv(struct fh_pll_regs *regs,
+                             struct fh_pll_data *data)
+{
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 140

Quoted 6 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +6 -0
+   unsigned int regval;
+
+   regval = (readl(regs->reg_con_postdiv) & data->postdiv_mask)
+             >> data->postdiv_offset;
+
+   return data->postdiv_table[regval];

Can we instead simply reuse struct clk_div_table from clk-provider.h?

Quoted 3 lines

"postdiv" is part of setting in PCW_CON, not a individual clk divider. I think it's not suitable to use struct clk_div_table here.

Uhm, I don't think that struct clk_div_table is tied to individual clk dividers in its definition... I mean, it shouldn't be a problem to reuse it in this case... The advantage of it is that we are able to set a clear idx<->divider relation.

Anyway, if you have strong feelings about not using clk_div_table, it's ok, unless anyone else has considerations about that.

#9 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Quoted 56 lines · Chen-Yu Tsai
Quoted 54 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

     PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

         PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.
So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

Quoted 7 lines
Quoted 7 lines

(note: the pll names in the example are random and not specific to anything)

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Debugging feature is not used yet, we can removed it.

Quoted 2 lines
Quoted 2 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Quoted 6 lines
Quoted 6 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Quoted 137 lines
Quoted 127 lines
Quoted 113 lines
Quoted 33 lines

In any case, to speed up development (I believe that transferring this in clk-pll means that the code will still be more or less the same), I've performed a review on the code; check below.

Quoted 27 lines
---
   drivers/clk/mediatek/Kconfig          |   8 +
   drivers/clk/mediatek/Makefile         |   2 +
   drivers/clk/mediatek/clk-fhctl-ap.c   | 347 ++++++++++++++++++++++++++
   drivers/clk/mediatek/clk-fhctl-pll.c  | 209 ++++++++++++++++
   drivers/clk/mediatek/clk-fhctl-pll.h  |  74 ++++++
   drivers/clk/mediatek/clk-fhctl-util.h |  24 ++
   drivers/clk/mediatek/clk-fhctl.c      | 191 ++++++++++++++
   drivers/clk/mediatek/clk-fhctl.h      |  45 ++++
   drivers/clk/mediatek/clk-pll.c        |   5 +-
   drivers/clk/mediatek/clk-pll.h        |   5 +
   10 files changed, 909 insertions(+), 1 deletion(-)

create mode 100644 drivers/clk/mediatek/clk-fhctl-ap.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.c create mode 100644 drivers/clk/mediatek/clk-fhctl-pll.h create mode 100644 drivers/clk/mediatek/clk-fhctl-util.h create mode 100644 drivers/clk/mediatek/clk-fhctl.c create mode 100644 drivers/clk/mediatek/clk-fhctl.h

drivers/clk/mediatek/Kconfig +0 -0
index d5936cfb3bee..fd887c537a91 100644
@@ -622,4 +622,12 @@ config COMMON_CLK_MT8516_AUDSYS
    help
      This driver supports MediaTek MT8516 audsys clocks.
 
Quoted 4 lines

drivers/clk/mediatek/Kconfig config COMMON_CLK_MT8516_AUDSYS · line 626

Quoted 2 lines
drivers/clk/mediatek/Kconfig +2 -0
+config COMMON_CLK_MTK_FREQ_HOPPING
+   tristate "MediaTek frequency hopping driver"

If this goes inside of clk-pll, this configuration option can be safely removed.

I think we should keep this for clk-fhctl* files.

Quoted 64 lines
Quoted 64 lines
drivers/clk/mediatek/Kconfig +6 -0
+   depends on ARCH_MEDIATEK || COMPILE_TEST
+   select COMMON_CLK_MEDIATEK
+   help
+     This driver supports frequency hopping and spread spectrum clocking
+     control for some MediaTek SoCs.
+
   endmenu
drivers/clk/mediatek/Makefile +2 -0
index caf2ce93d666..3c0e9bd3978b 100644
@@ -99,3 +99,5 @@ obj-$(CONFIG_COMMON_CLK_MT8195) += clk-mt8195-apmixedsys.o clk-mt8195-topckgen.o
                               clk-mt8195-apusys_pll.o
   obj-$(CONFIG_COMMON_CLK_MT8516) += clk-mt8516.o
   obj-$(CONFIG_COMMON_CLK_MT8516_AUDSYS) += clk-mt8516-aud.o
+obj-$(CONFIG_COMMON_CLK_MTK_FREQ_HOPPING) += fhctl.o
+fhctl-objs += clk-fhctl.o clk-fhctl-ap.o clk-fhctl-pll.o
drivers/clk/mediatek/clk-fhctl-ap.c new file +40 -0
new file mode 100644
index 000000000000..9e3226a9c1ca
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022 MediaTek Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include "clk-fhctl.h"
+#include "clk-fhctl-pll.h"
+#include "clk-fhctl-util.h"
+
+#define FHCTL_TARGET FHCTL_AP
+
+#define PERCENT_TO_DDSLMT(dds, percent_m10) \
+   ((((dds) * (percent_m10)) >> 5) / 100)
+
+struct fh_ap_match {
+   char *name;
+   struct fh_hdlr *hdlr;
+   int (*init)(struct pll_dts *array, struct fh_ap_match *match);
+};
+
+struct hdlr_data {
+   struct pll_dts *array;
+   struct fh_pll_domain *domain;
+   spinlock_t *lock;
+};
+
+static int fhctl_set_ssc_regs(struct fh_pll_regs *regs,
+                         struct fh_pll_data *data,
+                         int fh_id, int rate)
+{
+   unsigned int updnlmt_val;
+
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 44

Quoted 4 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +4 -0
+   if (rate > 0) {
+           fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+           fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+           fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);

Are all of these writes to be performed with a barrier?
Can't we use writel_relaxed() for some, with a "final" writel() where ordering *really* matters?

Quoted 2 lines

Do this mean use writel_relaxed() on the first two and writel() on the thrid?

If it's important that the hardware has frddsx_en, sfstrx_en, fhctlx_en *before* programming df_val/dt_val, then yes.... otherwise, just use writel_relaxed() everywhere until the last write has to happen.

writel_relaxed(something,       a)
writel_relaxed(something_else,  b)
writel_relaxed(something_more,  a)
writel_relaxed(another_one,     c)
writel_relaxed(blah,            b)
writel(reg_cfg,                 X)
Quoted 3 lines

....but having a second look at it, this doesn't make a lot of sense because you are anyway performing multiple writes to the same reg_cfg, so I think that you can as well aggregate the writes in one and reduce the barriers like that, so

Writes to the same endpoint are always ordered. The barrier gives you ordering against writes to memory _only_.

MMIO addresses are mapped as Device-nGnRE. nR specifically means non-reordering.

Quoted 8 lines

If you want to make sure writes hit the hardware, instead of getting queued up in the interconnects, you need to read back the register from the hardware.

For reference, see this talk: https://urldefense.com/v3/__https://youtu.be/i6DayghhA8Q__;!!CTRNKA9wMg0ARbw!yxO58UINgJKmjJDMFJoDC40Y_B9sd6L5xnumLLgXJMaj1z0tnQMXw-fv-cEzghuy1cPSXKc$
Note: I banged my head against this more than a couple times.

ChenYu

Thanks for example.
Confirm with our HW design, frddsx_en, sfsrx_en, fhctlx_en must to set by order. We will use writel here instead of writel_relaxed.

Quoted 11 lines
Quoted 11 lines

we'd have something like:

u32 pcw_val, val;

/* Important: This assumes that the contents of reg_cfg never change during the
execution of this programming sequence. */
val = readl_relaxed(regs->reg_cfg);
pcw_val = readl_relaxed(regs->reg_con_pcw) & data->dds_mask;

Quoted 4 lines
Quoted 4 lines

/* Pause/disable Frequency Hopping controller for reconfiguration */

/* P.S.: can't we use FHCTLx_PAUSE instead of turning off?? */

Should disable Frequency Hopping controller before configuration.

Quoted 48 lines
Quoted 48 lines

/* SSC: Disable free-run mode */
val &= ~data->frddsx_en;

/* Disable Soft-start mode */
val &= ~data->sfstrx_en;

/* Disable Frequency Hopping controller */
val &= ~data->fhctlx_en;

writel(val, regs->reg_cfg);

/* **** warning: I'm covering only the enablement flow, not the disablement **** */

/* SSC Slope: Set delta frequency, delta time (df/dt) */ val |= data->df_val & data->msk_frddsx_dys;
val |= data->dt_val & data->msk_frddsx_dts;

/* is it important to write these before DDS?
* no -> writel_relaxed; yes -> writel
*/
writel_relaxed(val, regs->reg_cfg);

/* Update PLL Toggle value */ writel_relaxed(pcw_val | data->tgl_org, regs->reg_dds);

/* SSC Swing: Calculate upper/lower limits */
updnlmt_val = PERCENT_TO_DDSLMT((readl_relaxed(regs->reg_dds) & data->dds_mask),

                                rate << data->updnlmt_shft);

writel_relaxed(updnlmt_val, regs->reg_updnlmt);

/* Hand over PLL control to FHCTL */
fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);

/* Re-Enable SSC and Hopping control */
val |= data->frddsx_en | data->fhctlx_en;
writel(val, data->reg_cfg);

Roughly, that's the idea.
Also, keep in mind that aggregating the writes when possible is already improving the flow... relaxing R/W is another improvement though... but beware that technically this is important only in performance paths (so if this function gets called only very few times in a kernel life, it's not really important to use _relaxed accessors).

Quoted 3 lines
Quoted 3 lines

Besides... I don't *really* like seeing the fh_{set, get}_field helpers... they're confusing at best, and open-coding the R/W makes you able to aggregate fields in one write without impacting on human readability.

We will change fh_{set, get}_filed helpers to writel, readl. Thanks.

Quoted 149 lines
Quoted 149 lines
Quoted 148 lines
Quoted 5 lines

Also, at least these three field settings are common between (rate > 0) and (rate <= 0), so they can go outside of the conditional.

OK, we will move them to outside of conditional. Thanks.

Quoted 77 lines
Quoted 45 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +44 -0
+
+           /* Set the relative parameter registers (dt/df/upbnd/downbnd) */
+           fh_set_field(regs->reg_cfg, data->msk_frddsx_dys, data->df_val);
+           fh_set_field(regs->reg_cfg, data->msk_frddsx_dts, data->dt_val);
+
+           writel((readl(regs->reg_con_pcw) & data->dds_mask) |
+                   data->tgl_org, regs->reg_dds);
+
+           /* Calculate UPDNLMT */
+           updnlmt_val = PERCENT_TO_DDSLMT((readl(regs->reg_dds) &
+                                            data->dds_mask), rate) <<
+                                            data->updnlmt_shft;
+
+           writel(updnlmt_val, regs->reg_updnlmt);
+
+           fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
+
+           /* Enable SSC */
+           fh_set_field(regs->reg_cfg, data->frddsx_en, 1);
+           /* Enable Hopping control */
+           fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+
+   } else {
+           fh_set_field(regs->reg_cfg, data->frddsx_en, 0);
+           fh_set_field(regs->reg_cfg, data->sfstrx_en, 0);
+           fh_set_field(regs->reg_cfg, data->fhctlx_en, 0);
+
+           /* Switch to APMIXEDSYS control */
+           fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+           /* Wait for DDS to be stable */
+           udelay(30);
+   }
+
+   return 0;
+}
+
+static int hopping_hw_flow(void *priv_data, char *domain_name, int fh_id,
+                      unsigned int new_dds, int postdiv)
+{
+   struct fh_pll_domain *domain;
+   struct fh_pll_regs *regs;
+   struct fh_pll_data *data;
+   unsigned int dds_mask;

drivers/clk/mediatek/clk-fhctl-ap.c line 98

Quoted 10 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +10 -0
+   unsigned int mon_dds = 0;
+   int ret = 0;
+   unsigned int con_pcw_tmp;
+   struct hdlr_data *d = (struct hdlr_data *)priv_data;
+   struct pll_dts *array = d->array;
+
+   domain = d->domain;
+   regs = &domain->regs[fh_id];
+   data = &domain->data[fh_id];
+   dds_mask = data->dds_mask;

Just perform these assignments in the variable declarations... with some reordering as well, and drop the zero assignment to ret.

In few words:

     struct hdlr_data *d = (struct hdlr_data *)priv_data;

     struct fh_pll_domain *domain = d->domain;

     struct fh_pll_regs *regs = &domain->regs[fh_id];

     struct fh_pll_data *data = &domain->data[fh_id];

     struct pll_dts *array = d->array;

     u32 con_pcw_tmp, dds_mask;

     u32 mon_dds = 0;

     int ret;
Quoted 2 lines

This comment is valid for some other functions as well - I won't repeat this for every instance... :-)

OK, we will merge them. Thanks.

Quoted 14 lines
Quoted 14 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +13 -0
+
+   if (array->ssc_rate)
+           fhctl_set_ssc_regs(regs, data, fh_id, 0);
+
+   writel((readl(regs->reg_con_pcw) & dds_mask) |
+           data->tgl_org, regs->reg_dds);
+
+   fh_set_field(regs->reg_cfg, data->sfstrx_en, 1);
+   fh_set_field(regs->reg_cfg, data->fhctlx_en, 1);
+   writel(data->slope0_value, regs->reg_slope0);
+   writel(data->slope1_value, regs->reg_slope1);
+
+   fh_set_field(regs->reg_hp_en, BIT(fh_id), 1);
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 116

Quoted 5 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +5 -0
+   writel((new_dds) | (data->dvfs_tri), regs->reg_dvfs);
+
+   /* Wait 1000 us until DDS stable */
+   ret = readl_poll_timeout_atomic(regs->reg_mon, mon_dds,
+                           (mon_dds & dds_mask) == new_dds, 10, 1000);

Why are you writing to CON_PCW even when this returns en error? Please add a comment explaining the reasons.

Oh, we will add a warning log and dump HW register when this returns an error The reg_mon is a register reflects the current frequency rate. So, it's fine to write the current rate back to CON_PCW. We will also add a comment on it. Thanks

Quoted 19 lines
Quoted 19 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +18 -0
+
+   con_pcw_tmp = readl(regs->reg_con_pcw) & (~dds_mask);
+   con_pcw_tmp = (con_pcw_tmp | (readl(regs->reg_mon) & dds_mask) |
+                  data->pcwchg);
+
+   writel(con_pcw_tmp, regs->reg_con_pcw);
+
+   fh_set_field(regs->reg_hp_en, BIT(fh_id), 0);
+
+   if (array->ssc_rate)
+           fhctl_set_ssc_regs(regs, data, fh_id, array->ssc_rate);
+
+   return ret;
+}
+
+static unsigned int __get_postdiv(struct fh_pll_regs *regs,
+                             struct fh_pll_data *data)
+{
Quoted 8 lines

drivers/clk/mediatek/clk-fhctl-ap.c line 140

Quoted 6 lines
drivers/clk/mediatek/clk-fhctl-ap.c new file +6 -0
+   unsigned int regval;
+
+   regval = (readl(regs->reg_con_postdiv) & data->postdiv_mask)
+             >> data->postdiv_offset;
+
+   return data->postdiv_table[regval];

Can we instead simply reuse struct clk_div_table from clk-provider.h?

"postdiv" is part of setting in PCW_CON, not a individual clk divider. I think it's not suitable to use struct clk_div_table here.

Quoted 7 lines
Quoted 7 lines

Uhm, I don't think that struct clk_div_table is tied to individual clk dividers in its definition... I mean, it shouldn't be a problem to reuse it in this case... The advantage of it is that we are able to set a clear idx<->divider relation.

Anyway, if you have strong feelings about not using clk_div_table, it's ok, unless anyone else has considerations about that.

We still want to use postdiv_table. Thanks.

#10 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 60 lines · Chen-Yu Tsai
Quoted 59 lines
Quoted 57 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

      PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

          PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;
Quoted 3 lines

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.

Where we declare the PLLs, for example, in clk-mt8186-apmixedsys.c, we can declare that such PLL can be managed by FHCTL, for example:

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208),

becomes

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208, true);

where 'true' means "FHCTL is supported".

Then, we register the PLLs with something like:

mtk_clk_register_plls(node, plls, num_plls, clk_data, fhctl_register_version);

...where fhctl_register_version is used to assign the right fhctl register offsets. Also, it's not needed to assign all of the register offsets statically, because they can be easily calculated based on the number of supported PLLs, since the registers are structured like

[FHCTL GLOBAL REGISTERS] <--- hp_en...slope1 [FHCTL SSC GLOBAL REGISTERS] <--- DSSC_CFG, DSSC0...x_CON

[FHCTL PER-PLL REGISTERS] <--- CFG...MON
^^^ where this is repeated X times for X PLLs.

so, keeping the example of MT8186, we can get the per-pll register like:

#define FHCTL_PLL_OFFSET 0x3c
#define FHCTL_PLL_LEN 0x14

#define FHCTLx_CFG(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN))
#define FHCTLx_UPDNLMT(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x4)
#define FHCTLx_DDS(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x8)

we don't need to put all of them in a structure and for each PLL.

Quoted 3 lines

So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

As already said, we preventively know which PLLs support FHCTL and which does not, so we can use a different .set_rate() callback.

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 7 lines

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

For case N.1, if this is board-specific, we have to resort to devicetree properties that will enable/disable FHCTL on specific PLLs.

mediatek,fhctl-disable = <CLK_APMIXED_MSDCPLL>, <CLK_APMIXED_NNAPLL>;

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>;

These are just examples - I don't currently know if it's a better idea to have an allowlist or a blocklist as devicetree properties, as that depends on the expected number of PLLs for which we en/dis fhctl or just ssc (if we generally want fhctl enabled on all but one PLLs, we should use fhctl-disable, otherwise, fhctl-enable).

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Ok, so it's definitely board specific. Devicetree is the way to go for this.

Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Debugging feature is not used yet, we can removed it.

If the debugging features of the FHCTL driver will be like what I can see on the downstream MT6893 5.10 kernel, that's not really applicable to upstream.

In that case, please remove the debug.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Use the flag to set the right .set_rate() callback, set at probe time, instead of checking that flag at every set_rate() call.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Board specific -> devicetree

SoC specific -> hardcode, no devicetree.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 5 lines

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Let's use clk-fhctl as an helper then, we can make sure to call the init flow for the microP in the SoC-specific clock drivers, I think that's not a problem?

clk_mtfuturesoc_someip_probe()

{
	.... register clocks ....

	freqhopping_microp_init();

	return ret;
}

If there's hardware out there that supports such feature and a downstream kernel to look at, please tell me which one, so that I will be able to check it out and perhaps understand how this flow works.

P.S.: I guess it's not fhctl-sspm?

Regards,
Angelo

#11 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Quoted 82 lines · On Thu, 14 Jul 2022 13:04:49 +0200 AngeloGioacchino Del Regno…

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 66 lines · Chen-Yu Tsai
Quoted 65 lines
Quoted 63 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 44 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 39 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

      PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

          PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2,

FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the
direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe?
They're SoC-related, not board-related, right?

Quoted 7 lines
Quoted 4 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.:
mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 3 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

Quoted 7 lines

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;

Looks like accepting my patch [1] wouldn't be a bad idea, after all.
[1] https://lists.infradead.org/pipermail/linux-mediatek/2022-May/041293.html

#12 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

In reply to AngeloGioacchino Del Regno

Hi AngeloGioacchino,

Thanks for all the advices and examples.

Quoted 95 lines · AngeloGioacchino Del Regno

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 60 lines · Chen-Yu Tsai
Quoted 59 lines
Quoted 57 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

      PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

          PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;
Quoted 3 lines

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.

Where we declare the PLLs, for example, in clk-mt8186-apmixedsys.c, we can declare that such PLL can be managed by FHCTL, for example:

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208),
Quoted 7 lines

becomes

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208, true);

where 'true' means "FHCTL is supported".

Does it still have an independent FHCTL driver after modifying to this? From your example, setup a clk_ops and add FHCTL properities into PLL(), seems FHCTL driver is merged into clk-pll and become part of clk-pll driver.

We tend to have an indepentent driver and dts for FHCTL, and mutate only .set_rate() callback function instead of whole clk_ops. The boot-up sequence is like:

1. clk-pll + clk dts

	probe  -> clk-pll original flow, nothing to change

        /* clk-pll provide multation API for set_rate */
	/* mutate necessary set_rate() instead of mutating all ops */
		def register_fhctl_set_rate(pll_name, callback)
			ops = find_pll_ops_by_name(pll_name)
			log("change set_rate to fhctl callback for $pll_name")
			ops->set_rate = callback

2. FHCTL driver + fhctl dts
probe
options = parsing dts (board specific, hopping disalbe or ssc-rate) init FHCTL HW
for PLL in dts
if (ssc-rate > 0)
enable_ssc(ssc-rate)
if (hop-enabled)
/* mutate CCF set_rate, FHCTL engaged CCF */ register_fhctl_CCF(pll_name, callback)

Quoted 18 lines

Then, we register the PLLs with something like:

mtk_clk_register_plls(node, plls, num_plls, clk_data, fhctl_register_version);

...where fhctl_register_version is used to assign the right fhctl register offsets. Also, it's not needed to assign all of the register offsets statically, because they can be easily calculated based on the number of supported PLLs, since the registers are structured like

[FHCTL GLOBAL REGISTERS] <--- hp_en...slope1 [FHCTL SSC GLOBAL REGISTERS] <--- DSSC_CFG, DSSC0...x_CON

[FHCTL PER-PLL REGISTERS] <--- CFG...MON
^^^ where this is repeated X times for X PLLs.

so, keeping the example of MT8186, we can get the per-pll register like:

Quoted 8 lines

#define FHCTL_PLL_OFFSET 0x3c
#define FHCTL_PLL_LEN 0x14

#define FHCTLx_CFG(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN))
#define FHCTLx_UPDNLMT(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x4)
#define FHCTLx_DDS(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x8)

we don't need to put all of them in a structure and for each PLL.

We use structure instead of using macros is because the register offset may have difference between ICs. If we use macro, we need to maintain different versions of macros. Using structure to store these register offsets is more flexible.

Quoted 7 lines
Quoted 4 lines

So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

As already said, we preventively know which PLLs support FHCTL and which does not, so we can use a different .set_rate() callback.

Ok, we can use a different .set_rate() callback when fhctl driver probing.

Quoted 28 lines
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 7 lines

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

For case N.1, if this is board-specific, we have to resort to devicetree properties that will enable/disable FHCTL on specific PLLs.

Quoted 8 lines

mediatek,fhctl-disable = <CLK_APMIXED_MSDCPLL>, <CLK_APMIXED_NNAPLL>;

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>;

These are just examples - I don't currently know if it's a better idea to have an allowlist or a blocklist as devicetree properties, as that depends on the expected number of PLLs for which we en/dis fhctl or just ssc (if we generally want fhctl enabled on all but one PLLs, we should use fhctl-disable, otherwise, fhctl-enable).

We also have a properity "ssc-rate" for setting up the ssc rate in percentage. The "ssc- rate" properity is under fhctl dts node and can be setup on each fhctl-PLL.

Quoted 28 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Ok, so it's definitely board specific. Devicetree is the way to go for this.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Quoted 7 lines
Quoted 2 lines

Debugging feature is not used yet, we can removed it.

If the debugging features of the FHCTL driver will be like what I can see on the downstream MT6893 5.10 kernel, that's not really applicable to upstream.

In that case, please remove the debug.

Ok, we will remove it.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Use the flag to set the right .set_rate() callback, set at probe time, instead of checking that flag at every set_rate() call.

We will setup .set_rate() callback when doing fhctl-pll init.

Quoted 59 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Board specific -> devicetree

SoC specific -> hardcode, no devicetree.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 5 lines

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Let's use clk-fhctl as an helper then, we can make sure to call the init flow for the microP in the SoC-specific clock drivers, I think that's not a problem?

clk_mtfuturesoc_someip_probe()

{
	.... register clocks ....

	freqhopping_microp_init();

	return ret;
}
Quoted 5 lines

If there's hardware out there that supports such feature and a downstream kernel to look at, please tell me which one, so that I will be able to check it out and perhaps understand how this flow works.

P.S.: I guess it's not fhctl-sspm?

You could find clk-fhctl-mcupm.c and clk-fhctl-gpueb.c on the downstream MT6893 5.10 kernel. Those codes require the PLL hardware specification to determine which PLL group(eg. PLL TOP group, GPUEB group) runs on which microP and has responsibilty to communicate with the microP.

If we implement these things into clk-pll driver, clk-pll driver not only needs to control PLL frequency but also needs to deal with microP IPI. It makes clk-pll driver have others works that is not belong to PLL operation. That's why we tend to have a standalone driver for FHCTL.

Quoted 3 lines

Regards,
Angelo

#13 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Il 20/07/22 15:51, Edward-JW Yang ha scritto:

Quoted 104 lines

Hi AngeloGioacchino,

Thanks for all the advices and examples.

Quoted 99 lines · AngeloGioacchino Del Regno

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 60 lines · Chen-Yu Tsai
Quoted 59 lines
Quoted 57 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

       PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

           PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;
Quoted 3 lines

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.

Where we declare the PLLs, for example, in clk-mt8186-apmixedsys.c, we can declare that such PLL can be managed by FHCTL, for example:

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208),

becomes

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,
Quoted 8 lines
Quoted 3 lines
	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208, true);

where 'true' means "FHCTL is supported".

Does it still have an independent FHCTL driver after modifying to this? From your example, setup a clk_ops and add FHCTL properities into PLL(), seems FHCTL driver is merged into clk-pll and become part of clk-pll driver.

The direct-MMIO part of FHCTL becomes part of the clk-pll driver, yes - but then I also find it unacceptable to embed the IPI communication inside of there, so we can have an "external" helper for that.

Quoted 13 lines

We tend to have an indepentent driver and dts for FHCTL, and mutate only .set_rate() callback function instead of whole clk_ops. The boot-up sequence is like:

1. clk-pll + clk dts

	probe  -> clk-pll original flow, nothing to change

         /* clk-pll provide multation API for set_rate */
	/* mutate necessary set_rate() instead of mutating all ops */
		def register_fhctl_set_rate(pll_name, callback)
			ops = find_pll_ops_by_name(pll_name)
			log("change set_rate to fhctl callback for $pll_name")
			ops->set_rate = callback
Quoted 11 lines

2. FHCTL driver + fhctl dts
probe
options = parsing dts (board specific, hopping disalbe or ssc-rate) init FHCTL HW
for PLL in dts
if (ssc-rate > 0)
enable_ssc(ssc-rate)
if (hop-enabled)
/* mutate CCF set_rate, FHCTL engaged CCF */ register_fhctl_CCF(pll_name, callback)

I really don't like having PLL names in devicetree: they're already defined in clock drivers and they will change on a per-SoC basis - and we do have per-SoC drivers...

Whatever goes to devicetree should be something that we need to vary on a per-board/platform(project) basis, so, enablement of FHCTL per-pll (by using handles and numeral bindings as per the example that I previously wrote), enablement of spread spectrum and its rate... and nothing else.

Quoted 25 lines
Quoted 25 lines

Then, we register the PLLs with something like:

mtk_clk_register_plls(node, plls, num_plls, clk_data, fhctl_register_version);

...where fhctl_register_version is used to assign the right fhctl register offsets. Also, it's not needed to assign all of the register offsets statically, because they can be easily calculated based on the number of supported PLLs, since the registers are structured like

[FHCTL GLOBAL REGISTERS] <--- hp_en...slope1 [FHCTL SSC GLOBAL REGISTERS] <--- DSSC_CFG, DSSC0...x_CON

[FHCTL PER-PLL REGISTERS] <--- CFG...MON
^^^ where this is repeated X times for X PLLs.

so, keeping the example of MT8186, we can get the per-pll register like:

#define FHCTL_PLL_OFFSET 0x3c
#define FHCTL_PLL_LEN 0x14

#define FHCTLx_CFG(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN))
#define FHCTLx_UPDNLMT(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x4)
#define FHCTLx_DDS(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x8)

Quoted 6 lines
Quoted 1 line

we don't need to put all of them in a structure and for each PLL.

We use structure instead of using macros is because the register offset may have difference between ICs. If we use macro, we need to maintain different versions of macros. Using structure to store these register offsets is more flexible.

I understand. What I don't like about your specific approach is the amount of register offsets that we store in that structure, looks like it's a bit too many.

I've seen that there's a common pattern at least by checking downstream 5.10 and MT8186/95 layouts, so I still think that using these macros will be beneficial.

We can always add parameters to the structure in a later commit: in my opinion, that will help to engineer a better, shorter, cleaner solution for calculating these registers anyway... but I will leave this choice to you, anyway, since you know about way more SoCs than I do.

Quoted 42 lines
Quoted 7 lines
Quoted 4 lines

So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

As already said, we preventively know which PLLs support FHCTL and which does not, so we can use a different .set_rate() callback.

Ok, we can use a different .set_rate() callback when fhctl driver probing.

Quoted 32 lines
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 7 lines

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

For case N.1, if this is board-specific, we have to resort to devicetree properties that will enable/disable FHCTL on specific PLLs.

mediatek,fhctl-disable = <CLK_APMIXED_MSDCPLL>, <CLK_APMIXED_NNAPLL>;

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>;

Quoted 8 lines
Quoted 4 lines

These are just examples - I don't currently know if it's a better idea to have an allowlist or a blocklist as devicetree properties, as that depends on the expected number of PLLs for which we en/dis fhctl or just ssc (if we generally want fhctl enabled on all but one PLLs, we should use fhctl-disable, otherwise, fhctl-enable).

We also have a properity "ssc-rate" for setting up the ssc rate in percentage. The "ssc- rate" properity is under fhctl dts node and can be setup on each fhctl-PLL.

Right. For that, we could have a default sensible percentage when SSC is enabled but no rate is set in devicetree, or we can perhaps consider SSC enabled when any meaningful SSC rate is set... For example:

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>; mediatek,ssc-percent = <5>, <5>;

... or something like:

mediatek,ssc = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

...but I'd like to have some feedback on that from somebody else, as I don't know if that would be acceptable in devicetree, or if there's any cleaner, niftier solution.

Quoted 121 lines
Quoted 28 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Ok, so it's definitely board specific. Devicetree is the way to go for this.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Quoted 7 lines
Quoted 2 lines

Debugging feature is not used yet, we can removed it.

If the debugging features of the FHCTL driver will be like what I can see on the downstream MT6893 5.10 kernel, that's not really applicable to upstream.

In that case, please remove the debug.

Ok, we will remove it.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Use the flag to set the right .set_rate() callback, set at probe time, instead of checking that flag at every set_rate() call.

We will setup .set_rate() callback when doing fhctl-pll init.

Quoted 59 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Board specific -> devicetree

SoC specific -> hardcode, no devicetree.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 5 lines

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Let's use clk-fhctl as an helper then, we can make sure to call the init flow for the microP in the SoC-specific clock drivers, I think that's not a problem?

clk_mtfuturesoc_someip_probe()

{
	.... register clocks ....

	freqhopping_microp_init();

	return ret;
}
Quoted 5 lines

If there's hardware out there that supports such feature and a downstream kernel to look at, please tell me which one, so that I will be able to check it out and perhaps understand how this flow works.

P.S.: I guess it's not fhctl-sspm?

You could find clk-fhctl-mcupm.c and clk-fhctl-gpueb.c on the downstream MT6893 5.10 kernel. Those codes require the PLL hardware specification to determine which PLL group(eg. PLL TOP group, GPUEB group) runs on which microP and has responsibilty to communicate with the microP.

Quoted 5 lines

If we implement these things into clk-pll driver, clk-pll driver not only needs to control PLL frequency but also needs to deal with microP IPI. It makes clk-pll driver have others works that is not belong to PLL operation. That's why we tend to have a standalone driver for FHCTL.

Ok having something to analyze made this entire thing a bit more clear in my mind, thanks for the pointers.

Analyzing clk-fhctl-mcupm and clk-fhctl-gpueb makes me see that there's a lot of common code between the two: x_hopping_v1(), x_ssc_enable_v1(), x_ssc_disable_v1() (where x = {gpueb,mcupm}) are really the same functions, duplicated and renamed and nothing else.
The only difference is the get_xxxx_ipidev(), which is avoidable by assigning mboxes = <...something...> in devicetree (gpueb mailbox, or mcupm mailbox).

Even the FH_DEVCTL_CMD_ID enumeration uses the same values!

To unroll that riddle, I would at that point add a new MediaTek specific clock driver (like clk-pll) and call it clk-ipi.c, because that's what it does in the end: whatever we do, goes through a mailbox instead of a direct MMIO write.

That clk-fhctl-ipi would contain a probe function that gets the mailbox handle, then we would add something like clk_fhctl_set_rate() function, export it in the clk-mtk.h or in a new clk-fhctl.h header, then assign the right callback in either the SoC's clock driver (by registering a different clock type, which, in this case, would be clk-fhctl-ipi instead of clk-pll), or in clk-pll itself...

In the end, I'm effectively proposing to:

1. Merge the direct-MMIO handling of FHCTL in clk-pll;
2. Create a new driver (and clock type, eventually) for the IPI handling of FHCTL.

Regards,
Angelo

#14 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Hi AngeloGioacchino,

Thanks for the advices.

Quoted 109 lines · AngeloGioacchino Del Regno

Il 20/07/22 15:51, Edward-JW Yang ha scritto:

Quoted 108 lines

Hi AngeloGioacchino,

Thanks for all the advices and examples.

Quoted 102 lines · AngeloGioacchino Del Regno

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 60 lines · Chen-Yu Tsai
Quoted 59 lines
Quoted 57 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

       PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

           PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;
Quoted 3 lines

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.

Where we declare the PLLs, for example, in clk-mt8186-apmixedsys.c, we can declare that such PLL can be managed by FHCTL, for example:

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208),

becomes

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208, true);

where 'true' means "FHCTL is supported".

Quoted 8 lines
Quoted 4 lines

Does it still have an independent FHCTL driver after modifying to this? From your example, setup a clk_ops and add FHCTL properities into PLL(), seems FHCTL driver is merged into clk-pll and become part of clk-pll driver.

The direct-MMIO part of FHCTL becomes part of the clk-pll driver, yes - but then I also find it unacceptable to embed the IPI communication inside of there, so we can have an "external" helper for that.

I think clk-pll driver should focus on PLL HW itself. Since PLL can work alone without FHCTL, adding FHCTL control into clk-pll may be a little strange. For this PLL+FHCTL combination, I want to add a new type of clock driver, like clk-pll-fh. It might be a easier way to maintain FHCTL HW related changes and won't affect to clk-pll.

Quoted 27 lines
Quoted 15 lines

We tend to have an indepentent driver and dts for FHCTL, and mutate only .set_rate() callback function instead of whole clk_ops. The boot-up sequence is like:

1. clk-pll + clk dts

	probe  -> clk-pll original flow, nothing to change

         /* clk-pll provide multation API for set_rate */
	/* mutate necessary set_rate() instead of mutating all ops */
		def register_fhctl_set_rate(pll_name, callback)
			ops = find_pll_ops_by_name(pll_name)
			log("change set_rate to fhctl callback for $pll_name")
			ops->set_rate = callback
Quoted 11 lines

2. FHCTL driver + fhctl dts
probe
options = parsing dts (board specific, hopping disalbe or ssc-rate) init FHCTL HW
for PLL in dts
if (ssc-rate > 0)
enable_ssc(ssc-rate)
if (hop-enabled)
/* mutate CCF set_rate, FHCTL engaged CCF */ register_fhctl_CCF(pll_name, callback)

Quoted 8 lines

I really don't like having PLL names in devicetree: they're already defined in clock drivers and they will change on a per-SoC basis - and we do have per-SoC drivers...

Whatever goes to devicetree should be something that we need to vary on a per-board/platform(project) basis, so, enablement of FHCTL per-pll (by using handles and numeral bindings as per the example that I previously wrote), enablement of spread spectrum and its rate... and nothing else.

OK, we will remove PLL names in devicetree.

Quoted 36 lines
Quoted 26 lines
Quoted 26 lines

Then, we register the PLLs with something like:

mtk_clk_register_plls(node, plls, num_plls, clk_data, fhctl_register_version);

...where fhctl_register_version is used to assign the right fhctl register offsets. Also, it's not needed to assign all of the register offsets statically, because they can be easily calculated based on the number of supported PLLs, since the registers are structured like

[FHCTL GLOBAL REGISTERS] <--- hp_en...slope1 [FHCTL SSC GLOBAL REGISTERS] <--- DSSC_CFG, DSSC0...x_CON

[FHCTL PER-PLL REGISTERS] <--- CFG...MON
^^^ where this is repeated X times for X PLLs.

so, keeping the example of MT8186, we can get the per-pll register like:

#define FHCTL_PLL_OFFSET 0x3c
#define FHCTL_PLL_LEN 0x14

#define FHCTLx_CFG(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN))
#define FHCTLx_UPDNLMT(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x4)
#define FHCTLx_DDS(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x8)

Quoted 6 lines
Quoted 1 line

we don't need to put all of them in a structure and for each PLL.

We use structure instead of using macros is because the register offset may have difference between ICs. If we use macro, we need to maintain different versions of macros. Using structure to store these register offsets is more flexible.

I understand. What I don't like about your specific approach is the amount of register offsets that we store in that structure, looks like it's a bit too many.

Quoted 7 lines

I've seen that there's a common pattern at least by checking downstream 5.10 and MT8186/95 layouts, so I still think that using these macros will be beneficial.

We can always add parameters to the structure in a later commit: in my opinion, that will help to engineer a better, shorter, cleaner solution for calculating these registers anyway... but I will leave this choice to you, anyway, since you know about way more SoCs than I do.

OK, we will reduce the structure.

Quoted 59 lines
Quoted 43 lines
Quoted 8 lines
Quoted 5 lines

So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

As already said, we preventively know which PLLs support FHCTL and which does not, so we can use a different .set_rate() callback.

Ok, we can use a different .set_rate() callback when fhctl driver probing.

Quoted 32 lines
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 7 lines

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

For case N.1, if this is board-specific, we have to resort to devicetree properties that will enable/disable FHCTL on specific PLLs.

mediatek,fhctl-disable = <CLK_APMIXED_MSDCPLL>, <CLK_APMIXED_NNAPLL>;

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>;

Quoted 8 lines
Quoted 4 lines

These are just examples - I don't currently know if it's a better idea to have an allowlist or a blocklist as devicetree properties, as that depends on the expected number of PLLs for which we en/dis fhctl or just ssc (if we generally want fhctl enabled on all but one PLLs, we should use fhctl-disable, otherwise, fhctl-enable).

We also have a properity "ssc-rate" for setting up the ssc rate in percentage. The "ssc- rate" properity is under fhctl dts node and can be setup on each fhctl-PLL.

Right. For that, we could have a default sensible percentage when SSC is enabled but no rate is set in devicetree, or we can perhaps consider SSC enabled when any meaningful SSC rate is set... For example:

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>; mediatek,ssc-percent = <5>, <5>;

Quoted 7 lines

... or something like:

mediatek,ssc = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

...but I'd like to have some feedback on that from somebody else, as I don't know if that would be acceptable in devicetree, or if there's any cleaner, niftier solution.

OK, we will use this:

mediatek,hopping-ssc-percent = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

Quoted 150 lines
Quoted 122 lines
Quoted 29 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Ok, so it's definitely board specific. Devicetree is the way to go for this.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Quoted 7 lines
Quoted 2 lines

Debugging feature is not used yet, we can removed it.

If the debugging features of the FHCTL driver will be like what I can see on the downstream MT6893 5.10 kernel, that's not really applicable to upstream.

In that case, please remove the debug.

Ok, we will remove it.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Use the flag to set the right .set_rate() callback, set at probe time, instead of checking that flag at every set_rate() call.

We will setup .set_rate() callback when doing fhctl-pll init.

Quoted 59 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Board specific -> devicetree

SoC specific -> hardcode, no devicetree.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 5 lines

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Let's use clk-fhctl as an helper then, we can make sure to call the init flow for the microP in the SoC-specific clock drivers, I think that's not a problem?

clk_mtfuturesoc_someip_probe()

{
	.... register clocks ....

	freqhopping_microp_init();

	return ret;
}
Quoted 5 lines

If there's hardware out there that supports such feature and a downstream kernel to look at, please tell me which one, so that I will be able to check it out and perhaps understand how this flow works.

P.S.: I guess it's not fhctl-sspm?

You could find clk-fhctl-mcupm.c and clk-fhctl-gpueb.c on the downstream MT6893 5.10 kernel. Those codes require the PLL hardware specification to determine which PLL group(eg. PLL TOP group, GPUEB group) runs on which microP and has responsibilty to communicate with the microP.

Quoted 5 lines

If we implement these things into clk-pll driver, clk-pll driver not only needs to control PLL frequency but also needs to deal with microP IPI. It makes clk-pll driver have others works that is not belong to PLL operation. That's why we tend to have a standalone driver for FHCTL.

Ok having something to analyze made this entire thing a bit more clear in my mind, thanks for the pointers.

Analyzing clk-fhctl-mcupm and clk-fhctl-gpueb makes me see that there's a lot of common code between the two: x_hopping_v1(), x_ssc_enable_v1(), x_ssc_disable_v1() (where x = {gpueb,mcupm}) are really the same functions, duplicated and renamed and nothing else.
The only difference is the get_xxxx_ipidev(), which is avoidable by assigning mboxes = <...something...> in devicetree (gpueb mailbox, or mcupm mailbox).

Even the FH_DEVCTL_CMD_ID enumeration uses the same values!

To unroll that riddle, I would at that point add a new MediaTek specific clock driver (like clk-pll) and call it clk-ipi.c, because that's what it does in the end: whatever we do, goes through a mailbox instead of a direct MMIO write.

That clk-fhctl-ipi would contain a probe function that gets the mailbox handle, then we would add something like clk_fhctl_set_rate() function, export it in the clk-mtk.h or in a new clk-fhctl.h header, then assign the right callback in either the SoC's clock driver (by registering a different clock type, which, in this case, would be clk-fhctl-ipi instead of clk-pll), or in clk-pll itself...

Quoted 4 lines

In the end, I'm effectively proposing to:

1. Merge the direct-MMIO handling of FHCTL in clk-pll;
2. Create a new driver (and clock type, eventually) for the IPI handling of FHCTL.

From your idea, I think we can also create a new clock type for fhctl such as clk-pll-fh and add a new PLL register function for PLL+FHCTL. Then we can change the registery interface and won't affect the legacy ICs. Also, if FHCTL has changes, we only need to modify clk-pll-fh.
I think using a new clock type has extendibility for FHCTL changes and also compatiable with legacy ICs.

clk-pll.h

	/* Define FHCTL data structure and contains mtk_pll_data. 
	 * We can use mtk_pll_data later. */
	mtk_pll_fh_data {
		struct mtk_pll_data pll_data;
		/* fhctl_data */
		unsigned int fh_id;
		unsigned int ssc_rate;
		...
	}

clk-mt8186-apmixedsys.c

	func clk_mt8186_apmixed_probe()
		/* There are two implementations.
		 * If ICs need FHCTL such as MT8186, use mtk_clk_register_pllfhs()
		 * For those legacy ICs which don't need FHCTL, still use 
		 * mtk_clk_register_plls().
		 */
		/* 1. Need FHCTL. Use API from clk-pll-fh.c */
		fhctl_parse_dt()
		mtk_clk_register_pllfhs(plls data, fh-plls data)
		
		/* 2. Legacy ICs. Use API from clk-pll.c */
		mtk_clk_register_plls()

clk-pll.c

	/* No functional changes, so legacy ICs won't be affected. 
	 * Export clk_ops functions to clk-pll-fh.c
	 */ 
	func mtk_clk_register_plls()
		init.ops = &mtk_pll_ops;

clk-pll-fh.c

	/* A clock type of FHCTL PLL. Used to setup HW data and ops.
	 * Most of ops functions inherit from clk-pll.c.
	 * If PLL not support or not enable FHCTL, fallback to use &mtk_pll_ops.
	 */
	func mtk_clk_register_pllfhs(plls data, fh-plls data)
		fhctl_match_pll_data()  /* match mtk_pll_data and mtk_pll_fh_data */
		fhctl_hw_init()
		if (fhctl_is_supported_and_enabled(pll))
			init.ops = &mtk_pll_fhctl_ops;
		else
			init.ops = &mtk_pll_ops;
	
		if (ssc_is_enable(pll))
			fhctl_ssc_enable(pll)

clk-fhctl.c

	/* APIs of FHCTL HW operations */
	func fhctl_hw_init()
	func fhctl_hopping()
	func fhctl_ssc_enable()
Quoted 3 lines

Regards,
Angelo

#15 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Il 28/07/22 06:37, Edward-JW Yang ha scritto:

Quoted 119 lines

Hi AngeloGioacchino,

Thanks for the advices.

Quoted 114 lines · AngeloGioacchino Del Regno

Il 20/07/22 15:51, Edward-JW Yang ha scritto:

Quoted 112 lines

Hi AngeloGioacchino,

Thanks for all the advices and examples.

Quoted 95 lines · AngeloGioacchino Del Regno

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 60 lines · Chen-Yu Tsai
Quoted 59 lines
Quoted 57 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

        PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

            PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;
Quoted 3 lines

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.

Where we declare the PLLs, for example, in clk-mt8186-apmixedsys.c, we can declare that such PLL can be managed by FHCTL, for example:

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208),
Quoted 7 lines

becomes

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208, true);

where 'true' means "FHCTL is supported".

Does it still have an independent FHCTL driver after modifying to this? From your example, setup a clk_ops and add FHCTL properities into PLL(), seems FHCTL driver is merged into clk-pll and become part of clk-pll driver.

Quoted 9 lines
Quoted 3 lines

The direct-MMIO part of FHCTL becomes part of the clk-pll driver, yes - but then I also find it unacceptable to embed the IPI communication inside of there, so we can have an "external" helper for that.

I think clk-pll driver should focus on PLL HW itself. Since PLL can work alone without FHCTL, adding FHCTL control into clk-pll may be a little strange. For this PLL+FHCTL combination, I want to add a new type of clock driver, like clk-pll-fh. It might be a easier way to maintain FHCTL HW related changes and won't affect to clk-pll.

That makes sense, and it's doable as long as we're not duplicating clk-pll's code to clk-pll-fh and also as long as we're hardcoding the availability of FHCTL in the SoC-specific clock drivers like I explained in the PLL macro from the previous example. Let's go!

Quoted 31 lines
Quoted 31 lines
Quoted 15 lines

We tend to have an indepentent driver and dts for FHCTL, and mutate only .set_rate() callback function instead of whole clk_ops. The boot-up sequence is like:

1. clk-pll + clk dts

	probe  -> clk-pll original flow, nothing to change

          /* clk-pll provide multation API for set_rate */
	/* mutate necessary set_rate() instead of mutating all ops */
		def register_fhctl_set_rate(pll_name, callback)
			ops = find_pll_ops_by_name(pll_name)
			log("change set_rate to fhctl callback for $pll_name")
			ops->set_rate = callback
Quoted 11 lines

2. FHCTL driver + fhctl dts
probe
options = parsing dts (board specific, hopping disalbe or ssc-rate) init FHCTL HW
for PLL in dts
if (ssc-rate > 0)
enable_ssc(ssc-rate)
if (hop-enabled)
/* mutate CCF set_rate, FHCTL engaged CCF */ register_fhctl_CCF(pll_name, callback)

I really don't like having PLL names in devicetree: they're already defined in clock drivers and they will change on a per-SoC basis - and we do have per-SoC drivers...

Quoted 7 lines
Quoted 4 lines

Whatever goes to devicetree should be something that we need to vary on a per-board/platform(project) basis, so, enablement of FHCTL per-pll (by using handles and numeral bindings as per the example that I previously wrote), enablement of spread spectrum and its rate... and nothing else.

OK, we will remove PLL names in devicetree.

Great.

Quoted 39 lines
Quoted 39 lines
Quoted 26 lines
Quoted 26 lines

Then, we register the PLLs with something like:

mtk_clk_register_plls(node, plls, num_plls, clk_data, fhctl_register_version);

...where fhctl_register_version is used to assign the right fhctl register offsets. Also, it's not needed to assign all of the register offsets statically, because they can be easily calculated based on the number of supported PLLs, since the registers are structured like

[FHCTL GLOBAL REGISTERS] <--- hp_en...slope1 [FHCTL SSC GLOBAL REGISTERS] <--- DSSC_CFG, DSSC0...x_CON

[FHCTL PER-PLL REGISTERS] <--- CFG...MON
^^^ where this is repeated X times for X PLLs.

so, keeping the example of MT8186, we can get the per-pll register like:

#define FHCTL_PLL_OFFSET 0x3c
#define FHCTL_PLL_LEN 0x14

#define FHCTLx_CFG(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN))
#define FHCTLx_UPDNLMT(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x4)
#define FHCTLx_DDS(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x8)

Quoted 6 lines
Quoted 1 line

we don't need to put all of them in a structure and for each PLL.

We use structure instead of using macros is because the register offset may have difference between ICs. If we use macro, we need to maintain different versions of macros. Using structure to store these register offsets is more flexible.

I understand. What I don't like about your specific approach is the amount of register offsets that we store in that structure, looks like it's a bit too many.

I've seen that there's a common pattern at least by checking downstream 5.10 and MT8186/95 layouts, so I still think that using these macros will be beneficial.

Quoted 7 lines
Quoted 4 lines

We can always add parameters to the structure in a later commit: in my opinion, that will help to engineer a better, shorter, cleaner solution for calculating these registers anyway... but I will leave this choice to you, anyway, since you know about way more SoCs than I do.

OK, we will reduce the structure.

Perfect!

Quoted 63 lines
Quoted 63 lines
Quoted 43 lines
Quoted 8 lines
Quoted 5 lines

So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

As already said, we preventively know which PLLs support FHCTL and which does not, so we can use a different .set_rate() callback.

Ok, we can use a different .set_rate() callback when fhctl driver probing.

Quoted 32 lines
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 7 lines

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

For case N.1, if this is board-specific, we have to resort to devicetree properties that will enable/disable FHCTL on specific PLLs.

mediatek,fhctl-disable = <CLK_APMIXED_MSDCPLL>, <CLK_APMIXED_NNAPLL>;

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>;

Quoted 8 lines
Quoted 4 lines

These are just examples - I don't currently know if it's a better idea to have an allowlist or a blocklist as devicetree properties, as that depends on the expected number of PLLs for which we en/dis fhctl or just ssc (if we generally want fhctl enabled on all but one PLLs, we should use fhctl-disable, otherwise, fhctl-enable).

We also have a properity "ssc-rate" for setting up the ssc rate in percentage. The "ssc- rate" properity is under fhctl dts node and can be setup on each fhctl-PLL.

Right. For that, we could have a default sensible percentage when SSC is enabled but no rate is set in devicetree, or we can perhaps consider SSC enabled when any meaningful SSC rate is set... For example:

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>; mediatek,ssc-percent = <5>, <5>;

... or something like:

mediatek,ssc = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

Quoted 8 lines
Quoted 3 lines

...but I'd like to have some feedback on that from somebody else, as I don't know if that would be acceptable in devicetree, or if there's any cleaner, niftier solution.

OK, we will use this:

mediatek,hopping-ssc-percent = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

Looks good.

Quoted 174 lines
Quoted 150 lines
Quoted 122 lines
Quoted 29 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Ok, so it's definitely board specific. Devicetree is the way to go for this.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Quoted 7 lines
Quoted 2 lines

Debugging feature is not used yet, we can removed it.

If the debugging features of the FHCTL driver will be like what I can see on the downstream MT6893 5.10 kernel, that's not really applicable to upstream.

In that case, please remove the debug.

Ok, we will remove it.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Use the flag to set the right .set_rate() callback, set at probe time, instead of checking that flag at every set_rate() call.

We will setup .set_rate() callback when doing fhctl-pll init.

Quoted 59 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Board specific -> devicetree

SoC specific -> hardcode, no devicetree.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 5 lines

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Let's use clk-fhctl as an helper then, we can make sure to call the init flow for the microP in the SoC-specific clock drivers, I think that's not a problem?

clk_mtfuturesoc_someip_probe()

{
	.... register clocks ....

	freqhopping_microp_init();

	return ret;
}
Quoted 5 lines

If there's hardware out there that supports such feature and a downstream kernel to look at, please tell me which one, so that I will be able to check it out and perhaps understand how this flow works.

P.S.: I guess it's not fhctl-sspm?

You could find clk-fhctl-mcupm.c and clk-fhctl-gpueb.c on the downstream MT6893 5.10 kernel. Those codes require the PLL hardware specification to determine which PLL group(eg. PLL TOP group, GPUEB group) runs on which microP and has responsibilty to communicate with the microP.

Quoted 5 lines

If we implement these things into clk-pll driver, clk-pll driver not only needs to control PLL frequency but also needs to deal with microP IPI. It makes clk-pll driver have others works that is not belong to PLL operation. That's why we tend to have a standalone driver for FHCTL.

Ok having something to analyze made this entire thing a bit more clear in my mind, thanks for the pointers.

Analyzing clk-fhctl-mcupm and clk-fhctl-gpueb makes me see that there's a lot of common code between the two: x_hopping_v1(), x_ssc_enable_v1(), x_ssc_disable_v1() (where x = {gpueb,mcupm}) are really the same functions, duplicated and renamed and nothing else.
The only difference is the get_xxxx_ipidev(), which is avoidable by assigning mboxes = <...something...> in devicetree (gpueb mailbox, or mcupm mailbox).

Even the FH_DEVCTL_CMD_ID enumeration uses the same values!

To unroll that riddle, I would at that point add a new MediaTek specific clock driver (like clk-pll) and call it clk-ipi.c, because that's what it does in the end: whatever we do, goes through a mailbox instead of a direct MMIO write.

That clk-fhctl-ipi would contain a probe function that gets the mailbox handle, then we would add something like clk_fhctl_set_rate() function, export it in the clk-mtk.h or in a new clk-fhctl.h header, then assign the right callback in either the SoC's clock driver (by registering a different clock type, which, in this case, would be clk-fhctl-ipi instead of clk-pll), or in clk-pll itself...

Quoted 4 lines

In the end, I'm effectively proposing to:

1. Merge the direct-MMIO handling of FHCTL in clk-pll;
2. Create a new driver (and clock type, eventually) for the IPI handling of FHCTL.

From your idea, I think we can also create a new clock type for fhctl such as clk-pll-fh
and add a new PLL register function for PLL+FHCTL. Then we can change the registery interface and won't affect the legacy ICs. Also, if FHCTL has changes, we only need to modify clk-pll-fh.
I think using a new clock type has extendibility for FHCTL changes and also compatiable with legacy ICs.

clk-pll.h

	/* Define FHCTL data structure and contains mtk_pll_data.
	 * We can use mtk_pll_data later. */
	mtk_pll_fh_data {
		struct mtk_pll_data pll_data;
		/* fhctl_data */
		unsigned int fh_id;
		unsigned int ssc_rate;
		...
	}

clk-mt8186-apmixedsys.c

Quoted 12 lines
	func clk_mt8186_apmixed_probe()
		/* There are two implementations.
		 * If ICs need FHCTL such as MT8186, use mtk_clk_register_pllfhs()
		 * For those legacy ICs which don't need FHCTL, still use
		 * mtk_clk_register_plls().
		 */
		/* 1. Need FHCTL. Use API from clk-pll-fh.c */
		fhctl_parse_dt()
		mtk_clk_register_pllfhs(plls data, fh-plls data)
		
		/* 2. Legacy ICs. Use API from clk-pll.c */
		mtk_clk_register_plls()

I'm not sure if we're saying the very same thing here, but for the sake of being clear and avoiding any misunderstanding, here's my description.

We should call both functions: register_pllfhs() for the PLLs that have support for freqhopping, register_plls() for the ones that *do not support* freqhopping.

Example for PLL_A, PLL_B, PLL_C, PLL_D:

Freqhopping supported (enabled or not): PLL_A, PLL_B Freqhopping NOT supported at all: PLL_C, PLL_D

fhplls_data[] = { PLL_A, PLL_B };
plls_data[] = { PLL_C, PLL_D };

func mtk_clk_register_pllfhs(fhdata)
walk through fhplls_data only, other plls are not passed to this function

func clk_mt8186_apmixed_probe()
/* Some PLLs must be controlled directly via MMIO, while others
* support Frequency Hopping through FHCTL.
* Where FHCTL is supported, register clock with register_pllfhs.
* PLLs that are not supported by FHCTL: register with register_plls. */

/* Register FHCTL PLLs */
fhctl_parse_dt() mtk_clk_register_pllfhs(array of plls supporting pllfh)

/* Register the PLLs that do not support FHCTL at all */ mtk_clk_register_plls(all the other PLLs that cannot be FHCTL-controlled)

Quoted 8 lines

clk-pll.c

	/* No functional changes, so legacy ICs won't be affected.
	 * Export clk_ops functions to clk-pll-fh.c
	 */
	func mtk_clk_register_plls()
		init.ops = &mtk_pll_ops;
Quoted 9 lines

clk-pll-fh.c
/* A clock type of FHCTL PLL. Used to setup HW data and ops.
* Most of ops functions inherit from clk-pll.c.
* If PLL not support or not enable FHCTL, fallback to use &mtk_pll_ops. */
func mtk_clk_register_pllfhs(plls data, fh-plls data)
fhctl_match_pll_data() /* match mtk_pll_data and mtk_pll_fh_data */ fhctl_hw_init()
if (fhctl_is_supported_and_enabled(pll))

Overall, this seems to look good, minor one nit: if FHCTL is *not supported* on a PLL, we should *not* even call mtk_clk_register_pllfhs on that PLL, so your pseudocode would be just:

if (fhctl_is_enabled(pll))

Quoted 7 lines
			init.ops = &mtk_pll_fhctl_ops;
		else
			init.ops = &mtk_pll_ops;
	
		if (ssc_is_enable(pll))
			fhctl_ssc_enable(pll)
Quoted 7 lines

clk-fhctl.c

	/* APIs of FHCTL HW operations */
	func fhctl_hw_init()
	func fhctl_hopping()
	func fhctl_ssc_enable()

So it seems that we've reached an agreement here, this was a nice planning discussion; we should now have a nice and solid base to work on, which is great.

Cheers,
Angelo

#16 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Hi AngeloGioacchino,

Thanks for the discussion.

Quoted 512 lines · AngeloGioacchino Del Regno

Il 28/07/22 06:37, Edward-JW Yang ha scritto:

Quoted 119 lines

Hi AngeloGioacchino,

Thanks for the advices.

Quoted 114 lines · AngeloGioacchino Del Regno

Il 20/07/22 15:51, Edward-JW Yang ha scritto:

Quoted 112 lines

Hi AngeloGioacchino,

Thanks for all the advices and examples.

Quoted 95 lines · AngeloGioacchino Del Regno

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 60 lines · Chen-Yu Tsai
Quoted 59 lines
Quoted 57 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

        PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

            PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;
Quoted 3 lines

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.

Where we declare the PLLs, for example, in clk-mt8186-apmixedsys.c, we can declare that such PLL can be managed by FHCTL, for example:

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208),
Quoted 7 lines

becomes

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208, true);

where 'true' means "FHCTL is supported".

Does it still have an independent FHCTL driver after modifying to this? From your example, setup a clk_ops and add FHCTL properities into PLL(), seems FHCTL driver is merged into clk-pll and become part of clk-pll driver.

Quoted 9 lines
Quoted 3 lines

The direct-MMIO part of FHCTL becomes part of the clk-pll driver, yes - but then I also find it unacceptable to embed the IPI communication inside of there, so we can have an "external" helper for that.

I think clk-pll driver should focus on PLL HW itself. Since PLL can work alone without FHCTL, adding FHCTL control into clk-pll may be a little strange. For this PLL+FHCTL combination, I want to add a new type of clock driver, like clk-pll-fh. It might be a easier way to maintain FHCTL HW related changes and won't affect to clk-pll.

That makes sense, and it's doable as long as we're not duplicating clk-pll's code to clk-pll-fh and also as long as we're hardcoding the availability of FHCTL in the SoC-specific clock drivers like I explained in the PLL macro from the previous example. Let's go!

Quoted 31 lines
Quoted 31 lines
Quoted 15 lines

We tend to have an indepentent driver and dts for FHCTL, and mutate only .set_rate() callback function instead of whole clk_ops. The boot-up sequence is like:

1. clk-pll + clk dts

	probe  -> clk-pll original flow, nothing to change

          /* clk-pll provide multation API for set_rate */
	/* mutate necessary set_rate() instead of mutating all ops */
		def register_fhctl_set_rate(pll_name, callback)
			ops = find_pll_ops_by_name(pll_name)
			log("change set_rate to fhctl callback for $pll_name")
			ops->set_rate = callback
Quoted 11 lines

2. FHCTL driver + fhctl dts
probe
options = parsing dts (board specific, hopping disalbe or ssc-rate) init FHCTL HW
for PLL in dts
if (ssc-rate > 0)
enable_ssc(ssc-rate)
if (hop-enabled)
/* mutate CCF set_rate, FHCTL engaged CCF */ register_fhctl_CCF(pll_name, callback)

I really don't like having PLL names in devicetree: they're already defined in clock drivers and they will change on a per-SoC basis - and we do have per-SoC drivers...

Quoted 7 lines
Quoted 4 lines

Whatever goes to devicetree should be something that we need to vary on a per-board/platform(project) basis, so, enablement of FHCTL per-pll (by using handles and numeral bindings as per the example that I previously wrote), enablement of spread spectrum and its rate... and nothing else.

OK, we will remove PLL names in devicetree.

Great.

Quoted 39 lines
Quoted 39 lines
Quoted 26 lines
Quoted 26 lines

Then, we register the PLLs with something like:

mtk_clk_register_plls(node, plls, num_plls, clk_data, fhctl_register_version);

...where fhctl_register_version is used to assign the right fhctl register offsets. Also, it's not needed to assign all of the register offsets statically, because they can be easily calculated based on the number of supported PLLs, since the registers are structured like

[FHCTL GLOBAL REGISTERS] <--- hp_en...slope1 [FHCTL SSC GLOBAL REGISTERS] <--- DSSC_CFG, DSSC0...x_CON

[FHCTL PER-PLL REGISTERS] <--- CFG...MON
^^^ where this is repeated X times for X PLLs.

so, keeping the example of MT8186, we can get the per-pll register like:

#define FHCTL_PLL_OFFSET 0x3c
#define FHCTL_PLL_LEN 0x14

#define FHCTLx_CFG(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN))
#define FHCTLx_UPDNLMT(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x4)
#define FHCTLx_DDS(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x8)

Quoted 6 lines
Quoted 1 line

we don't need to put all of them in a structure and for each PLL.

We use structure instead of using macros is because the register offset may have difference between ICs. If we use macro, we need to maintain different versions of macros. Using structure to store these register offsets is more flexible.

I understand. What I don't like about your specific approach is the amount of register offsets that we store in that structure, looks like it's a bit too many.

I've seen that there's a common pattern at least by checking downstream 5.10 and MT8186/95 layouts, so I still think that using these macros will be beneficial.

Quoted 7 lines
Quoted 4 lines

We can always add parameters to the structure in a later commit: in my opinion, that will help to engineer a better, shorter, cleaner solution for calculating these registers anyway... but I will leave this choice to you, anyway, since you know about way more SoCs than I do.

OK, we will reduce the structure.

Perfect!

Quoted 63 lines
Quoted 63 lines
Quoted 43 lines
Quoted 8 lines
Quoted 5 lines

So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

As already said, we preventively know which PLLs support FHCTL and which does not, so we can use a different .set_rate() callback.

Ok, we can use a different .set_rate() callback when fhctl driver probing.

Quoted 32 lines
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 7 lines

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

For case N.1, if this is board-specific, we have to resort to devicetree properties that will enable/disable FHCTL on specific PLLs.

mediatek,fhctl-disable = <CLK_APMIXED_MSDCPLL>, <CLK_APMIXED_NNAPLL>;

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>;

Quoted 8 lines
Quoted 4 lines

These are just examples - I don't currently know if it's a better idea to have an allowlist or a blocklist as devicetree properties, as that depends on the expected number of PLLs for which we en/dis fhctl or just ssc (if we generally want fhctl enabled on all but one PLLs, we should use fhctl-disable, otherwise, fhctl-enable).

We also have a properity "ssc-rate" for setting up the ssc rate in percentage. The "ssc- rate" properity is under fhctl dts node and can be setup on each fhctl-PLL.

Right. For that, we could have a default sensible percentage when SSC is enabled but no rate is set in devicetree, or we can perhaps consider SSC enabled when any meaningful SSC rate is set... For example:

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>; mediatek,ssc-percent = <5>, <5>;

... or something like:

mediatek,ssc = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

Quoted 8 lines
Quoted 3 lines

...but I'd like to have some feedback on that from somebody else, as I don't know if that would be acceptable in devicetree, or if there's any cleaner, niftier solution.

OK, we will use this:

mediatek,hopping-ssc-percent = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

Looks good.

Quoted 174 lines
Quoted 150 lines
Quoted 122 lines
Quoted 29 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Ok, so it's definitely board specific. Devicetree is the way to go for this.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Quoted 7 lines
Quoted 2 lines

Debugging feature is not used yet, we can removed it.

If the debugging features of the FHCTL driver will be like what I can see on the downstream MT6893 5.10 kernel, that's not really applicable to upstream.

In that case, please remove the debug.

Ok, we will remove it.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Use the flag to set the right .set_rate() callback, set at probe time, instead of checking that flag at every set_rate() call.

We will setup .set_rate() callback when doing fhctl-pll init.

Quoted 59 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Board specific -> devicetree

SoC specific -> hardcode, no devicetree.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 5 lines

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Let's use clk-fhctl as an helper then, we can make sure to call the init flow for the microP in the SoC-specific clock drivers, I think that's not a problem?

clk_mtfuturesoc_someip_probe()

{
	.... register clocks ....

	freqhopping_microp_init();

	return ret;
}
Quoted 5 lines

If there's hardware out there that supports such feature and a downstream kernel to look at, please tell me which one, so that I will be able to check it out and perhaps understand how this flow works.

P.S.: I guess it's not fhctl-sspm?

You could find clk-fhctl-mcupm.c and clk-fhctl-gpueb.c on the downstream MT6893 5.10 kernel. Those codes require the PLL hardware specification to determine which PLL group(eg. PLL TOP group, GPUEB group) runs on which microP and has responsibilty to communicate with the microP.

Quoted 5 lines

If we implement these things into clk-pll driver, clk-pll driver not only needs to control PLL frequency but also needs to deal with microP IPI. It makes clk-pll driver have others works that is not belong to PLL operation. That's why we tend to have a standalone driver for FHCTL.

Ok having something to analyze made this entire thing a bit more clear in my mind, thanks for the pointers.

Analyzing clk-fhctl-mcupm and clk-fhctl-gpueb makes me see that there's a lot of common code between the two: x_hopping_v1(), x_ssc_enable_v1(), x_ssc_disable_v1() (where x = {gpueb,mcupm}) are really the same functions, duplicated and renamed and nothing else.
The only difference is the get_xxxx_ipidev(), which is avoidable by assigning mboxes = <...something...> in devicetree (gpueb mailbox, or mcupm mailbox).

Even the FH_DEVCTL_CMD_ID enumeration uses the same values!

To unroll that riddle, I would at that point add a new MediaTek specific clock driver (like clk-pll) and call it clk-ipi.c, because that's what it does in the end: whatever we do, goes through a mailbox instead of a direct MMIO write.

That clk-fhctl-ipi would contain a probe function that gets the mailbox handle, then we would add something like clk_fhctl_set_rate() function, export it in the clk-mtk.h or in a new clk-fhctl.h header, then assign the right callback in either the SoC's clock driver (by registering a different clock type, which, in this case, would be clk-fhctl-ipi instead of clk-pll), or in clk-pll itself...

Quoted 4 lines

In the end, I'm effectively proposing to:

1. Merge the direct-MMIO handling of FHCTL in clk-pll;
2. Create a new driver (and clock type, eventually) for the IPI handling of FHCTL.

From your idea, I think we can also create a new clock type for fhctl such as clk-pll-fh
and add a new PLL register function for PLL+FHCTL. Then we can change the registery interface and won't affect the legacy ICs. Also, if FHCTL has changes, we only need to modify clk-pll-fh.
I think using a new clock type has extendibility for FHCTL changes and also compatiable with legacy ICs.

clk-pll.h

	/* Define FHCTL data structure and contains mtk_pll_data.
	 * We can use mtk_pll_data later. */
	mtk_pll_fh_data {
		struct mtk_pll_data pll_data;
		/* fhctl_data */
		unsigned int fh_id;
		unsigned int ssc_rate;
		...
	}

clk-mt8186-apmixedsys.c

Quoted 12 lines
	func clk_mt8186_apmixed_probe()
		/* There are two implementations.
		 * If ICs need FHCTL such as MT8186, use mtk_clk_register_pllfhs()
		 * For those legacy ICs which don't need FHCTL, still use
		 * mtk_clk_register_plls().
		 */
		/* 1. Need FHCTL. Use API from clk-pll-fh.c */
		fhctl_parse_dt()
		mtk_clk_register_pllfhs(plls data, fh-plls data)
		
		/* 2. Legacy ICs. Use API from clk-pll.c */
		mtk_clk_register_plls()

I'm not sure if we're saying the very same thing here, but for the sake of being clear and avoiding any misunderstanding, here's my description.

We should call both functions: register_pllfhs() for the PLLs that have support for freqhopping, register_plls() for the ones that *do not support* freqhopping.

Example for PLL_A, PLL_B, PLL_C, PLL_D:

Freqhopping supported (enabled or not): PLL_A, PLL_B Freqhopping NOT supported at all: PLL_C, PLL_D

fhplls_data[] = { PLL_A, PLL_B };
plls_data[] = { PLL_C, PLL_D };

func mtk_clk_register_pllfhs(fhdata)
walk through fhplls_data only, other plls are not passed to this function

func clk_mt8186_apmixed_probe()
/* Some PLLs must be controlled directly via MMIO, while others
* support Frequency Hopping through FHCTL.
* Where FHCTL is supported, register clock with register_pllfhs.
* PLLs that are not supported by FHCTL: register with register_plls. */

Quoted 7 lines

/* Register FHCTL PLLs */
fhctl_parse_dt() mtk_clk_register_pllfhs(array of plls supporting pllfh)

/* Register the PLLs that do not support FHCTL at all */ mtk_clk_register_plls(all the other PLLs that cannot be FHCTL-controlled)

I have a different opinion here. I think we should just choose one register function when probe and it depends on platform implementation instead of a PLL is supported FHCTL or not. We have an option to use clk-pll-fh or clk-pll at the beginning of new IC development. And, we want it's easy to add FHCTL support on legecy ICs.

The way of how we use the register function is affected by the way we use fhplls_data. If FHCTL supported or not is used to decide which registery function should we use, it has to change the exist plls_data. But most of IC's PLL data is already exist. So I want to use a structure to descript FHCTL HW itself and have a link to existed PLL data.

From your example, it will look like:

plls_data = { PLL_A, PLL_B, PLL_C, PLL_D }; /* Already existed */ fhplls_data = { FH_A,
FH_B }; /* New added for FHCTL data */

func mtk_clk_register_pllfhs(plls_data, fhplls_data)

	fhctl_match_pll_data()
		/* Link fhpll and pll:
		 *   FH_A -> PLL_A
		 *   FH_B -> PLL_B
		 */

For MT8186 and ICs need FHCTL supported:

	func clk_mt8186_apmixed_probe()
		fhctl_parse_dt()
		mtk_clk_register_pllfhs(plls_data, fhplls_data)

For those don't need FHCTL and legacy ICs:
func clk_mtxxxx_apmixed_probe()
mtk_clk_register_plls(plls_data)

When one ICs that need to support FHCTL but they didn't implement before, we only need to change the registery function and add FHCTL data, no need to modify existed plls_data. I think it can save our development time and maintenance effort.

Quoted 18 lines
Quoted 8 lines

clk-pll.c

	/* No functional changes, so legacy ICs won't be affected.
	 * Export clk_ops functions to clk-pll-fh.c
	 */
	func mtk_clk_register_plls()
		init.ops = &mtk_pll_ops;
Quoted 9 lines

clk-pll-fh.c
/* A clock type of FHCTL PLL. Used to setup HW data and ops.
* Most of ops functions inherit from clk-pll.c.
* If PLL not support or not enable FHCTL, fallback to use &mtk_pll_ops. */
func mtk_clk_register_pllfhs(plls data, fh-plls data)
fhctl_match_pll_data() /* match mtk_pll_data and mtk_pll_fh_data */ fhctl_hw_init()
if (fhctl_is_supported_and_enabled(pll))

Quoted 5 lines

Overall, this seems to look good, minor one nit: if FHCTL is *not supported* on a PLL, we should *not* even call mtk_clk_register_pllfhs on that PLL, so your pseudocode would be just:

if (fhctl_is_enabled(pll))

The requirements of clk-pll-fh are:
1. It is compatible with those PLLs that don't support FHCTL HW.
2. It can handle PLLs that supported FHCTL but aren't enabled.
3. It can handle PLLs that supported FHCTL but are enabled.

So, I use fhctl_is_supported_and_enabled(PLL) here. No matter a PLL supports FHCTL or not, it can work.

Quoted 22 lines
Quoted 8 lines
			init.ops = &mtk_pll_fhctl_ops;
		else
			init.ops = &mtk_pll_ops;
	
		if (ssc_is_enable(pll))
			fhctl_ssc_enable(pll)
Quoted 7 lines

clk-fhctl.c

	/* APIs of FHCTL HW operations */
	func fhctl_hw_init()
	func fhctl_hopping()
	func fhctl_ssc_enable()

So it seems that we've reached an agreement here, this was a nice planning discussion; we should now have a nice and solid base to work on, which is great.

Cheers,
Angelo

#17 lore raw

Re: [RFC PATCH 2/2] clk: mediatek: Add frequency hopping support

Il 28/07/22 17:30, Edward-JW Yang ha scritto:

Quoted 553 lines

Hi AngeloGioacchino,

Thanks for the discussion.

Quoted 512 lines · AngeloGioacchino Del Regno

Il 28/07/22 06:37, Edward-JW Yang ha scritto:

Quoted 119 lines

Hi AngeloGioacchino,

Thanks for the advices.

Quoted 114 lines · AngeloGioacchino Del Regno

Il 20/07/22 15:51, Edward-JW Yang ha scritto:

Quoted 112 lines

Hi AngeloGioacchino,

Thanks for all the advices and examples.

Quoted 95 lines · AngeloGioacchino Del Regno

Il 06/07/22 15:07, Edward-JW Yang ha scritto:

Quoted 60 lines · Chen-Yu Tsai
Quoted 59 lines
Quoted 57 lines · AngeloGioacchino Del Regno

Il 24/06/22 09:12, Edward-JW Yang ha scritto:

Quoted 41 lines

Hi AngeloGioacchino,

Thanks for all the advices.

Quoted 36 lines · AngeloGioacchino Del Regno

Il 12/06/22 15:54, Johnson Wang ha scritto:

Quoted 5 lines

Add frequency hopping support and spread spectrum clocking control for MT8186.

Before going on with the review, there's one important consideration:
the Frequency Hopping control is related to PLLs only (so, no other clock types get in the mix).

Checking the code, the *main* thing that we do here is initializing the FHCTL by setting some registers, and we're performing the actual frequency hopping operation in clk-pll, which is right but, at this point, I think that the best way to proceed is to add the "FHCTL superpowers" to clk-pll itself, instead of adding multiple new files and devicetree bindings that are specific to the FHCTL itself.

This would mean that the fh-id and perms params that you're setting in the devicetree get transferred to clk-mt8186 (and hardcoded there), as to extend the PLL declarations to include these two: that will also simplify the driver so that you won't have to match names here and there.

Just an example:

         PLL(CLK_APMIXED_CCIPLL, "ccipll", 0x0224, 0x0230, 0,

             PLL_AO, 0, 22, 0x0228, 24, 0, 0, 0, 0x0228, 2, FHCTL_PERM_DBG_DUMP),

Besides, there are another couple of reasons why you should do that instead, of which:
- The devicetree should be "generic enough", we shall not see the direct value to write to the registers in there (yet, perms assigns exactly that)
- These values won't change on a per-device basis, I believe? They're SoC-related, not board-related, right?

Quoted 6 lines
Quoted 3 lines

In case they're board related (and/or related to TZ permissions), we can always add a bool property to the apmixedsys to advertise that board X needs to use an alternative permission (ex.: mediatek,secure-fhctl).

I think we should remain clk-fhctl files because FHCTL is a independent HW and is not a necessary component of clk-pll.

I know what FHCTL is, but thank you anyway for the explanation, that's appreciated. In any case, this not being a *mandatory* component doesn't mean that when it is enabled it's not changing the way we manage the PLLs..........

Quoted 2 lines

Frequency hopping function from FHCTL is not used to replace original flow of set_rate in clk-pll. They are two different ways to change PLL's frequency. The

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I disagree: when we want to use FHCTL, we effectively hand-over PLL control from APMIXEDSYS to the Frequency Hopping controller - and we're effectively replacing the set_rate() logic of clk-pll.

Do you mean we need to drop the current set_rate() logic (direct register write) and use Frequency Hopping Controller instead?

On PLLs that are supported by the Frequency Hopping controller, yes: we should simply use a different .set_rate() callback in clk-pll.c, and we should return a failure if the FHCTL fails to set the rate - so we should *not* fall back to direct register writes, as on some platforms and in some conditions, using direct register writes (which means that we skip FHCTL), may lead to unstable system.

This means that we need logic such that, in mtk_clk_register_pll(), we end up having something like that:

if (fhctl_is_enabled(pll))

	init.ops = &mtk_pll_fhctl_ops;
else
	init.ops = &mtk_pll_ops;
Quoted 3 lines

I need to mention that not all PLL support FHCTL, only those PLLs with FHCTL HW can choose to use FHCTL. Take 8186 for example, there are three PLLs don't support FHCTL HW.

Where we declare the PLLs, for example, in clk-mt8186-apmixedsys.c, we can declare that such PLL can be managed by FHCTL, for example:

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208),
Quoted 7 lines

becomes

	PLL(CLK_APMIXED_ARMPLL_LL, "armpll_ll", 0x0204, 0x0210, 0,

	    PLL_AO, 0, 22, 0x0208, 24, 0, 0, 0, 0x0208, true);

where 'true' means "FHCTL is supported".

Does it still have an independent FHCTL driver after modifying to this? From your example, setup a clk_ops and add FHCTL properities into PLL(), seems FHCTL driver is merged into clk-pll and become part of clk-pll driver.

Quoted 9 lines
Quoted 3 lines

The direct-MMIO part of FHCTL becomes part of the clk-pll driver, yes - but then I also find it unacceptable to embed the IPI communication inside of there, so we can have an "external" helper for that.

I think clk-pll driver should focus on PLL HW itself. Since PLL can work alone without FHCTL, adding FHCTL control into clk-pll may be a little strange. For this PLL+FHCTL combination, I want to add a new type of clock driver, like clk-pll-fh. It might be a easier way to maintain FHCTL HW related changes and won't affect to clk-pll.

That makes sense, and it's doable as long as we're not duplicating clk-pll's code to clk-pll-fh and also as long as we're hardcoding the availability of FHCTL in the SoC-specific clock drivers like I explained in the PLL macro from the previous example. Let's go!

Quoted 31 lines
Quoted 31 lines
Quoted 15 lines

We tend to have an indepentent driver and dts for FHCTL, and mutate only .set_rate() callback function instead of whole clk_ops. The boot-up sequence is like:

1. clk-pll + clk dts

	probe  -> clk-pll original flow, nothing to change

           /* clk-pll provide multation API for set_rate */
	/* mutate necessary set_rate() instead of mutating all ops */
		def register_fhctl_set_rate(pll_name, callback)
			ops = find_pll_ops_by_name(pll_name)
			log("change set_rate to fhctl callback for $pll_name")
			ops->set_rate = callback
Quoted 11 lines

2. FHCTL driver + fhctl dts
probe
options = parsing dts (board specific, hopping disalbe or ssc-rate) init FHCTL HW
for PLL in dts
if (ssc-rate > 0)
enable_ssc(ssc-rate)
if (hop-enabled)
/* mutate CCF set_rate, FHCTL engaged CCF */ register_fhctl_CCF(pll_name, callback)

I really don't like having PLL names in devicetree: they're already defined in clock drivers and they will change on a per-SoC basis - and we do have per-SoC drivers...

Quoted 7 lines
Quoted 4 lines

Whatever goes to devicetree should be something that we need to vary on a per-board/platform(project) basis, so, enablement of FHCTL per-pll (by using handles and numeral bindings as per the example that I previously wrote), enablement of spread spectrum and its rate... and nothing else.

OK, we will remove PLL names in devicetree.

Great.

Quoted 39 lines
Quoted 39 lines
Quoted 26 lines
Quoted 26 lines

Then, we register the PLLs with something like:

mtk_clk_register_plls(node, plls, num_plls, clk_data, fhctl_register_version);

...where fhctl_register_version is used to assign the right fhctl register offsets. Also, it's not needed to assign all of the register offsets statically, because they can be easily calculated based on the number of supported PLLs, since the registers are structured like

[FHCTL GLOBAL REGISTERS] <--- hp_en...slope1 [FHCTL SSC GLOBAL REGISTERS] <--- DSSC_CFG, DSSC0...x_CON

[FHCTL PER-PLL REGISTERS] <--- CFG...MON
^^^ where this is repeated X times for X PLLs.

so, keeping the example of MT8186, we can get the per-pll register like:

#define FHCTL_PLL_OFFSET 0x3c
#define FHCTL_PLL_LEN 0x14

#define FHCTLx_CFG(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN))
#define FHCTLx_UPDNLMT(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x4)
#define FHCTLx_DDS(pll_id) (FHCTL_PLL_OFFSET + (pll_id * FHCTL_PLL_LEN) + 0x8)

Quoted 6 lines
Quoted 1 line

we don't need to put all of them in a structure and for each PLL.

We use structure instead of using macros is because the register offset may have difference between ICs. If we use macro, we need to maintain different versions of macros. Using structure to store these register offsets is more flexible.

I understand. What I don't like about your specific approach is the amount of register offsets that we store in that structure, looks like it's a bit too many.

I've seen that there's a common pattern at least by checking downstream 5.10 and MT8186/95 layouts, so I still think that using these macros will be beneficial.

Quoted 7 lines
Quoted 4 lines

We can always add parameters to the structure in a later commit: in my opinion, that will help to engineer a better, shorter, cleaner solution for calculating these registers anyway... but I will leave this choice to you, anyway, since you know about way more SoCs than I do.

OK, we will reduce the structure.

Perfect!

Quoted 63 lines
Quoted 63 lines
Quoted 43 lines
Quoted 8 lines
Quoted 5 lines

So, we need both APMIXEDSYS and Frequency Hopping Controller in set_rate() logic to handle this two types of PLL.

As already said, we preventively know which PLLs support FHCTL and which does not, so we can use a different .set_rate() callback.

Ok, we can use a different .set_rate() callback when fhctl driver probing.

Quoted 32 lines
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

current set_rate method in clk-pll changes PLL register setting directly. Another way uses FHCTL to change PLL rate.

...and of course, if we change that, we're effectively mutating the functionality of the MediaTek clk-pll driver and please understand that seeing a clear mutation in that driver is a bit more human-readable.

Quoted 7 lines
Quoted 7 lines

Besides, this makes me think about one question: is there any instance in which, when FHCTL rate setting fails, we fall back to direct register writes?

I don't think that this is feasible because we have a register in FHCTL that effectively hands over control to it, so direct register writes should not work when the PLL is not under APMIXEDSYS control, but I'm asking just to be extremely sure that my understanding is right.

Quoted 7 lines

It won't fall back to direct register writes when FHCTL rate setting fails. But, PLL control mode will switch back to APMIXEDSYS after frequency hopping completed.

There are two cases that we need to fall back to direct register writes:
1. PLL support FHCTL but it doesn't want to use FHCTL.
2. PLL doesn't support FHCTL HW.

For case N.1, if this is board-specific, we have to resort to devicetree properties that will enable/disable FHCTL on specific PLLs.

mediatek,fhctl-disable = <CLK_APMIXED_MSDCPLL>, <CLK_APMIXED_NNAPLL>;

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>;

Quoted 8 lines
Quoted 4 lines

These are just examples - I don't currently know if it's a better idea to have an allowlist or a blocklist as devicetree properties, as that depends on the expected number of PLLs for which we en/dis fhctl or just ssc (if we generally want fhctl enabled on all but one PLLs, we should use fhctl-disable, otherwise, fhctl-enable).

We also have a properity "ssc-rate" for setting up the ssc rate in percentage. The "ssc- rate" properity is under fhctl dts node and can be setup on each fhctl-PLL.

Right. For that, we could have a default sensible percentage when SSC is enabled but no rate is set in devicetree, or we can perhaps consider SSC enabled when any meaningful SSC rate is set... For example:

mediatek,ssc-enable = <CLK_APMIXED_MFGPLL>, <CLK_APMIXED_TVDPLL>; mediatek,ssc-percent = <5>, <5>;

... or something like:

mediatek,ssc = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

Quoted 8 lines
Quoted 3 lines

...but I'd like to have some feedback on that from somebody else, as I don't know if that would be acceptable in devicetree, or if there's any cleaner, niftier solution.

OK, we will use this:

mediatek,hopping-ssc-percent = <CLK_APMIXED_MFGPLL 5>, <CLK_APMIXED_TVDPLL 5>;

Looks good.

Quoted 174 lines
Quoted 150 lines
Quoted 122 lines
Quoted 29 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

We will set some PLL's frequency be controlled
by clk-pll and some are controlled by FHCTL.

Another question: is this also changing on a per-board basis?

(note: the pll names in the example are random and not specific to anything)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Example: board A wants FHCTL on MMPLL, TVDPLL, MPLL, but *shall not* hand over
NNAPLL, MFGPLL
board B wants FHCTL on NNAPLL, TVDPLL but *shall not* hand over MMPLL

Granted that the two A, B boards are using the same SoC, can that ever happen?

This could happen if A, B boards have different desense issue.

Ok, so it's definitely board specific. Devicetree is the way to go for this.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

And use perms param to decide
whether a PLL is using FHCTL to change its frequency.

The perms param seems to be about:
* Enabling debug (but you're not providing any way to actually use debugging features, so what's the point?)

Quoted 7 lines
Quoted 2 lines

Debugging feature is not used yet, we can removed it.

If the debugging features of the FHCTL driver will be like what I can see on the downstream MT6893 5.10 kernel, that's not really applicable to upstream.

In that case, please remove the debug.

Ok, we will remove it.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

* Handing over PLL control to FHCTL for hopping (can be as well done with simply using a different .set_rate() callback instead of a flag)

There has some PLL that have FHCTL but don't want to use FHCTL. The flag is used in this case.

Use the flag to set the right .set_rate() callback, set at probe time, instead of checking that flag at every set_rate() call.

We will setup .set_rate() callback when doing fhctl-pll init.

Quoted 59 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

* Enabling/disabling Spread Spectrum Clocking (and I think that this is a legit use for flags, but if it's just one flag, you can as well use a bool and manage this with a devicetree param like "enable-ssc")

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

That said, I think that the current way of enabling the FHCTL is more complicated than how it should really be.

Here needs an option to decide whether to enable FHCTL-hopping or FHCTL-ssc since these two are per-board basis.

We cannot force all PLL hand over to FHCTL for hopping casue not all PLLs support FHCTL and not all PLLs have need of using FHCTL-hopping.

Board specific -> devicetree

SoC specific -> hardcode, no devicetree.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

FHCTL has another function called SSC(spread spectrum clocking) which is used to solve PLL de-sense problem. De-sense problem is board-related so we introduce a ssc-rate param in the devicetree to decide whether SSC is enabled and how many rate should be set. Mixing SSC function into clk-pll may cause clk-pll more complex.

Thing is, I don't get why you think that adding SSC to clk-pll would complicate it so much... it's really just a few register writes and nothing else, so I really don't see where the problem is, here.

Quoted 8 lines
Quoted 8 lines

Another issue is that this driver may be largely incomplete, so perhaps I can't really see the complications you're talking about? Is this the case?

Regarding keeping the FHCTL code in separated files, that's fine, but I would still integrate it tightly in clk-pll and its registration flow, because - yes, this is for sure not mandatory, but the main parameters are constant, they never change for a specific PLL, as they're register offsets, bits and masks (which, again, will never change as long as we're using the same SoC).

Quoted 5 lines

The driver may need to supoport microP by future HW design, standalone file clk- fhctl.c helps to trigger init flow of such as ap-init-flow, microP-init-flow ....., and those different init-flow also need to run some communication API with microP. Those communication APIs are not suitable to merge into clk-pll.

Let's use clk-fhctl as an helper then, we can make sure to call the init flow for the microP in the SoC-specific clock drivers, I think that's not a problem?

clk_mtfuturesoc_someip_probe()

{
	.... register clocks ....

	freqhopping_microp_init();

	return ret;
}
Quoted 5 lines

If there's hardware out there that supports such feature and a downstream kernel to look at, please tell me which one, so that I will be able to check it out and perhaps understand how this flow works.

P.S.: I guess it's not fhctl-sspm?

You could find clk-fhctl-mcupm.c and clk-fhctl-gpueb.c on the downstream MT6893 5.10 kernel. Those codes require the PLL hardware specification to determine which PLL group(eg. PLL TOP group, GPUEB group) runs on which microP and has responsibilty to communicate with the microP.

Quoted 5 lines

If we implement these things into clk-pll driver, clk-pll driver not only needs to control PLL frequency but also needs to deal with microP IPI. It makes clk-pll driver have others works that is not belong to PLL operation. That's why we tend to have a standalone driver for FHCTL.

Ok having something to analyze made this entire thing a bit more clear in my mind, thanks for the pointers.

Analyzing clk-fhctl-mcupm and clk-fhctl-gpueb makes me see that there's a lot of common code between the two: x_hopping_v1(), x_ssc_enable_v1(), x_ssc_disable_v1() (where x = {gpueb,mcupm}) are really the same functions, duplicated and renamed and nothing else.
The only difference is the get_xxxx_ipidev(), which is avoidable by assigning mboxes = <...something...> in devicetree (gpueb mailbox, or mcupm mailbox).

Even the FH_DEVCTL_CMD_ID enumeration uses the same values!

To unroll that riddle, I would at that point add a new MediaTek specific clock driver (like clk-pll) and call it clk-ipi.c, because that's what it does in the end: whatever we do, goes through a mailbox instead of a direct MMIO write.

That clk-fhctl-ipi would contain a probe function that gets the mailbox handle, then we would add something like clk_fhctl_set_rate() function, export it in the clk-mtk.h or in a new clk-fhctl.h header, then assign the right callback in either the SoC's clock driver (by registering a different clock type, which, in this case, would be clk-fhctl-ipi instead of clk-pll), or in clk-pll itself...

Quoted 4 lines

In the end, I'm effectively proposing to:

1. Merge the direct-MMIO handling of FHCTL in clk-pll;
2. Create a new driver (and clock type, eventually) for the IPI handling of FHCTL.

From your idea, I think we can also create a new clock type for fhctl such as clk-pll-fh
and add a new PLL register function for PLL+FHCTL. Then we can change the registery interface and won't affect the legacy ICs. Also, if FHCTL has changes, we only need to modify clk-pll-fh.
I think using a new clock type has extendibility for FHCTL changes and also compatiable with legacy ICs.

clk-pll.h

	/* Define FHCTL data structure and contains mtk_pll_data.
	 * We can use mtk_pll_data later. */
	mtk_pll_fh_data {
		struct mtk_pll_data pll_data;
		/* fhctl_data */
		unsigned int fh_id;
		unsigned int ssc_rate;
		...
	}

clk-mt8186-apmixedsys.c

Quoted 12 lines
	func clk_mt8186_apmixed_probe()
		/* There are two implementations.
		 * If ICs need FHCTL such as MT8186, use mtk_clk_register_pllfhs()
		 * For those legacy ICs which don't need FHCTL, still use
		 * mtk_clk_register_plls().
		 */
		/* 1. Need FHCTL. Use API from clk-pll-fh.c */
		fhctl_parse_dt()
		mtk_clk_register_pllfhs(plls data, fh-plls data)
		
		/* 2. Legacy ICs. Use API from clk-pll.c */
		mtk_clk_register_plls()

I'm not sure if we're saying the very same thing here, but for the sake of being clear and avoiding any misunderstanding, here's my description.

We should call both functions: register_pllfhs() for the PLLs that have support for freqhopping, register_plls() for the ones that *do not support* freqhopping.

Example for PLL_A, PLL_B, PLL_C, PLL_D:

Freqhopping supported (enabled or not): PLL_A, PLL_B Freqhopping NOT supported at all: PLL_C, PLL_D

fhplls_data[] = { PLL_A, PLL_B };
plls_data[] = { PLL_C, PLL_D };

func mtk_clk_register_pllfhs(fhdata)
walk through fhplls_data only, other plls are not passed to this function

func clk_mt8186_apmixed_probe()
/* Some PLLs must be controlled directly via MMIO, while others
* support Frequency Hopping through FHCTL.
* Where FHCTL is supported, register clock with register_pllfhs.
* PLLs that are not supported by FHCTL: register with register_plls. */

Quoted 7 lines

/* Register FHCTL PLLs */
fhctl_parse_dt() mtk_clk_register_pllfhs(array of plls supporting pllfh)

/* Register the PLLs that do not support FHCTL at all */ mtk_clk_register_plls(all the other PLLs that cannot be FHCTL-controlled)

I have a different opinion here. I think we should just choose one register function when probe and it depends on platform implementation instead of a PLL is supported FHCTL or not. We have an option to use clk-pll-fh or clk-pll at the beginning of new IC development. And, we want it's easy to add FHCTL support on legecy ICs.

The way of how we use the register function is affected by the way we use fhplls_data. If FHCTL supported or not is used to decide which registery function should we use, it has to change the exist plls_data. But most of IC's PLL data is already exist. So I want to use a structure to descript FHCTL HW itself and have a link to existed PLL data.

From your example, it will look like:

plls_data = { PLL_A, PLL_B, PLL_C, PLL_D }; /* Already existed */ fhplls_data = { FH_A,
FH_B }; /* New added for FHCTL data */

func mtk_clk_register_pllfhs(plls_data, fhplls_data)

	fhctl_match_pll_data()
		/* Link fhpll and pll:
		 *   FH_A -> PLL_A
		 *   FH_B -> PLL_B
		 */

For MT8186 and ICs need FHCTL supported:

	func clk_mt8186_apmixed_probe()
		fhctl_parse_dt()
		mtk_clk_register_pllfhs(plls_data, fhplls_data)
Quoted 8 lines

For those don't need FHCTL and legacy ICs:
func clk_mtxxxx_apmixed_probe()
mtk_clk_register_plls(plls_data)

When one ICs that need to support FHCTL but they didn't implement before, we only need to change the registery function and add FHCTL data, no need to modify existed plls_data. I think it can save our development time and maintenance effort.

I see what you mean now. Okay, that seems sensible.

Quoted 24 lines
Quoted 18 lines
Quoted 8 lines

clk-pll.c

	/* No functional changes, so legacy ICs won't be affected.
	 * Export clk_ops functions to clk-pll-fh.c
	 */
	func mtk_clk_register_plls()
		init.ops = &mtk_pll_ops;
Quoted 9 lines

clk-pll-fh.c
/* A clock type of FHCTL PLL. Used to setup HW data and ops.
* Most of ops functions inherit from clk-pll.c.
* If PLL not support or not enable FHCTL, fallback to use &mtk_pll_ops. */
func mtk_clk_register_pllfhs(plls data, fh-plls data)
fhctl_match_pll_data() /* match mtk_pll_data and mtk_pll_fh_data */ fhctl_hw_init()
if (fhctl_is_supported_and_enabled(pll))

Quoted 5 lines

Overall, this seems to look good, minor one nit: if FHCTL is *not supported* on a PLL, we should *not* even call mtk_clk_register_pllfhs on that PLL, so your pseudocode would be just:

if (fhctl_is_enabled(pll))

Quoted 8 lines

The requirements of clk-pll-fh are:
1. It is compatible with those PLLs that don't support FHCTL HW.
2. It can handle PLLs that supported FHCTL but aren't enabled.
3. It can handle PLLs that supported FHCTL but are enabled.

So, I use fhctl_is_supported_and_enabled(PLL) here. No matter a PLL supports FHCTL or not, it can work.

Alright, let's go with that!

Quoted 23 lines
Quoted 22 lines
Quoted 8 lines
			init.ops = &mtk_pll_fhctl_ops;
		else
			init.ops = &mtk_pll_ops;
	
		if (ssc_is_enable(pll))
			fhctl_ssc_enable(pll)
Quoted 7 lines

clk-fhctl.c

	/* APIs of FHCTL HW operations */
	func fhctl_hw_init()
	func fhctl_hopping()
	func fhctl_ssc_enable()

So it seems that we've reached an agreement here, this was a nice planning discussion; we should now have a nice and solid base to work on, which is great.

Cheers,
Angelo