Skip to contentSkip to conversation

clk: tc9564: introduce a clock and reset driver

PATCH In review Alex Elder 3 participants 9 messages Nov 20 – Nov 23

Subsystems (2)
lore
Share & tools
lore

Alex Elder

author
#0 lore raw

v2 cover clk: tc9564: introduce a clock and reset driver

This code has gone around and around in various forms, having been included most recently in a series that also added a system controller driver. That system controller has now been removed.

This series introduces a devicetree binding and single driver to support the clock and reset components present within the Toshiba TC9564 chip.

The Toshiba TC9564 is an SoC that attaches to a host system via PCIe. It includes a PCIe switch with three downstream ports. To one of these ports an embedded PCIe endpoint is attached, and that implements two PCI functions. BAR 4 for both PCI functions provides access to registers controlling blocks within the SoC, including the clock and resets controllers supported by this series.

To avoid any confusion I'm only going to refer back to the "syscon" series", which provides some additional information including links to earlier versions of this code.

-Alex

The previous version of this code was included in this series:
https://lore.kernel.org/all/[email protected]/

Between that series (v1) and this one (v2):
- There is no more syscon, so its devicetree binding and driver have been dropped
- The clock and reset components now have a single devicetree binding
- We no longer use a syscon-generated regmap for accessing the common range of memory used by multiple drivers; specific sub-ranges are instead mapped
- The clock and reset drivers have been combined into a single driver located under drivers/clk
- The config option for the driver includes COMPILE_TEST, as requested (last month) by Jerome Brunet
- The help text for the config option is simplified, first suggested by Philipp Zabel
- The driver now names clocks using dev_name() rather than trying to hand-code a unique name into a fixed-size name array (also suggested by Jerome)
- Certain functions now always return 0, which simplifies callers so they can simply return what the called function returns
- As suggested by sashiko, a counted-by field in a flex array is now assigned explicitly

Alex Elder (2):
dt-bindings: toshiba: introduce toshiba,tc9564-clock.yaml
clk: toshiba: introduce a TC9564 clock/reset driver

 .../bindings/clock/toshiba,tc9564-clock.yaml  |  73 +++
 MAINTAINERS                                   |   9 +
 drivers/clk/Kconfig                           |   8 +
 drivers/clk/Makefile                          |   1 +
 drivers/clk/clk-tc9564.c                      | 501 ++++++++++++++++++
 include/dt-bindings/clock/toshiba,tc9564.h    |  50 ++
 6 files changed, 642 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
 create mode 100644 drivers/clk/clk-tc9564.c
 create mode 100644 include/dt-bindings/clock/toshiba,tc9564.h

base-commit: 909ef7a9a30fa139e69e555e3606680f5cca7726

Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v2 1/2 dt-bindings: toshiba: introduce toshiba,tc9564-clock.yaml

Define the binding for the clock and reset controller that present in the Toshiba TC9564 SoC.

---
v2: - New binding that combines clock and reset under the clock node

 .../bindings/clock/toshiba,tc9564-clock.yaml  | 73 +++++++++++++++++++
 MAINTAINERS                                   |  8 ++
 include/dt-bindings/clock/toshiba,tc9564.h    | 50 +++++++++++++
 3 files changed, 131 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
 create mode 100644 include/dt-bindings/clock/toshiba,tc9564.h
Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml new file +73 -0
new file mode 100644
index 0000000000000..e789ce8c6a3ac
@@ -0,0 +1,73 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/toshiba,tc9564-clock.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Toshiba TC9564 Clock and Reset Controller
+
+maintainers:
+  - Lorenzo Bianconi <[email protected]>
+  - Alex Elder <[email protected]>
+  - Daniel Thompson <[email protected]>
+
+description:
+  The Toshiba TC9564 is an SoC accessed by a host system through the
+  upstream PCIe port on the PCIe switch it implements.  The chip includes
+  an embedded PCIe endpoint that provides access to various peripherals
+  within the SoC via its BARs.  These peripherals include a simple
+  controller to gate internal clocks, and a reset controller.
+
+  The TC9564 chip has a single input clock.  Internally that clock is
+  multiplied by a PLL, and the PLL signal is divided into a set of
+  fixed rate, always-on clocks.
+
+  The clock controller uses two registers to control a set of simple
+  gated clocks, each derived from one of the internal fixed-rate
+  clocks.  A total of 21 gated clocks are implemented within the chip,
+  but support for enabling and disabling only a subset of them is
+  provided at this time.  Two clocks (I2C and PCIE) are marked critical
+  to make it clear that they should never be disabled.
+
+  The reset controller uses two other registers to control a set of 13
+  resets that can be asserted or deasserted.  Here too, only a subset
+  of the resets can currently be managed, and the I2C and PCIE resets
+  are treated as critical.
+
+  Clock and reset IDs are defined <dt-bindings/clock/toshiba,tc9564.h>.
+
+properties:
+  compatible:
+    items:
+      - const: toshiba,tc9564-clock
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  "#clock-cells":
+    const: 1
+
+  "#reset-cells":
+    const: 1
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - "#clock-cells"
+  - "#reset-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    clock-controller@1004 {
+        compatible = "toshiba,tc9564-clock";
+        reg = <0x1004 0x10>;
+        clocks = <&tc9564_xo_board>;
+        #clock-cells = <1>;
+        #reset-cells = <1>;
+    };
MAINTAINERS +8 -0
index c00d3612c9acf..af088e1092e63 100644
@@ -28683,6 +28683,14 @@ F:	Documentation/devicetree/bindings/media/i2c/toshiba,tc358743.yaml
 F:	drivers/media/i2c/tc358743*
 F:	include/media/i2c/tc358743.h
 
+TOSHIBA TC9564 CLOCK/RESET CONTROLLER DRIVER
+M:	Lorenzo Bianconi <[email protected]>
+M:	Alex Elder <[email protected]>
+M:	Daniel Thompson <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
+F:	include/dt-bindings/clock/toshiba,tc9564.h
+
 TOSHIBA TC9564 PCI DRIVER
 M:	Alex Elder <[email protected]>
 M:	Daniel Thompson <[email protected]>
include/dt-bindings/clock/toshiba,tc9564.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.53.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] dt-bindings: toshiba: introduce toshiba,tc9564-clock.yaml

Quoted 6 lines

Define the binding for the clock and reset controller that present in the Toshiba TC9564 SoC.

Quoted 167 lines

---
v2: - New binding that combines clock and reset under the clock node

 .../bindings/clock/toshiba,tc9564-clock.yaml  | 73 +++++++++++++++++++
 MAINTAINERS                                   |  8 ++
 include/dt-bindings/clock/toshiba,tc9564.h    | 50 +++++++++++++
 3 files changed, 131 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
 create mode 100644 include/dt-bindings/clock/toshiba,tc9564.h
Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml new file +73 -0
new file mode 100644
index 0000000000000..e789ce8c6a3ac
@@ -0,0 +1,73 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/toshiba,tc9564-clock.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Toshiba TC9564 Clock and Reset Controller
+
+maintainers:
+  - Lorenzo Bianconi <[email protected]>
+  - Alex Elder <[email protected]>
+  - Daniel Thompson <[email protected]>
+
+description:
+  The Toshiba TC9564 is an SoC accessed by a host system through the
+  upstream PCIe port on the PCIe switch it implements.  The chip includes
+  an embedded PCIe endpoint that provides access to various peripherals
+  within the SoC via its BARs.  These peripherals include a simple
+  controller to gate internal clocks, and a reset controller.
+
+  The TC9564 chip has a single input clock.  Internally that clock is
+  multiplied by a PLL, and the PLL signal is divided into a set of
+  fixed rate, always-on clocks.
+
+  The clock controller uses two registers to control a set of simple
+  gated clocks, each derived from one of the internal fixed-rate
+  clocks.  A total of 21 gated clocks are implemented within the chip,
+  but support for enabling and disabling only a subset of them is
+  provided at this time.  Two clocks (I2C and PCIE) are marked critical
+  to make it clear that they should never be disabled.
+
+  The reset controller uses two other registers to control a set of 13
+  resets that can be asserted or deasserted.  Here too, only a subset
+  of the resets can currently be managed, and the I2C and PCIE resets
+  are treated as critical.
+
+  Clock and reset IDs are defined <dt-bindings/clock/toshiba,tc9564.h>.
+
+properties:
+  compatible:
+    items:
+      - const: toshiba,tc9564-clock
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  "#clock-cells":
+    const: 1
+
+  "#reset-cells":
+    const: 1
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - "#clock-cells"
+  - "#reset-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    clock-controller@1004 {
+        compatible = "toshiba,tc9564-clock";
+        reg = <0x1004 0x10>;
+        clocks = <&tc9564_xo_board>;
+        #clock-cells = <1>;
+        #reset-cells = <1>;
+    };
MAINTAINERS +8 -0
index c00d3612c9acf..af088e1092e63 100644
@@ -28683,6 +28683,14 @@ F:	Documentation/devicetree/bindings/media/i2c/toshiba,tc358743.yaml
 F:	drivers/media/i2c/tc358743*
 F:	include/media/i2c/tc358743.h
 
