|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2024 RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2024-09-25 zhujiale the first version |
| 9 | + */ |
| 10 | +#include <rtthread.h> |
| 11 | +#include "ofw.h" |
| 12 | + |
| 13 | +static const char* const rt_phy_modes[] = |
| 14 | +{ |
| 15 | + [RT_PHY_INTERFACE_MODE_NA] = "", |
| 16 | + [RT_PHY_INTERFACE_MODE_INTERNAL] = "internal", |
| 17 | + [RT_PHY_INTERFACE_MODE_MII] = "mii", |
| 18 | + [RT_PHY_INTERFACE_MODE_GMII] = "gmii", |
| 19 | + [RT_PHY_INTERFACE_MODE_SGMII] = "sgmii", |
| 20 | + [RT_PHY_INTERFACE_MODE_TBI] = "tbi", |
| 21 | + [RT_PHY_INTERFACE_MODE_REVMII] = "rev-mii", |
| 22 | + [RT_PHY_INTERFACE_MODE_RMII] = "rmii", |
| 23 | + [RT_PHY_INTERFACE_MODE_REVRMII] = "rev-rmii", |
| 24 | + [RT_PHY_INTERFACE_MODE_RGMII] = "rgmii", |
| 25 | + [RT_PHY_INTERFACE_MODE_RGMII_ID] = "rgmii-id", |
| 26 | + [RT_PHY_INTERFACE_MODE_RGMII_RXID] = "rgmii-rxid", |
| 27 | + [RT_PHY_INTERFACE_MODE_RGMII_TXID] = "rgmii-txid", |
| 28 | + [RT_PHY_INTERFACE_MODE_RTBI] = "rtbi", |
| 29 | + [RT_PHY_INTERFACE_MODE_SMII] = "smii", |
| 30 | + [RT_PHY_INTERFACE_MODE_XGMII] = "xgmii", |
| 31 | + [RT_PHY_INTERFACE_MODE_XLGMII] = "xlgmii", |
| 32 | + [RT_PHY_INTERFACE_MODE_MOCA] = "moca", |
| 33 | + [RT_PHY_INTERFACE_MODE_PSGMII] = "psgmii", |
| 34 | + [RT_PHY_INTERFACE_MODE_QSGMII] = "qsgmii", |
| 35 | + [RT_PHY_INTERFACE_MODE_TRGMII] = "trgmii", |
| 36 | + [RT_PHY_INTERFACE_MODE_1000BASEX] = "1000base-x", |
| 37 | + [RT_PHY_INTERFACE_MODE_1000BASEKX] = "1000base-kx", |
| 38 | + [RT_PHY_INTERFACE_MODE_2500BASEX] = "2500base-x", |
| 39 | + [RT_PHY_INTERFACE_MODE_5GBASER] = "5gbase-r", |
| 40 | + [RT_PHY_INTERFACE_MODE_RXAUI] = "rxaui", |
| 41 | + [RT_PHY_INTERFACE_MODE_XAUI] = "xaui", |
| 42 | + [RT_PHY_INTERFACE_MODE_10GBASER] = "10gbase-r", |
| 43 | + [RT_PHY_INTERFACE_MODE_25GBASER] = "25gbase-r", |
| 44 | + [RT_PHY_INTERFACE_MODE_USXGMII] = "usxgmii", |
| 45 | + [RT_PHY_INTERFACE_MODE_10GKR] = "10gbase-kr", |
| 46 | + [RT_PHY_INTERFACE_MODE_100BASEX] = "100base-x", |
| 47 | + [RT_PHY_INTERFACE_MODE_QUSGMII] = "qusgmii", |
| 48 | + [RT_PHY_INTERFACE_MODE_MAX] = "", |
| 49 | +}; |
| 50 | + |
| 51 | +static rt_err_t _get_interface_by_name(const char *name, rt_phy_interface *interface) |
| 52 | +{ |
| 53 | + for (int i = 0; i < RT_PHY_INTERFACE_MODE_MAX; i++) |
| 54 | + { |
| 55 | + if (!strcmp(name, rt_phy_modes[i])) |
| 56 | + { |
| 57 | + *interface = i; |
| 58 | + return RT_EOK; |
| 59 | + } |
| 60 | + } |
| 61 | + return -RT_ERROR; |
| 62 | +} |
| 63 | + |
| 64 | +rt_err_t rt_ofw_get_interface(struct rt_ofw_node *np, rt_phy_interface *interface) |
| 65 | +{ |
| 66 | + const char *phy_mode = RT_NULL; |
| 67 | + |
| 68 | + if (rt_ofw_prop_read_string(np, "phy-mode", &phy_mode)) |
| 69 | + rt_ofw_prop_read_string(np, "phy-connection-type", &phy_mode); |
| 70 | + if (!phy_mode) |
| 71 | + return -RT_ERROR; |
| 72 | + |
| 73 | + return _get_interface_by_name(phy_mode, interface); |
| 74 | +} |
| 75 | + |
| 76 | +rt_err_t rt_ofw_get_mac_addr_by_name(struct rt_ofw_node *np, const char *name, rt_uint8_t *addr) |
| 77 | +{ |
| 78 | + rt_ssize_t len; |
| 79 | + const void *p; |
| 80 | + p = rt_ofw_prop_read_raw(np, name, &len); |
| 81 | + if (p) |
| 82 | + { |
| 83 | + rt_memcpy(addr, p, len); |
| 84 | + return RT_EOK; |
| 85 | + } |
| 86 | + |
| 87 | + return -RT_ERROR; |
| 88 | +} |
| 89 | + |
| 90 | +rt_err_t rt_ofw_get_mac_addr(struct rt_ofw_node *np, rt_uint8_t *addr) |
| 91 | +{ |
| 92 | + if (!rt_ofw_get_mac_addr_by_name(np, "mac-address", addr)) |
| 93 | + return RT_EOK; |
| 94 | + |
| 95 | + if (!rt_ofw_get_mac_addr_by_name(np, "local-mac-address", addr)) |
| 96 | + return RT_EOK; |
| 97 | + |
| 98 | + if (!rt_ofw_get_mac_addr_by_name(np, "address", addr)) |
| 99 | + return RT_EOK; |
| 100 | + |
| 101 | + return -RT_ERROR; |
| 102 | +} |
0 commit comments