[dm][ethernet] add MACB and DWMAC ethernet support

They support PTP, too.

Signed-off-by: GuEe-GUI <2991707448@qq.com>
This commit is contained in:
GuEe-GUI authored and Rbb666 committed 2026-08-06 09:43:10 +08:00
1 parent 84da9b9393
commit c3e1dd7946
16 files changed
+5383

No files matched your search

+2
View File
@@ -7,5 +7,7 @@ menuconfig RT_USING_ETHERNET
default n
if RT_USING_ETHERNET
rsource "cadence/Kconfig"
rsource "synopsys/Kconfig"
osource "$(SOC_DM_ETHERNET_DIR)/Kconfig"
endif
@@ -0,0 +1,15 @@
config RT_ETHERNET_CADENCE
bool "Cadence GEM/MACB Ethernet"
depends on RT_USING_PHY_V2
depends on RT_USING_DMA
depends on RT_USING_PIN
config RT_ETHERNET_CADENCE_PCI
bool "PCI supported"
depends on RT_ETHERNET_CADENCE
depends on RT_USING_PCI
config RT_ETHERNET_CADENCE_PTP
bool "PTP supported"
depends on RT_ETHERNET_CADENCE
depends on RT_USING_PTP
@@ -0,0 +1,21 @@
from building import *
group = []
if not GetDepend(['RT_ETHERNET_CADENCE']):
Return('group')
cwd = GetCurrentDir()
CPPPATH = [cwd + '/../../include', cwd + '/../../phy']
src = ['macb.c']
if GetDepend(['RT_ETHERNET_CADENCE_PCI']):
src += ['macb_pci.c']
if GetDepend(['RT_ETHERNET_CADENCE_PTP']):
src += ['macb_ptp.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,179 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-11-25 GuEe-GUI first version
*/
#include "macb.h"
#define DBG_TAG "eth.macb.pci"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
static rt_ubase_t gem_clk_recalc_rate(struct rt_clk_cell *cell, rt_ubase_t parent_rate)
{
(void)cell;
(void)parent_rate;
return 50000000;
}
static struct rt_clk_ops gem_clk_ops =
{
.recalc_rate = gem_clk_recalc_rate,
};
static struct rt_clk_cell *gem_cells[] =
{
&(struct rt_clk_cell)
{
.cell.name = "macb_pci_pclk",
.cell.ops = &gem_clk_ops,
},
&(struct rt_clk_cell)
{
.cell.name = "macb_pci_hclk",
.cell.ops = &gem_clk_ops,
},
};
static rt_err_t macb_pci_clk_init(void)
{
rt_err_t err;
static struct rt_clk_node *clk_np = RT_NULL;
if (clk_np)
{
return RT_EOK;
}
if (!(clk_np = rt_calloc(1, sizeof(*clk_np))))
{
return -RT_ENOMEM;
}
clk_np->cells = gem_cells;
clk_np->cells_nr = RT_ARRAY_SIZE(gem_cells);
if ((err = rt_clk_register(clk_np)))
{
rt_free(clk_np);
}
return err;
}
static rt_err_t macb_pci_probe(struct rt_pci_device *pdev)
{
rt_err_t err;
struct macb_eth *eth;
if ((err = macb_pci_clk_init()))
{
return err;
}
if (!(eth = rt_calloc(1, sizeof(*eth))))
{
return -RT_ENOMEM;
}
rt_pci_set_master(pdev);
eth->dev = &pdev->parent;
eth->regs = rt_pci_iomap(pdev, 0);
if (!eth->regs)
{
err = -RT_EIO;
goto _free_eth;
}
eth->irq = pdev->irq;
rt_pci_irq_unmask(pdev);
eth->pclk = rt_clk_get_by_name(eth->dev, "macb_pci_pclk");
if (rt_is_err_or_null(eth->pclk))
{
err = eth->pclk ? rt_ptr_err(eth->pclk) : -RT_EIO;
goto _unmap;
}
eth->hclk = rt_clk_get_by_name(eth->dev, "macb_pci_hclk");
if (rt_is_err_or_null(eth->hclk))
{
err = eth->hclk ? rt_ptr_err(eth->hclk) : -RT_EIO;
goto _put_pclk;
}
ethernet_random_addr(&eth->parent, eth->mac);
if ((err = macb_eth_hw_init(eth)))
{
goto _put_clks;
}
pdev->parent.user_data = eth;
if ((err = macb_eth_common_probe(eth)))
{
macb_eth_common_remove(eth);
pdev->parent.user_data = RT_NULL;
goto _put_clks;
}
return RT_EOK;
_put_clks:
rt_clk_put(eth->hclk);
_put_pclk:
rt_clk_put(eth->pclk);
_unmap:
rt_iounmap(eth->regs);
rt_pci_irq_mask(pdev);
_free_eth:
rt_free(eth);
return err;
}
static rt_err_t macb_pci_remove(struct rt_pci_device *pdev)
{
struct macb_eth *eth = pdev->parent.user_data;
macb_eth_common_remove(eth);
/* INTx is shared, don't mask all */
rt_hw_interrupt_umask(pdev->irq);
rt_pci_irq_mask(pdev);
rt_pci_clear_master(pdev);
rt_iounmap(eth->regs);
rt_clk_put(eth->hclk);
rt_clk_put(eth->pclk);
rt_free(eth);
return RT_EOK;
}
static const struct rt_pci_device_id macb_pci_ids[] =
{
{ RT_PCI_DEVICE_ID(PCI_VENDOR_ID_CDNS, 0xe007), },
{ /* sentinel */ }
};
static struct rt_pci_driver macb_pci_driver =
{
.name = "macb-pci",
.ids = macb_pci_ids,
.probe = macb_pci_probe,
.remove = macb_pci_remove,
};
RT_PCI_DRIVER_EXPORT(macb_pci_driver);
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,162 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-11-25 GuEe-GUI first version
*/
#include "macb.h"
#define DBG_TAG "eth.macb.ptp"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define NSEC_PER_SEC 1000000000L
#define GEM_PTP_MAX_FREQ 500000000
#define TSU_NSEC_MAX_VAL ((1 << 30) - 1)
rt_inline rt_uint32_t gem_readl(struct macb_eth *eth, rt_uint32_t off)
{
return *(volatile rt_uint32_t *)((rt_uint8_t *)eth->regs + off);
}
rt_inline void gem_writel(struct macb_eth *eth, rt_uint32_t off, rt_uint32_t val)
{
*(volatile rt_uint32_t *)((rt_uint8_t *)eth->regs + off) = val;
}
static rt_bool_t gem_tsu_present(struct macb_eth *eth)
{
rt_uint32_t dcfg5 = gem_readl(eth, GEM_DCFG5);
return (dcfg5 & (1u << GEM_DCFG5_TSU_SHIFT)) ? RT_TRUE : RT_FALSE;
}
static rt_err_t macb_ptp_adjfreq(struct rt_ptp_clock *ptp, rt_base_t freq)
{
rt_int64_t adj;
rt_uint32_t inc;
struct macb_eth *eth = raw_to_macb_ptp(ptp);
/* Base increment in GEM_TI (NSINCR field) */
inc = gem_readl(eth, GEM_TI) & 0xff;
if (inc == 0)
{
inc = 1;
}
adj = (rt_int64_t)inc * (rt_int64_t)freq / 1000000000LL;
if (adj > 0x7fffffff || adj < -0x80000000LL)
{
return -RT_EINVAL;
}
gem_writel(eth, GEM_TI, (inc + (rt_uint32_t)adj) & 0xff);
return RT_EOK;
}
static rt_err_t macb_ptp_adjtime(struct rt_ptp_clock *ptp, rt_int64_t delta)
{
rt_uint32_t adj;
struct macb_eth *eth = raw_to_macb_ptp(ptp);
if (delta < 0)
{
delta = -delta;
adj = RT_BIT(31) | (rt_uint32_t)(delta > TSU_NSEC_MAX_VAL ? TSU_NSEC_MAX_VAL : delta);
}
else
{
adj = (rt_uint32_t)(delta > TSU_NSEC_MAX_VAL ? TSU_NSEC_MAX_VAL : delta);
}
gem_writel(eth, GEM_TA, adj);
return RT_EOK;
}
static rt_err_t macb_ptp_gettime(struct rt_ptp_clock *ptp, struct rt_ptp_clock_time *ts)
{
rt_uint32_t tsh, tsl, tn;
struct macb_eth *eth = raw_to_macb_ptp(ptp);
tn = gem_readl(eth, GEM_TN);
tsl = gem_readl(eth, GEM_TSL);
tsh = gem_readl(eth, GEM_TSH) & 0xffffU;
ts->sec = ((rt_int64_t)tsh << 32) | tsl;
ts->nsec = (rt_int32_t)(tn & 0x3fffffffU);
return RT_EOK;
}
static rt_err_t macb_ptp_settime(struct rt_ptp_clock *ptp, const struct rt_ptp_clock_time *ts)
{
struct macb_eth *eth = raw_to_macb_ptp(ptp);
if (!ts || ts->nsec < 0 || ts->nsec >= NSEC_PER_SEC)
{
return -RT_EINVAL;
}
/* TN clear, TSH, TSL, then TN */
gem_writel(eth, GEM_TN, 0);
gem_writel(eth, GEM_TSH, (rt_uint32_t)((rt_uint64_t)ts->sec >> 32) & 0xffffU);
gem_writel(eth, GEM_TSL, (rt_uint32_t)ts->sec);
gem_writel(eth, GEM_TN, (rt_uint32_t)ts->nsec);
return RT_EOK;
}
static rt_err_t macb_ptp_enable(struct rt_ptp_clock *ptp, struct rt_ptp_clock_request *req, rt_bool_t on)
{
return RT_EOK;
}
static const struct rt_ptp_ops macb_gem_ptp_ops =
{
.adjfreq = macb_ptp_adjfreq,
.adjtime = macb_ptp_adjtime,
.gettime = macb_ptp_gettime,
.settime = macb_ptp_settime,
.enable = macb_ptp_enable,
};
rt_err_t macb_ptp_register(struct macb_eth *eth)
{
rt_err_t err;
struct rt_ptp_clock *ptp = &eth->ptp_parent;
if (!gem_tsu_present(eth))
{
return -RT_ENOSYS;
}
ptp->ops = &macb_gem_ptp_ops;
ptp->max_freq = GEM_PTP_MAX_FREQ;
if ((err = rt_ptp_clock_register(ptp)))
{
ptp->ops = RT_NULL;
return err;
}
return RT_EOK;
}
void macb_ptp_unregister(struct macb_eth *eth)
{
struct rt_ptp_clock *ptp = &eth->ptp_parent;
if (!ptp->ops)
{
return;
}
rt_ptp_clock_unregister(ptp);
ptp->ops = RT_NULL;
}
@@ -0,0 +1,11 @@
config RT_ETHERNET_DWMAC
bool "Synopsys DesignWare Ethernet MAC (GMAC4)"
depends on RT_USING_PHY_V2
depends on RT_USING_DMA
depends on RT_USING_PIN
default n
config RT_ETHERNET_DWMAC_PTP
bool "PTP supported"
depends on RT_ETHERNET_DWMAC
depends on RT_USING_PTP
@@ -0,0 +1,18 @@
from building import *
group = []
if not GetDepend(['RT_ETHERNET_DWMAC']):
Return('group')
cwd = GetCurrentDir()
CPPPATH = [cwd + '/../../include', cwd + '/../../phy']
src = ['dwmac.c', 'dwmac-platform.c']
if GetDepend(['RT_ETHERNET_DWMAC_PTP']):
src += ['dwmac_ptp.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-26 GuEe-GUI first version
*/
#include "dwmac-platform.h"
rt_err_t dwmac_platform_register(struct rt_platform_device *pdev,
const struct dwmac_config *config)
{
rt_err_t err;
struct rt_device *dev = &pdev->parent;
struct dwmac_eth *eth = rt_calloc(1, sizeof(*eth));
if (!eth)
{
return -RT_ENOMEM;
}
eth->dev = dev;
eth->config = config;
eth->irq = rt_dm_dev_get_irq(dev, 0);
eth->mac_base = rt_dm_dev_iomap(dev, 0);
if (!eth->mac_base)
{
err = -RT_EIO;
goto _fail;
}
#ifdef RT_USING_OFW
if (rt_ofw_get_mac_addr(dev->ofw_node, eth->mac))
#endif
{
ethernet_random_addr(&eth->parent, eth->mac);
}
dev->user_data = eth;
if ((err = dwmac_probe(eth)))
{
goto _fail_unmap;
}
return RT_EOK;
_fail_unmap:
dev->user_data = RT_NULL;
rt_iounmap(eth->mac_base);
_fail:
rt_free(eth);
return err;
}
rt_err_t dwmac_platform_remove(struct rt_platform_device *pdev)
{
struct dwmac_eth *eth = pdev->parent.user_data;
if (!eth)
{
return RT_EOK;
}
dwmac_remove(eth);
rt_iounmap(eth->mac_base);
rt_free(eth);
pdev->parent.user_data = RT_NULL;
return RT_EOK;
}
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-26 GuEe-GUI first version
*/
#ifndef __DWMAC_PLATFORM_H__
#define __DWMAC_PLATFORM_H__
#include "dwmac.h"
rt_err_t dwmac_platform_register(struct rt_platform_device *pdev,
const struct dwmac_config *config);
rt_err_t dwmac_platform_remove(struct rt_platform_device *pdev);
#endif /* __DWMAC_PLATFORM_H__ */
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-26 GuEe-GUI first version
*/
#ifndef __DWMAC_PTP_H__
#define __DWMAC_PTP_H__
#include "dwmac.h"
#define DWMAC_PTP_OFFSET 0xb00
#define PTP_TCR 0x00
#define PTP_SSIR 0x04
#define PTP_STSR 0x08
#define PTP_STNSR 0x0c
#define PTP_STSUR 0x10
#define PTP_STNSUR 0x14
#define PTP_TAR 0x18
#define PTP_ACR 0x40
#define PTP_ACR_ATSFC RT_BIT(0)
#define PTP_ACR_ATSEN0 RT_BIT(4)
#define PTP_ACR_ATSEN(i) RT_BIT(4 + (i))
#define MAC_PPS_CONTROL 0xb70
#define MAC_PPSx_TARGET_TIME_SEC(x) (0xb80 + ((x) * 0x10))
#define MAC_PPSx_TARGET_TIME_NSEC(x) (0xb84 + ((x) * 0x10))
#define MAC_PPSx_INTERVAL(x) (0xb88 + ((x) * 0x10))
#define MAC_PPSx_WIDTH(x) (0xb8c + ((x) * 0x10))
#define PPSEN0 RT_BIT(4)
#define TRGTBUSY0 RT_BIT(31)
#define PPS_MAXIDX(x) ((((x) + 1) * 8) - 1)
#define PPS_MINIDX(x) ((x) * 8)
#define PPSx_MASK(x) RT_GENMASK(PPS_MAXIDX(x), PPS_MINIDX(x))
#define PPSCMDx(x, val) (RT_GENMASK(PPS_MINIDX(x) + 3, PPS_MINIDX(x)) & \
((val) << PPS_MINIDX(x)))
#define TRGTMODSELx(x, val) (RT_GENMASK(PPS_MAXIDX(x) - 1, PPS_MAXIDX(x) - 2) & \
((val) << (PPS_MAXIDX(x) - 2)))
#define dwmac_reg_readl(eth, reg) HWREG32((rt_uint8_t *)(eth)->mac_base + (reg))
#define dwmac_reg_writel(eth, reg, val) HWREG32((rt_uint8_t *)(eth)->mac_base + (reg)) = (val)
#define GMAC_HW_FEATURE1 0x120
#define GMAC_HW_FEATURE2 0x124
#define GMAC_HW_FEAT_PPSOUTNUM_MASK RT_GENMASK(26, 24)
#define GMAC_HW_FEAT_AUXSNAPNUM_MASK RT_GENMASK(30, 28)
#define PTP_TCR_TSENA RT_BIT(0)
#define PTP_TCR_TSCFUPDT RT_BIT(1)
#define PTP_TCR_TSINIT RT_BIT(2)
#define PTP_TCR_TSUPDT RT_BIT(3)
#define PTP_TCR_TSADDREG RT_BIT(5)
#define PTP_TCR_TSCTRLSSR RT_BIT(9)
#define PTP_STNSUR_ADDSUB_SHIFT 31
#define PTP_DIGITAL_ROLLOVER_MODE 0x3b9aca00
#define PTP_SSIR_SSINC_MAX 0xff
#define GMAC4_PTP_SSIR_SSINC_SHIFT 16
#define DWMAC_PTP_HWTS_ACTIVE (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR)
#define dwmac_ptp_readl(eth, reg) HWREG32((rt_uint8_t *)(eth)->mac_base + DWMAC_PTP_OFFSET + (reg))
#define dwmac_ptp_writel(eth, reg, val) HWREG32((rt_uint8_t *)(eth)->mac_base + DWMAC_PTP_OFFSET + (reg)) = (val)
#ifdef RT_ETHERNET_DWMAC_PTP
#define raw_to_dwmac_ptp(raw) rt_container_of(raw, struct dwmac_eth, ptp_parent)
rt_err_t dwmac_ptp_hw_init(struct dwmac_eth *eth);
rt_err_t dwmac_ptp_register(struct dwmac_eth *eth);
void dwmac_ptp_unregister(struct dwmac_eth *eth);
#endif /* RT_ETHERNET_DWMAC_PTP */
#endif /* __DWMAC_PTP_H__ */