Skip to contentSkip to conversation

Add Cortina Systems CS7542 MDIO controller support

PATCH In review Fil Dunsky 4 participants 8 messages Dec 4 – Dec 5

Subsystems (4)
lore
Share & tools
lore

Fil Dunsky

author
#0 lore raw

v5 cover Add Cortina Systems CS7542 MDIO controller support

Add the CS7542 Clause 22 MDIO controller with bounded, sleepable polling. The driver is independent of the pending platform series and COMPILE_TEST. It retains the firmware MDC prescaler while enabling software-initiated transactions.

Since v4, the driver acquires and enables the APB input required by the binding before the first register access. The managed clock remains enabled until the managed MDIO bus is unregistered. Clock lookup or enable failures, including probe deferral, return before touching registers. No clock-rate or MDC-divisor programming is added.

The APB source was established previously on Almond+ by changing APB from 150 to 100 MHz while AXI stayed at 150 MHz and comparing MDIO transaction timing. The binding describes that source and allows clock-frequency from mdio.yaml; the exact prescaler divisor encoding remains unestablished.

Validation for v4:
- The actual probe passes 10 allocation, map, clock, registration and cleanup-order cases with GCC/Clang ASan/UBSan. The pre-fix probe reproduces an MMIO access without an enabled clock under the same helpers.
- Native ARM driver=y object and exact Linux 6.18 driver=m module compile with W=1 and KCFLAGS=-Werror; existing configurations are unchanged.
- A read-only consumer probe on the running Almond+ obtained and enabled cs7542-apb at 150 MHz. MDIO CFG stayed 00200001; all references were dropped, the same OpenWrt boot continued and its watchdog/settings were preserved. This exercises the consumer/provider path, not a full live bus reprobe.

Earlier v2/v3 full independent builds, binding checks and RAM PHY read/write results remain recorded. The transaction logic and firmware prescaler are unchanged here; the old hardware matrix was not repeated.

Changes in v5:
- Rebase onto current net-next after the HiSilicon FEMAC MDIO driver
was removed (Willem de Bruijn).
- Pick up Rob Herring's Reviewed-by on the unchanged binding.
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Enable the required APB input before accessing MDIO registers and keep it enabled through bus teardown.
- Clarify MANUAL transaction selection and preservation of the firmware
MDC prescaler.
- Link to v3: https://lore.kernel.org/all/[email protected]/

Changes in v3:
- Use sleepable polling in the MDIO bus callbacks.
- Describe the experimentally established APB input and allow the inherited optional clock-frequency property.
- Remove the unused private mii_bus pointer.
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Drop pending-frame state after Andrew Lunn's review.
- Sort Kconfig/Makefile entries and retain Andrew's Reviewed-by on the MAINTAINERS patch only.
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
Fil Dunsky (3):
dt-bindings: net: Add Cortina CS7542 MDIO controller
net: mdio: Add Cortina CS7542 bus controller
MAINTAINERS: Add Cortina CS7542 MDIO controller entry

 .../bindings/net/cortina,cs7542-mdio.yaml          |  52 +++++++++
 MAINTAINERS                                        |   7 ++
 drivers/net/mdio/Kconfig                           |  12 ++
 drivers/net/mdio/Makefile                          |   1 +
 drivers/net/mdio/mdio-cs75xx.c                     | 126 +++++++++++++++++++++
 5 files changed, 198 insertions(+)

---
base-commit: aea6deaf6b3c8a204242a4ca1fdb26e8e74de5fc
change-id: 20261123-b4-cs7542-mdio-ec44e757e824

Best regards,

Signature · 1 line
Fil Dunsky <[email protected]>
author
#1 lore raw

PATCH v5 1/3 dt-bindings: net: Add Cortina CS7542 MDIO controller

Describe the Clause 22 controller, its MDC prescaler and the devices on its management bus.

---
 .../bindings/net/cortina,cs7542-mdio.yaml          | 52 ++++++++++++++++++++++
 1 file changed, 52 insertions(+)