+TOSHIBA TC9564 CLOCK/RESET CONTROLLER DRIVER
+M:	Lorenzo Bianconi <[email protected]>
+M:	Alex Elder <[email protected]>
+M:	Daniel Thompson <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
+F:	include/dt-bindings/clock/toshiba,tc9564.h
+
 TOSHIBA TC9564 PCI DRIVER
 M:	Alex Elder <[email protected]>
 M:	Daniel Thompson <[email protected]>
include/dt-bindings/clock/toshiba,tc9564.h new file +50 -0
new file mode 100644
index 0000000000000..a0325b10b6c36
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+/*
+ * Copyright (C) 2026 by RISCstar Solutions Corporation.  All rights reserved.
+ */
+
+#ifndef __CLOCK_TOSHIBA_TC9564_H__
+#define __CLOCK_TOSHIBA_TC9564_H__
+
+/* Clock IDs */
+
+#define CLOCK_PLL		0
+
+#define CLOCK_62_5M		1
+#define CLOCK_100M		2
+#define CLOCK_125M		3
+#define CLOCK_250M		4
+#define CLOCK_312_5M		5
+
+#define CLOCK_I2C		6	/* critical */
+#define CLOCK_PCIE		7	/* critical */
+#define CLOCK_MSIGEN		8
+
+#define CLOCK_MAC0_TX		9
+#define CLOCK_MAC0_RX		10
+#define CLOCK_MAC0_125M		11
+#define CLOCK_MAC0_312_5M	12
+#define CLOCK_MAC0_ALL		13
+
+#define CLOCK_MAC1_TX		14
+#define CLOCK_MAC1_RX		15
+#define CLOCK_MAC1_125M		16
+#define CLOCK_MAC1_312_5M	17
+#define CLOCK_MAC1_ALL		18
+
+/* Reset IDs */
+
+#define RESET_I2C		0	/* critical */
+#define RESET_PCIE		1	/* critical */
+#define RESET_MSIGEN		2
+
+#define RESET_MAC0_MAC		3
+#define RESET_MAC0_PMA		4
+#define RESET_MAC0_XPCS		5
+
+#define RESET_MAC1_MAC		6
+#define RESET_MAC1_PMA		7
+#define RESET_MAC1_XPCS		8
+
+#endif /* __CLOCK_TOSHIBA_TC9564_H__*/
Signature · 1 line
2.53.0
author
#3 lore raw

PATCH v2 2/2 clk: toshiba: introduce a TC9564 clock/reset driver

In reply to Alex Elder

Define a new platform driver that manages clocks implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but currently only the clocks that are (or will soon be) used are defined to be controllable.

Two registers manage the state of the clocks. The registers are accessed via a regmap supplied by a system controller. Access to the memory region is provided via a BAR on a PCIe endpoint function embedded in the TC9564 SoC.

Define the binding for the clock and reset controller that present in the Toshiba TC9564 SoC.

Define a new platform driver that manages clocks and resets implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but some of these won't be used any time soon by software, so they are omitted.

There are also 13 resets that can be asserted or deasserted, but support for some of these is omitted as well.

The I2C and PCIE clocks and resets are treated as CRITICAL, meaning their state should never be changed (and a warning is issued if changing their state is attempted).

---
v2: - New driver that combines clock and reset functionality
- Map the platform memory resource rather than using a regmap
- Kconfig adds COMPILE_TEST, and its help text is simplified
- Clock names now defined based on dev_name()
- gate_clk_manage() and reset_manage() return 0, so their callers can be made a little simpler
- Flex array counted-by field is now explicitly assigned

 MAINTAINERS              |   1 +
 drivers/clk/Kconfig      |   8 +
 drivers/clk/Makefile     |   1 +
 drivers/clk/clk-tc9564.c | 501 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 511 insertions(+)
 create mode 100644 drivers/clk/clk-tc9564.c
MAINTAINERS +1 -0
index af088e1092e63..9503c304c35ed 100644
@@ -28689,6 +28689,7 @@ M:	Alex Elder <[email protected]>
 M:	Daniel Thompson <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
+F:	drivers/clk/clk-tc9564.c
 F:	include/dt-bindings/clock/toshiba,tc9564.h
 
 TOSHIBA TC9564 PCI DRIVER
drivers/clk/Kconfig +8 -0
index 6bfa2c8fffe1b..b27807ee93863 100644
@@ -185,6 +185,14 @@ config COMMON_CLK_CDCE706
 	help
 	  This driver supports TI CDCE706 programmable 3-PLL clock synthesizer.
 
+config COMMON_CLK_TC9564
+	tristate "Toshiba TC9564 clock and reset support"
+	depends on TC9564_PCI || COMPILE_TEST
+	default TC9564_PCI
+	help
+	  This enables support for the clock and reset controllers embedded
+	  in the Toshiba TC9564 SoC.
+
 config COMMON_CLK_TPS68470
 	tristate "Clock Driver for TI TPS68470 PMIC"
 	depends on I2C
drivers/clk/Makefile +1 -0
index 99377a13ebc57..d67cfb6d623d3 100644
@@ -111,6 +111,7 @@ obj-$(CONFIG_COMMON_CLK_SI570)		+= clk-si570.o
 obj-$(CONFIG_COMMON_CLK_SP7021)		+= clk-sp7021.o
 obj-$(CONFIG_COMMON_CLK_STM32F)		+= clk-stm32f4.o
 obj-$(CONFIG_COMMON_CLK_STM32H7)	+= clk-stm32h7.o
+obj-$(CONFIG_COMMON_CLK_TC9564)		+= clk-tc9564.o
 obj-$(CONFIG_COMMON_CLK_TPS68470)      += clk-tps68470.o
 obj-$(CONFIG_CLK_TWL6040)		+= clk-twl6040.o
 obj-$(CONFIG_CLK_TWL)			+= clk-twl.o
drivers/clk/clk-tc9564.c new file +501 -0

501 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH v2 2/2] clk: toshiba: introduce a TC9564 clock/reset driver

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] clk: tc9564: unlocked register read-modify-write in reset_manage() - [Medium] clk: tc9564: missing…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] clk: tc9564: unlocked register read-modify-write in reset_manage()
- [Medium] clk: tc9564: missing select RESET_CONTROLLER in COMMON_CLK_TC9564
- [Low] clk: tc9564: stale v1 and dt-binding text in commit message
- [Low] clk: tc9564: duplicate clk_gate implementation in gate_register()
--

commit c8a057c6e9911108f130ae1447d0fae1c9ba8eff
Author: Alex Elder <[email protected]>

clk: toshiba: introduce a TC9564 clock/reset driver

This commit adds a platform driver for the clock and reset controllers on the Toshiba TC9564 SoC.

Quoted 5 lines

Define a new platform driver that manages clocks implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but currently only the clocks that are (or will soon be) used are defined to be controllable.

Quoted 7 lines

Two registers manage the state of the clocks. The registers are accessed via a regmap supplied by a system controller. Access to the memory region is provided via a BAR on a PCIe endpoint function embedded in the TC9564 SoC.

Define the binding for the clock and reset controller that present in the Toshiba TC9564 SoC.

[Severity: Low]
These first three paragraphs appear to be leftover text from v1 and the devicetree binding patch: the driver now maps the platform MMIO resource directly via devm_platform_ioremap_resource() instead of using a syscon regmap, and the description restarts in the fourth paragraph.

Quoted 5 lines
drivers/clk/Kconfig +0 -0
index 6bfa2c8fffe1b..b27807ee93863 100644

drivers/clk/Kconfig config COMMON_CLK_CDCE706 · line 194

Quoted 10 lines
drivers/clk/Kconfig +7 -0
@@ -185,6 +185,14 @@ config COMMON_CLK_CDCE706
 	help
 	  This driver supports TI CDCE706 programmable 3-PLL clock synthesizer.
 
+config COMMON_CLK_TC9564
+	tristate "Toshiba TC9564 clock and reset support"
+	depends on TC9564_PCI || COMPILE_TEST
+	default TC9564_PCI
+	help
+	  This enables support for the clock and reset controllers embedded
+	  in the Toshiba TC9564 SoC.

[Severity: Medium]
Should COMMON_CLK_TC9564 select RESET_CONTROLLER? Because TC9564 is a PCIe endpoint device, this driver can be enabled on host architectures that do not select ARCH_HAS_RESET_CONTROLLER. When CONFIG_RESET_CONTROLLER is disabled, devm_reset_controller_register() compiles to a no-op stub returning 0, silently omitting the reset controller functionality and causing non-optional reset consumers to fail with -ENOTSUPP.

Quoted 5 lines
drivers/clk/clk-tc9564.c new file +0 -0
new file mode 100644
index 0000000000000..65ed524bf2ee7

[ ... ]

Quoted 41 lines
drivers/clk/clk-tc9564.c new file +41 -0
+static const struct reset_init reset_init[] = {
+	[RESET_I2C] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(12),
+	},
+	[RESET_PCIE] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(9),
+	},
+	[RESET_MSIGEN] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(18),
+	},
+
+	[RESET_MAC0_MAC] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC0_PMA] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC0_XPCS] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(31),
+	},
+
+	[RESET_MAC1_MAC] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC1_PMA] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC1_XPCS] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(31),
+	},
+};
+#define RESET_COUNT	ARRAY_SIZE(reset_init)

[ ... ]

