1 /*
2 * Copyright 2024 NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nxp_imx_netc_psi
8
9 #define LOG_LEVEL CONFIG_ETHERNET_LOG_LEVEL
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(nxp_imx_eth_psi);
12
13 #include <zephyr/kernel.h>
14 #include <zephyr/device.h>
15 #include <zephyr/drivers/pinctrl.h>
16 #include <zephyr/net/ethernet.h>
17 #include <zephyr/net/net_if.h>
18 #include <zephyr/net/net_pkt.h>
19 #include <zephyr/net/phy.h>
20 #include <ethernet/eth_stats.h>
21
22 #include "eth.h"
23 #include "eth_nxp_imx_netc_priv.h"
24
netc_eth_phylink_callback(const struct device * pdev,struct phy_link_state * state,void * user_data)25 static void netc_eth_phylink_callback(const struct device *pdev, struct phy_link_state *state,
26 void *user_data)
27 {
28 const struct device *dev = (struct device *)user_data;
29 struct netc_eth_data *data = dev->data;
30 status_t result;
31
32 ARG_UNUSED(pdev);
33
34 if (state->is_up) {
35 LOG_DBG("Link up");
36 result = EP_Up(&data->handle, PHY_TO_NETC_SPEED(state->speed),
37 PHY_TO_NETC_DUPLEX_MODE(state->speed));
38 if (result != kStatus_Success) {
39 LOG_ERR("Failed to set MAC up");
40 }
41 net_eth_carrier_on(data->iface);
42 } else {
43 LOG_DBG("Link down");
44 result = EP_Down(&data->handle);
45 if (result != kStatus_Success) {
46 LOG_ERR("Failed to set MAC down");
47 }
48 net_eth_carrier_off(data->iface);
49 }
50 }
51
netc_eth_iface_init(struct net_if * iface)52 static void netc_eth_iface_init(struct net_if *iface)
53 {
54 const struct device *dev = net_if_get_device(iface);
55 struct netc_eth_data *data = dev->data;
56 const struct netc_eth_config *cfg = dev->config;
57 status_t result;
58
59 /*
60 * For VLAN, this value is only used to get the correct L2 driver.
61 * The iface pointer in context should contain the main interface
62 * if the VLANs are enabled.
63 */
64 if (data->iface == NULL) {
65 data->iface = iface;
66 }
67
68 /* Set MAC address */
69 result = EP_SetPrimaryMacAddr(&data->handle, (uint8_t *)data->mac_addr);
70 if (result != kStatus_Success) {
71 LOG_ERR("Failed to set MAC address");
72 }
73
74 net_if_set_link_addr(iface, data->mac_addr, sizeof(data->mac_addr), NET_LINK_ETHERNET);
75
76 LOG_INF("SI%d MAC: %02x:%02x:%02x:%02x:%02x:%02x", cfg->si_idx, data->mac_addr[0],
77 data->mac_addr[1], data->mac_addr[2], data->mac_addr[3], data->mac_addr[4],
78 data->mac_addr[5]);
79
80 ethernet_init(iface);
81
82 /*
83 * PSI controls the PHY. If PHY is configured either as fixed
84 * link or autoneg, the callback is executed at least once
85 * immediately after setting it.
86 */
87 if (!device_is_ready(cfg->phy_dev)) {
88 LOG_ERR("PHY device (%p) is not ready, cannot init iface", cfg->phy_dev);
89 return;
90 }
91 phy_link_callback_set(cfg->phy_dev, &netc_eth_phylink_callback, (void *)dev);
92
93 /* Do not start the interface until PHY link is up */
94 net_if_carrier_off(iface);
95 }
96
netc_eth_init(const struct device * dev)97 static int netc_eth_init(const struct device *dev)
98 {
99 const struct netc_eth_config *cfg = dev->config;
100 int err;
101
102 err = pinctrl_apply_state(cfg->pincfg, PINCTRL_STATE_DEFAULT);
103 if (err) {
104 return err;
105 }
106
107 return netc_eth_init_common(dev);
108 }
109
netc_eth_get_phy(const struct device * dev)110 static const struct device *netc_eth_get_phy(const struct device *dev)
111 {
112 const struct netc_eth_config *cfg = dev->config;
113
114 return cfg->phy_dev;
115 }
116
117 static const struct ethernet_api netc_eth_api = {.iface_api.init = netc_eth_iface_init,
118 .get_capabilities = netc_eth_get_capabilities,
119 .get_phy = netc_eth_get_phy,
120 .set_config = netc_eth_set_config,
121 .send = netc_eth_tx};
122
123 #define NETC_PSI_INSTANCE_DEFINE(n) \
124 PINCTRL_DT_INST_DEFINE(n); \
125 NETC_GENERATE_MAC_ADDRESS(n) \
126 AT_NONCACHEABLE_SECTION_ALIGN( \
127 static uint8_t eth##n##_tx_buff[CONFIG_ETH_NXP_IMX_TX_RING_BUF_SIZE], \
128 NETC_BUFF_ALIGN); \
129 AT_NONCACHEABLE_SECTION_ALIGN( \
130 static netc_tx_bd_t eth##n##_txbd_array[CONFIG_ETH_NXP_IMX_TX_RING_NUM] \
131 [CONFIG_ETH_NXP_IMX_TX_RING_LEN], \
132 NETC_BD_ALIGN); \
133 static netc_tx_frame_info_t eth##n##_txdirty_array[CONFIG_ETH_NXP_IMX_TX_RING_NUM] \
134 [CONFIG_ETH_NXP_IMX_TX_RING_LEN]; \
135 AT_NONCACHEABLE_SECTION_ALIGN( \
136 static rx_buffer_t eth##n##_rx_buff[CONFIG_ETH_NXP_IMX_RX_RING_NUM] \
137 [CONFIG_ETH_NXP_IMX_RX_RING_LEN], \
138 NETC_BUFF_ALIGN); \
139 static uint64_t eth##n##_rx_buff_addr_array[CONFIG_ETH_NXP_IMX_RX_RING_NUM] \
140 [CONFIG_ETH_NXP_IMX_RX_RING_LEN]; \
141 AT_NONCACHEABLE_SECTION(static uint8_t eth##n##_rx_frame[NETC_RX_RING_BUF_SIZE_ALIGN]); \
142 AT_NONCACHEABLE_SECTION_ALIGN( \
143 static netc_rx_bd_t eth##n##_rxbd_array[CONFIG_ETH_NXP_IMX_RX_RING_NUM] \
144 [CONFIG_ETH_NXP_IMX_RX_RING_LEN], \
145 NETC_BD_ALIGN); \
146 static void netc_eth##n##_bdr_init(netc_bdr_config_t *bdr_config, \
147 netc_rx_bdr_config_t *rx_bdr_config, \
148 netc_tx_bdr_config_t *tx_bdr_config) \
149 { \
150 for (uint8_t ring = 0U; ring < CONFIG_ETH_NXP_IMX_RX_RING_NUM; ring++) { \
151 for (uint8_t bd = 0U; bd < CONFIG_ETH_NXP_IMX_RX_RING_LEN; bd++) { \
152 eth##n##_rx_buff_addr_array[ring][bd] = \
153 (uint64_t)(uintptr_t)ð##n##_rx_buff[ring][bd]; \
154 } \
155 } \
156 memset(bdr_config, 0, sizeof(netc_bdr_config_t)); \
157 memset(rx_bdr_config, 0, sizeof(netc_rx_bdr_config_t)); \
158 memset(tx_bdr_config, 0, sizeof(netc_tx_bdr_config_t)); \
159 bdr_config->rxBdrConfig = rx_bdr_config; \
160 bdr_config->txBdrConfig = tx_bdr_config; \
161 bdr_config->rxBdrConfig[0].bdArray = ð##n##_rxbd_array[0][0]; \
162 bdr_config->rxBdrConfig[0].len = CONFIG_ETH_NXP_IMX_RX_RING_LEN; \
163 bdr_config->rxBdrConfig[0].buffAddrArray = ð##n##_rx_buff_addr_array[0][0]; \
164 bdr_config->rxBdrConfig[0].buffSize = NETC_RX_RING_BUF_SIZE_ALIGN; \
165 bdr_config->rxBdrConfig[0].msixEntryIdx = NETC_RX_MSIX_ENTRY_IDX; \
166 bdr_config->rxBdrConfig[0].extendDescEn = false; \
167 bdr_config->rxBdrConfig[0].enThresIntr = true; \
168 bdr_config->rxBdrConfig[0].enCoalIntr = true; \
169 bdr_config->rxBdrConfig[0].intrThreshold = 1; \
170 bdr_config->txBdrConfig[0].bdArray = ð##n##_txbd_array[0][0]; \
171 bdr_config->txBdrConfig[0].len = CONFIG_ETH_NXP_IMX_TX_RING_LEN; \
172 bdr_config->txBdrConfig[0].dirtyArray = ð##n##_txdirty_array[0][0]; \
173 bdr_config->txBdrConfig[0].msixEntryIdx = NETC_TX_MSIX_ENTRY_IDX; \
174 bdr_config->txBdrConfig[0].enIntr = true; \
175 } \
176 static struct netc_eth_data netc_eth##n##_data = { \
177 .mac_addr = DT_INST_PROP_OR(n, local_mac_address, {0}), \
178 .tx_buff = eth##n##_tx_buff, \
179 .rx_frame = eth##n##_rx_frame, \
180 }; \
181 static const struct netc_eth_config netc_eth##n##_config = { \
182 .generate_mac = netc_eth##n##_generate_mac, \
183 .bdr_init = netc_eth##n##_bdr_init, \
184 .phy_dev = DEVICE_DT_GET(DT_INST_PHANDLE(n, phy_handle)), \
185 .pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
186 .si_idx = (DT_INST_PROP(n, mac_index) << 8) | DT_INST_PROP(n, si_index), \
187 .tx_intr_msg_data = NETC_TX_INTR_MSG_DATA_START + n, \
188 .rx_intr_msg_data = NETC_RX_INTR_MSG_DATA_START + n, \
189 }; \
190 ETH_NET_DEVICE_DT_INST_DEFINE(n, netc_eth_init, NULL, &netc_eth##n##_data, \
191 &netc_eth##n##_config, CONFIG_ETH_INIT_PRIORITY, \
192 &netc_eth_api, NET_ETH_MTU);
193 DT_INST_FOREACH_STATUS_OKAY(NETC_PSI_INSTANCE_DEFINE)
194