1 /*
2 * Copyright 2022-2023 NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nxp_s32_netc_vsi
8
9 #define LOG_LEVEL CONFIG_ETHERNET_LOG_LEVEL
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(nxp_s32_eth_vsi);
12
13 #include <zephyr/kernel.h>
14 #include <zephyr/device.h>
15 #include <zephyr/drivers/mbox.h>
16 #include <zephyr/drivers/pinctrl.h>
17 #include <zephyr/net/ethernet.h>
18 #include <zephyr/net/net_if.h>
19 #include <zephyr/net/net_pkt.h>
20 #include <zephyr/net/phy.h>
21 #include <ethernet/eth_stats.h>
22
23 #include <soc.h>
24 #include <Netc_Eth_Ip.h>
25 #include <Netc_Eth_Ip_Irq.h>
26 #include <Netc_EthSwt_Ip.h>
27
28 #include "eth.h"
29 #include "eth_nxp_s32_netc_priv.h"
30
31 #define TX_RING_IDX 0
32 #define RX_RING_IDX 0
33
nxp_s32_eth_iface_init(struct net_if * iface)34 static void nxp_s32_eth_iface_init(struct net_if *iface)
35 {
36 const struct device *dev = net_if_get_device(iface);
37 struct nxp_s32_eth_data *ctx = dev->data;
38 const struct nxp_s32_eth_config *cfg = dev->config;
39 const struct nxp_s32_eth_msix *msix;
40 #if defined(CONFIG_NET_IPV6)
41 static struct net_if_mcast_monitor mon;
42
43 net_if_mcast_mon_register(&mon, iface, nxp_s32_eth_mcast_cb);
44 #endif /* CONFIG_NET_IPV6 */
45
46 /*
47 * For VLAN, this value is only used to get the correct L2 driver.
48 * The iface pointer in context should contain the main interface
49 * if the VLANs are enabled.
50 */
51 if (ctx->iface == NULL) {
52 ctx->iface = iface;
53 }
54
55 Netc_Eth_Ip_SetMacAddr(cfg->si_idx, (const uint8_t *)ctx->mac_addr);
56 net_if_set_link_addr(iface, ctx->mac_addr, sizeof(ctx->mac_addr), NET_LINK_ETHERNET);
57
58 LOG_INF("SI%d MAC: %02x:%02x:%02x:%02x:%02x:%02x", cfg->si_idx,
59 ctx->mac_addr[0], ctx->mac_addr[1], ctx->mac_addr[2],
60 ctx->mac_addr[3], ctx->mac_addr[4], ctx->mac_addr[5]);
61
62 ethernet_init(iface);
63
64 /* Assumes PSI is already started and link is up, iface will auto-start after init */
65 net_eth_carrier_on(iface);
66
67 for (int i = 0; i < NETC_MSIX_EVENTS_COUNT; i++) {
68 msix = &cfg->msix[i];
69 if (msix->mbox_channel.dev != NULL) {
70 if (mbox_set_enabled(&msix->mbox_channel, true)) {
71 LOG_ERR("Failed to enable MRU channel %u", msix->mbox_channel.id);
72 }
73 }
74 }
75 }
76
77 static const struct ethernet_api nxp_s32_eth_api = {
78 .iface_api.init = nxp_s32_eth_iface_init,
79 .get_capabilities = nxp_s32_eth_get_capabilities,
80 .set_config = nxp_s32_eth_set_config,
81 .send = nxp_s32_eth_tx
82 };
83
84 BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(nxp_s32_netc_vsi) == 1, "Only one VSI enabled supported");
85
86 #define NETC_VSI_INSTANCE_DEFINE(n) \
87 NETC_GENERATE_MAC_ADDRESS(n) \
88 \
89 void nxp_s32_eth_vsi##n##_rx_event(uint8_t chan, const uint32_t *buf, uint8_t buf_size) \
90 { \
91 Netc_Eth_Ip_MSIX_Rx(NETC_SI_NXP_S32_HW_INSTANCE(n)); \
92 } \
93 \
94 static void nxp_s32_eth##n##_rx_callback(const uint8_t unused, const uint8_t ring) \
95 { \
96 const struct device *dev = DEVICE_DT_INST_GET(n); \
97 const struct nxp_s32_eth_config *cfg = dev->config; \
98 struct nxp_s32_eth_data *ctx = dev->data; \
99 \
100 if (ring == cfg->rx_ring_idx) { \
101 k_sem_give(&ctx->rx_sem); \
102 } \
103 } \
104 \
105 static Netc_Eth_Ip_StateType nxp_s32_eth##n##_state; \
106 Netc_Eth_Ip_VsiToPsiMsgType nxp_s32_eth##n##_vsi2psi_msg \
107 __aligned(FEATURE_NETC_ETH_VSI_MSG_ALIGNMENT); \
108 static Netc_Eth_Ip_MACFilterHashTableEntryType \
109 nxp_s32_eth##n##_mac_filter_hash_table[CONFIG_ETH_NXP_S32_MAC_FILTER_TABLE_SIZE]; \
110 \
111 NETC_RX_RING(n, TX_RING_IDX, CONFIG_ETH_NXP_S32_RX_RING_LEN, \
112 CONFIG_ETH_NXP_S32_RX_RING_BUF_SIZE); \
113 NETC_TX_RING(n, RX_RING_IDX, CONFIG_ETH_NXP_S32_TX_RING_LEN, \
114 CONFIG_ETH_NXP_S32_TX_RING_BUF_SIZE); \
115 \
116 static const Netc_Eth_Ip_RxRingConfigType nxp_s32_eth##n##_rxring_cfg[1] = { \
117 { \
118 .RingDesc = nxp_s32_eth##n##_rxring0_desc, \
119 .Buffer = nxp_s32_eth##n##_rxring0_buf, \
120 .ringSize = CONFIG_ETH_NXP_S32_RX_RING_LEN, \
121 .maxRingSize = CONFIG_ETH_NXP_S32_RX_RING_LEN, \
122 .bufferLen = CONFIG_ETH_NXP_S32_TX_RING_BUF_SIZE, \
123 .maxBuffLen = CONFIG_ETH_NXP_S32_TX_RING_BUF_SIZE, \
124 .TimerThreshold = CONFIG_ETH_NXP_S32_RX_IRQ_TIMER_THRESHOLD, \
125 .PacketsThreshold = CONFIG_ETH_NXP_S32_RX_IRQ_PACKET_THRESHOLD, \
126 .Callback = nxp_s32_eth##n##_rx_callback, \
127 } \
128 }; \
129 static const Netc_Eth_Ip_TxRingConfigType nxp_s32_eth##n##_txring_cfg[1] = { \
130 { \
131 .RingDesc = nxp_s32_eth##n##_txring0_desc, \
132 .Buffer = nxp_s32_eth##n##_txring0_buf, \
133 .ringSize = CONFIG_ETH_NXP_S32_TX_RING_LEN, \
134 .maxRingSize = CONFIG_ETH_NXP_S32_TX_RING_LEN, \
135 .bufferLen = CONFIG_ETH_NXP_S32_TX_RING_BUF_SIZE, \
136 .maxBuffLen = CONFIG_ETH_NXP_S32_TX_RING_BUF_SIZE, \
137 } \
138 }; \
139 \
140 static const Netc_Eth_Ip_StationInterfaceConfigType nxp_s32_eth##n##_si_cfg = { \
141 .NumberOfRxBDR = 1, \
142 .NumberOfTxBDR = 1, \
143 .txMruMailboxAddr = NULL, \
144 .rxMruMailboxAddr = (uint32_t *)MRU_MBOX_ADDR(DT_DRV_INST(n), rx), \
145 .RxInterrupts = (uint32_t)true, \
146 .TxInterrupts = (uint32_t)false, \
147 .MACFilterTableMaxNumOfEntries = CONFIG_ETH_NXP_S32_MAC_FILTER_TABLE_SIZE, \
148 .VSItoPSIMsgCommand = &nxp_s32_eth##n##_vsi2psi_msg, \
149 }; \
150 \
151 static struct nxp_s32_eth_data nxp_s32_eth##n##_data = { \
152 .mac_addr = DT_INST_PROP_OR(n, local_mac_address, {0}), \
153 }; \
154 \
155 static const struct nxp_s32_eth_config nxp_s32_eth##n##_cfg = { \
156 .netc_cfg = { \
157 .SiType = NETC_ETH_IP_VIRTUAL_SI, \
158 .siConfig = &nxp_s32_eth##n##_si_cfg, \
159 .stateStructure = &nxp_s32_eth##n##_state, \
160 .paCtrlRxRingConfig = &nxp_s32_eth##n##_rxring_cfg, \
161 .paCtrlTxRingConfig = &nxp_s32_eth##n##_txring_cfg, \
162 }, \
163 .si_idx = NETC_SI_NXP_S32_HW_INSTANCE(n), \
164 .tx_ring_idx = TX_RING_IDX, \
165 .rx_ring_idx = RX_RING_IDX, \
166 .msix = { \
167 NETC_MSIX(DT_DRV_INST(n), rx, nxp_s32_eth_vsi##n##_rx_event), \
168 }, \
169 .mac_filter_hash_table = &nxp_s32_eth##n##_mac_filter_hash_table[0], \
170 .generate_mac = nxp_s32_eth##n##_generate_mac, \
171 }; \
172 \
173 ETH_NET_DEVICE_DT_INST_DEFINE(n, \
174 nxp_s32_eth_initialize_common, \
175 NULL, \
176 &nxp_s32_eth##n##_data, \
177 &nxp_s32_eth##n##_cfg, \
178 CONFIG_ETH_NXP_S32_VSI_INIT_PRIORITY, \
179 &nxp_s32_eth_api, \
180 NET_ETH_MTU);
181
182 DT_INST_FOREACH_STATUS_OKAY(NETC_VSI_INSTANCE_DEFINE)
183