Quoted 73 lines
drivers/clk/clk-tc9564.c new file +73 -0
+static const struct gate_clock *hw_to_gate_clock(struct clk_hw *hw)
+{
+	return container_of_const(hw, struct gate_clock, hw);
+}
+
+static int gate_clk_manage(struct clk_hw *hw, bool enable)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
+	u32 mask = clock->init->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	val = readl(ioaddr);
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel(val, ioaddr);
+
+	return 0;
+}
+
+static int gate_clk_enable(struct clk_hw *hw)
+{
+	return gate_clk_manage(hw, true);
+}
+
+static void gate_clk_disable(struct clk_hw *hw)
+{
+	(void)gate_clk_manage(hw, false);
+}
+
+static int gate_clk_is_enabled(struct clk_hw *hw)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
+	void __iomem *ioaddr;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	return readl(ioaddr) & clock->init->mask ? 1 : 0;
+}
+
+static const struct clk_ops gate_clk_ops = {
+	.enable = gate_clk_enable,
+	.disable = gate_clk_disable,
+	.is_enabled = gate_clk_is_enabled,
+};
+
+static struct clk_hw *gate_register(struct device *dev, const char *name,
+				    const struct clock_init *init)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(dev);
+	struct clk_init_data clk_init = { };
+	const struct clk_hw *parent_hw;
+	struct gate_clock *clock;
+	int ret;
+
+	clock = devm_kzalloc_obj(dev, *clock);
+	if (!clock)
+		return ERR_PTR(-ENOMEM);
+
+	parent_hw = tc9564_clock->clk_data->hws[init->parent];
+
+	clk_init.name = name;
+	clk_init.ops = &gate_clk_ops;
+	clk_init.parent_hws = &parent_hw;
+	clk_init.num_parents = 1;
+	if (init->type == CRITICAL_GATE)
+		clk_init.flags = CLK_IS_CRITICAL;

drivers/clk/clk-tc9564.c line 347

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+
+	clock->hw.init = &clk_init;
+	clock->init = init;
+
+	ret = devm_clk_hw_register(dev, &clock->hw);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return &clock->hw;
+}

[Severity: Low]
This isn't a bug, but now that the driver accesses MMIO registers directly rather than through regmap, gate_register() and gate_clk_ops duplicate the standard single-bit MMIO gate implementation. Using devm_clk_hw_register_gate_parent_hw() (as pll_register() and fixed_register() do with the fixed-factor helpers) would allow dropping struct gate_clock and the custom gate callbacks.

Quoted 20 lines
drivers/clk/clk-tc9564.c new file +20 -0
+
+/* Warn on requests to assert or deassert critical resets */
+static bool reset_is_critical(unsigned long id)
+{
+	return WARN_ON(id == RESET_I2C) || WARN_ON(id == RESET_PCIE);
+}
+
+static int reset_manage(struct reset_controller_dev *rcdev,
+			unsigned long id, bool assert)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(rcdev->dev);
+	const struct reset_init *reset = &reset_init[id];
+	u32 mask = reset->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	if (reset_is_critical(id))
+		return 0;		/* Just ignore the request */
+
+	ioaddr = tc9564_clock->ioaddr + reset->offset;

drivers/clk/clk-tc9564.c line 377

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+
+	val = readl(ioaddr);
+	if (assert)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel(val, ioaddr);
+
+	return 0;
+}

[Severity: Medium]
Can concurrent reset operations on lines sharing RESET0_OFFSET or RESET1_OFFSET race in reset_manage()?

Unlike the clock framework (which holds enable_lock across enable and disable callbacks), the reset controller core only acquires a per-reset-control SRCU read lock in reset_control_assert() and reset_control_deassert(). Two consumers asserting or deasserting different resets in the same register (such as RESET_MSIGEN and RESET_MAC0_MAC in RESET0_OFFSET) can perform concurrent read-modify-write sequences and overwrite each other's bit changes. A spinlock in struct tc9564_clock around the readl()/writel() sequence in reset_manage() would prevent this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

Re: [PATCH v2 2/2] clk: toshiba: introduce a TC9564 clock/reset driver

Quoted 10 lines · On 11/20/26 12:42 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] clk: tc9564: unlocked register read-modify-write in reset_manage()
- [Medium] clk: tc9564: missing select RESET_CONTROLLER in COMMON_CLK_TC9564
- [Low] clk: tc9564: stale v1 and dt-binding text in commit message
- [Low] clk: tc9564: duplicate clk_gate implementation in gate_register()
--

Quick summary (more below): I will act on all of the observed issues mentioned here.

Quoted 18 lines

commit c8a057c6e9911108f130ae1447d0fae1c9ba8eff
Author: Alex Elder <[email protected]>

clk: toshiba: introduce a TC9564 clock/reset driver

This commit adds a platform driver for the clock and reset controllers on the Toshiba TC9564 SoC.

Quoted 10 lines

Define a new platform driver that manages clocks implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but currently only the clocks that are (or will soon be) used are defined to be controllable.

Two registers manage the state of the clocks. The registers are accessed via a regmap supplied by a system controller. Access to the memory region is provided via a BAR on a PCIe endpoint function embedded in the TC9564 SoC.

Quoted 8 lines
Quoted 2 lines

Define the binding for the clock and reset controller that present in the Toshiba TC9564 SoC.

[Severity: Low]
These first three paragraphs appear to be leftover text from v1 and the devicetree binding patch: the driver now maps the platform MMIO resource directly via devm_platform_ioremap_resource() instead of using a syscon regmap, and the description restarts in the fourth paragraph.

This is correct, I neglected to update the text that talked about regmaps. I will fix this.

I will also fix the e-mail addresses provided for Daniel and me. I'll use the our @kernel.org addresses, to be consistent with what's in the MAINTAINERS file.

Quoted 9 lines
Quoted 9 lines
drivers/clk/Kconfig +0 -0
index 6bfa2c8fffe1b..b27807ee93863 100644
@@ -185,6 +185,14 @@ config COMMON_CLK_CDCE706
  	help
  	  This driver supports TI CDCE706 programmable 3-PLL clock synthesizer.
 
Quoted 16 lines

drivers/clk/Kconfig config COMMON_CLK_CDCE706 · line 194

Quoted 7 lines
drivers/clk/Kconfig +7 -0
+config COMMON_CLK_TC9564
+	tristate "Toshiba TC9564 clock and reset support"
+	depends on TC9564_PCI || COMPILE_TEST
+	default TC9564_PCI
+	help
+	  This enables support for the clock and reset controllers embedded
+	  in the Toshiba TC9564 SoC.

[Severity: Medium]
Should COMMON_CLK_TC9564 select RESET_CONTROLLER? Because TC9564 is a PCIe endpoint device, this driver can be enabled on host architectures that do not select ARCH_HAS_RESET_CONTROLLER. When CONFIG_RESET_CONTROLLER is disabled, devm_reset_controller_register() compiles to a no-op stub returning 0, silently omitting the reset controller functionality and causing non-optional reset consumers to fail with -ENOTSUPP.

Yes, it should. It passed COMPILE_TEST checks, but this code sits outside drivers/reset so this should be selected.

Quoted 137 lines
Quoted 6 lines
drivers/clk/clk-tc9564.c new file +0 -0
new file mode 100644
index 0000000000000..65ed524bf2ee7

[ ... ]

Quoted 41 lines
drivers/clk/clk-tc9564.c new file +41 -0
+static const struct reset_init reset_init[] = {
+	[RESET_I2C] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(12),
+	},
+	[RESET_PCIE] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(9),
+	},
+	[RESET_MSIGEN] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(18),
+	},
+
+	[RESET_MAC0_MAC] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC0_PMA] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC0_XPCS] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(31),
+	},
+
+	[RESET_MAC1_MAC] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC1_PMA] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC1_XPCS] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(31),
+	},
+};
+#define RESET_COUNT	ARRAY_SIZE(reset_init)

[ ... ]

Quoted 73 lines
drivers/clk/clk-tc9564.c new file +73 -0
+static const struct gate_clock *hw_to_gate_clock(struct clk_hw *hw)
+{
+	return container_of_const(hw, struct gate_clock, hw);
+}
+
+static int gate_clk_manage(struct clk_hw *hw, bool enable)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
+	u32 mask = clock->init->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	val = readl(ioaddr);
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel(val, ioaddr);
+
+	return 0;
+}
+
+static int gate_clk_enable(struct clk_hw *hw)
+{
+	return gate_clk_manage(hw, true);
+}
+
+static void gate_clk_disable(struct clk_hw *hw)
+{
+	(void)gate_clk_manage(hw, false);
+}
+
+static int gate_clk_is_enabled(struct clk_hw *hw)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
+	void __iomem *ioaddr;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	return readl(ioaddr) & clock->init->mask ? 1 : 0;
+}
+
+static const struct clk_ops gate_clk_ops = {
+	.enable = gate_clk_enable,
+	.disable = gate_clk_disable,
+	.is_enabled = gate_clk_is_enabled,
+};
+
+static struct clk_hw *gate_register(struct device *dev, const char *name,
+				    const struct clock_init *init)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(dev);
+	struct clk_init_data clk_init = { };
+	const struct clk_hw *parent_hw;
+	struct gate_clock *clock;
+	int ret;
+
+	clock = devm_kzalloc_obj(dev, *clock);
+	if (!clock)
+		return ERR_PTR(-ENOMEM);
+
+	parent_hw = tc9564_clock->clk_data->hws[init->parent];
+
+	clk_init.name = name;
+	clk_init.ops = &gate_clk_ops;
+	clk_init.parent_hws = &parent_hw;
+	clk_init.num_parents = 1;
+	if (init->type == CRITICAL_GATE)
+		clk_init.flags = CLK_IS_CRITICAL;