Documentation/devicetree/bindings/net/cortina,cs7542-mdio.yaml new file +52 -0
new file mode 100644
index 000000000..66b5199c7
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/cortina,cs7542-mdio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Cortina Systems CS7542 MDIO controller
+
+maintainers:
+  - Fil Dunsky <[email protected]>
+
+description:
+  Clause 22 MDIO controller with a programmable 16-bit MDC prescaler.
+
+allOf:
+  - $ref: mdio.yaml#
+
+properties:
+  compatible:
+    const: cortina,cs7542-mdio
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+    description: APB input clock
+
+  suppress-preamble: false
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - "#address-cells"
+  - "#size-cells"
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    mdio@f00700a0 {
+        compatible = "cortina,cs7542-mdio";
+        reg = <0xf00700a0 0x14>;
+        clocks = <&apb_clk>;
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        ethernet-phy@0 {
+            reg = <0>;
+        };
+    };
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH v5 2/3 net: mdio: Add Cortina CS7542 bus controller

In reply to Fil Dunsky

Support Clause 22 reads and writes through the CS7542 management interface. Retain the firmware-configured prescaler and unrelated configuration bits while enabling manual operation.

Use sleepable polling for the bounded wait in the MDIO bus callbacks. Report timeout errors directly without tracking a pending transaction. Use managed bus allocation and registration to cover probe failures and removal.

Acquire and enable the required APB input before accessing registers, and retain it for the lifetime of the registered bus. Managed bus teardown runs before the managed clock is disabled. The firmware MDC prescaler is preserved; no clock-rate or divisor programming is introduced.

---
 drivers/net/mdio/Kconfig       |  12 ++++
 drivers/net/mdio/Makefile      |   1 +
 drivers/net/mdio/mdio-cs75xx.c | 126 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+)
drivers/net/mdio/Kconfig +12 -0
index 6f272fae4..3fd35d6be 100644
@@ -87,6 +87,18 @@ config MDIO_BCM_UNIMAC
 config MDIO_CAVIUM
 	tristate
 
+config MDIO_CS75XX
+	tristate "Cortina Systems CS7542 MDIO controller"
+	depends on OF_MDIO && HAS_IOMEM
+	help
+	  Support Clause 22 PHY register access through the CS7542 MDIO
+	  controller, retaining the prescaler configured by platform firmware.
+	  Transactions use bounded polling.
+	  Device Tree describes the PHYs sharing this management bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called mdio-cs75xx.
+
 config MDIO_GPIO
 	tristate "GPIO lib-based bitbanged MDIO buses"
 	depends on MDIO_BITBANG
drivers/net/mdio/Makefile +1 -0
index e02c66a02..3680c781c 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_MDIO_BCM_IPROC)		+= mdio-bcm-iproc.o
 obj-$(CONFIG_MDIO_BCM_UNIMAC)		+= mdio-bcm-unimac.o
 obj-$(CONFIG_MDIO_BITBANG)		+= mdio-bitbang.o
 obj-$(CONFIG_MDIO_CAVIUM)		+= mdio-cavium.o
+obj-$(CONFIG_MDIO_CS75XX)		+= mdio-cs75xx.o
 obj-$(CONFIG_MDIO_GPIO)			+= mdio-gpio.o
 obj-$(CONFIG_MDIO_I2C)			+= mdio-i2c.o
 obj-$(CONFIG_MDIO_IPQ4019)		+= mdio-ipq4019.o
drivers/net/mdio/mdio-cs75xx.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer Reviewed-by on v5 2/3
#3 lore raw

Re: [PATCH net-next v5 2/3] net: mdio: Add Cortina CS7542 bus controller

Quoted 11 lines · On Fri, Dec 04, 2026 at 06:57:54AM +0300, Fil Dunsky via B4 Relay wrote:

From: Fil Dunsky <[email protected]>

Support Clause 22 reads and writes through the CS7542 management interface. Retain the firmware-configured prescaler and unrelated configuration bits while enabling manual operation.

Use sleepable polling for the bounded wait in the MDIO bus callbacks. Report timeout errors directly without tracking a pending transaction. Use managed bus allocation and registration to cover probe failures and removal.

Quoted 6 lines

Acquire and enable the required APB input before accessing registers, and retain it for the lifetime of the registered bus. Managed bus teardown runs before the managed clock is disabled. The firmware MDC prescaler is preserved; no clock-rate or divisor programming is introduced.

Andrew

#4 lore raw

