U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yao Zi <me@ziyao.cc>
To: Zixian Zeng <sycamoremoon376@gmail.com>,
	u-boot@lists.u-boot-project.org, u-boot-spacemit@groups.io
Cc: Tom Rini <trini@konsulko.com>,
	Jerome Forissier <jerome.forissier@arm.com>,
	Huan Zhou <pericycle.cc@gmail.com>,
	Guodong Xu <guodong.xu@riscstar.com>,
	Quentin Schulz <quentin.schulz@cherry.de>,
	Julien Stephan <jstephan@baylibre.com>,
	Johan Jonker <jbx6244@gmail.com>,
	David Lechner <dlechner@baylibre.com>,
	Raymond Mao <raymond.mao@riscstar.com>,
	Eric Chung <eric.chung@riscstar.com>, Yixun Lan <dlan@kernel.org>,
	James Hilliard <james.hilliard1@gmail.com>,
	Guodong Xu <guodong@riscstar.com>, E Shattow <e@freeshell.de>,
	Dave Klotz <DaveWK225@protonmail.com>, Yao Zi <me@ziyao.cc>
Subject: Re: [PATCH v3 1/2] net: spacemit: k1: add Ethernet MAC driver
Date: Fri, 20 Nov 2026 16:56:12 +0000	[thread overview]
Message-ID: <awB7rFETr-jVP5_b@pie> (raw)
In-Reply-To: <20261120-b4-k1-emac-v3-1-120679f97b45@gmail.com>

On Fri, Nov 20, 2026 at 02:35:56PM +0800, Zixian Zeng wrote:
> Add Ethernet MAC driver support for the SpacemiT K1 SoC. The driver
> supports RGMII and RMII interfaces, synchronous transmission, and exposes
> the integrated MDIO controller through the MDIO uclass.
> 
> The register definitions, descriptor layout, MDIO protocol, RGMII delay
> line handling and hardware initialisation sequences are derived from the
> mainline Linux commit bfec6d7f2001 (net: spacemit: Add K1 Ethernet MAC).
> 
> Link: https://lore.kernel.org/all/20250914-net-k1-emac-v12-0-65b31b398f44@iscas.ac.cn/
> Tested-by: Dave Klotz <DaveWK225@protonmail.com>
> Tested-by: Yixun Lan <dlan@kernel.org> # MusePi Pro
> Signed-off-by: Zixian Zeng <sycamoremoon376@gmail.com>
> ---
>  drivers/net/Kconfig            |   1 +
>  drivers/net/Makefile           |   1 +
>  drivers/net/spacemit/Kconfig   |  14 +
>  drivers/net/spacemit/Makefile  |   3 +
>  drivers/net/spacemit/k1_emac.c | 957 +++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 976 insertions(+)

...

> diff --git a/drivers/net/spacemit/k1_emac.c b/drivers/net/spacemit/k1_emac.c
> new file mode 100644
> index 00000000000..67baaade97e
> --- /dev/null
> +++ b/drivers/net/spacemit/k1_emac.c
> @@ -0,0 +1,957 @@
> +	struct regmap *apmu;
> +	u32 apmu_offset;
> +	phy_interface_t interface;
> +	u32 max_speed;
> +	u32 tx_delay_code;
> +	u32 rx_delay_code;
> +	struct clk clk;
> +	struct reset_ctl reset;
> +	struct phy_device *phy;
> +	struct k1_emac_desc *tx_desc;
> +	struct k1_emac_desc *rx_desc;
> +	u8 *tx_buf;
> +	u8 *rx_buf;
> +	u32 tx_desc_dma;
> +	u32 rx_desc_dma;
> +	u32 tx_buf_dma[K1_TX_DESC_COUNT];
> +	u32 rx_buf_dma[K1_RX_DESC_COUNT];
> +	u32 tx_index;
> +	u32 rx_index;
> +	char mdio_name[MDIO_NAME_LEN];
> +	bool rx_pending;
> +	bool clk_enabled;
> +	bool reset_deasserted;
> +};
> +
> +static inline struct k1_emac_desc *k1_desc_at(struct k1_emac_desc *ring,
> +					      unsigned int index)
> +{
> +	return &ring[index];
> +}

