Skip to contentSkip to conversation

hw/char/stm32l4x5_usart.c: Support 16-bit writes

PATCH Applied Jacob Whitaker Abrams 3 participants 3 messages Sep 26 – Sep 28 Arm STM32

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 hw/char/stm32l4x5_usart.c: Support 16-bit writes

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

---
v3:
- Add suggested comment and set only valid.min_access_size to 4
v2:
- Remove all changes other than supporting 16-bit writes UART regs

 hw/char/stm32l4x5_usart.c          |  7 ++++++-
 tests/qtest/stm32l4x5_usart-test.c | 22 +++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)
hw/char/stm32l4x5_usart.c +6 -1
index 0b02dde1f8..c4ffc0d6ce 100644
@@ -522,7 +522,12 @@ static const MemoryRegionOps stm32l4x5_usart_base_ops = {
     .endianness = DEVICE_NATIVE_ENDIAN,
     .valid = {
         .max_access_size = 4,
-        .min_access_size = 4,
+        /*
+         * Official STM32CubeL4 drivers use and require support for 16-bit
+         * writes to UART registers for proper function despite datasheet
+         * claims.
+         */
+        .min_access_size = 2,
         .unaligned = false
     },
     .impl = {
tests/qtest/stm32l4x5_usart-test.c +21 -1
index a72c5a685d..3c6437e75d 100644
@@ -332,6 +332,26 @@ static void test_ack(void)
     qtest_quit(qts);
 }
 
+static void test_16bit_access(void)
+{
+    QTestState *qts = qtest_init("-M b-l475e-iot01a");
+
+    /* GTPR is declared uint16_t in the official STM driver. */
+    qtest_writew(qts, USART1_BASE_ADDR + A_GTPR, 0xABCD);
+    uint16_t gtpr16 = qtest_readw(qts, USART1_BASE_ADDR + A_GTPR);
+    g_assert_cmpuint(gtpr16, ==, 0xABCD);
+
+    /* TDR is uint16_t and r/w, register is 9-bits */
+    qtest_writew(qts, USART1_BASE_ADDR + A_TDR, 0x01CD);
+    uint16_t tdr16 = qtest_readw(qts, USART1_BASE_ADDR + A_TDR);
+    g_assert_cmpuint(tdr16, ==, 0x01CD);
+
+    /* RDR is uint16_t and read only, register is 9-bits */
+    qtest_writew(qts, USART1_BASE_ADDR + A_RDR, 0x01FA);
+
+    qtest_quit(qts);
+}
+
 static void check_clock(QTestState *qts, const char *path, uint32_t rcc_reg,
                         uint32_t reg_offset)
 {
@@ -370,6 +390,6 @@ int main(int argc, char **argv)
     qtest_add_func("stm32l4x5/usart/send_str", test_send_str);
     qtest_add_func("stm32l4x5/usart/ack", test_ack);
     qtest_add_func("stm32l4x5/usart/clock_enable", test_clock_enable);
+    qtest_add_func("stm32l4x5/usart/16bit_access", test_16bit_access);
     return g_test_run();
 }
-
Signature · 1 line
2.43.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · Jacob Whitaker Abrams

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

---
v3:
- Add suggested comment and set only valid.min_access_size to 4
v2:
- Remove all changes other than supporting 16-bit writes UART regs

Quoted 3 lines
  hw/char/stm32l4x5_usart.c          |  7 ++++++-
  tests/qtest/stm32l4x5_usart-test.c | 22 +++++++++++++++++++++-
  2 files changed, 27 insertions(+), 2 deletions(-)
#2 lore raw
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Jacob Whitaker Abrams

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

---
v3:
- Add suggested comment and set only valid.min_access_size to 4
v2:
- Remove all changes other than supporting 16-bit writes UART regs

Quoted 5 lines
Quoted 3 lines
  hw/char/stm32l4x5_usart.c          |  7 ++++++-
  tests/qtest/stm32l4x5_usart-test.c | 22 +++++++++++++++++++++-
  2 files changed, 27 insertions(+), 2 deletions(-)

Applied to target-arm.next, thanks. I expanded the commit message slightly, to add:

"As the only 16 bit accesses we need to handle are ones to a register's base address for registers that have no defined fields in their top 16 bits, the emulation of 16-bit accesses provided by the memory system core is good enough, and we can implement this by updating .valid.min_access_size."

-- PMM