Re: [PATCH net-next v5 2/3] net: mdio: Add Cortina CS7542 bus controller

In reply to Fil Dunsky
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: mdio: cs75xx_mdio_wait() uses 1 us sleep in readl_poll_timeout() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: mdio: cs75xx_mdio_wait() uses 1 us sleep in readl_poll_timeout()
--

commit 4c7efb2b12f84962992af0098d7dfb6dd1589019
Author: Fil Dunsky <[email protected]>

net: mdio: Add Cortina CS7542 bus controller

This commit adds a Clause 22 MDIO bus controller driver for the Cortina Systems CS7542 using sleepable register polling and managed device resources.

Quoted 6 lines
drivers/net/mdio/mdio-cs75xx.c new file +0 -0
new file mode 100644
index 0000000000000..0405723eb7932
@@ -0,0 +1,126 @@

[ ... ]

drivers/net/mdio/mdio-cs75xx.c line 41

Quoted 11 lines
drivers/net/mdio/mdio-cs75xx.c new file +11 -0
+static int cs75xx_mdio_wait(struct cs75xx_mdio *priv)
+{
+	u32 val;
+	int ret;
+
+	ret = readl_poll_timeout(priv->base + PER_MDIO_CTRL, val,
+				 val & MDIO_CTRL_DONE, 1, CS75XX_MDIO_TIMEOUT_US);
+	if (!ret)
+		writel(MDIO_CTRL_DONE, priv->base + PER_MDIO_CTRL);
+	return ret;
+}

[Severity: Medium]
Should cs75xx_mdio_wait() use a larger sleep interval than 1 us in readl_poll_timeout()?

poll_timeout_us() calls usleep_range((__sleep_us >> 2) + 1, __sleep_us), so passing 1 results in usleep_range(1, 1) with zero slack. That disables hrtimer coalescing and arms an exact 1 us hrtimer on every iteration, where timer and scheduling overhead exceed the delay.

A 64-bit Clause 22 transfer takes at least 25.6 us at 2.5 MHz MDC, so every MDIO transaction loops through dozens of 1 us hrtimer wakeups (and up to 10,000 on timeout). Using a sleep interval of at least 10 us (for example, 10 to 50 us) avoids hrtimer thrashing and allows timer coalescing.

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

Re: [PATCH net-next v5 2/3] net: mdio: Add Cortina CS7542 bus controller

In reply to Fil Dunsky
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 3 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] cs75xx_mdio_probe() (drivers/net/mdio/mdio-cs75xx.c:96-98) keeps whatever MDC prescaler is in PER_MDIO_CFG[31:16] and never…
- [Medium] When cs75xx_mdio_wait() (drivers/net/mdio/mdio-cs75xx.c:31-41) times out, it returns -ETIMEDOUT and leaves the hardware alone.
- [Medium] net: mdio: cs75xx_mdio_wait() uses 1 us sleep in readl_poll_timeout()

--

net: mdio: Add Cortina CS7542 bus controller

This adds a platform driver for the Cortina CS7542 MDIO controller that supports Clause 22 reads and writes. Probe enables the APB clock and switches the controller to manual mode, keeping the firmware-configured MDC prescaler. Bus allocation and registration use managed helpers.

Quoted 5 lines
drivers/net/mdio/mdio-cs75xx.c new file +0 -0
new file mode 100644
index 0000000000000..0405723eb7932

[ ... ]

Quoted 16 lines
drivers/net/mdio/mdio-cs75xx.c new file +16 -0
+static int cs75xx_mdio_wait(struct cs75xx_mdio *priv)
+{
+	u32 val;
+	int ret;
+
+	ret = readl_poll_timeout(priv->base + PER_MDIO_CTRL, val,
+				 val & MDIO_CTRL_DONE, 1, CS75XX_MDIO_TIMEOUT_US);
+	if (!ret)
+		writel(MDIO_CTRL_DONE, priv->base + PER_MDIO_CTRL);
+	return ret;
+}
+
+static int cs75xx_mdio_read(struct mii_bus *bus, int phy, int reg)
+{
+	struct cs75xx_mdio *priv = bus->priv;
+	int ret;

drivers/net/mdio/mdio-cs75xx.c line 56

Quoted 10 lines
drivers/net/mdio/mdio-cs75xx.c new file +10 -0
+
+	writel(MDIO_CTRL_DONE, priv->base + PER_MDIO_CTRL);
+	writel(MDIO_ADDR_READ | FIELD_PREP(MDIO_ADDR_REG, reg) |
+	       FIELD_PREP(MDIO_ADDR_PHY, phy), priv->base + PER_MDIO_ADDR);
+	writel(MDIO_CTRL_START, priv->base + PER_MDIO_CTRL);
+	ret = cs75xx_mdio_wait(priv);
+	if (ret)
+		return ret;
+	return readl(priv->base + PER_MDIO_RDDATA) & 0xffff;
+}