drivers/clk/clk-tc9564.c line 347

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+
+	clock->hw.init = &clk_init;
+	clock->init = init;
+
+	ret = devm_clk_hw_register(dev, &clock->hw);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return &clock->hw;
+}
Quoted 7 lines

[Severity: Low]
This isn't a bug, but now that the driver accesses MMIO registers directly rather than through regmap, gate_register() and gate_clk_ops duplicate the standard single-bit MMIO gate implementation. Using devm_clk_hw_register_gate_parent_hw() (as pll_register() and fixed_register() do with the fixed-factor helpers) would allow dropping struct gate_clock and the custom gate callbacks.

This is a very good point. Previously I avoided the standard gate clocks *because* we were using regmap. I will review this but I expect to be able to use the existing code instead.

Quoted 27 lines
Quoted 27 lines
drivers/clk/clk-tc9564.c new file +26 -0
+
+/* Warn on requests to assert or deassert critical resets */
+static bool reset_is_critical(unsigned long id)
+{
+	return WARN_ON(id == RESET_I2C) || WARN_ON(id == RESET_PCIE);
+}
+
+static int reset_manage(struct reset_controller_dev *rcdev,
+			unsigned long id, bool assert)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(rcdev->dev);
+	const struct reset_init *reset = &reset_init[id];
+	u32 mask = reset->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	if (reset_is_critical(id))
+		return 0;		/* Just ignore the request */
+
+	ioaddr = tc9564_clock->ioaddr + reset->offset;
+
+	val = readl(ioaddr);
+	if (assert)
+		val |= mask;
+	else
+		val &= ~mask;
Quoted 8 lines

drivers/clk/clk-tc9564.c line 288

Quoted 4 lines
drivers/clk/clk-tc9564.c new file +4 -0
+	writel(val, ioaddr);
+
+	return 0;
+}

[Severity: Medium]
Can concurrent reset operations on lines sharing RESET0_OFFSET or RESET1_OFFSET race in reset_manage()?

I hadn't considered that, but yes, we could be exercising this code concurrently, so a spin lock should be used.

Great review feedback!

-Alex

Quoted 10 lines

Unlike the clock framework (which holds enable_lock across enable and disable callbacks), the reset controller core only acquires a per-reset-control SRCU read lock in reset_control_assert() and reset_control_deassert(). Two consumers asserting or deasserting different resets in the same register (such as RESET_MSIGEN and RESET_MAC0_MAC in RESET0_OFFSET) can perform concurrent read-modify-write sequences and overwrite each other's bit changes. A spinlock in struct tc9564_clock around the readl()/writel() sequence in reset_manage() would prevent this.

#6 lore raw

Re: [PATCH v2 2/2] clk: toshiba: introduce a TC9564 clock/reset driver

In reply to Alex Elder
Quoted 4 lines

Define a new platform driver that manages clocks implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but currently only the clocks that are (or will soon be) used are defined to be controllable.

Is it a repetition of the sentences below?

Quoted 5 lines

Two registers manage the state of the clocks. The registers are accessed via a regmap supplied by a system controller. Access to the memory region is provided via a BAR on a PCIe endpoint function embedded in the TC9564 SoC.

I guess this is a leftover, right?

Quoted 99 lines

Define the binding for the clock and reset controller that present in the Toshiba TC9564 SoC.

Define a new platform driver that manages clocks and resets implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but some of these won't be used any time soon by software, so they are omitted.

There are also 13 resets that can be asserted or deasserted, but support for some of these is omitted as well.

The I2C and PCIE clocks and resets are treated as CRITICAL, meaning their state should never be changed (and a warning is issued if changing their state is attempted).

---
v2: - New driver that combines clock and reset functionality
- Map the platform memory resource rather than using a regmap
- Kconfig adds COMPILE_TEST, and its help text is simplified
- Clock names now defined based on dev_name()
- gate_clk_manage() and reset_manage() return 0, so their callers can be made a little simpler
- Flex array counted-by field is now explicitly assigned

 MAINTAINERS              |   1 +
 drivers/clk/Kconfig      |   8 +
 drivers/clk/Makefile     |   1 +
 drivers/clk/clk-tc9564.c | 501 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 511 insertions(+)
 create mode 100644 drivers/clk/clk-tc9564.c
MAINTAINERS +1 -0
index af088e1092e63..9503c304c35ed 100644
@@ -28689,6 +28689,7 @@ M:	Alex Elder <[email protected]>
 M:	Daniel Thompson <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
+F:	drivers/clk/clk-tc9564.c
 F:	include/dt-bindings/clock/toshiba,tc9564.h
 
 TOSHIBA TC9564 PCI DRIVER
drivers/clk/Kconfig +8 -0
index 6bfa2c8fffe1b..b27807ee93863 100644
@@ -185,6 +185,14 @@ config COMMON_CLK_CDCE706
 	help
 	  This driver supports TI CDCE706 programmable 3-PLL clock synthesizer.
 
+config COMMON_CLK_TC9564
+	tristate "Toshiba TC9564 clock and reset support"
+	depends on TC9564_PCI || COMPILE_TEST
+	default TC9564_PCI
+	help
+	  This enables support for the clock and reset controllers embedded
+	  in the Toshiba TC9564 SoC.
+
 config COMMON_CLK_TPS68470
 	tristate "Clock Driver for TI TPS68470 PMIC"
 	depends on I2C
drivers/clk/Makefile +1 -0
index 99377a13ebc57..d67cfb6d623d3 100644
@@ -111,6 +111,7 @@ obj-$(CONFIG_COMMON_CLK_SI570)		+= clk-si570.o
 obj-$(CONFIG_COMMON_CLK_SP7021)		+= clk-sp7021.o
 obj-$(CONFIG_COMMON_CLK_STM32F)		+= clk-stm32f4.o
 obj-$(CONFIG_COMMON_CLK_STM32H7)	+= clk-stm32h7.o
+obj-$(CONFIG_COMMON_CLK_TC9564)		+= clk-tc9564.o
 obj-$(CONFIG_COMMON_CLK_TPS68470)      += clk-tps68470.o
 obj-$(CONFIG_CLK_TWL6040)		+= clk-twl6040.o
 obj-$(CONFIG_CLK_TWL)			+= clk-twl.o
drivers/clk/clk-tc9564.c new file +15 -0
new file mode 100644
index 0000000000000..65ed524bf2ee7
@@ -0,0 +1,501 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2026 by RISCstar Solutions Corporation.  All rights reserved.
+ */
+
+#include <linux/bits.h>
+#include <linux/clk-provider.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/reset-controller.h>
+#include <linux/slab.h>
+
+#include <dt-bindings/clock/toshiba,tc9564.h>

drivers/clk/clk-tc9564.c line 25

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+
+/*
+ * Two registers implement clock enable/disable bits for all gated clocks.
+ * And two registers implement reset assertion bits for all resets.
+ * These offsets are relative to the address space defined in devicetree.
+ */
+#define CLOCK0_OFFSET	0x0
+#define CLOCK1_OFFSET	0x8
+#define RESET0_OFFSET	0x4
+#define RESET1_OFFSET	0xc

nit: I guess you can just put these values in clock_init/reset_init structs.

Quoted 34 lines
drivers/clk/clk-tc9564.c new file +34 -0
+
+struct tc9564_clock {
+	void __iomem *ioaddr;		/* Mapped clock/reset registers */
+	struct clk_hw_onecell_data *clk_data;
+	struct reset_controller_dev rcdev;
+};
+
+struct gate_clock {
+	const struct clock_init *init;
+	struct clk_hw hw;
+};
+
+enum clock_type { PLL, FIXED, GATE, CRITICAL_GATE };
+
+struct clock_init {
+	const char *name;
+	enum clock_type type;
+	union {
+		u32 mult;	/* PLL rate multiplier */
+
+		struct {	/* Fixed rate clock; parent is PLL */
+			u32 numerator;	/* 0 is treated as 1 */
+			u32 denominator;
+		};
+
+		struct {	/* Gated clock; parent is a fixed rate clock */
+			u16 parent;	/* Parent clock ID, e.g. CLOCK_100M */
+			u16 offset;	/* Register offset */
+			u32 mask;	/* Value is 2^n (only one bit set) */
+		};
+	};
+};
+
+struct reset_init {

drivers/clk/clk-tc9564.c line 69

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+	u32 offset;	/* Register offset */
+	u32 mask;	/* Value is 2^n (only one bit set) */
+};
+
+/*
+ * Clocks are initialized and registered in the order of their IDs.
+ * There is one PLL, and it is initialized first.  Fixed clocks are
+ * initialized after that, and the gated clocks are initialized last
+ * (after their parent clocks have been registered).
+ */

please see the comment below.

