@@ -27,7 +27,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
27
27
28
28
#include "eth.h"
29
29
30
- #define MAX_TX_FAILURE 100
30
+ #define MAX_TX_FAILURE K_MSEC( 100)
31
31
32
32
#define ETH_LITEX_SLOT_SIZE 0x0800
33
33
@@ -36,6 +36,7 @@ struct eth_litex_dev_data {
36
36
uint8_t mac_addr [6 ];
37
37
uint8_t txslot ;
38
38
struct k_mutex tx_mutex ;
39
+ struct k_sem sem_tx_ready ;
39
40
};
40
41
41
42
struct eth_litex_config {
@@ -64,12 +65,10 @@ static int eth_initialize(const struct device *dev)
64
65
struct eth_litex_dev_data * context = dev -> data ;
65
66
66
67
k_mutex_init (& context -> tx_mutex );
68
+ k_sem_init (& context -> sem_tx_ready , 1 , 1 );
67
69
68
70
config -> config_func (dev );
69
71
70
- /* TX event is disabled because it isn't used by this driver */
71
- litex_write8 (0 , config -> tx_ev_enable_addr );
72
-
73
72
if (config -> random_mac_address ) {
74
73
/* generate random MAC address */
75
74
gen_random_mac (context -> mac_addr , 0x10 , 0xe2 , 0xd5 );
@@ -83,7 +82,7 @@ static int eth_tx(const struct device *dev, struct net_pkt *pkt)
83
82
uint16_t len ;
84
83
struct eth_litex_dev_data * context = dev -> data ;
85
84
const struct eth_litex_config * config = dev -> config ;
86
- int attempts = 0 ;
85
+ int ret ;
87
86
88
87
k_mutex_lock (& context -> tx_mutex , K_FOREVER );
89
88
@@ -97,13 +96,10 @@ static int eth_tx(const struct device *dev, struct net_pkt *pkt)
97
96
litex_write16 (len , config -> tx_length_addr );
98
97
99
98
/* wait for the device to be ready to transmit */
100
- while (litex_read8 (config -> tx_ready_addr ) == 0 ) {
101
- if (attempts ++ == MAX_TX_FAILURE ) {
102
- goto error ;
103
- }
104
- k_sleep (K_MSEC (1 ));
105
- }
106
-
99
+ ret = k_sem_take (& context -> sem_tx_ready , MAX_TX_FAILURE );
100
+ if (ret < 0 ) {
101
+ goto error ;
102
+ };
107
103
/* start transmitting */
108
104
litex_write8 (1 , config -> tx_start_addr );
109
105
@@ -166,12 +162,13 @@ static void eth_rx(const struct device *port)
166
162
167
163
static void eth_irq_handler (const struct device * port )
168
164
{
165
+ struct eth_litex_dev_data * context = port -> data ;
169
166
const struct eth_litex_config * config = port -> config ;
170
167
/* check sram reader events (tx) */
171
168
if (litex_read8 (config -> tx_ev_pending_addr ) & BIT (0 )) {
172
- /* TX event is not enabled nor used by this driver; ack just
173
- * in case if some rogue TX event appeared
174
- */
169
+ k_sem_give ( & context -> sem_tx_ready );
170
+
171
+ /* ack reader irq */
175
172
litex_write8 (BIT (0 ), config -> tx_ev_pending_addr );
176
173
}
177
174
@@ -205,8 +202,14 @@ static int eth_set_config(const struct device *dev, enum ethernet_config_type ty
205
202
206
203
static int eth_start (const struct device * dev )
207
204
{
205
+ struct eth_litex_dev_data * context = dev -> data ;
208
206
const struct eth_litex_config * config = dev -> config ;
209
207
208
+ if (litex_read8 (config -> tx_ready_addr )) {
209
+ k_sem_give (& context -> sem_tx_ready );
210
+ }
211
+
212
+ litex_write8 (1 , config -> tx_ev_enable_addr );
210
213
litex_write8 (1 , config -> rx_ev_enable_addr );
211
214
212
215
litex_write8 (BIT (0 ), config -> tx_ev_pending_addr );
@@ -219,6 +222,7 @@ static int eth_stop(const struct device *dev)
219
222
{
220
223
const struct eth_litex_config * config = dev -> config ;
221
224
225
+ litex_write8 (0 , config -> tx_ev_enable_addr );
222
226
litex_write8 (0 , config -> rx_ev_enable_addr );
223
227
224
228
return 0 ;
0 commit comments