[Severity: Medium]
cs75xx_mdio_wait() passes a sleep_us of 1 to readl_poll_timeout(). In poll_timeout_us() a non-zero sleep turns into usleep_range((sleep_us >> 2) + 1, sleep_us), which here becomes usleep_range(1, 1).

With min == max, usleep_range_state() computes a delta of 0. Every poll iteration therefore arms an exact-deadline 1 us hrtimer with no slack for coalescing, and then schedules out.

The first poll right after MDIO_CTRL_START will always see MDIO_CTRL_DONE clear. The binding keeps the preamble, so a Clause 22 frame is 64 MDC cycles. That is at least 25.6 us at 2.5 MHz, and longer with a slower firmware prescaler.

As a result, every cs75xx_mdio_read() and cs75xx_mdio_write() goes through a run of back-to-back 1 us sleeps. Each one costs a clockevent reprogram, a timer interrupt and a wakeup. On a timeout that could be up to roughly 10,000 of them within CS75XX_MDIO_TIMEOUT_US.

This matters because the path runs on every PHY access. That includes probe-time bus scanning and the once-a-second phy_state_machine() polling for each PHY on the bus.

The comment above fsleep() in include/linux/delay.h notes that hrtimer-based sleeps are not worth it at or below about 10 us. Most drivers under drivers/net/mdio/ poll with an interval of 10 us or more.

Would a sleep of 10 to 20 us work for this controller, given the expected frame time at the inherited MDC rate? Or is there a reason the 1 us interval is needed here?

[Severity: Medium]
What happens to the in-flight transaction when cs75xx_mdio_wait() times out? It returns -ETIMEDOUT but does not abort the transfer, wait for it to finish, or reset the engine. The controller may still be shifting the frame out.

The next cs75xx_mdio_read() or cs75xx_mdio_write() does not check that the controller is idle first. It clears DONE, rewrites PER_MDIO_ADDR (and PER_MDIO_WRDATA for writes), then sets START. If the earlier transaction finishes after that DONE clear:

cs75xx_mdio_read(bus, phyA, regA)

    cs75xx_mdio_wait() -> -ETIMEDOUT, transfer still running
  cs75xx_mdio_read(bus, phyB, regB)
    writel(MDIO_CTRL_DONE, priv->base + PER_MDIO_CTRL);
    writel(... phyB/regB ..., priv->base + PER_MDIO_ADDR);
    writel(MDIO_CTRL_START, priv->base + PER_MDIO_CTRL);
                          <- phyA/regA transfer finishes, DONE set again
    cs75xx_mdio_wait() -> returns 0 immediately
    readl(priv->base + PER_MDIO_RDDATA) -> phyA/regA data

Could phylib then get the RDDATA from the earlier transaction as a valid value for phyB/regB? In the same way, cs75xx_mdio_write() could return 0 for a write that never went out. Rewriting ADDR/WRDATA while the shifter is busy might also corrupt the frame on the wire. This assumes DONE is write-1-to-clear and that START has no effect while busy.

The inherited 16-bit prescaler makes this plausible. A large divisor can slow MDC enough that a 64-bit Clause 22 frame takes longer than CS75XX_MDIO_TIMEOUT_US (10 ms).

The commit message says:

Report timeout errors directly without tracking a pending transaction.

That suggests nothing returns the controller to idle after a timeout.

Probe has a similar gap. It sets MDIO_CFG_MANUAL and clears DONE without waiting for any auto-poll cycle that firmware may have left running.