drivers/clk/clk-tc9564.c line 75

Quoted 6 lines
drivers/clk/clk-tc9564.c new file +6 -0
+static const struct clock_init clock_init[] = {
+	[CLOCK_PLL] = {
+		.name = "pll",
+		.type = PLL,
+		.mult = 50,
+	},

unnecessary new-line

Quoted 127 lines
drivers/clk/clk-tc9564.c new file +127 -0
+
+	[CLOCK_62_5M] = {
+		.name = "62.5MHz",
+		.type = FIXED,
+		.denominator = 20,
+	},
+	[CLOCK_100M] = {
+		.name = "100MHz",
+		.type = FIXED,
+		.numerator = 2,
+		.denominator = 25,
+	},
+	[CLOCK_125M] = {
+		.name = "125MHz",
+		.type = FIXED,
+		.denominator = 10,
+	},
+	[CLOCK_250M] = {
+		.name = "250MHz",
+		.type = FIXED,
+		.denominator = 5,
+	},
+	[CLOCK_312_5M] = {
+		.name = "312.5MHz",
+		.type = FIXED,
+		.denominator = 4,
+	},
+
+	[CLOCK_I2C] = {
+		.name = "i2c",
+		.type = CRITICAL_GATE,
+		.parent = CLOCK_100M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(12),
+	},
+	[CLOCK_PCIE] = {
+		.name = "PCIe",
+		.type = CRITICAL_GATE,
+		.parent = CLOCK_100M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(9),
+	},
+	[CLOCK_MSIGEN] = {
+		.name = "msigen",
+		.type = GATE,
+		.parent = CLOCK_62_5M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(18),
+	},
+
+	[CLOCK_MAC0_TX] = {
+		.name = "MAC0-TX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(7),
+	},
+	[CLOCK_MAC0_RX] = {
+		.name = "MAC0-RX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(14),
+	},
+	[CLOCK_MAC0_125M] = {
+		.name = "MAC0-125MHz",
+		.type = GATE,
+		.parent = CLOCK_125M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(29),
+	},
+	[CLOCK_MAC0_312_5M] = {
+		.name = "MAC0-312.5MHz",
+		.type = GATE,
+		.parent = CLOCK_312_5M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(30),
+	},
+	[CLOCK_MAC0_ALL] = {
+		.name = "MAC0-ALL",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(31),
+	},
+
+	[CLOCK_MAC1_TX] = {
+		.name = "MAC1-TX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(7),
+	},
+	[CLOCK_MAC1_RX] = {
+		.name = "MAC1-RX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(14),
+	},
+	[CLOCK_MAC1_125M] = {
+		.name = "MAC1-125MHz",
+		.type = GATE,
+		.parent = CLOCK_125M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(29),
+	},
+	[CLOCK_MAC1_312_5M] = {
+		.name = "MAC1-312.5MHz",
+		.type = GATE,
+		.parent = CLOCK_312_5M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(30),
+	},
+	[CLOCK_MAC1_ALL] = {
+		.name = "MAC1-ALL",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(31),
+	},
+};
+#define CLOCK_COUNT	ARRAY_SIZE(clock_init)
+
+static const struct reset_init reset_init[] = {
+	[RESET_I2C] = {
+		.offset = RESET0_OFFSET,

drivers/clk/clk-tc9564.c line 212

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+		.mask = BIT(12),
+	},
+	[RESET_PCIE] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(9),
+	},
+	[RESET_MSIGEN] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(18),
+	},

unnecessary new-line

Quoted 86 lines
drivers/clk/clk-tc9564.c new file +86 -0
+
+	[RESET_MAC0_MAC] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC0_PMA] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC0_XPCS] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(31),
+	},
+
+	[RESET_MAC1_MAC] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC1_PMA] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC1_XPCS] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(31),
+	},
+};
+#define RESET_COUNT	ARRAY_SIZE(reset_init)
+
+/* Parent is the input oscillator running at 25 MHz; PLL runs at 1250 MHz */
+static struct clk_hw *pll_register(struct device *dev, const char *name,
+				   const struct clock_init *init)
+{
+	return devm_clk_hw_register_fixed_factor_index(dev, name, 0, 0,
+						       init->mult, 1);
+}
+
+/* The PLL is the parent of all fixed clocks */
+static struct clk_hw *fixed_register(struct device *dev, const char *name,
+				     const struct clock_init *init)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(dev);
+	u32 numerator = init->numerator ? : 1;
+	struct clk_hw *pll_hw;
+
+	pll_hw = tc9564_clock->clk_data->hws[CLOCK_PLL];
+
+	return devm_clk_hw_register_fixed_factor_parent_hw(dev, name, pll_hw,
+							   0, numerator,
+							   init->denominator);
+}
+
+static const struct gate_clock *hw_to_gate_clock(struct clk_hw *hw)
+{
+	return container_of_const(hw, struct gate_clock, hw);
+}
+
+static int gate_clk_manage(struct clk_hw *hw, bool enable)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
+	u32 mask = clock->init->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	val = readl(ioaddr);
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel(val, ioaddr);
+
+	return 0;
+}
+
+static int gate_clk_enable(struct clk_hw *hw)
+{
+	return gate_clk_manage(hw, true);
+}
+
+static void gate_clk_disable(struct clk_hw *hw)
+{
+	(void)gate_clk_manage(hw, false);
+}

drivers/clk/clk-tc9564.c line 308

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+
+static int gate_clk_is_enabled(struct clk_hw *hw)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
+	void __iomem *ioaddr;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	return readl(ioaddr) & clock->init->mask ? 1 : 0;

This is racy with gate_clk_manage() but it is not harmful as for reset (please see below) but I guess regmap can be helpful for clock as well.

Quoted 49 lines
drivers/clk/clk-tc9564.c new file +49 -0
+}
+
+static const struct clk_ops gate_clk_ops = {
+	.enable = gate_clk_enable,
+	.disable = gate_clk_disable,
+	.is_enabled = gate_clk_is_enabled,
+};
+
+static struct clk_hw *gate_register(struct device *dev, const char *name,
+				    const struct clock_init *init)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(dev);
+	struct clk_init_data clk_init = { };
+	const struct clk_hw *parent_hw;
+	struct gate_clock *clock;
+	int ret;
+
+	clock = devm_kzalloc_obj(dev, *clock);
+	if (!clock)
+		return ERR_PTR(-ENOMEM);
+
+	parent_hw = tc9564_clock->clk_data->hws[init->parent];
+
+	clk_init.name = name;
+	clk_init.ops = &gate_clk_ops;
+	clk_init.parent_hws = &parent_hw;
+	clk_init.num_parents = 1;
+	if (init->type == CRITICAL_GATE)
+		clk_init.flags = CLK_IS_CRITICAL;
+
+	clock->hw.init = &clk_init;
+	clock->init = init;
+
+	ret = devm_clk_hw_register(dev, &clock->hw);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return &clock->hw;
+}
+
+/* Warn on requests to assert or deassert critical resets */
+static bool reset_is_critical(unsigned long id)
+{
+	return WARN_ON(id == RESET_I2C) || WARN_ON(id == RESET_PCIE);
+}
+
+static int reset_manage(struct reset_controller_dev *rcdev,
+			unsigned long id, bool assert)
+{

drivers/clk/clk-tc9564.c line 367

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(rcdev->dev);
+	const struct reset_init *reset = &reset_init[id];
+	u32 mask = reset->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	if (reset_is_critical(id))
+		return 0;		/* Just ignore the request */
+
+	ioaddr = tc9564_clock->ioaddr + reset->offset;

AFAIK reset framework does not provide any serialization for reset_control_ops. Since multiple reset lines share the same register, reset_manage() is racy and multiple calls to reset_control_assert() can write stale values. I think you should add a spin_lock here or rely on regmap.

Quoted 68 lines
drivers/clk/clk-tc9564.c new file +68 -0
+
+	val = readl(ioaddr);
+	if (assert)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel(val, ioaddr);
+
+	return 0;
+}
+
+static int reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	return reset_manage(rcdev, id, true);
+}
+
+static int reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	return reset_manage(rcdev, id, false);
+}
+
+static int reset_status(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(rcdev->dev);
+	const struct reset_init *reset = &reset_init[id];
+	void __iomem *ioaddr;
+
+	ioaddr = tc9564_clock->ioaddr + reset->offset;
+
+	return readl(ioaddr) & reset->mask ? 1 : 0;
+}
+
+static const struct reset_control_ops reset_control_ops = {
+	.assert = reset_assert,
+	.deassert = reset_deassert,
+	.status = reset_status,
+};
+
+static int tc9564_clk_probe(struct platform_device *pdev)
+{
+	struct clk_hw_onecell_data *clk_data;
+	struct reset_controller_dev *rcdev;
+	struct tc9564_clock *tc9564_clock;
+	struct device *dev = &pdev->dev;
+	struct device_node *np;
+	int ret;
+
+	np = dev_of_node(dev);
+	if (!np)
+		return dev_err_probe(dev, -EINVAL, "no devicetree node\n");
+
+	tc9564_clock = devm_kzalloc_obj(dev, *tc9564_clock);
+	if (!tc9564_clock)
+		return -ENOMEM;
+
+	tc9564_clock->ioaddr = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tc9564_clock->ioaddr))
+		return dev_err_probe(dev, PTR_ERR(tc9564_clock->ioaddr),
+				     "failed to map registers\n");
+
+	clk_data = devm_kzalloc_flex(dev, *tc9564_clock->clk_data, hws,
+				     CLOCK_COUNT);
+	if (!clk_data)
+		return -ENOMEM;
+	clk_data->num = CLOCK_COUNT;
+
+	tc9564_clock->clk_data = clk_data;
+

