Skip to content

Commit 86a52d9

Browse files
maass-hamburgkartben
authored andcommitted
drivers: ethernet: litex: use tx interrupt
use tx interrupt to check, if we can send something, instead of checking the register periodically. Signed-off-by: Fin Maaß <[email protected]>
1 parent e1d2f09 commit 86a52d9

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

drivers/ethernet/eth_litex_liteeth.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
2727

2828
#include "eth.h"
2929

30-
#define MAX_TX_FAILURE 100
30+
#define MAX_TX_FAILURE K_MSEC(100)
3131

3232
#define ETH_LITEX_SLOT_SIZE 0x0800
3333

@@ -36,6 +36,7 @@ struct eth_litex_dev_data {
3636
uint8_t mac_addr[6];
3737
uint8_t txslot;
3838
struct k_mutex tx_mutex;
39+
struct k_sem sem_tx_ready;
3940
};
4041

4142
struct eth_litex_config {
@@ -64,12 +65,10 @@ static int eth_initialize(const struct device *dev)
6465
struct eth_litex_dev_data *context = dev->data;
6566

6667
k_mutex_init(&context->tx_mutex);
68+
k_sem_init(&context->sem_tx_ready, 1, 1);
6769

6870
config->config_func(dev);
6971

70-
/* TX event is disabled because it isn't used by this driver */
71-
litex_write8(0, config->tx_ev_enable_addr);
72-
7372
if (config->random_mac_address) {
7473
/* generate random MAC address */
7574
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)
8382
uint16_t len;
8483
struct eth_litex_dev_data *context = dev->data;
8584
const struct eth_litex_config *config = dev->config;
86-
int attempts = 0;
85+
int ret;
8786

8887
k_mutex_lock(&context->tx_mutex, K_FOREVER);
8988

@@ -97,13 +96,10 @@ static int eth_tx(const struct device *dev, struct net_pkt *pkt)
9796
litex_write16(len, config->tx_length_addr);
9897

9998
/* 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+
};
107103
/* start transmitting */
108104
litex_write8(1, config->tx_start_addr);
109105

@@ -166,12 +162,13 @@ static void eth_rx(const struct device *port)
166162

167163
static void eth_irq_handler(const struct device *port)
168164
{
165+
struct eth_litex_dev_data *context = port->data;
169166
const struct eth_litex_config *config = port->config;
170167
/* check sram reader events (tx) */
171168
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 */
175172
litex_write8(BIT(0), config->tx_ev_pending_addr);
176173
}
177174

@@ -205,8 +202,14 @@ static int eth_set_config(const struct device *dev, enum ethernet_config_type ty
205202

206203
static int eth_start(const struct device *dev)
207204
{
205+
struct eth_litex_dev_data *context = dev->data;
208206
const struct eth_litex_config *config = dev->config;
209207

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);
210213
litex_write8(1, config->rx_ev_enable_addr);
211214

212215
litex_write8(BIT(0), config->tx_ev_pending_addr);
@@ -219,6 +222,7 @@ static int eth_stop(const struct device *dev)
219222
{
220223
const struct eth_litex_config *config = dev->config;
221224

225+
litex_write8(0, config->tx_ev_enable_addr);
222226
litex_write8(0, config->rx_ev_enable_addr);
223227

224228
return 0;

0 commit comments

Comments
 (0)