Skip to contentSkip to conversation

hw/char/stm32l4x5_usart.c: Support writes smaller than 32-bit

PATCH Superseded Jacob Whitaker Abrams 3 participants 5 messages Aug 17 – Sep 21 Arm STM32

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/char/stm32l4x5_usart.c: Support writes smaller than 32-bit

Official STM32CubeL4 drivers use and require support for 16-bit writes to UART registers for proper function.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2540

---
 hw/char/stm32l4x5_usart.c          | 93 +++++++++++++++++++++++-------
 tests/qtest/stm32l4x5_usart-test.c | 50 +++++++++++++++-
 2 files changed, 120 insertions(+), 23 deletions(-)
hw/char/stm32l4x5_usart.c +71 -22
index dd1b099195..88c4a3b5a7 100644
@@ -154,8 +154,15 @@ REG32(RDR, 0x24)
 REG32(TDR, 0x28)
     FIELD(TDR, TDR, 0, 9)
 
+#define ISR_RESET_VALUE (0x020000C0)
+
 static void stm32l4x5_update_isr(Stm32l4x5UsartBaseState *s)
 {
+    if (!(s->cr1 & R_CR1_UE_MASK)) {
+        s->isr = ISR_RESET_VALUE;
+        return;
+    }
+
     if (s->cr1 & R_CR1_TE_MASK) {
         s->isr |= R_ISR_TEACK_MASK;
     } else {
@@ -404,9 +411,11 @@ static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr,
                                      unsigned int size)
 {
     Stm32l4x5UsartBaseState *s = opaque;
+    hwaddr base = addr & ~0x3ULL;
+    unsigned int offset = addr & 0x3;
     uint64_t retvalue = 0;
 
-    switch (addr) {
+    switch (base) {
     case A_CR1:
         retvalue = s->cr1;
         break;
@@ -451,6 +460,13 @@ static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr,
         break;
     }
 
+    /* Adjust for partial access */
+    if (size == 1) {
+        retvalue = (retvalue >> (offset * 8)) & 0xFF;
+    } else if (size == 2) {
+        retvalue = (retvalue >> (offset * 8)) & 0xFFFF;
+    }
+
     trace_stm32l4x5_usart_read(addr, retvalue);
 
     return retvalue;
@@ -460,55 +476,88 @@ static void stm32l4x5_usart_base_write(void *opaque, hwaddr addr,
                                   uint64_t val64, unsigned int size)
 {
     Stm32l4x5UsartBaseState *s = opaque;
-    const uint32_t value = val64;
+    hwaddr base = addr & ~0x3ULL;
+    unsigned int offset = addr & 0x3;
+    uint32_t value = (uint32_t)val64;
+
+    /* Build mask for partial access */
+    uint32_t mask;
+    if (size == 4) {
+        mask = 0xFFFFFFFF;
+    } else if (size == 2) {
+        mask = 0xFFFF << (offset * 8);
+    } else if (size == 1) {
+        mask = 0xFF << (offset * 8);
+    } else {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Unsupported access size %u\n", __func__, size);
+        return;
+    }
+    value = (value << (offset * 8)) & mask;
 
     trace_stm32l4x5_usart_write(addr, value);
 
-    switch (addr) {
-    case A_CR1:
-        s->cr1 = value;
+    switch (base) {
+    case A_CR1: {
+        uint32_t old = s->cr1;
+        s->cr1 = (old & ~mask) | value;
         stm32l4x5_update_params(s);
         stm32l4x5_update_isr(s);
         stm32l4x5_update_irq(s);
         return;
-    case A_CR2:
-        s->cr2 = value;
+    }
+    case A_CR2: {
+        uint32_t old = s->cr2;
+        s->cr2 = (old & ~mask) | value;
         stm32l4x5_update_params(s);
         return;
-    case A_CR3:
-        s->cr3 = value;
+    }
+    case A_CR3: {
+        uint32_t old = s->cr3;
+        s->cr3 = (old & ~mask) | value;
         return;
-    case A_BRR:
-        s->brr = value;
+    }
+    case A_BRR: {
+        uint32_t old = s->brr;
+        s->brr = (old & ~mask) | value;
         stm32l4x5_update_params(s);
         return;
-    case A_GTPR:
-        s->gtpr = value;
+    }
+    case A_GTPR: {
+        uint32_t old = s->gtpr;
+        s->gtpr = (old & ~mask) | value;
         return;
-    case A_RTOR:
-        s->rtor = value;
+    }
+    case A_RTOR: {
+        uint32_t old = s->rtor;
+        s->rtor = (old & ~mask) | value;
         return;
-    case A_RQR:
+    }
+    case A_RQR: {
+        /* RQR is write-only, assume full 32-bit access */
         usart_update_rqr(s, value);
         return;
+    }
     case A_ISR:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: ISR is read only !\n", __func__);
         return;
-    case A_ICR:
-        /* Clear the status flags */
+    case A_ICR: {
+        /* Clear flags: value is masked to written bytes */
         s->isr &= ~value;
         stm32l4x5_update_irq(s);
         return;
+    }
     case A_RDR:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: RDR is read only !\n", __func__);
         return;
-    case A_TDR:
-        s->tdr = value;
+    case A_TDR: {
+        uint32_t old = s->tdr;
+        s->tdr = (old & ~mask) | value;
         s->isr &= ~R_ISR_TXE_MASK;
         usart_transmit(NULL, G_IO_OUT, s);
         return;
+    }
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
@@ -521,12 +570,12 @@ static const MemoryRegionOps stm32l4x5_usart_base_ops = {
     .endianness = DEVICE_NATIVE_ENDIAN,
     .valid = {
         .max_access_size = 4,
-        .min_access_size = 4,
+        .min_access_size = 2,
         .unaligned = false
     },
     .impl = {
         .max_access_size = 4,
-        .min_access_size = 4,
+        .min_access_size = 2,
         .unaligned = false
     },
 };
tests/qtest/stm32l4x5_usart-test.c +49 -1

62 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

I started the effort back then but I never fully finished. This is to complete that work.

Regards,
Jacob

Quoted 12 lines · On Mon, Sep 14, 2026, 02:23 Peter Maydell <[email protected]> wrote:
Quoted 5 lines · Jacob Whitaker Abrams

Official STM32CubeL4 drivers use and require support for 16-bit writes to UART registers for proper function.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2540

This is a bug that was closed in 2024...

-- PMM

Jacob

#3 lore raw
In reply to Jacob Whitaker Abrams
Quoted 3 lines · Jacob Whitaker Abrams

Official STM32CubeL4 drivers use and require support for 16-bit writes to UART registers for proper function.

This is a breach of the datasheet (assuming I have the right one:
RM0351 "STM32L47xxx, STM32L48xxx, STM32L49xxx and STM32L4Axxx advanced Arm®-based 32-bit MCUs"), which is pretty clear:
"The peripheral registers have to be accessed by words (32 bits)".

Still, if the official drivers are doing it then presumably the hardware actually does allow smaller accesses.

Do the drivers do small accesses to the top parts of registers, or do they always use 4-aligned addresses but just sometimes do smaller width accesses ? If only the latter is needed, we can make the code simpler (reads return the full register value and the QEMU core code chops off the unneeded high parts, writes are "write as if zero-extended").

Quoted 13 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2540

---
 hw/char/stm32l4x5_usart.c          | 93 +++++++++++++++++++++++-------
 tests/qtest/stm32l4x5_usart-test.c | 50 +++++++++++++++-
 2 files changed, 120 insertions(+), 23 deletions(-)
hw/char/stm32l4x5_usart.c +0 -0
index dd1b099195..88c4a3b5a7 100644
@@ -154,8 +154,15 @@ REG32(RDR, 0x24)
 REG32(TDR, 0x28)

hw/char/stm32l4x5_usart.c REG32(RDR, 0x24) · line 164

Quoted 10 lines
hw/char/stm32l4x5_usart.c +6 -0
     FIELD(TDR, TDR, 0, 9)
 
+#define ISR_RESET_VALUE (0x020000C0)
+
 static void stm32l4x5_update_isr(Stm32l4x5UsartBaseState *s)
 {
+    if (!(s->cr1 & R_CR1_UE_MASK)) {
+        s->isr = ISR_RESET_VALUE;
+        return;
+    }

This seems to be an unrelated change to adding 16-bit access handling ?

Quoted 18 lines
hw/char/stm32l4x5_usart.c +4 -1
+
     if (s->cr1 & R_CR1_TE_MASK) {
         s->isr |= R_ISR_TEACK_MASK;
     } else {
@@ -404,9 +411,11 @@ static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr,
                                      unsigned int size)
 {
     Stm32l4x5UsartBaseState *s = opaque;
+    hwaddr base = addr & ~0x3ULL;
+    unsigned int offset = addr & 0x3;
     uint64_t retvalue = 0;
 
-    switch (addr) {
+    switch (base) {
     case A_CR1:
         retvalue = s->cr1;
         break;

hw/char/stm32l4x5_usart.c static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr, · line 468

Quoted 9 lines
hw/char/stm32l4x5_usart.c +6 -0
@@ -451,6 +460,13 @@ static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr,
         break;
     }
 
+    /* Adjust for partial access */
+    if (size == 1) {
+        retvalue = (retvalue >> (offset * 8)) & 0xFF;
+    } else if (size == 2) {
+        retvalue = (retvalue >> (offset * 8)) & 0xFFFF;
+    }

You can avoid the if():

   retvalue = extract32(retvalue, offset * 8, size * 8);
Quoted 26 lines
hw/char/stm32l4x5_usart.c +18 -1
+
     trace_stm32l4x5_usart_read(addr, retvalue);
 
     return retvalue;
@@ -460,55 +476,88 @@ static void stm32l4x5_usart_base_write(void *opaque, hwaddr addr,
                                   uint64_t val64, unsigned int size)
 {
     Stm32l4x5UsartBaseState *s = opaque;
-    const uint32_t value = val64;
+    hwaddr base = addr & ~0x3ULL;
+    unsigned int offset = addr & 0x3;
+    uint32_t value = (uint32_t)val64;
+
+    /* Build mask for partial access */
+    uint32_t mask;
+    if (size == 4) {
+        mask = 0xFFFFFFFF;
+    } else if (size == 2) {
+        mask = 0xFFFF << (offset * 8);
+    } else if (size == 1) {
+        mask = 0xFF << (offset * 8);
+    } else {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Unsupported access size %u\n", __func__, size);
+        return;
+    }
+    value = (value << (offset * 8)) & mask;

hw/char/stm32l4x5_usart.c static void stm32l4x5_usart_base_write(void *opaque, hwaddr addr, · line 502

Quoted 10 lines
hw/char/stm32l4x5_usart.c +4 -3
 
     trace_stm32l4x5_usart_write(addr, value);
 
-    switch (addr) {
-    case A_CR1:
-        s->cr1 = value;
+    switch (base) {
+    case A_CR1: {
+        uint32_t old = s->cr1;
+        s->cr1 = (old & ~mask) | value;

If you write these as
s->cr1 = deposit32(s->cr1, value, offset * 8, size * 8);

then you don't need to calculate a mask or shift-and-mask the value by hand.

thanks
-- PMM

#4 lore raw
Quoted 12 lines · Peter Maydell
Quoted 2 lines · Jacob Whitaker Abrams

Official STM32CubeL4 drivers use and require support for 16-bit writes to UART registers for proper function.

This is a breach of the datasheet (assuming I have the right one:
RM0351 "STM32L47xxx, STM32L48xxx, STM32L49xxx and STM32L4Axxx advanced Arm®-based 32-bit MCUs"), which is pretty clear:
"The peripheral registers have to be accessed by words (32 bits)".

Still, if the official drivers are doing it then presumably the hardware actually does allow smaller accesses.

Quoted 6 lines

Do the drivers do small accesses to the top parts of registers, or do they always use 4-aligned addresses but just sometimes do smaller width accesses ? If only the latter is needed, we can make the code simpler (reads return the full register value and the QEMU core code chops off the unneeded high parts, writes are "write as if zero-extended").

Yes it is a documentation error, we discussed this in 2024, Nicolas Fillon at STM wrote back to me "I see we are making 16 bit access read and write to these 16 bit registers in our library for both HAL and LL so this should be a documentation issue."

It appears specifically to affect the USART peripheral inside the chip, here is the official C struct they use in their source code, it contains uint16_t sized registers resulting in 16-bit memory assembly operations:

typedef struct
{
  __IO uint32_t CR1;         /*!< USART Control register 1,                 Address offset: 0x00 */ __IO uint32_t CR2;         /*!< USART Control register 2,                 Address offset: 0x04 */ __IO uint32_t CR3;         /*!< USART Control register 3,                 Address offset: 0x08 */ __IO uint32_t BRR;         /*!< USART Baud rate register,                 Address offset: 0x0C */ __IO uint16_t GTPR;        /*!< USART Guard time and prescaler register,  Address offset: 0x10 */ uint16_t  RESERVED2;       /*!< Reserved, 0x12                                                 */ __IO uint32_t RTOR;        /*!< USART Receiver Time Out register,         Address offset: 0x14 */ __IO uint16_t RQR;         /*!< USART Request register,                   Address offset: 0x18 */ uint16_t  RESERVED3;       /*!< Reserved, 0x1A                                                 */ __IO uint32_t ISR;         /*!< USART Interrupt and status register,      Address offset: 0x1C */ __IO uint32_t ICR;         /*!< USART Interrupt flag Clear register,      Address offset: 0x20 */ __IO uint16_t RDR;         /*!< USART Receive Data register,              Address offset: 0x24 */ uint16_t  RESERVED4;       /*!< Reserved, 0x26                                                 */ __IO uint16_t TDR;         /*!< USART Transmit Data register,             Address offset: 0x28 */ uint16_t  RESERVED5;       /*!< Reserved, 0x2A                                                 */ __IO uint32_t PRESC;       /*!< USART Prescaler register,                 Address offset: 0x2C */ } USART_TypeDef;

Quoted 15 lines
Quoted 15 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2540

---
 hw/char/stm32l4x5_usart.c          | 93 +++++++++++++++++++++++-------
 tests/qtest/stm32l4x5_usart-test.c | 50 +++++++++++++++-
 2 files changed, 120 insertions(+), 23 deletions(-)
hw/char/stm32l4x5_usart.c +0 -0
index dd1b099195..88c4a3b5a7 100644
@@ -154,8 +154,15 @@ REG32(RDR, 0x24)
 REG32(TDR, 0x28)
     FIELD(TDR, TDR, 0, 9)
 
Quoted 9 lines

hw/char/stm32l4x5_usart.c REG32(RDR, 0x24) · line 164

Quoted 8 lines
hw/char/stm32l4x5_usart.c +6 -0
+#define ISR_RESET_VALUE (0x020000C0)
+
 static void stm32l4x5_update_isr(Stm32l4x5UsartBaseState *s)
 {
+    if (!(s->cr1 & R_CR1_UE_MASK)) {
+        s->isr = ISR_RESET_VALUE;
+        return;
+    }

This seems to be an unrelated change to adding 16-bit access handling ?

Fair point, I will remove that from the upcoming v2 patch.

Quoted 29 lines
Quoted 18 lines
hw/char/stm32l4x5_usart.c +4 -1
+
     if (s->cr1 & R_CR1_TE_MASK) {
         s->isr |= R_ISR_TEACK_MASK;
     } else {
@@ -404,9 +411,11 @@ static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr,
                                      unsigned int size)
 {
     Stm32l4x5UsartBaseState *s = opaque;
+    hwaddr base = addr & ~0x3ULL;
+    unsigned int offset = addr & 0x3;
     uint64_t retvalue = 0;
 
-    switch (addr) {
+    switch (base) {
     case A_CR1:
         retvalue = s->cr1;
         break;

hw/char/stm32l4x5_usart.c static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr, · line 468

Quoted 9 lines
hw/char/stm32l4x5_usart.c +6 -0
@@ -451,6 +460,13 @@ static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr,
         break;
     }
 
+    /* Adjust for partial access */
+    if (size == 1) {
+        retvalue = (retvalue >> (offset * 8)) & 0xFF;
+    } else if (size == 2) {
+        retvalue = (retvalue >> (offset * 8)) & 0xFFFF;
+    }

You can avoid the if():

Quoted 1 line
   retvalue = extract32(retvalue, offset * 8, size * 8);

OK I will do so.

Quoted 39 lines
Quoted 26 lines
hw/char/stm32l4x5_usart.c +18 -1
+
     trace_stm32l4x5_usart_read(addr, retvalue);
 
     return retvalue;
@@ -460,55 +476,88 @@ static void stm32l4x5_usart_base_write(void *opaque, hwaddr addr,
                                   uint64_t val64, unsigned int size)
 {
     Stm32l4x5UsartBaseState *s = opaque;
-    const uint32_t value = val64;
+    hwaddr base = addr & ~0x3ULL;
+    unsigned int offset = addr & 0x3;
+    uint32_t value = (uint32_t)val64;
+
+    /* Build mask for partial access */
+    uint32_t mask;
+    if (size == 4) {
+        mask = 0xFFFFFFFF;
+    } else if (size == 2) {
+        mask = 0xFFFF << (offset * 8);
+    } else if (size == 1) {
+        mask = 0xFF << (offset * 8);
+    } else {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Unsupported access size %u\n", __func__, size);
+        return;
+    }
+    value = (value << (offset * 8)) & mask;

hw/char/stm32l4x5_usart.c static void stm32l4x5_usart_base_write(void *opaque, hwaddr addr, · line 502

Quoted 10 lines
hw/char/stm32l4x5_usart.c +4 -3
 
     trace_stm32l4x5_usart_write(addr, value);
 
-    switch (addr) {
-    case A_CR1:
-        s->cr1 = value;
+    switch (base) {
+    case A_CR1: {
+        uint32_t old = s->cr1;
+        s->cr1 = (old & ~mask) | value;

If you write these as
s->cr1 = deposit32(s->cr1, value, offset * 8, size * 8);

Quoted 2 lines

then you don't need to calculate a mask or shift-and-mask the value by hand.

OK I will.

Regards,

Jacob Abrams