drivers/clk/clk-tc9564.c line 445

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+	/* Record the driver data pointer before we register anything */
+	dev_set_drvdata(dev, tc9564_clock);
+
+	/* Initialize all the clocks, then register the provider */
+	for (u32 i = 0; i < clk_data->num; i++) {
+		const struct clock_init *init = &clock_init[i];
+		struct clk_hw *hw;
+		char *name;
+
+		name = kasprintf(GFP_KERNEL, "%s-%s", dev_name(dev), init->name);

nit: I guess you can use devm_kasprintf() here and get rid of the kfree().

drivers/clk/clk-tc9564.c line 449

Quoted 4 lines
drivers/clk/clk-tc9564.c new file +4 -0
+		if (!name)
+			return -ENOMEM;
+
+		if (init->type == PLL)

nit: I would use a switch here.

drivers/clk/clk-tc9564.c line 454

Quoted 5 lines
drivers/clk/clk-tc9564.c new file +5 -0
+			hw = pll_register(dev, name, init);
+		else if (init->type == FIXED)
+			hw = fixed_register(dev, name, init);
+		else
+			hw = gate_register(dev, name, init);

I think this approach is a bit fragile since the clock dependency is guaranteed by the order defined in include/dt-bindings/clock/toshiba,tc9564.h, right?

PLL clock --> fixed clocks --> gate_clocks

Would it better to enforce it in the code allocating first the PLL out of the loop, then fixed rate clocks with a specific routine and, at the end, gate clocks?

Regards,
Lorenzo

Quoted 50 lines
drivers/clk/clk-tc9564.c new file +47 -0
+		kfree(name);
+
+		if (IS_ERR(hw))
+			return dev_err_probe(dev, PTR_ERR(hw),
+					     "failed to register %s\n",
+					     init->name);
+		clk_data->hws[i] = hw;
+	}
+
+	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add clock provider\n");
+
+	/* Set up and register the reset controller */
+	rcdev = &tc9564_clock->rcdev;
+	rcdev->ops = &reset_control_ops;
+	rcdev->owner = THIS_MODULE;
+	rcdev->dev = dev;
+	rcdev->of_node = np;
+	rcdev->nr_resets = RESET_COUNT;
+
+	ret = devm_reset_controller_register(dev, rcdev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "failed to register reset controller\n");
+
+	return 0;
+}
+
+static const struct of_device_id tc9564_clock_match[] = {
+	{ .compatible = "toshiba,tc9564-clock", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tc9564_clock_match);
+
+static struct platform_driver tc9564_clk_driver = {
+	.probe = tc9564_clk_probe,
+	.driver = {
+		.name = KBUILD_MODNAME,
+		.of_match_table = tc9564_clock_match,
+		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+	},
+};
+module_platform_driver(tc9564_clk_driver);
+
+MODULE_DESCRIPTION("Toshiba TC9564 Clock Driver");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.53.0
author
#7 lore raw

Re: [PATCH v2 2/2] clk: toshiba: introduce a TC9564 clock/reset driver

Quoted 6 lines · Lorenzo Bianconi
Quoted 4 lines

Define a new platform driver that manages clocks implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but currently only the clocks that are (or will soon be) used are defined to be controllable.

Is it a repetition of the sentences below?

Yes. It seems I started editing this and never finished. Sashiko also pointed out there was a problem here.

Quoted 6 lines
Quoted 4 lines

Two registers manage the state of the clocks. The registers are accessed via a regmap supplied by a system controller. Access to the memory region is provided via a BAR on a PCIe endpoint function embedded in the TC9564 SoC.

I guess this is a leftover, right?

Yes.

Quoted 104 lines
Quoted 104 lines

Define the binding for the clock and reset controller that present in the Toshiba TC9564 SoC.

Define a new platform driver that manages clocks and resets implemented by the Toshiba TC9564 SoC. There are 21 gate clocks that can be enabled and disabled, but some of these won't be used any time soon by software, so they are omitted.

There are also 13 resets that can be asserted or deasserted, but support for some of these is omitted as well.

The I2C and PCIE clocks and resets are treated as CRITICAL, meaning their state should never be changed (and a warning is issued if changing their state is attempted).

---
v2: - New driver that combines clock and reset functionality
- Map the platform memory resource rather than using a regmap
- Kconfig adds COMPILE_TEST, and its help text is simplified
- Clock names now defined based on dev_name()
- gate_clk_manage() and reset_manage() return 0, so their callers can be made a little simpler
- Flex array counted-by field is now explicitly assigned

  MAINTAINERS              |   1 +
  drivers/clk/Kconfig      |   8 +
  drivers/clk/Makefile     |   1 +
  drivers/clk/clk-tc9564.c | 501 +++++++++++++++++++++++++++++++++++++++
  4 files changed, 511 insertions(+)

create mode 100644 drivers/clk/clk-tc9564.c

MAINTAINERS +1 -0
index af088e1092e63..9503c304c35ed 100644
@@ -28689,6 +28689,7 @@ M:	Alex Elder <[email protected]>
  M:	Daniel Thompson <[email protected]>
  S:	Maintained
  F:	Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml
+F:	drivers/clk/clk-tc9564.c
  F:	include/dt-bindings/clock/toshiba,tc9564.h
 
  TOSHIBA TC9564 PCI DRIVER
drivers/clk/Kconfig +8 -0
index 6bfa2c8fffe1b..b27807ee93863 100644
@@ -185,6 +185,14 @@ config COMMON_CLK_CDCE706
  	help
  	  This driver supports TI CDCE706 programmable 3-PLL clock synthesizer.
 
+config COMMON_CLK_TC9564
+	tristate "Toshiba TC9564 clock and reset support"
+	depends on TC9564_PCI || COMPILE_TEST
+	default TC9564_PCI
+	help
+	  This enables support for the clock and reset controllers embedded
+	  in the Toshiba TC9564 SoC.
+
  config COMMON_CLK_TPS68470
  	tristate "Clock Driver for TI TPS68470 PMIC"
  	depends on I2C
drivers/clk/Makefile +1 -0
index 99377a13ebc57..d67cfb6d623d3 100644
@@ -111,6 +111,7 @@ obj-$(CONFIG_COMMON_CLK_SI570)		+= clk-si570.o
  obj-$(CONFIG_COMMON_CLK_SP7021)		+= clk-sp7021.o
  obj-$(CONFIG_COMMON_CLK_STM32F)		+= clk-stm32f4.o
  obj-$(CONFIG_COMMON_CLK_STM32H7)	+= clk-stm32h7.o
+obj-$(CONFIG_COMMON_CLK_TC9564)		+= clk-tc9564.o
  obj-$(CONFIG_COMMON_CLK_TPS68470)      += clk-tps68470.o
  obj-$(CONFIG_CLK_TWL6040)		+= clk-twl6040.o
  obj-$(CONFIG_CLK_TWL)			+= clk-twl.o
drivers/clk/clk-tc9564.c new file +19 -0
new file mode 100644
index 0000000000000..65ed524bf2ee7
@@ -0,0 +1,501 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2026 by RISCstar Solutions Corporation.  All rights reserved.
+ */
+
+#include <linux/bits.h>
+#include <linux/clk-provider.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/reset-controller.h>
+#include <linux/slab.h>
+
+#include <dt-bindings/clock/toshiba,tc9564.h>
+
+/*
+ * Two registers implement clock enable/disable bits for all gated clocks.
+ * And two registers implement reset assertion bits for all resets.
Quoted 8 lines

drivers/clk/clk-tc9564.c line 25

Quoted 6 lines
drivers/clk/clk-tc9564.c new file +6 -0
+ * These offsets are relative to the address space defined in devicetree.
+ */
+#define CLOCK0_OFFSET	0x0
+#define CLOCK1_OFFSET	0x8
+#define RESET0_OFFSET	0x4
+#define RESET1_OFFSET	0xc

nit: I guess you can just put these values in clock_init/reset_init structs.

You mean just put the "raw numbers" there? I actually started with that but thought it was clearer to use the symbolic names defined above. My plan is to keep what I have on this.

Quoted 48 lines
Quoted 35 lines
drivers/clk/clk-tc9564.c new file +34 -0
+
+struct tc9564_clock {
+	void __iomem *ioaddr;		/* Mapped clock/reset registers */
+	struct clk_hw_onecell_data *clk_data;
+	struct reset_controller_dev rcdev;
+};
+
+struct gate_clock {
+	const struct clock_init *init;
+	struct clk_hw hw;
+};
+
+enum clock_type { PLL, FIXED, GATE, CRITICAL_GATE };
+
+struct clock_init {
+	const char *name;
+	enum clock_type type;
+	union {
+		u32 mult;	/* PLL rate multiplier */
+
+		struct {	/* Fixed rate clock; parent is PLL */
+			u32 numerator;	/* 0 is treated as 1 */
+			u32 denominator;
+		};
+
+		struct {	/* Gated clock; parent is a fixed rate clock */
+			u16 parent;	/* Parent clock ID, e.g. CLOCK_100M */
+			u16 offset;	/* Register offset */
+			u32 mask;	/* Value is 2^n (only one bit set) */
+		};
+	};
+};
+
+struct reset_init {

drivers/clk/clk-tc9564.c line 69

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+	u32 offset;	/* Register offset */
+	u32 mask;	/* Value is 2^n (only one bit set) */
+};
+
+/*
+ * Clocks are initialized and registered in the order of their IDs.
+ * There is one PLL, and it is initialized first.  Fixed clocks are
+ * initialized after that, and the gated clocks are initialized last
+ * (after their parent clocks have been registered).
+ */

please see the comment below.

Quoted 8 lines

drivers/clk/clk-tc9564.c line 75

Quoted 6 lines
drivers/clk/clk-tc9564.c new file +6 -0
+static const struct clock_init clock_init[] = {
+	[CLOCK_PLL] = {
+		.name = "pll",
+		.type = PLL,
+		.mult = 50,
+	},

unnecessary new-line

The newlines separated the three clock types in the array.

But they might go away anyway after I reconsider grouping them differently (based on your suggestion).

Quoted 232 lines
Quoted 128 lines
drivers/clk/clk-tc9564.c new file +127 -0
+
+	[CLOCK_62_5M] = {
+		.name = "62.5MHz",
+		.type = FIXED,
+		.denominator = 20,
+	},
+	[CLOCK_100M] = {
+		.name = "100MHz",
+		.type = FIXED,
+		.numerator = 2,
+		.denominator = 25,
+	},
+	[CLOCK_125M] = {
+		.name = "125MHz",
+		.type = FIXED,
+		.denominator = 10,
+	},
+	[CLOCK_250M] = {
+		.name = "250MHz",
+		.type = FIXED,
+		.denominator = 5,
+	},
+	[CLOCK_312_5M] = {
+		.name = "312.5MHz",
+		.type = FIXED,
+		.denominator = 4,
+	},
+
+	[CLOCK_I2C] = {
+		.name = "i2c",
+		.type = CRITICAL_GATE,
+		.parent = CLOCK_100M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(12),
+	},
+	[CLOCK_PCIE] = {
+		.name = "PCIe",
+		.type = CRITICAL_GATE,
+		.parent = CLOCK_100M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(9),
+	},
+	[CLOCK_MSIGEN] = {
+		.name = "msigen",
+		.type = GATE,
+		.parent = CLOCK_62_5M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(18),
+	},
+
+	[CLOCK_MAC0_TX] = {
+		.name = "MAC0-TX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(7),
+	},
+	[CLOCK_MAC0_RX] = {
+		.name = "MAC0-RX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(14),
+	},
+	[CLOCK_MAC0_125M] = {
+		.name = "MAC0-125MHz",
+		.type = GATE,
+		.parent = CLOCK_125M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(29),
+	},
+	[CLOCK_MAC0_312_5M] = {
+		.name = "MAC0-312.5MHz",
+		.type = GATE,
+		.parent = CLOCK_312_5M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(30),
+	},
+	[CLOCK_MAC0_ALL] = {
+		.name = "MAC0-ALL",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK0_OFFSET,
+		.mask = BIT(31),
+	},
+
+	[CLOCK_MAC1_TX] = {
+		.name = "MAC1-TX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(7),
+	},
+	[CLOCK_MAC1_RX] = {
+		.name = "MAC1-RX",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(14),
+	},
+	[CLOCK_MAC1_125M] = {
+		.name = "MAC1-125MHz",
+		.type = GATE,
+		.parent = CLOCK_125M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(29),
+	},
+	[CLOCK_MAC1_312_5M] = {
+		.name = "MAC1-312.5MHz",
+		.type = GATE,
+		.parent = CLOCK_312_5M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(30),
+	},
+	[CLOCK_MAC1_ALL] = {
+		.name = "MAC1-ALL",
+		.type = GATE,
+		.parent = CLOCK_250M,
+		.offset = CLOCK1_OFFSET,
+		.mask = BIT(31),
+	},
+};
+#define CLOCK_COUNT	ARRAY_SIZE(clock_init)
+
+static const struct reset_init reset_init[] = {
+	[RESET_I2C] = {
+		.offset = RESET0_OFFSET,

drivers/clk/clk-tc9564.c line 212

Quoted 10 lines
drivers/clk/clk-tc9564.c new file +10 -0
+		.mask = BIT(12),
+	},
+	[RESET_PCIE] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(9),
+	},
+	[RESET_MSIGEN] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(18),
+	},

