From 6e7847499871ca518a66f26041b5ec93fa42c8f2 Mon Sep 17 00:00:00 2001 From: zhujiale <945386260@qq.com> Date: Sat, 14 Sep 2024 11:50:16 +0800 Subject: [PATCH] pci --- components/net/ofw/SConscript | 10 ++++ components/net/ofw/ofw.c | 102 ++++++++++++++++++++++++++++++++++ components/net/ofw/ofw.h | 48 ++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 components/net/ofw/SConscript create mode 100644 components/net/ofw/ofw.c create mode 100644 components/net/ofw/ofw.h diff --git a/components/net/ofw/SConscript b/components/net/ofw/SConscript new file mode 100644 index 0000000000..6142f31ab3 --- /dev/null +++ b/components/net/ofw/SConscript @@ -0,0 +1,10 @@ +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') + +CPPPATH = [cwd] + +group = DefineGroup('ofw', src, depend = ['RT_USING_OFW'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/net/ofw/ofw.c b/components/net/ofw/ofw.c new file mode 100644 index 0000000000..189695c7ea --- /dev/null +++ b/components/net/ofw/ofw.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2006-2024 RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-09-25 zhujiale the first version + */ +#include +#include "ofw.h" + +static const char* const rt_phy_modes[] = +{ + [RT_PHY_INTERFACE_MODE_NA] = "", + [RT_PHY_INTERFACE_MODE_INTERNAL] = "internal", + [RT_PHY_INTERFACE_MODE_MII] = "mii", + [RT_PHY_INTERFACE_MODE_GMII] = "gmii", + [RT_PHY_INTERFACE_MODE_SGMII] = "sgmii", + [RT_PHY_INTERFACE_MODE_TBI] = "tbi", + [RT_PHY_INTERFACE_MODE_REVMII] = "rev-mii", + [RT_PHY_INTERFACE_MODE_RMII] = "rmii", + [RT_PHY_INTERFACE_MODE_REVRMII] = "rev-rmii", + [RT_PHY_INTERFACE_MODE_RGMII] = "rgmii", + [RT_PHY_INTERFACE_MODE_RGMII_ID] = "rgmii-id", + [RT_PHY_INTERFACE_MODE_RGMII_RXID] = "rgmii-rxid", + [RT_PHY_INTERFACE_MODE_RGMII_TXID] = "rgmii-txid", + [RT_PHY_INTERFACE_MODE_RTBI] = "rtbi", + [RT_PHY_INTERFACE_MODE_SMII] = "smii", + [RT_PHY_INTERFACE_MODE_XGMII] = "xgmii", + [RT_PHY_INTERFACE_MODE_XLGMII] = "xlgmii", + [RT_PHY_INTERFACE_MODE_MOCA] = "moca", + [RT_PHY_INTERFACE_MODE_PSGMII] = "psgmii", + [RT_PHY_INTERFACE_MODE_QSGMII] = "qsgmii", + [RT_PHY_INTERFACE_MODE_TRGMII] = "trgmii", + [RT_PHY_INTERFACE_MODE_1000BASEX] = "1000base-x", + [RT_PHY_INTERFACE_MODE_1000BASEKX] = "1000base-kx", + [RT_PHY_INTERFACE_MODE_2500BASEX] = "2500base-x", + [RT_PHY_INTERFACE_MODE_5GBASER] = "5gbase-r", + [RT_PHY_INTERFACE_MODE_RXAUI] = "rxaui", + [RT_PHY_INTERFACE_MODE_XAUI] = "xaui", + [RT_PHY_INTERFACE_MODE_10GBASER] = "10gbase-r", + [RT_PHY_INTERFACE_MODE_25GBASER] = "25gbase-r", + [RT_PHY_INTERFACE_MODE_USXGMII] = "usxgmii", + [RT_PHY_INTERFACE_MODE_10GKR] = "10gbase-kr", + [RT_PHY_INTERFACE_MODE_100BASEX] = "100base-x", + [RT_PHY_INTERFACE_MODE_QUSGMII] = "qusgmii", + [RT_PHY_INTERFACE_MODE_MAX] = "", +}; + +static rt_err_t _get_interface_by_name(const char *name, rt_phy_interface *interface) +{ + for (int i = 0; i < RT_PHY_INTERFACE_MODE_MAX; i++) + { + if (!strcmp(name, rt_phy_modes[i])) + { + *interface = i; + return RT_EOK; + } + } + return -RT_ERROR; +} + +rt_err_t rt_ofw_get_interface(struct rt_ofw_node *np, rt_phy_interface *interface) +{ + const char *phy_mode = RT_NULL; + + if (rt_ofw_prop_read_string(np, "phy-mode", &phy_mode)) + rt_ofw_prop_read_string(np, "phy-connection-type", &phy_mode); + if (!phy_mode) + return -RT_ERROR; + + return _get_interface_by_name(phy_mode, interface); +} + +rt_err_t rt_ofw_get_mac_addr_by_name(struct rt_ofw_node *np, const char *name, rt_uint8_t *addr) +{ + rt_ssize_t len; + const void *p; + p = rt_ofw_prop_read_raw(np, name, &len); + if (p) + { + rt_memcpy(addr, p, len); + return RT_EOK; + } + + return -RT_ERROR; +} + +rt_err_t rt_ofw_get_mac_addr(struct rt_ofw_node *np, rt_uint8_t *addr) +{ + if (!rt_ofw_get_mac_addr_by_name(np, "mac-address", addr)) + return RT_EOK; + + if (!rt_ofw_get_mac_addr_by_name(np, "local-mac-address", addr)) + return RT_EOK; + + if (!rt_ofw_get_mac_addr_by_name(np, "address", addr)) + return RT_EOK; + + return -RT_ERROR; +} diff --git a/components/net/ofw/ofw.h b/components/net/ofw/ofw.h new file mode 100644 index 0000000000..b2af43e800 --- /dev/null +++ b/components/net/ofw/ofw.h @@ -0,0 +1,48 @@ +#ifndef __NET_OFW_H__ +#define __NET_OFW_H__ +#include +#include +typedef enum +{ + RT_PHY_INTERFACE_MODE_NA, + RT_PHY_INTERFACE_MODE_INTERNAL, + RT_PHY_INTERFACE_MODE_MII, + RT_PHY_INTERFACE_MODE_GMII, + RT_PHY_INTERFACE_MODE_SGMII, + RT_PHY_INTERFACE_MODE_TBI, + RT_PHY_INTERFACE_MODE_REVMII, + RT_PHY_INTERFACE_MODE_RMII, + RT_PHY_INTERFACE_MODE_REVRMII, + RT_PHY_INTERFACE_MODE_RGMII, + RT_PHY_INTERFACE_MODE_RGMII_ID, + RT_PHY_INTERFACE_MODE_RGMII_RXID, + RT_PHY_INTERFACE_MODE_RGMII_TXID, + RT_PHY_INTERFACE_MODE_RTBI, + RT_PHY_INTERFACE_MODE_SMII, + RT_PHY_INTERFACE_MODE_XGMII, + RT_PHY_INTERFACE_MODE_XLGMII, + RT_PHY_INTERFACE_MODE_MOCA, + RT_PHY_INTERFACE_MODE_PSGMII, + RT_PHY_INTERFACE_MODE_QSGMII, + RT_PHY_INTERFACE_MODE_TRGMII, + RT_PHY_INTERFACE_MODE_100BASEX, + RT_PHY_INTERFACE_MODE_1000BASEX, + RT_PHY_INTERFACE_MODE_2500BASEX, + RT_PHY_INTERFACE_MODE_5GBASER, + RT_PHY_INTERFACE_MODE_RXAUI, + RT_PHY_INTERFACE_MODE_XAUI, + /* 10GBASE-R, XFI, SFI - single lane 10G Serdes */ + RT_PHY_INTERFACE_MODE_10GBASER, + RT_PHY_INTERFACE_MODE_25GBASER, + RT_PHY_INTERFACE_MODE_USXGMII, + /* 10GBASE-KR - with Clause 73 AN */ + RT_PHY_INTERFACE_MODE_10GKR, + RT_PHY_INTERFACE_MODE_QUSGMII, + RT_PHY_INTERFACE_MODE_1000BASEKX, + RT_PHY_INTERFACE_MODE_MAX, +} rt_phy_interface; + +rt_err_t rt_ofw_get_mac_addr(struct rt_ofw_node *np, rt_uint8_t *addr); +rt_err_t rt_ofw_get_mac_addr_by_name(struct rt_ofw_node *np, const char *name, rt_uint8_t *addr); +rt_err_t rt_ofw_get_interface(struct rt_ofw_node *np, rt_phy_interface *interface); +#endif