From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>, rcz@pengutronix.de
Subject: [PATCH 11/12] net: macb: add SiFive support
Date: Tue, 27 Apr 2021 22:23:08 +0200 [thread overview]
Message-ID: <20210427202309.32077-11-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210427202309.32077-1-a.fatoum@pengutronix.de>
The Ethernet controller on the SiFive SoCs needs some special TX clock
setup. Port the relevant Linux v5.12 bits to enable it.
Tested on qemu-system-riscv64 -M sifive_u with dhcp.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/net/macb.c | 113 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 113 insertions(+)
diff --git a/drivers/net/macb.c b/drivers/net/macb.c
index 14a0b45322bf..9c4fdbd5bc4f 100644
--- a/drivers/net/macb.c
+++ b/drivers/net/macb.c
@@ -52,6 +52,17 @@
#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
#define GEM_Q1_DESC_BYTES (sizeof(struct macb_dma_desc) * GEM_Q1_DESCS)
+/* This structure is only used for MACB on SiFive FU540 devices */
+struct sifive_fu540_macb_mgmt {
+ void __iomem *reg;
+ unsigned long rate;
+ struct clk clk;
+};
+
+struct macb_config {
+ int (*txclk_init)(struct device_d *dev, struct clk **tx_clk);
+};
+
struct macb_device {
void __iomem *regs;
@@ -666,12 +677,101 @@ static void macb_init_rx_buffer_size(struct macb_device *bp, size_t size)
size, bp->rx_buffer_size);
}
+static struct sifive_fu540_macb_mgmt *mgmt;
+
+static unsigned long fu540_macb_tx_recalc_rate(struct clk *clk,
+ unsigned long parent_rate)
+{
+ return mgmt->rate;
+}
+
+static long fu540_macb_tx_round_rate(struct clk *clk, unsigned long rate,
+ unsigned long *parent_rate)
+{
+ if (WARN_ON(rate < 2500000))
+ return 2500000;
+ else if (rate == 2500000)
+ return 2500000;
+ else if (WARN_ON(rate < 13750000))
+ return 2500000;
+ else if (WARN_ON(rate < 25000000))
+ return 25000000;
+ else if (rate == 25000000)
+ return 25000000;
+ else if (WARN_ON(rate < 75000000))
+ return 25000000;
+ else if (WARN_ON(rate < 125000000))
+ return 125000000;
+ else if (rate == 125000000)
+ return 125000000;
+
+ WARN_ON(rate > 125000000);
+
+ return 125000000;
+}
+
+static int fu540_macb_tx_set_rate(struct clk *clk, unsigned long rate,
+ unsigned long parent_rate)
+{
+ rate = fu540_macb_tx_round_rate(clk, rate, &parent_rate);
+ if (rate != 125000000)
+ iowrite32(1, mgmt->reg);
+ else
+ iowrite32(0, mgmt->reg);
+ mgmt->rate = rate;
+
+ return 0;
+}
+
+static const struct clk_ops fu540_c000_ops = {
+ .recalc_rate = fu540_macb_tx_recalc_rate,
+ .round_rate = fu540_macb_tx_round_rate,
+ .set_rate = fu540_macb_tx_set_rate,
+};
+
+static int fu540_c000_txclk_init(struct device_d *dev, struct clk **tx_clk)
+{
+ struct clk *clk;
+ struct resource *res;
+ int err = 0;
+
+ mgmt = xzalloc(sizeof(*mgmt));
+
+ res = dev_request_mem_resource(dev, 1);
+ if (IS_ERR(res))
+ return PTR_ERR(res);
+
+ mgmt->reg = IOMEM(res->start);
+
+ clk = &mgmt->clk;
+
+ clk->name = "sifive-gemgxl-mgmt";
+ clk->ops = &fu540_c000_ops;
+
+ err = clk_register(&mgmt->clk);
+ if (err)
+ return err;
+
+ *tx_clk = &mgmt->clk;
+
+ err = clk_enable(*tx_clk);
+ if (err) {
+ dev_err(dev, "failed to enable tx_clk (%u)\n", err);
+ *tx_clk = NULL;
+ return err;
+ }
+
+ dev_info(dev, "Registered clk switch '%s'\n", clk->name);
+ return 0;
+}
+
static int macb_probe(struct device_d *dev)
{
struct resource *iores;
struct eth_device *edev;
struct macb_device *macb;
const char *pclk_name, *hclk_name;
+ const struct macb_config *config = NULL;
u32 ncfgr;
macb = xzalloc(sizeof(*macb));
@@ -725,6 +825,8 @@ static int macb_probe(struct device_d *dev)
macb->phy_addr = -1;
pclk_name = "pclk";
hclk_name = "hclk";
+
+ config = device_get_match_data(dev);
} else {
dev_err(dev, "macb: no platform_data\n");
return -ENODEV;
@@ -767,6 +869,12 @@ static int macb_probe(struct device_d *dev)
if (!IS_ERR(macb->rxclk))
clk_enable(macb->rxclk);
+ if (config) {
+ int ret = config->txclk_init(dev, &macb->txclk);
+ if (ret)
+ return ret;
+ }
+
macb->is_gem = read_is_gem(macb);
if (macb_is_gem(macb))
@@ -808,12 +916,17 @@ static void macb_remove(struct device_d *dev)
macb_halt(&macb->netdev);
}
+static const struct macb_config fu540_c000_config = {
+ .txclk_init = fu540_c000_txclk_init,
+};
+
static const struct of_device_id macb_dt_ids[] = {
{ .compatible = "cdns,at91sam9260-macb",},
{ .compatible = "atmel,sama5d2-gem",},
{ .compatible = "atmel,sama5d3-gem",},
{ .compatible = "cdns,zynq-gem",},
{ .compatible = "cdns,zynqmp-gem",},
+ { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
{ /* sentinel */ }
};
--
2.29.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2021-04-27 20:24 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-27 20:22 [PATCH 01/12] clk: add SiFive PRCI clock controller support Ahmad Fatoum
2021-04-27 20:22 ` [PATCH 02/12] serial: implement SiFive UART support Ahmad Fatoum
2021-04-27 20:23 ` [PATCH 03/12] debug_ll: support <asm/debug_ll.h> to get rid of mach directories Ahmad Fatoum
2021-04-27 20:23 ` [PATCH 04/12] RISC-V: support multi-image for all machines Ahmad Fatoum
2021-05-03 11:33 ` Sascha Hauer
2021-05-03 11:39 ` Ahmad Fatoum
2021-05-03 12:00 ` Sascha Hauer
2021-04-27 20:23 ` [PATCH 05/12] RISC-V: erizo: restrict to RV32I Ahmad Fatoum
2021-04-27 20:23 ` [PATCH 06/12] RISC-V: erizo: drop mach-erizo directory Ahmad Fatoum
2021-05-05 10:18 ` Antony Pavlov
2021-05-05 10:21 ` Ahmad Fatoum
2021-05-07 11:27 ` Antony Pavlov
2021-04-27 20:23 ` [PATCH 07/12] RISC-V: add SBI based cpuinfo Ahmad Fatoum
2021-04-27 20:23 ` [PATCH 08/12] gpio: gpio-generic-platform: remove unused non-DT support Ahmad Fatoum
2021-04-27 20:23 ` [PATCH 09/12] gpio: generic: sync with upstream Linux gpio-mmio driver Ahmad Fatoum
2021-05-06 23:45 ` Antony Pavlov
2021-04-27 20:23 ` [PATCH 10/12] gpio: add SiFive GPIO controller support Ahmad Fatoum
2021-04-27 20:23 ` Ahmad Fatoum [this message]
2021-05-17 6:39 ` [PATCH 11/12] net: macb: add SiFive support Sascha Hauer
2021-04-27 20:23 ` [PATCH 12/12] RISC-V: sifive: add HiFive board support Ahmad Fatoum
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=20210427202309.32077-11-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=rcz@pengutronix.de \
/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