From fe632259c9f45399402a3d4e354f10aa74baa2da Mon Sep 17 00:00:00 2001 From: Hao Luo Date: Thu, 5 Jun 2025 09:39:10 +0800 Subject: [PATCH] drivers: uart: ambiq: optimize ambiq uart runtime pm This commit optimizes the runtime pm for ambiq uart driver by adding pinctrl sleep/resume. Signed-off-by: Hao Luo --- drivers/serial/uart_ambiq.c | 47 +++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/drivers/serial/uart_ambiq.c b/drivers/serial/uart_ambiq.c index 640e4103558..d9ee57aef11 100644 --- a/drivers/serial/uart_ambiq.c +++ b/drivers/serial/uart_ambiq.c @@ -251,12 +251,15 @@ static void uart_ambiq_poll_out(const struct device *dev, unsigned char c) { struct uart_ambiq_data *data = dev->data; uint32_t flag = 0; + unsigned int key; /* Wait for space in FIFO */ do { am_hal_uart_flags_get(data->uart_handler, &flag); } while (flag & UART0_FR_TXFF_Msk); + key = irq_lock(); + /* If an interrupt transmission is in progress, the pm constraint is already managed by the * call of uart_ambiq_irq_tx_[en|dis]able */ @@ -267,10 +270,13 @@ static void uart_ambiq_poll_out(const struct device *dev, unsigned char c) * transmission has completed */ uart_ambiq_pm_policy_state_lock_get(dev); + am_hal_uart_interrupt_enable(data->uart_handler, AM_HAL_UART_INT_TXCMP); } /* Send a character */ am_hal_uart_fifo_write(data->uart_handler, &c, 1, NULL); + + irq_unlock(key); } static int uart_ambiq_err_check(const struct device *dev) @@ -302,9 +308,15 @@ static int uart_ambiq_fifo_fill(const struct device *dev, const uint8_t *tx_data { struct uart_ambiq_data *data = dev->data; int num_tx = 0U; + unsigned int key; + + /* Lock interrupts to prevent nested interrupts or thread switch */ + key = irq_lock(); am_hal_uart_fifo_write(data->uart_handler, (uint8_t *)tx_data, len, &num_tx); + irq_unlock(key); + return num_tx; } @@ -322,7 +334,9 @@ static void uart_ambiq_irq_tx_enable(const struct device *dev) { const struct uart_ambiq_config *cfg = dev->config; struct uart_ambiq_data *data = dev->data; + unsigned int key; + key = irq_lock(); data->tx_poll_trans_on = false; data->tx_int_trans_on = true; uart_ambiq_pm_policy_state_lock_get(dev); @@ -330,6 +344,8 @@ static void uart_ambiq_irq_tx_enable(const struct device *dev) am_hal_uart_interrupt_enable(data->uart_handler, (AM_HAL_UART_INT_TX | AM_HAL_UART_INT_TXCMP)); + irq_unlock(key); + if (!data->sw_call_txdrdy) { return; } @@ -367,12 +383,17 @@ static void uart_ambiq_irq_tx_enable(const struct device *dev) static void uart_ambiq_irq_tx_disable(const struct device *dev) { struct uart_ambiq_data *data = dev->data; + unsigned int key; + + key = irq_lock(); data->sw_call_txdrdy = true; am_hal_uart_interrupt_disable(data->uart_handler, (AM_HAL_UART_INT_TX | AM_HAL_UART_INT_TXCMP)); data->tx_int_trans_on = false; uart_ambiq_pm_policy_state_lock_put(dev); + + irq_unlock(key); } static int uart_ambiq_irq_tx_complete(const struct device *dev) @@ -526,24 +547,42 @@ end: #ifdef CONFIG_PM_DEVICE static int uart_ambiq_pm_action(const struct device *dev, enum pm_device_action action) { + const struct uart_ambiq_config *config = dev->config; struct uart_ambiq_data *data = dev->data; - uint32_t ret; am_hal_sysctrl_power_state_e status; + int err; switch (action) { case PM_DEVICE_ACTION_RESUME: + /* Set pins to active state */ + err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); + if (err < 0) { + return err; + } status = AM_HAL_SYSCTRL_WAKE; break; case PM_DEVICE_ACTION_SUSPEND: + /* Move pins to sleep state */ + err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_SLEEP); + if ((err < 0) && (err != -ENOENT)) { + /* + * If returning -ENOENT, no pins where defined for sleep mode : + * Do not output on console (might sleep already) when going to sleep, + * "(LP)UART pinctrl sleep state not available" + * and don't block PM suspend. + * Else return the error. + */ + return err; + } status = AM_HAL_SYSCTRL_DEEPSLEEP; break; default: return -ENOTSUP; } - ret = am_hal_uart_power_control(data->uart_handler, status, true); + err = am_hal_uart_power_control(data->uart_handler, status, true); - if (ret != AM_HAL_STATUS_SUCCESS) { + if (err != AM_HAL_STATUS_SUCCESS) { return -EPERM; } else { return 0; @@ -558,6 +597,7 @@ void uart_ambiq_isr(const struct device *dev) uint32_t status = 0; am_hal_uart_interrupt_status_get(data->uart_handler, &status, false); + am_hal_uart_interrupt_clear(data->uart_handler, status); if (status & AM_HAL_UART_INT_TXCMP) { if (data->tx_poll_trans_on) { @@ -605,7 +645,6 @@ void uart_ambiq_isr(const struct device *dev) k_work_reschedule(&data->async.rx.timeout_work, K_USEC(data->async.rx.timeout)); } #endif /* CONFIG_UART_ASYNC_API */ - am_hal_uart_interrupt_clear(data->uart_handler, status); } #endif /* CONFIG_UART_INTERRUPT_DRIVEN */