1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! # FXMAC Ethernet Driver
//!
//! A `no_std` Rust driver for the FXMAC Ethernet controller found on the PhytiumPi (Phytium Pi) board.
//! This driver supports DMA-based packet transmission and reception, providing a foundation for
//! network communication in embedded and bare-metal environments.
//!
//! ## Features
//!
//! - **DMA Support**: Efficient packet transmission and reception using DMA buffer descriptors.
//! - **PHY Management**: Support for PHY initialization, auto-negotiation, and manual speed configuration.
//! - **Interrupt Handling**: Built-in interrupt handlers for TX/RX completion and error conditions.
//! - **Multiple PHY Interfaces**: Support for SGMII, RGMII, RMII, XGMII, and other interface modes.
//! - **Configurable**: Supports jumbo frames, multicast filtering, and various MAC options.
//!
//! ## Target Platform
//!
//! This driver is designed for the aarch64 architecture, specifically targeting the PhytiumPi board
//! with the Motorcomm YT8521 PHY.
//!
//! ## Quick Start
//!
//! To use this driver, you need to implement the [`KernelFunc`] trait to provide the necessary
//! kernel functions for address translation and DMA memory allocation.
//!
//! ```ignore
//! use fxmac_rs::{KernelFunc, xmac_init, FXmacLwipPortTx, FXmacRecvHandler};
//!
//! // Implement the KernelFunc trait for your platform
//! pub struct FXmacDriver;
//!
//! #[ax_crate_interface::impl_interface]
//! impl KernelFunc for FXmacDriver {
//! fn virt_to_phys(addr: usize) -> usize {
//! // Your implementation
//! addr
//! }
//!
//! fn phys_to_virt(addr: usize) -> usize {
//! // Your implementation
//! addr
//! }
//!
//! fn dma_alloc_coherent(pages: usize) -> (usize, usize) {
//! // Your implementation: returns (virtual_addr, physical_addr)
//! unimplemented!()
//! }
//!
//! fn dma_free_coherent(vaddr: usize, pages: usize) {
//! // Your implementation
//! }
//!
//! fn dma_request_irq(irq: usize, handler: fn()) {
//! // Your implementation
//! }
//! }
//!
//! // Initialize the driver
//! let hwaddr: [u8; 6] = [0x55, 0x44, 0x33, 0x22, 0x11, 0x00];
//! let fxmac = xmac_init(&hwaddr);
//!
//! // Send packets
//! let mut tx_vec = Vec::new();
//! tx_vec.push(packet_data.to_vec());
//! FXmacLwipPortTx(fxmac, tx_vec);
//!
//! // Receive packets
//! if let Some(recv_packets) = FXmacRecvHandler(fxmac) {
//! for packet in recv_packets {
//! // Process received packet
//! }
//! }
//! ```
//!
//! ## Module Structure
//!
//! - [`fxmac`]: Core MAC controller functionality and configuration.
//! - [`fxmac_dma`]: DMA buffer descriptor management and packet handling.
//! - [`fxmac_intr`]: Interrupt handling and callback management.
//! - [`fxmac_phy`]: PHY initialization and management functions.
//!
//! ## Safety and Environment
//!
//! - This crate targets `no_std` and assumes the platform provides DMA-coherent
//! memory and interrupt routing.
//! - Most APIs interact with memory-mapped registers and should be used with
//! care in the correct execution context.
//!
//! ## Feature Flags
//!
//! - `debug`: Enable logging via the `log` crate. Without this feature, logging
//! macros become no-ops.
extern crate alloc;
extern crate log;
// mod mii_const;
// Re-exports for core MAC functionality
pub use *;
// Re-exports for DMA operations
pub use *;
// Re-exports for interrupt handling
pub use ;
// Re-exports for PHY interface
pub use ;
/// Kernel function interface required by the FXMAC Ethernet driver.
///
/// This trait defines the platform-specific functions that must be implemented
/// by the host system to support the FXMAC driver. These functions handle
/// address translation, DMA memory management, and interrupt registration.
///
/// # Implementation Requirements
///
/// All implementations must be `#[ax_crate_interface::impl_interface]` compatible
/// and provide thread-safe operations where applicable.
///
/// # Example
///
/// ```ignore
/// pub struct MyPlatform;
///
/// #[ax_crate_interface::impl_interface]
/// impl fxmac_rs::KernelFunc for MyPlatform {
/// fn virt_to_phys(addr: usize) -> usize {
/// // Platform-specific virtual to physical address translation
/// addr - KERNEL_OFFSET
/// }
///
/// fn phys_to_virt(addr: usize) -> usize {
/// // Platform-specific physical to virtual address translation
/// addr + KERNEL_OFFSET
/// }
///
/// fn dma_alloc_coherent(pages: usize) -> (usize, usize) {
/// // Allocate DMA-capable coherent memory
/// // Returns (virtual_address, physical_address)
/// allocator.alloc_dma(pages)
/// }
///
/// fn dma_free_coherent(vaddr: usize, pages: usize) {
/// // Free previously allocated DMA memory
/// allocator.free_dma(vaddr, pages)
/// }
///
/// fn dma_request_irq(irq: usize, handler: fn()) {
/// // Register interrupt handler for the specified IRQ
/// interrupt_controller.register(irq, handler)
/// }
/// }
/// ```