1 /*
2  * Copyright 2024 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_ETHERNET_ETH_NXP_IMX_NETC_PRIV_H_
8 #define ZEPHYR_DRIVERS_ETHERNET_ETH_NXP_IMX_NETC_PRIV_H_
9 
10 #include "fsl_netc_endpoint.h"
11 #include "fsl_msgintr.h"
12 
13 /* Buffer and descriptor alignment */
14 #define NETC_BD_ALIGN   128
15 #define NETC_BUFF_ALIGN 64
16 #define NETC_RX_RING_BUF_SIZE_ALIGN                                                                \
17 	SDK_SIZEALIGN(CONFIG_ETH_NXP_IMX_RX_RING_BUF_SIZE, NETC_BUFF_ALIGN)
18 
19 /* MSIX definitions */
20 #define NETC_TX_MSIX_ENTRY_IDX 0
21 #define NETC_RX_MSIX_ENTRY_IDX 1
22 #define NETC_MSIX_ENTRY_NUM    2
23 
24 #define NETC_MSIX_EVENTS_COUNT      NETC_MSIX_ENTRY_NUM
25 #define NETC_TX_INTR_MSG_DATA_START 0
26 #define NETC_RX_INTR_MSG_DATA_START 16
27 #define NETC_DRV_MAX_INST_SUPPORT   16
28 
29 /* MSGINTR */
30 #define NETC_MSGINTR_CHANNEL 0
31 
32 #if (CONFIG_ETH_NXP_IMX_MSGINTR == 1)
33 #define NETC_MSGINTR     MSGINTR1
34 #define NETC_MSGINTR_IRQ MSGINTR1_IRQn
35 #elif (CONFIG_ETH_NXP_IMX_MSGINTR == 2)
36 #define NETC_MSGINTR     MSGINTR2
37 #define NETC_MSGINTR_IRQ MSGINTR2_IRQn
38 #else
39 #error "Current CONFIG_ETH_NXP_IMX_MSGINTR not support"
40 #endif
41 
42 /* Timeout for various operations */
43 #define NETC_TIMEOUT K_MSEC(20)
44 
45 /* Helper macros to convert from Zephyr PHY speed to NETC speed/duplex types */
46 #define PHY_TO_NETC_SPEED(x)                                                                       \
47 	(PHY_LINK_IS_SPEED_1000M(x)                                                                \
48 		 ? kNETC_MiiSpeed1000M                                                             \
49 		 : (PHY_LINK_IS_SPEED_100M(x) ? kNETC_MiiSpeed100M : kNETC_MiiSpeed10M))
50 
51 #define PHY_TO_NETC_DUPLEX_MODE(x)                                                                 \
52 	(PHY_LINK_IS_FULL_DUPLEX(x) ? kNETC_MiiFullDuplex : kNETC_MiiHalfDuplex)
53 
54 /* Helper function to generate an Ethernet MAC address for a given ENETC instance */
55 #define FREESCALE_OUI_B0 0x00
56 #define FREESCALE_OUI_B1 0x04
57 #define FREESCALE_OUI_B2 0x9f
58 
59 #define _NETC_GENERATE_MAC_ADDRESS_RANDOM                                                          \
60 	gen_random_mac(mac_addr, FREESCALE_OUI_B0, FREESCALE_OUI_B1, FREESCALE_OUI_B2)
61 
62 #define _NETC_GENERATE_MAC_ADDRESS_UNIQUE(n)                                                       \
63 	do {                                                                                       \
64 		uint32_t id = 0x001100;                                                            \
65                                                                                                    \
66 		mac_addr[0] = FREESCALE_OUI_B0;                                                    \
67 		mac_addr[1] = FREESCALE_OUI_B1;                                                    \
68 		/* Set MAC address locally administered bit (LAA) */                               \
69 		mac_addr[2] = FREESCALE_OUI_B2 | 0x02;                                             \
70 		mac_addr[3] = (id >> 16) & 0xff;                                                   \
71 		mac_addr[4] = (id >> 8) & 0xff;                                                    \
72 		mac_addr[5] = (id + n) & 0xff;                                                     \
73 	} while (0)
74 
75 #define NETC_GENERATE_MAC_ADDRESS(n)                                                               \
76 	static void netc_eth##n##_generate_mac(uint8_t mac_addr[6])                                \
77 	{                                                                                          \
78 		COND_CODE_1(DT_INST_PROP(n, zephyr_random_mac_address),                            \
79 			    (_NETC_GENERATE_MAC_ADDRESS_RANDOM),                                   \
80 			    (COND_CODE_0(DT_INST_NODE_HAS_PROP(n, local_mac_address),              \
81 					 (_NETC_GENERATE_MAC_ADDRESS_UNIQUE(n)),                   \
82 					 (ARG_UNUSED(mac_addr)))));                      \
83 	}
84 
85 struct netc_eth_config {
86 	uint16_t si_idx;
87 	const struct device *phy_dev;
88 	void (*generate_mac)(uint8_t *mac_addr);
89 	void (*bdr_init)(netc_bdr_config_t *bdr_config, netc_rx_bdr_config_t *rx_bdr_config,
90 			 netc_tx_bdr_config_t *tx_bdr_config);
91 	const struct pinctrl_dev_config *pincfg;
92 	uint8_t tx_intr_msg_data;
93 	uint8_t rx_intr_msg_data;
94 };
95 
96 typedef uint8_t rx_buffer_t[NETC_RX_RING_BUF_SIZE_ALIGN];
97 
98 struct netc_eth_data {
99 	ep_handle_t handle;
100 	struct net_if *iface;
101 	uint8_t mac_addr[6];
102 	/* TX */
103 	struct k_mutex tx_mutex;
104 	netc_tx_frame_info_t tx_info;
105 	uint8_t *tx_buff;
106 	volatile bool tx_done;
107 	/* RX */
108 	struct k_sem rx_sem;
109 	struct k_thread rx_thread;
110 
111 	K_KERNEL_STACK_MEMBER(rx_thread_stack, CONFIG_ETH_NXP_IMX_RX_THREAD_STACK_SIZE);
112 	uint8_t *rx_frame;
113 };
114 
115 int netc_eth_init_common(const struct device *dev);
116 int netc_eth_tx(const struct device *dev, struct net_pkt *pkt);
117 enum ethernet_hw_caps netc_eth_get_capabilities(const struct device *dev);
118 int netc_eth_set_config(const struct device *dev, enum ethernet_config_type type,
119 			const struct ethernet_config *config);
120 
121 #endif /* ZEPHYR_DRIVERS_ETHERNET_ETH_NXP_IMX_NETC_PRIV_H_ */
122