unnecessary new-line

Quoted 91 lines
drivers/clk/clk-tc9564.c new file +91 -0
+
+	[RESET_MAC0_MAC] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC0_PMA] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC0_XPCS] = {
+		.offset = RESET0_OFFSET,
+		.mask = BIT(31),
+	},
+
+	[RESET_MAC1_MAC] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(7),
+	},
+	[RESET_MAC1_PMA] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(30),
+	},
+	[RESET_MAC1_XPCS] = {
+		.offset = RESET1_OFFSET,
+		.mask = BIT(31),
+	},
+};
+#define RESET_COUNT	ARRAY_SIZE(reset_init)
+
+/* Parent is the input oscillator running at 25 MHz; PLL runs at 1250 MHz */
+static struct clk_hw *pll_register(struct device *dev, const char *name,
+				   const struct clock_init *init)
+{
+	return devm_clk_hw_register_fixed_factor_index(dev, name, 0, 0,
+						       init->mult, 1);
+}
+
+/* The PLL is the parent of all fixed clocks */
+static struct clk_hw *fixed_register(struct device *dev, const char *name,
+				     const struct clock_init *init)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(dev);
+	u32 numerator = init->numerator ? : 1;
+	struct clk_hw *pll_hw;
+
+	pll_hw = tc9564_clock->clk_data->hws[CLOCK_PLL];
+
+	return devm_clk_hw_register_fixed_factor_parent_hw(dev, name, pll_hw,
+							   0, numerator,
+							   init->denominator);
+}
+
+static const struct gate_clock *hw_to_gate_clock(struct clk_hw *hw)
+{
+	return container_of_const(hw, struct gate_clock, hw);
+}
+
+static int gate_clk_manage(struct clk_hw *hw, bool enable)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
+	u32 mask = clock->init->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	val = readl(ioaddr);
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel(val, ioaddr);
+
+	return 0;
+}
+
+static int gate_clk_enable(struct clk_hw *hw)
+{
+	return gate_clk_manage(hw, true);
+}
+
+static void gate_clk_disable(struct clk_hw *hw)
+{
+	(void)gate_clk_manage(hw, false);
+}
+
+static int gate_clk_is_enabled(struct clk_hw *hw)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(clk_hw_get_dev(hw));
+	const struct gate_clock *clock = hw_to_gate_clock(hw);
Quoted 8 lines

drivers/clk/clk-tc9564.c line 308

Quoted 5 lines
drivers/clk/clk-tc9564.c new file +5 -0
+	void __iomem *ioaddr;
+
+	ioaddr = tc9564_clock->ioaddr + clock->init->offset;
+
+	return readl(ioaddr) & clock->init->mask ? 1 : 0;

This is racy with gate_clk_manage() but it is not harmful as for reset (please see below) but I guess regmap can be helpful for clock as well.

You're saying this should be protected in case a concurrent update is happening?

This read will return either the value before such an update or after it, but either way it will be a valid value. It is always possible the state changes immediately after the read (even with spinlock protection). I don't think there's any need for locking here.

Quoted 57 lines
Quoted 57 lines
drivers/clk/clk-tc9564.c new file +56 -0
+}
+
+static const struct clk_ops gate_clk_ops = {
+	.enable = gate_clk_enable,
+	.disable = gate_clk_disable,
+	.is_enabled = gate_clk_is_enabled,
+};
+
+static struct clk_hw *gate_register(struct device *dev, const char *name,
+				    const struct clock_init *init)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(dev);
+	struct clk_init_data clk_init = { };
+	const struct clk_hw *parent_hw;
+	struct gate_clock *clock;
+	int ret;
+
+	clock = devm_kzalloc_obj(dev, *clock);
+	if (!clock)
+		return ERR_PTR(-ENOMEM);
+
+	parent_hw = tc9564_clock->clk_data->hws[init->parent];
+
+	clk_init.name = name;
+	clk_init.ops = &gate_clk_ops;
+	clk_init.parent_hws = &parent_hw;
+	clk_init.num_parents = 1;
+	if (init->type == CRITICAL_GATE)
+		clk_init.flags = CLK_IS_CRITICAL;
+
+	clock->hw.init = &clk_init;
+	clock->init = init;
+
+	ret = devm_clk_hw_register(dev, &clock->hw);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return &clock->hw;
+}
+
+/* Warn on requests to assert or deassert critical resets */
+static bool reset_is_critical(unsigned long id)
+{
+	return WARN_ON(id == RESET_I2C) || WARN_ON(id == RESET_PCIE);
+}
+
+static int reset_manage(struct reset_controller_dev *rcdev,
+			unsigned long id, bool assert)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(rcdev->dev);
+	const struct reset_init *reset = &reset_init[id];
+	u32 mask = reset->mask;
+	void __iomem *ioaddr;
+	u32 val;
+
+	if (reset_is_critical(id))
Quoted 8 lines

