From 8959b838bf15118dadc12c21e3d4ca5a92039c77 Mon Sep 17 00:00:00 2001 From: Aymeric Aillet Date: Thu, 27 Apr 2023 15:59:31 +0200 Subject: [PATCH] drivers: can: Fix R-Car driver can_stop behavior Calling for can_stop() method was not emptying FIFOs and tx_msgq. Resetting TX FIFO to empty it. Emptying tx_msgq with "ENETDOWN" return code. Fixes #50546 Signed-off-by: Aymeric Aillet --- drivers/can/can_rcar.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/can/can_rcar.c b/drivers/can/can_rcar.c index c12d4a8f48..7e108ebd42 100644 --- a/drivers/can/can_rcar.c +++ b/drivers/can/can_rcar.c @@ -217,7 +217,7 @@ static inline void can_rcar_write16(const struct can_rcar_cfg *config, sys_write16(value, config->reg_addr + offs); } -static void can_rcar_tx_done(const struct device *dev) +static void can_rcar_tx_done(const struct device *dev, uint8_t err) { struct can_rcar_data *data = dev->data; struct can_rcar_tx_cb *tx_cb; @@ -229,7 +229,7 @@ static void can_rcar_tx_done(const struct device *dev) } data->tx_unsent--; - tx_cb->cb(dev, 0, tx_cb->cb_arg); + tx_cb->cb(dev, err, tx_cb->cb_arg); k_sem_give(&data->tx_sem); } @@ -453,7 +453,7 @@ static void can_rcar_isr(const struct device *dev) if (data->tx_unsent <= unsent) { break; } - can_rcar_tx_done(dev); + can_rcar_tx_done(dev, 0); } /* Clear the Tx interrupt */ @@ -642,6 +642,15 @@ static int can_rcar_stop(const struct device *dev) } } + /* Resetting TX FIFO, emptying it */ + sys_write8((uint8_t)~RCAR_CAN_TFCR_TFE, config->reg_addr + RCAR_CAN_TFCR); + sys_write8(RCAR_CAN_TFCR_TFE, config->reg_addr + RCAR_CAN_TFCR); + + /* Empty TX msgq, returning an error for each message */ + while (data->tx_unsent) { + can_rcar_tx_done(dev, -ENETDOWN); + } + return 0; }