Skip to content

Commit 5c7ce09

Browse files
ckhardincfriedt
authored andcommitted
drivers: ethernet: lan9250: check for errors during the init calls
During board bringup the spi interface was not working as expected and the lna9250 driver should have errored early when the chip was not detected and reset but ended up busy waiting on setup. So, this will error out on the init path when the chip can not be reset at the driver instance. Signed-off-by: Charles Hardin <ckhardin@gmail.com>
1 parent c102a8e commit 5c7ce09

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

drivers/ethernet/eth_lan9250.c

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,19 @@ static int lan9250_read_sys_reg(const struct device *dev, uint16_t address, uint
7575
static int lan9250_wait_ready(const struct device *dev, uint16_t address, uint32_t mask,
7676
uint32_t expected, uint32_t m_second)
7777
{
78+
int ret;
7879
uint32_t tmp;
79-
int wait_time = 0;
80+
k_timepoint_t end = sys_timepoint_calc(K_MSEC(m_second));
8081

8182
while (true) {
82-
lan9250_read_sys_reg(dev, address, &tmp);
83-
wait_time++;
84-
k_busy_wait(USEC_PER_MSEC * 1U);
85-
if ((tmp & mask) == expected) {
83+
ret = lan9250_read_sys_reg(dev, address, &tmp);
84+
if ((ret == 0) && ((tmp & mask) == expected)) {
8685
return 0;
87-
} else if (wait_time == m_second) {
88-
LOG_ERR("NOT READY");
86+
}
87+
if (sys_timepoint_expired(end)) {
8988
return -EIO;
9089
}
90+
k_busy_wait(USEC_PER_MSEC * 1U);
9191
}
9292
}
9393

@@ -128,18 +128,19 @@ static int lan9250_write_mac_reg(const struct device *dev, uint8_t address, uint
128128
static int lan9250_wait_mac_ready(const struct device *dev, uint8_t address, uint32_t mask,
129129
uint32_t expected, uint32_t m_second)
130130
{
131+
int ret;
131132
uint32_t tmp;
132-
int wait_time = 0;
133+
k_timepoint_t end = sys_timepoint_calc(K_MSEC(m_second));
133134

134135
while (true) {
135-
lan9250_read_mac_reg(dev, address, &tmp);
136-
wait_time++;
137-
k_msleep(1);
138-
if ((tmp & mask) == expected) {
136+
ret = lan9250_read_mac_reg(dev, address, &tmp);
137+
if ((ret == 0) && ((tmp & mask) == expected)) {
139138
return 0;
140-
} else if (wait_time == m_second) {
139+
}
140+
if (sys_timepoint_expired(end)) {
141141
return -EIO;
142142
}
143+
k_msleep(1);
143144
}
144145
}
145146

@@ -236,15 +237,19 @@ static int lan9250_hw_cfg_check(const struct device *dev)
236237

237238
static int lan9250_sw_reset(const struct device *dev)
238239
{
239-
lan9250_write_sys_reg(dev, LAN9250_RESET_CTL,
240-
LAN9250_RESET_CTL_HMAC_RST | LAN9250_RESET_CTL_PHY_RST |
241-
LAN9250_RESET_CTL_DIGITAL_RST);
240+
int ret;
241+
242+
ret = lan9250_write_sys_reg(dev, LAN9250_RESET_CTL,
243+
LAN9250_RESET_CTL_HMAC_RST |
244+
LAN9250_RESET_CTL_PHY_RST |
245+
LAN9250_RESET_CTL_DIGITAL_RST);
246+
if (ret < 0) {
247+
return ret;
248+
}
242249

243250
/* Wait until LAN9250 SPI bus is ready */
244-
lan9250_wait_ready(dev, LAN9250_BYTE_TEST, BOTR_MASK, LAN9250_BYTE_TEST_DEFAULT,
245-
LAN9250_RESET_TIMEOUT);
246-
247-
return 0;
251+
return lan9250_wait_ready(dev, LAN9250_BYTE_TEST, BOTR_MASK,
252+
LAN9250_BYTE_TEST_DEFAULT, LAN9250_RESET_TIMEOUT);
248253
}
249254

250255
static int lan9250_configure(const struct device *dev)
@@ -655,6 +660,7 @@ static const struct ethernet_api api_funcs = {
655660

656661
static int lan9250_init(const struct device *dev)
657662
{
663+
int ret;
658664
const struct lan9250_config *config = dev->config;
659665
struct lan9250_runtime *context = dev->data;
660666

@@ -684,10 +690,12 @@ static int lan9250_init(const struct device *dev)
684690

685691
gpio_pin_interrupt_configure_dt(&config->interrupt, GPIO_INT_EDGE_TO_ACTIVE);
686692

687-
/* Wait until LAN9250 SPI bus is ready */
688-
lan9250_wait_ready(dev, LAN9250_BYTE_TEST, BOTR_MASK, LAN9250_BYTE_TEST_DEFAULT,
689-
LAN9250_RESET_TIMEOUT);
690-
lan9250_sw_reset(dev);
693+
/* Reset and wait for ready on the LAN9250 SPI device */
694+
ret = lan9250_sw_reset(dev);
695+
if (ret < 0) {
696+
LOG_ERR("Reset failed");
697+
return ret;
698+
}
691699
lan9250_configure(dev);
692700
lan9250_set_macaddr(dev);
693701

0 commit comments

Comments
 (0)