Other MDIO drivers wait for idle before issuing a command. ipq4019_mdio_read_c22() calls ipq4019_mdio_wait_busy() first, and mscc_miim_read()/mscc_miim_write() call mscc_miim_wait_pending(). Does this driver need a similar idle check, or an abort on timeout?

Quoted 3 lines
drivers/net/mdio/mdio-cs75xx.c new file +3 -0
+
+static int cs75xx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
+{

[ ... ]

drivers/net/mdio/mdio-cs75xx.c line 99

Quoted 13 lines
drivers/net/mdio/mdio-cs75xx.c new file +13 -0
+	clk = devm_clk_get_enabled(dev, NULL);
+	if (IS_ERR(clk))
+		return dev_err_probe(dev, PTR_ERR(clk),
+				     "failed to enable APB clock\n");
+
+	/*
+	 * Select software-initiated accesses instead of automatic PHY polling.
+	 * Read-modify-write preserves the firmware MDC prescaler in bits 31:16.
+	 */
+	cfg = readl(priv->base + PER_MDIO_CFG);
+	cfg |= MDIO_CFG_MANUAL;
+	writel(cfg, priv->base + PER_MDIO_CFG);
+	writel(MDIO_CTRL_DONE, priv->base + PER_MDIO_CTRL);

[Severity: Medium]
Does anything make sure the prescaler inherited in PER_MDIO_CFG bits 31:16 gives a usable MDC? The APB clock is enabled, but clk_get_rate() is never called. The driver therefore cannot tell whether the resulting MDC is within the 2.5 MHz Clause 22 limit.

In several cases MDC is just whatever those bits hold:

- the bootloader never set up MDIO
- a kexec'd kernel inherited a different value
- CFG was reset

If MDC is too fast, PHY accesses could be silently wrong. If it is too slow, every access could time out. There are also no PM callbacks to restore CFG or MDIO_CFG_MANUAL if register context is lost.

The companion patch "dt-bindings: net: Add Cortina CS7542 MDIO controller" describes a "programmable 16-bit MDC prescaler". It references mdio.yaml and disables only suppress-preamble. With unevaluatedProperties: false, a DT that sets clock-frequency from mdio.yaml would still pass validation. This driver then ignores that property and prints no message.

The commit message says:

The firmware MDC prescaler is preserved; no clock-rate or divisor programming is introduced.

So leaving the divisor alone is a deliberate choice. If so, should the binding also disallow clock-frequency, as it already does for suppress-preamble? And should probe at least warn when the APB rate and prescaler give an out-of-spec MDC?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204-b4-cs7542-mdio-v5-0-c9b9b51bf58d%40gmail.com
author
#6 lore raw

Re: [PATCH net-next v5 2/3] net: mdio: Add Cortina CS7542 bus controller

Thanks for the review. The v6 draft uses a 6–20 us timer window for the polls. The immediate first read and the 10 ms timeout remain in place. The polling tests and ARM built-in and module builds passed.

The binding now rejects clock-frequency. The new schema validates the Almond+ DTB and rejects it with clock-frequency added. The driver still uses the firmware's prescaler and enables the APB input. The APB clock source is measured, but the divisor encoding is still unknown, so a rate check based on a guessed formula would be misleading.

The SDK's control register describes START and DONE. I found no documented idle status or abort command, and have no hardware evidence for a late completion being attributed to a subsequent transaction. The draft continues to report a timeout directly, as discussed in the earlier review.

pw-bot: cr

author
#7 lore raw

PATCH v5 3/3 MAINTAINERS: Add Cortina CS7542 MDIO controller entry

In reply to Fil Dunsky

List the binding and driver under the author maintaining the Almond+ platform support, together with the appropriate subsystem mailing list.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index 9336fa93c..ec5053dc5 100644
@@ -6933,6 +6933,13 @@ S:	Maintained
 F:	Documentation/hwmon/corsair-psu.rst
 F:	drivers/hwmon/corsair-psu.c
 
+CORTINA SYSTEMS CS7542 MDIO CONTROLLER
+M:	Fil Dunsky <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/net/cortina,cs7542-mdio.yaml
+F:	drivers/net/mdio/mdio-cs75xx.c
+
 COUNTER SUBSYSTEM
 M:	William Breathitt Gray <[email protected]>
 L:	[email protected]
Signature · 1 line
2.53.0