From patchwork Mon Jun 3 06:49:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13683340 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9B1A381CC for ; Mon, 3 Jun 2024 06:53:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717397583; cv=none; b=W+bDKTXJm4GtRFRCcUJ1+0rpveK+zshiyKfkeIlVU4g4lORrOQDRceTdRVhDyOZ7Z1lzcrW/OTp+fzARuqw/Yf4PSjFLj6iCJjsCtTkF9D9+4kk3ttNLYV5RO7NoQViz5t+YsqZmDic5EJuQdxrpq7zQ5xaTYtMTPHgbCd242i0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717397583; c=relaxed/simple; bh=9oT2+UqOuunVSDLyIpV0rUFhrVMy3DjAtUbXATY/Mus=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KMK+9zFe5wk0L5yD4neuEzwUmDSt9SuXBr+7I1imS1wuem6PYAyRpxPzQAIp59CV8Wt/3OiKvKelUU27+W+8lH5KRAZcZUMJ7+G+m7sAvXsiL1PvzLi85f+9FuzLOa79s/Dk9CqlTa9vVIoI/CpwVBXCkE+asbHcMuLMs0acW6g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZoCU5PZm; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZoCU5PZm" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2c1ae861b6dso526220a91.1 for ; Sun, 02 Jun 2024 23:53:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717397581; x=1718002381; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QshOR171TKumizHD0H4FWgFPBvbiOfTCjxmswigCJc4=; b=ZoCU5PZmuu/1qKkhcxqRzyRzDmFX1oLuE1SRFTehC+uv2hPKfhCxaFvroTZmsvBdjH 3rW+rZn3rsumtfBOl0p/kijaAjEnsIYbF8s63iQe3O0m6WaCx92EkXFxaIhdofF85mow 3g0eyFc2UneD9eCSlw1meWe0BB51QFbRm2pBOzd4LXM37f3BBdBB5xK+HEcnYCDHd5xA 1ddLdiOV0DmoeWf0clb+/dQ7JOuR8Sk9SW0Yy1VsM7/A4jNvMs2UdsaWxdWNGBg/wDnA 63OJx1iXDwzbAefq4t1C9d7E8UwPWn6JjGmsrIqV05kXC+DN1xwbF6efUGX168sHuxxP l2BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717397581; x=1718002381; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QshOR171TKumizHD0H4FWgFPBvbiOfTCjxmswigCJc4=; b=IsMEKVRKWvaSIRof2YrqqzqKHdsJjN1TIK6jZ9LFcPKVogMECeivQTeBmNDgN+S1Y5 Tn4MX/FKWzGVOfykc1HpRCvr1xCr1mEfWSxYyf1vYb34Lzn6xbDvAIb4z++kOcj/YQ6O y4zWSKHGVTcjzd29rSVyVqj0+WN7VNFR6T7HNlFUXA2U1/nvtbAImkQT2dpkGsywy8Ft F8lYbqKOFTGlXp8a3milwSTBRZe03oHNATnCO5DKz2KYU+lQ2BwN/aWREN+YEPpM3n4D LbiEeHQ7T53CW492zCiHnX8HodhMqjWgOOhCvrg9It7nRwz5lNxoN0ErvvAnl8Na68PL y5Sw== X-Gm-Message-State: AOJu0YwqeDHgDkQ/NZKtkoDs1G+wGxtKue6/vuqZEzwh9gYX5+Kju8oJ 0mhPIA00OFrnZ9hTQtYp78b9dnmwwhGjr5pIC4KBoCjhYgqlq4qu6Y7GF4gZ X-Google-Smtp-Source: AGHT+IFWy3GZLj9jg2ywDpqCu5nPJLJZ9eGJUw566cw3xSP01GFpXcxplEdfyLL9yfhpp6ijEh/ZoA== X-Received: by 2002:a17:90a:5984:b0:2bf:eddc:590b with SMTP id 98e67ed59e1d1-2c1dc5608d0mr7235386a91.1.1717397580797; Sun, 02 Jun 2024 23:53:00 -0700 (PDT) Received: from rpi.. (p5261226-ipxg23801hodogaya.kanagawa.ocn.ne.jp. [180.15.241.226]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c1c27e293csm5448263a91.28.2024.06.02.23.52.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 23:53:00 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: andrew@lunn.ch, horms@kernel.org, kuba@kernel.org, jiri@resnulli.us, pabeni@redhat.com, linux@armlinux.org.uk, hfdevel@gmx.net, naveenm@marvell.com, jdamato@fastly.com Subject: [PATCH net-next v8 5/6] net: tn40xx: add mdio bus support Date: Mon, 3 Jun 2024 15:49:54 +0900 Message-Id: <20240603064955.58327-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240603064955.58327-1-fujita.tomonori@gmail.com> References: <20240603064955.58327-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org This patch adds supports for mdio bus. A later path adds PHYLIB support on the top of this. Signed-off-by: FUJITA Tomonori --- drivers/net/ethernet/tehuti/Makefile | 2 +- drivers/net/ethernet/tehuti/tn40.h | 4 + drivers/net/ethernet/tehuti/tn40_mdio.c | 143 ++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 drivers/net/ethernet/tehuti/tn40_mdio.c diff --git a/drivers/net/ethernet/tehuti/Makefile b/drivers/net/ethernet/tehuti/Makefile index 1c468d99e476..7a0fe586a243 100644 --- a/drivers/net/ethernet/tehuti/Makefile +++ b/drivers/net/ethernet/tehuti/Makefile @@ -5,5 +5,5 @@ obj-$(CONFIG_TEHUTI) += tehuti.o -tn40xx-y := tn40.o +tn40xx-y := tn40.o tn40_mdio.o obj-$(CONFIG_TEHUTI_TN40) += tn40xx.o diff --git a/drivers/net/ethernet/tehuti/tn40.h b/drivers/net/ethernet/tehuti/tn40.h index afe85ce44d41..05a9adf9fe5a 100644 --- a/drivers/net/ethernet/tehuti/tn40.h +++ b/drivers/net/ethernet/tehuti/tn40.h @@ -141,6 +141,8 @@ struct tn40_priv { u32 b0_len; dma_addr_t b0_dma; /* Physical address of buffer */ char *b0_va; /* Virtual address of buffer */ + + struct mii_bus *mdio; }; /* RX FREE descriptor - 64bit */ @@ -218,4 +220,6 @@ static inline void tn40_write_reg(struct tn40_priv *priv, u32 reg, u32 val) writel(val, priv->regs + reg); } +int tn40_mdiobus_init(struct tn40_priv *priv); + #endif /* _TN40XX_H */ diff --git a/drivers/net/ethernet/tehuti/tn40_mdio.c b/drivers/net/ethernet/tehuti/tn40_mdio.c new file mode 100644 index 000000000000..8a1f0c9f51b2 --- /dev/null +++ b/drivers/net/ethernet/tehuti/tn40_mdio.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (c) Tehuti Networks Ltd. */ + +#include +#include +#include + +#include "tn40.h" + +#define TN40_MDIO_DEVAD_MASK GENMASK(4, 0) +#define TN40_MDIO_PRTAD_MASK GENMASK(9, 5) +#define TN40_MDIO_CMD_VAL(device, port) \ + (FIELD_PREP(TN40_MDIO_DEVAD_MASK, (device)) | \ + (FIELD_PREP(TN40_MDIO_PRTAD_MASK, (port)))) +#define TN40_MDIO_CMD_READ BIT(15) + +static void tn40_mdio_set_speed(struct tn40_priv *priv, u32 speed) +{ + void __iomem *regs = priv->regs; + int mdio_cfg; + + mdio_cfg = readl(regs + TN40_REG_MDIO_CMD_STAT); + if (speed == 1) + mdio_cfg = (0x7d << 7) | 0x08; /* 1MHz */ + else + mdio_cfg = 0xA08; /* 6MHz */ + mdio_cfg |= (1 << 6); + writel(mdio_cfg, regs + TN40_REG_MDIO_CMD_STAT); + msleep(100); +} + +static u32 tn40_mdio_stat(struct tn40_priv *priv) +{ + void __iomem *regs = priv->regs; + + return readl(regs + TN40_REG_MDIO_CMD_STAT); +} + +static int tn40_mdio_get(struct tn40_priv *priv, u32 *val) +{ + u32 stat; + int ret; + + ret = readx_poll_timeout_atomic(tn40_mdio_stat, priv, stat, + TN40_GET_MDIO_BUSY(stat) == 0, 10, + 10000); + if (val) + *val = stat; + return ret; +} + +static int tn40_mdio_read(struct tn40_priv *priv, int port, int device, + u16 regnum) +{ + void __iomem *regs = priv->regs; + u32 i; + + /* wait until MDIO is not busy */ + if (tn40_mdio_get(priv, NULL)) + return -EIO; + + i = TN40_MDIO_CMD_VAL(device, port); + writel(i, regs + TN40_REG_MDIO_CMD); + writel((u32)regnum, regs + TN40_REG_MDIO_ADDR); + if (tn40_mdio_get(priv, NULL)) + return -EIO; + + writel(TN40_MDIO_CMD_READ | i, regs + TN40_REG_MDIO_CMD); + /* read CMD_STAT until not busy */ + if (tn40_mdio_get(priv, NULL)) + return -EIO; + + return lower_16_bits(readl(regs + TN40_REG_MDIO_DATA)); +} + +static int tn40_mdio_write(struct tn40_priv *priv, int port, int device, + u16 regnum, u16 data) +{ + void __iomem *regs = priv->regs; + u32 tmp_reg = 0; + int ret; + + /* wait until MDIO is not busy */ + if (tn40_mdio_get(priv, NULL)) + return -EIO; + writel(TN40_MDIO_CMD_VAL(device, port), regs + TN40_REG_MDIO_CMD); + writel((u32)regnum, regs + TN40_REG_MDIO_ADDR); + if (tn40_mdio_get(priv, NULL)) + return -EIO; + writel((u32)data, regs + TN40_REG_MDIO_DATA); + /* read CMD_STAT until not busy */ + ret = tn40_mdio_get(priv, &tmp_reg); + if (ret) + return -EIO; + + if (TN40_GET_MDIO_RD_ERR(tmp_reg)) { + dev_err(&priv->pdev->dev, "MDIO error after write command\n"); + return -EIO; + } + return 0; +} + +static int tn40_mdio_read_cb(struct mii_bus *mii_bus, int addr, int devnum, + int regnum) +{ + return tn40_mdio_read(mii_bus->priv, addr, devnum, regnum); +} + +static int tn40_mdio_write_cb(struct mii_bus *mii_bus, int addr, int devnum, + int regnum, u16 val) +{ + return tn40_mdio_write(mii_bus->priv, addr, devnum, regnum, val); +} + +int tn40_mdiobus_init(struct tn40_priv *priv) +{ + struct pci_dev *pdev = priv->pdev; + struct mii_bus *bus; + int ret; + + bus = devm_mdiobus_alloc(&pdev->dev); + if (!bus) + return -ENOMEM; + + bus->name = TN40_DRV_NAME; + bus->parent = &pdev->dev; + snprintf(bus->id, MII_BUS_ID_SIZE, "tn40xx-%x-%x", + pci_domain_nr(pdev->bus), pci_dev_id(pdev)); + bus->priv = priv; + + bus->read_c45 = tn40_mdio_read_cb; + bus->write_c45 = tn40_mdio_write_cb; + + ret = devm_mdiobus_register(&pdev->dev, bus); + if (ret) { + dev_err(&pdev->dev, "failed to register mdiobus %d %u %u\n", + ret, bus->state, MDIOBUS_UNREGISTERED); + return ret; + } + tn40_mdio_set_speed(priv, TN40_MDIO_SPEED_6MHZ); + priv->mdio = bus; + return 0; +}