Honestly saying, I don't think this function helps readability much,
please consider removing it.

> +static inline u8 *k1_rx_buf_at(struct k1_emac_priv *priv, unsigned int index)
> +{
> +	return priv->rx_buf + index * K1_DMA_BUF_SIZE;
> +}
> +
> +static inline u8 *k1_tx_buf_at(struct k1_emac_priv *priv, unsigned int index)
> +{
> +	return priv->tx_buf + index * K1_DMA_BUF_SIZE;
> +}
> +
> +static void k1_desc_flush(struct k1_emac_desc *desc)
> +{
> +	ulong start = ALIGN_DOWN((ulong)desc, ARCH_DMA_MINALIGN);
> +
> +	flush_dcache_range(start, start + ARCH_DMA_MINALIGN);
> +	/* Complete the clean before notifying the DMA engine. */
> +	mb();

I suspect this barrier is redundant. Per the RISC-V unprivileged
specification,

> Additionally, invalidate, clean, and flush operations are classified
> as W or O (depending on the physical memory attributes for the
> corresponding physical addresses) for the purposes of predecessor and
> successor sets in FENCE instructions.

So cache management operations should be naturally ordered with the
writel() to DMA_RX/TX_POLL_DEMAND, which carries an __iowmb(), i.e.,
fence ow, ow.

> +}
> +
> +static void k1_desc_invalidate(struct k1_emac_desc *desc)
> +{
> +	ulong start = ALIGN_DOWN((ulong)desc, ARCH_DMA_MINALIGN);
> +
> +	invalidate_dcache_range(start, start + ARCH_DMA_MINALIGN);
> +	/* Complete the invalidation before inspecting DMA-written fields. */
> +	mb();

So is this barrier.

> +}

[...]

> +static int k1_emac_dma32(struct udevice *dev, void *ptr, size_t size,
> +			 u32 *address)
> +{
> +	dma_addr_t dma = dev_phys_to_bus(dev, virt_to_phys(ptr));
> +
> +	/* Both the start and the last byte must fit the 32-bit window. */
> +	if (upper_32_bits(dma) || upper_32_bits(dma + size - 1)) {
> +		dev_err(dev, "DMA range %#llx-%#llx is outside the 32-bit window\n",
> +			(unsigned long long)dma,
> +			(unsigned long long)(dma + size - 1));
> +		return -ERANGE;
> +	}
> +
> +	*address = lower_32_bits(dma);
> +	return 0;
> +}

This is technically correct, but I doubt whether we should do such
carefully validation for each allocation...

I suggest selecting LMB_LIMIT_DMA_BELOW_RAW_TOP to make the DMA
capability limitation clear instead, but it depends on you.

[...]