drivers/clk/clk-tc9564.c line 367

Quoted 3 lines
drivers/clk/clk-tc9564.c new file +3 -0
+		return 0;		/* Just ignore the request */
+
+	ioaddr = tc9564_clock->ioaddr + reset->offset;

AFAIK reset framework does not provide any serialization for reset_control_ops. Since multiple reset lines share the same register, reset_manage() is racy and multiple calls to reset_control_assert() can write stale values. I think you should add a spin_lock here or rely on regmap.

I think you're right. This was also mentioned by Sashiko.

Quoted 72 lines
Quoted 72 lines
drivers/clk/clk-tc9564.c new file +72 -0
+
+	val = readl(ioaddr);
+	if (assert)
+		val |= mask;
+	else
+		val &= ~mask;
+	writel(val, ioaddr);
+
+	return 0;
+}
+
+static int reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	return reset_manage(rcdev, id, true);
+}
+
+static int reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	return reset_manage(rcdev, id, false);
+}
+
+static int reset_status(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	struct tc9564_clock *tc9564_clock = dev_get_drvdata(rcdev->dev);
+	const struct reset_init *reset = &reset_init[id];
+	void __iomem *ioaddr;
+
+	ioaddr = tc9564_clock->ioaddr + reset->offset;
+
+	return readl(ioaddr) & reset->mask ? 1 : 0;
+}
+
+static const struct reset_control_ops reset_control_ops = {
+	.assert = reset_assert,
+	.deassert = reset_deassert,
+	.status = reset_status,
+};
+
+static int tc9564_clk_probe(struct platform_device *pdev)
+{
+	struct clk_hw_onecell_data *clk_data;
+	struct reset_controller_dev *rcdev;
+	struct tc9564_clock *tc9564_clock;
+	struct device *dev = &pdev->dev;
+	struct device_node *np;
+	int ret;
+
+	np = dev_of_node(dev);
+	if (!np)
+		return dev_err_probe(dev, -EINVAL, "no devicetree node\n");
+
+	tc9564_clock = devm_kzalloc_obj(dev, *tc9564_clock);
+	if (!tc9564_clock)
+		return -ENOMEM;
+
+	tc9564_clock->ioaddr = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(tc9564_clock->ioaddr))
+		return dev_err_probe(dev, PTR_ERR(tc9564_clock->ioaddr),
+				     "failed to map registers\n");
+
+	clk_data = devm_kzalloc_flex(dev, *tc9564_clock->clk_data, hws,
+				     CLOCK_COUNT);
+	if (!clk_data)
+		return -ENOMEM;
+	clk_data->num = CLOCK_COUNT;
+
+	tc9564_clock->clk_data = clk_data;
+
+	/* Record the driver data pointer before we register anything */
+	dev_set_drvdata(dev, tc9564_clock);
+
+	/* Initialize all the clocks, then register the provider */
Quoted 8 lines

drivers/clk/clk-tc9564.c line 445

Quoted 6 lines
drivers/clk/clk-tc9564.c new file +6 -0
+	for (u32 i = 0; i < clk_data->num; i++) {
+		const struct clock_init *init = &clock_init[i];
+		struct clk_hw *hw;
+		char *name;
+
+		name = kasprintf(GFP_KERNEL, "%s-%s", dev_name(dev), init->name);

nit: I guess you can use devm_kasprintf() here and get rid of the kfree().

I could, but why not free it right now rather than holding that memory until the device disappears?

Quoted 7 lines

drivers/clk/clk-tc9564.c line 449

Quoted 5 lines
drivers/clk/clk-tc9564.c new file +4 -0
+		if (!name)
+			return -ENOMEM;
+
+		if (init->type == PLL)

nit: I would use a switch here.

I considered a switch but with exactly three options I prefer this. I did add critical resets, and they're handled together with non-critical ones. I'm not sure I'll take your suggestion, but... I'll take another look.

Quoted 8 lines

drivers/clk/clk-tc9564.c line 454

Quoted 5 lines
drivers/clk/clk-tc9564.c new file +5 -0
+			hw = pll_register(dev, name, init);
+		else if (init->type == FIXED)
+			hw = fixed_register(dev, name, init);
+		else
+			hw = gate_register(dev, name, init);

I think this approach is a bit fragile since the clock dependency is guaranteed by the order defined in include/dt-bindings/clock/toshiba,tc9564.h, right?

That's right, but those numbers are not going to change.

Nevertheless, you're right and I've been slightly uneasy about relying on their numeric order. Let me look at this again. Maybe I can just have three groups of clocks (or one PLL and two other groups).

Quoted 4 lines

PLL clock --> fixed clocks --> gate_clocks

Would it better to enforce it in the code allocating first the PLL out of the loop, then fixed rate clocks with a specific routine and, at the end, gate clocks?

Thank you very much for your review Lorenzo.

-Alex

Quoted 53 lines

Regards,
Lorenzo

Quoted 50 lines
drivers/clk/clk-tc9564.c new file +47 -0
+		kfree(name);
+
+		if (IS_ERR(hw))
+			return dev_err_probe(dev, PTR_ERR(hw),
+					     "failed to register %s\n",
+					     init->name);
+		clk_data->hws[i] = hw;
+	}
+
+	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add clock provider\n");
+
+	/* Set up and register the reset controller */
+	rcdev = &tc9564_clock->rcdev;
+	rcdev->ops = &reset_control_ops;
+	rcdev->owner = THIS_MODULE;
+	rcdev->dev = dev;
+	rcdev->of_node = np;
+	rcdev->nr_resets = RESET_COUNT;
+
+	ret = devm_reset_controller_register(dev, rcdev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "failed to register reset controller\n");
+
+	return 0;
+}
+
+static const struct of_device_id tc9564_clock_match[] = {
+	{ .compatible = "toshiba,tc9564-clock", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tc9564_clock_match);
+
+static struct platform_driver tc9564_clk_driver = {
+	.probe = tc9564_clk_probe,
+	.driver = {
+		.name = KBUILD_MODNAME,
+		.of_match_table = tc9564_clock_match,
+		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+	},
+};
+module_platform_driver(tc9564_clk_driver);
+
+MODULE_DESCRIPTION("Toshiba TC9564 Clock Driver");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.53.0
author
#8 lore raw
In reply to Alex Elder
Quoted 14 lines · Alex Elder

This code has gone around and around in various forms, having been included most recently in a series that also added a system controller driver. That system controller has now been removed.

This series introduces a devicetree binding and single driver to support the clock and reset components present within the Toshiba TC9564 chip.

The Toshiba TC9564 is an SoC that attaches to a host system via PCIe. It includes a PCIe switch with three downstream ports. To one of these ports an embedded PCIe endpoint is attached, and that implements two PCI functions. BAR 4 for both PCI functions provides access to registers controlling blocks within the SoC, including the clock and resets controllers supported by this series.

Quoted 5 lines

To avoid any confusion I'm only going to refer back to the "syscon" series", which provides some additional information including links to earlier versions of this code.

-Alex

If anyone would like to see the DTS file that uses this clock and reset functionality, it's available here:

https://github.com/riscstar/linux/commit/de0515ee2967d8ff7753cc179a8e05283ee62829

-Alex

Quoted 42 lines

The previous version of this code was included in this series:

   https://lore.kernel.org/all/[email protected]/

Between that series (v1) and this one (v2):
- There is no more syscon, so its devicetree binding and driver have been dropped
- The clock and reset components now have a single devicetree binding
- We no longer use a syscon-generated regmap for accessing the common range of memory used by multiple drivers; specific sub-ranges are instead mapped
- The clock and reset drivers have been combined into a single driver located under drivers/clk
- The config option for the driver includes COMPILE_TEST, as requested (last month) by Jerome Brunet
- The help text for the config option is simplified, first suggested by Philipp Zabel
- The driver now names clocks using dev_name() rather than trying to hand-code a unique name into a fixed-size name array (also suggested by Jerome)
- Certain functions now always return 0, which simplifies callers so they can simply return what the called function returns
- As suggested by sashiko, a counted-by field in a flex array is now assigned explicitly

Alex Elder (2):
dt-bindings: toshiba: introduce toshiba,tc9564-clock.yaml
clk: toshiba: introduce a TC9564 clock/reset driver

  .../bindings/clock/toshiba,tc9564-clock.yaml  |  73 +++
  MAINTAINERS                                   |   9 +
  drivers/clk/Kconfig                           |   8 +
  drivers/clk/Makefile                          |   1 +
  drivers/clk/clk-tc9564.c                      | 501 ++++++++++++++++++
  include/dt-bindings/clock/toshiba,tc9564.h    |  50 ++
  6 files changed, 642 insertions(+)

create mode 100644 Documentation/devicetree/bindings/clock/toshiba,tc9564-clock.yaml create mode 100644 drivers/clk/clk-tc9564.c
create mode 100644 include/dt-bindings/clock/toshiba,tc9564.h

base-commit: 909ef7a9a30fa139e69e555e3606680f5cca7726