> +static int k1_emac_hw_init(struct k1_emac_priv *priv)
> +{
> +	u32 config;
> +	int ret;
> +
> +	writel(0, priv->base + K1_MAC_INTERRUPT_ENABLE);
> +	writel(0, priv->base + K1_DMA_INTERRUPT_ENABLE);
> +	writel(0, priv->base + K1_MAC_RX_CONTROL);
> +	writel(0, priv->base + K1_MAC_TX_CONTROL);
> +	writel(0, priv->base + K1_DMA_CONTROL);
> +
> +	ret = regmap_update_bits(priv->apmu,
> +				 priv->apmu_offset + K1_APMU_EMAC_CTRL,
> +				 K1_APMU_AXI_SINGLE_ID, K1_APMU_AXI_SINGLE_ID);
> +	if (ret)
> +		return ret;

Since this syscon is backed by MMIO, I don't think there's any need to
check for errors returned by it. This applies for other places in the
patch, too.

[...]

> +static void k1_emac_prepare_rx(struct k1_emac_priv *priv, unsigned int index)
> +{
> +	struct k1_emac_desc *desc = k1_desc_at(priv->rx_desc, index);
> +	u8 *buffer = k1_rx_buf_at(priv, index);
> +
> +	k1_buffer_flush(buffer);

Does flushing the RX buffer really make a difference on the hardware's
behavior?

> +	desc->control = FIELD_PREP(K1_RX_DESC_BUFFER_SIZE, K1_DMA_BUF_SIZE);
> +	if (index == K1_RX_DESC_COUNT - 1)
> +		desc->control |= K1_RX_DESC_END_RING;
> +	desc->buffer_addr1 = priv->rx_buf_dma[index];
> +	desc->buffer_addr2 = 0;
> +	desc->status = K1_RX_DESC_OWN;
> +}
> +
> +static void k1_emac_advance_rx(struct k1_emac_priv *priv)
> +{
> +	unsigned int index = priv->rx_index;
> +	unsigned int first;
> +
> +	/*
> +	 * Multiple 16-byte descriptors share a cache line. Do not clean that
> +	 * line while DMA may still be updating another descriptor in it. Return
> +	 * descriptors to DMA only after the complete cache-line-sized group has
> +	 * been consumed.
> +	 */
> +	if (!((index + 1) % K1_DESCS_PER_CACHELINE)) {
> +		first = index + 1 - K1_DESCS_PER_CACHELINE;
> +		for (; first <= index; first++)
> +			k1_emac_prepare_rx(priv, first);
> +
> +		/* Complete descriptor stores before cleaning the cache line. */
> +		mb();
> +		k1_desc_flush(k1_desc_at(priv->rx_desc, index));
> +		writel(1, priv->base + K1_DMA_RX_POLL_DEMAND);
> +	}
> +
> +	priv->rx_index = (index + 1) % K1_RX_DESC_COUNT;
> +}
> +
> +static void k1_emac_init_rings(struct k1_emac_priv *priv)
> +{
> +	struct k1_emac_desc *desc;
> +	unsigned int i;
> +
> +	memset(priv->tx_desc, 0, K1_TX_DESC_ALLOC_SIZE);
> +	memset(priv->rx_desc, 0, K1_RX_DESC_ALLOC_SIZE);
> +	memset(priv->tx_buf, 0, K1_TX_DESC_COUNT * K1_DMA_BUF_SIZE);
> +	memset(priv->rx_buf, 0, K1_RX_DESC_COUNT * K1_DMA_BUF_SIZE);
> +	flush_dcache_range((ulong)priv->tx_buf,
> +			   (ulong)priv->tx_buf +
> +			   K1_TX_DESC_COUNT * K1_DMA_BUF_SIZE);
> +	flush_dcache_range((ulong)priv->rx_buf,
> +			   (ulong)priv->rx_buf + K1_RX_DESC_COUNT * K1_DMA_BUF_SIZE);

Like k1_emac_prepare_rx()'s case, are these two memset() and flushes
really useful? Does the initial buffer content visible to the NIC really
affect its behavior?

> +	for (i = 0; i < K1_TX_DESC_COUNT; i++) {
> +		desc = k1_desc_at(priv->tx_desc, i);
> +		if (i == K1_TX_DESC_COUNT - 1)
> +			desc->control = K1_TX_DESC_END_RING;
> +		desc->buffer_addr1 = priv->tx_buf_dma[i];
> +	}
> +	flush_dcache_range((ulong)priv->tx_desc,
> +			   (ulong)priv->tx_desc + K1_TX_DESC_ALLOC_SIZE);
> +
> +	for (i = 0; i < K1_RX_DESC_COUNT; i++)
> +		k1_emac_prepare_rx(priv, i);
> +	/* Complete all descriptor stores before handing the ring to DMA. */
> +	mb();
> +
> +	flush_dcache_range((ulong)priv->rx_desc,
> +			   (ulong)priv->rx_desc + K1_RX_DESC_ALLOC_SIZE);
> +	/* Complete the clean before programming the DMA descriptor base. */
> +	mb();

Are these two barriers() really necessary? At the point
where k1_emac_init_rings() is called from k1_emac_start(), we haven't
even set the ring addresses to the DMA engine. And setting so with
writel() implies a fence wo, wo barrier, so it's impossible for the
engine to observe the ring address before writes to the ring.

> +	priv->tx_index = 0;
> +	priv->rx_index = 0;
> +	priv->rx_pending = false;
> +}

[...]

> +static int k1_emac_send(struct udevice *dev, void *packet, int length)
> +{
> +	struct k1_emac_priv *priv = dev_get_priv(dev);
> +	unsigned int index = priv->tx_index;
> +	struct k1_emac_desc *desc = k1_desc_at(priv->tx_desc, index);
> +	u8 *buffer = k1_tx_buf_at(priv, index);
> +	ulong start;
> +
> +	if (length <= 0 || length > K1_DMA_BUF_SIZE)
> +		return -EMSGSIZE;
> +
> +	k1_desc_invalidate(desc);
> +	if (desc->status & K1_TX_DESC_OWN)
> +		return -EBUSY;
> +
> +	memcpy(buffer, packet, length);
> +	flush_dcache_range((ulong)buffer,
> +			   ALIGN((ulong)buffer + length, ARCH_DMA_MINALIGN));
> +
> +	/* Clear stale state before handing the descriptor to DMA. */
> +	writel(K1_DMA_STATUS_TX_DESC_UNAVAILABLE, priv->base + K1_DMA_STATUS);
> +	/* Complete the W1C write before asking the DMA engine to resume. */
> +	readl(priv->base + K1_DMA_STATUS);
> +
> +	desc->buffer_addr1 = priv->tx_buf_dma[index];
> +	desc->buffer_addr2 = 0;
> +	desc->control = FIELD_PREP(K1_TX_DESC_BUFFER_SIZE, length) |
> +			K1_TX_DESC_FIRST | K1_TX_DESC_LAST;
> +	if (index == K1_TX_DESC_COUNT - 1)
> +		desc->control |= K1_TX_DESC_END_RING;
> +	desc->status = K1_TX_DESC_OWN;
> +	/* Complete descriptor stores before cleaning its cache line. */
> +	mb();

Probably redundant barrier since RISC-V guarantees order between
overlapping stores...

> +	k1_desc_flush(desc);
> +	writel(1, priv->base + K1_DMA_TX_POLL_DEMAND);
> +
> +	start = get_timer(0);
> +	do {

The could would be simpler if you use a loop simply bounded by retry
counts.

> +		k1_desc_invalidate(desc);
> +		/*
> +		 * OWN is cleared before transmission finishes. Descriptor
> +		 * unavailable is raised after DMA completes the packet and moves
> +		 * to the next CPU-owned descriptor.
> +		 */
> +		if (!(desc->status & K1_TX_DESC_OWN) &&
> +		    (readl(priv->base + K1_DMA_STATUS) &
> +		     K1_DMA_STATUS_TX_DESC_UNAVAILABLE)) {
> +			priv->tx_index = (index + 1) % K1_TX_DESC_COUNT;
> +			return 0;
> +		}
> +		udelay(10);
> +	} while (get_timer(start) <= K1_TX_TIMEOUT_MS);

[...]

> +static int k1_emac_recv(struct udevice *dev, int flags, uchar **packetp)
> +{
> +	struct k1_emac_priv *priv = dev_get_priv(dev);
> +	struct k1_emac_desc *desc;
> +	u32 status, length;
> +	u8 *buffer;
> +	unsigned int checked = 0;
> +
> +	if (priv->rx_pending)
> +		return -EBUSY;
> +
> +	while (checked++ < K1_RX_DESC_COUNT) {
> +		desc = k1_desc_at(priv->rx_desc, priv->rx_index);
> +		k1_desc_invalidate(desc);
> +		status = desc->status;
> +		if (status & K1_RX_DESC_OWN)
> +			return -EAGAIN;
> +
> +		length = FIELD_GET(K1_RX_DESC_FRAME_LEN, status);
> +		if ((status & (K1_RX_DESC_FIRST | K1_RX_DESC_LAST)) ==
> +		    (K1_RX_DESC_FIRST | K1_RX_DESC_LAST) &&
> +		    !(status & K1_RX_DESC_ERRORS) && length > ETH_FCS_LEN &&
> +		    length <= K1_DMA_BUF_SIZE) {
> +			buffer = k1_rx_buf_at(priv, priv->rx_index);
> +			k1_buffer_invalidate(buffer);
> +			/* Complete invalidation before exposing the received frame. */
> +			mb();

RISC-V guarantees that cache management operations couldn't be reordered
with later stores,

> Specifically, if a precedes b in program order, then a will precede b
> in the global memory order if:
>	a is an invalidate, clean, or flush, b is a load, and a and b
>	access overlapping memory addresses

Thus this mb() is redundant. Any access to buffer must be ordered after
this invalidation.

[...]

> +static void k1_emac_stop(struct udevice *dev)
> +{
> +	struct k1_emac_priv *priv = dev_get_priv(dev);
> +
> +	writel(0, priv->base + K1_MAC_INTERRUPT_ENABLE);
> +	writel(0, priv->base + K1_DMA_INTERRUPT_ENABLE);

What's the point of resetting interrupt enable bits here? They have been
cleared in k1_emac_hw_init(), and I don't think there's anything turning
it back on automatically.

> +	writel(0, priv->base + K1_DMA_CONTROL);
> +	writel(0, priv->base + K1_MAC_RX_CONTROL);
> +	writel(0, priv->base + K1_MAC_TX_CONTROL);
> +	priv->rx_pending = false;
> +	phy_shutdown(priv->phy);
> +}

[...]

> +static int k1_emac_of_to_plat(struct udevice *dev)
> +{
> +	struct eth_pdata *pdata = dev_get_plat(dev);
> +	struct k1_emac_priv *priv = dev_get_priv(dev);
> +	ofnode node;
> +	bool mdio_bound = false;
> +	int ret;
> +
> +	pdata->iobase = dev_read_addr(dev);
> +	if (pdata->iobase == FDT_ADDR_T_NONE)
> +		return -EINVAL;
> +
> +	pdata->phy_interface = dev_read_phy_mode(dev);
> +	switch (pdata->phy_interface) {
> +	case PHY_INTERFACE_MODE_RMII:
> +	case PHY_INTERFACE_MODE_RGMII:
> +	case PHY_INTERFACE_MODE_RGMII_ID:
> +	case PHY_INTERFACE_MODE_RGMII_RXID:
> +	case PHY_INTERFACE_MODE_RGMII_TXID:
> +		break;
> +	default:
> +		return -EINVAL;

Or -EOPNOTSUPP? I think this is more appropriate.

[...]

-- 
Thanks,
Yao Zi

Meow-mrrp-mrow, I'm a cat.

  reply	other threads:[~2026-11-20 16:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  6:35 [PATCH v3 0/2] net: add SpacemiT K1 EMAC support Zixian Zeng
2026-11-20  6:35 ` [PATCH v3 1/2] net: spacemit: k1: add Ethernet MAC driver Zixian Zeng
2026-11-20 16:56   ` Yao Zi [this message]
2026-11-21 18:31     ` Yao Zi
2026-11-22  6:23     ` Zixian Zeng
2026-11-22  8:10       ` Yao Zi
2026-11-22 12:37         ` Zixian Zeng
2026-11-22 17:10           ` Yao Zi
2026-11-20  6:35 ` [PATCH v3 2/2] configs: spacemit: k1: enable Ethernet support Zixian Zeng

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=awB7rFETr-jVP5_b@pie \
    --to=me@ziyao.cc \
    --cc=DaveWK225@protonmail.com \
    --cc=dlan@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=e@freeshell.de \
    --cc=eric.chung@riscstar.com \
    --cc=guodong.xu@riscstar.com \
    --cc=guodong@riscstar.com \
    --cc=james.hilliard1@gmail.com \
    --cc=jbx6244@gmail.com \
    --cc=jerome.forissier@arm.com \
    --cc=jstephan@baylibre.com \
    --cc=pericycle.cc@gmail.com \
    --cc=quentin.schulz@cherry.de \
    --cc=raymond.mao@riscstar.com \
    --cc=sycamoremoon376@gmail.com \
    --cc=trini@konsulko.com \
    --cc=u-boot-spacemit@groups.io \
    --cc=u-boot@lists.u-boot-project.org \
    /path/to/YOUR_REPLY

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

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