1*4fb37aeaSArnaud Minier /* 2*4fb37aeaSArnaud Minier * STM32L4X5 USART (Universal Synchronous Asynchronous Receiver Transmitter) 3*4fb37aeaSArnaud Minier * 4*4fb37aeaSArnaud Minier * Copyright (c) 2023 Arnaud Minier <arnaud.minier@telecom-paris.fr> 5*4fb37aeaSArnaud Minier * Copyright (c) 2023 Inès Varhol <ines.varhol@telecom-paris.fr> 6*4fb37aeaSArnaud Minier * 7*4fb37aeaSArnaud Minier * SPDX-License-Identifier: GPL-2.0-or-later 8*4fb37aeaSArnaud Minier * 9*4fb37aeaSArnaud Minier * This work is licensed under the terms of the GNU GPL, version 2 or later. 10*4fb37aeaSArnaud Minier * See the COPYING file in the top-level directory. 11*4fb37aeaSArnaud Minier * 12*4fb37aeaSArnaud Minier * The STM32L4X5 USART is heavily inspired by the stm32f2xx_usart 13*4fb37aeaSArnaud Minier * by Alistair Francis. 14*4fb37aeaSArnaud Minier * The reference used is the STMicroElectronics RM0351 Reference manual 15*4fb37aeaSArnaud Minier * for STM32L4x5 and STM32L4x6 advanced Arm ® -based 32-bit MCUs. 16*4fb37aeaSArnaud Minier */ 17*4fb37aeaSArnaud Minier 18*4fb37aeaSArnaud Minier #include "qemu/osdep.h" 19*4fb37aeaSArnaud Minier #include "qemu/log.h" 20*4fb37aeaSArnaud Minier #include "qemu/module.h" 21*4fb37aeaSArnaud Minier #include "qapi/error.h" 22*4fb37aeaSArnaud Minier #include "chardev/char-fe.h" 23*4fb37aeaSArnaud Minier #include "chardev/char-serial.h" 24*4fb37aeaSArnaud Minier #include "migration/vmstate.h" 25*4fb37aeaSArnaud Minier #include "hw/char/stm32l4x5_usart.h" 26*4fb37aeaSArnaud Minier #include "hw/clock.h" 27*4fb37aeaSArnaud Minier #include "hw/irq.h" 28*4fb37aeaSArnaud Minier #include "hw/qdev-clock.h" 29*4fb37aeaSArnaud Minier #include "hw/qdev-properties.h" 30*4fb37aeaSArnaud Minier #include "hw/qdev-properties-system.h" 31*4fb37aeaSArnaud Minier #include "hw/registerfields.h" 32*4fb37aeaSArnaud Minier #include "trace.h" 33*4fb37aeaSArnaud Minier 34*4fb37aeaSArnaud Minier 35*4fb37aeaSArnaud Minier REG32(CR1, 0x00) 36*4fb37aeaSArnaud Minier FIELD(CR1, M1, 28, 1) /* Word length (part 2, see M0) */ 37*4fb37aeaSArnaud Minier FIELD(CR1, EOBIE, 27, 1) /* End of Block interrupt enable */ 38*4fb37aeaSArnaud Minier FIELD(CR1, RTOIE, 26, 1) /* Receiver timeout interrupt enable */ 39*4fb37aeaSArnaud Minier FIELD(CR1, DEAT, 21, 5) /* Driver Enable assertion time */ 40*4fb37aeaSArnaud Minier FIELD(CR1, DEDT, 16, 5) /* Driver Enable de-assertion time */ 41*4fb37aeaSArnaud Minier FIELD(CR1, OVER8, 15, 1) /* Oversampling mode */ 42*4fb37aeaSArnaud Minier FIELD(CR1, CMIE, 14, 1) /* Character match interrupt enable */ 43*4fb37aeaSArnaud Minier FIELD(CR1, MME, 13, 1) /* Mute mode enable */ 44*4fb37aeaSArnaud Minier FIELD(CR1, M0, 12, 1) /* Word length (part 1, see M1) */ 45*4fb37aeaSArnaud Minier FIELD(CR1, WAKE, 11, 1) /* Receiver wakeup method */ 46*4fb37aeaSArnaud Minier FIELD(CR1, PCE, 10, 1) /* Parity control enable */ 47*4fb37aeaSArnaud Minier FIELD(CR1, PS, 9, 1) /* Parity selection */ 48*4fb37aeaSArnaud Minier FIELD(CR1, PEIE, 8, 1) /* PE interrupt enable */ 49*4fb37aeaSArnaud Minier FIELD(CR1, TXEIE, 7, 1) /* TXE interrupt enable */ 50*4fb37aeaSArnaud Minier FIELD(CR1, TCIE, 6, 1) /* Transmission complete interrupt enable */ 51*4fb37aeaSArnaud Minier FIELD(CR1, RXNEIE, 5, 1) /* RXNE interrupt enable */ 52*4fb37aeaSArnaud Minier FIELD(CR1, IDLEIE, 4, 1) /* IDLE interrupt enable */ 53*4fb37aeaSArnaud Minier FIELD(CR1, TE, 3, 1) /* Transmitter enable */ 54*4fb37aeaSArnaud Minier FIELD(CR1, RE, 2, 1) /* Receiver enable */ 55*4fb37aeaSArnaud Minier FIELD(CR1, UESM, 1, 1) /* USART enable in Stop mode */ 56*4fb37aeaSArnaud Minier FIELD(CR1, UE, 0, 1) /* USART enable */ 57*4fb37aeaSArnaud Minier REG32(CR2, 0x04) 58*4fb37aeaSArnaud Minier FIELD(CR2, ADD_1, 28, 4) /* ADD[7:4] */ 59*4fb37aeaSArnaud Minier FIELD(CR2, ADD_0, 24, 1) /* ADD[3:0] */ 60*4fb37aeaSArnaud Minier FIELD(CR2, RTOEN, 23, 1) /* Receiver timeout enable */ 61*4fb37aeaSArnaud Minier FIELD(CR2, ABRMOD, 21, 2) /* Auto baud rate mode */ 62*4fb37aeaSArnaud Minier FIELD(CR2, ABREN, 20, 1) /* Auto baud rate enable */ 63*4fb37aeaSArnaud Minier FIELD(CR2, MSBFIRST, 19, 1) /* Most significant bit first */ 64*4fb37aeaSArnaud Minier FIELD(CR2, DATAINV, 18, 1) /* Binary data inversion */ 65*4fb37aeaSArnaud Minier FIELD(CR2, TXINV, 17, 1) /* TX pin active level inversion */ 66*4fb37aeaSArnaud Minier FIELD(CR2, RXINV, 16, 1) /* RX pin active level inversion */ 67*4fb37aeaSArnaud Minier FIELD(CR2, SWAP, 15, 1) /* Swap RX/TX pins */ 68*4fb37aeaSArnaud Minier FIELD(CR2, LINEN, 14, 1) /* LIN mode enable */ 69*4fb37aeaSArnaud Minier FIELD(CR2, STOP, 12, 2) /* STOP bits */ 70*4fb37aeaSArnaud Minier FIELD(CR2, CLKEN, 11, 1) /* Clock enable */ 71*4fb37aeaSArnaud Minier FIELD(CR2, CPOL, 10, 1) /* Clock polarity */ 72*4fb37aeaSArnaud Minier FIELD(CR2, CPHA, 9, 1) /* Clock phase */ 73*4fb37aeaSArnaud Minier FIELD(CR2, LBCL, 8, 1) /* Last bit clock pulse */ 74*4fb37aeaSArnaud Minier FIELD(CR2, LBDIE, 6, 1) /* LIN break detection interrupt enable */ 75*4fb37aeaSArnaud Minier FIELD(CR2, LBDL, 5, 1) /* LIN break detection length */ 76*4fb37aeaSArnaud Minier FIELD(CR2, ADDM7, 4, 1) /* 7-bit / 4-bit Address Detection */ 77*4fb37aeaSArnaud Minier 78*4fb37aeaSArnaud Minier REG32(CR3, 0x08) 79*4fb37aeaSArnaud Minier /* TCBGTIE only on STM32L496xx/4A6xx devices */ 80*4fb37aeaSArnaud Minier FIELD(CR3, UCESM, 23, 1) /* USART Clock Enable in Stop Mode */ 81*4fb37aeaSArnaud Minier FIELD(CR3, WUFIE, 22, 1) /* Wakeup from Stop mode interrupt enable */ 82*4fb37aeaSArnaud Minier FIELD(CR3, WUS, 20, 2) /* Wakeup from Stop mode interrupt flag selection */ 83*4fb37aeaSArnaud Minier FIELD(CR3, SCARCNT, 17, 3) /* Smartcard auto-retry count */ 84*4fb37aeaSArnaud Minier FIELD(CR3, DEP, 15, 1) /* Driver enable polarity selection */ 85*4fb37aeaSArnaud Minier FIELD(CR3, DEM, 14, 1) /* Driver enable mode */ 86*4fb37aeaSArnaud Minier FIELD(CR3, DDRE, 13, 1) /* DMA Disable on Reception Error */ 87*4fb37aeaSArnaud Minier FIELD(CR3, OVRDIS, 12, 1) /* Overrun Disable */ 88*4fb37aeaSArnaud Minier FIELD(CR3, ONEBIT, 11, 1) /* One sample bit method enable */ 89*4fb37aeaSArnaud Minier FIELD(CR3, CTSIE, 10, 1) /* CTS interrupt enable */ 90*4fb37aeaSArnaud Minier FIELD(CR3, CTSE, 9, 1) /* CTS enable */ 91*4fb37aeaSArnaud Minier FIELD(CR3, RTSE, 8, 1) /* RTS enable */ 92*4fb37aeaSArnaud Minier FIELD(CR3, DMAT, 7, 1) /* DMA enable transmitter */ 93*4fb37aeaSArnaud Minier FIELD(CR3, DMAR, 6, 1) /* DMA enable receiver */ 94*4fb37aeaSArnaud Minier FIELD(CR3, SCEN, 5, 1) /* Smartcard mode enable */ 95*4fb37aeaSArnaud Minier FIELD(CR3, NACK, 4, 1) /* Smartcard NACK enable */ 96*4fb37aeaSArnaud Minier FIELD(CR3, HDSEL, 3, 1) /* Half-duplex selection */ 97*4fb37aeaSArnaud Minier FIELD(CR3, IRLP, 2, 1) /* IrDA low-power */ 98*4fb37aeaSArnaud Minier FIELD(CR3, IREN, 1, 1) /* IrDA mode enable */ 99*4fb37aeaSArnaud Minier FIELD(CR3, EIE, 0, 1) /* Error interrupt enable */ 100*4fb37aeaSArnaud Minier REG32(BRR, 0x0C) 101*4fb37aeaSArnaud Minier FIELD(BRR, BRR, 0, 16) 102*4fb37aeaSArnaud Minier REG32(GTPR, 0x10) 103*4fb37aeaSArnaud Minier FIELD(GTPR, GT, 8, 8) /* Guard time value */ 104*4fb37aeaSArnaud Minier FIELD(GTPR, PSC, 0, 8) /* Prescaler value */ 105*4fb37aeaSArnaud Minier REG32(RTOR, 0x14) 106*4fb37aeaSArnaud Minier FIELD(RTOR, BLEN, 24, 8) /* Block Length */ 107*4fb37aeaSArnaud Minier FIELD(RTOR, RTO, 0, 24) /* Receiver timeout value */ 108*4fb37aeaSArnaud Minier REG32(RQR, 0x18) 109*4fb37aeaSArnaud Minier FIELD(RQR, TXFRQ, 4, 1) /* Transmit data flush request */ 110*4fb37aeaSArnaud Minier FIELD(RQR, RXFRQ, 3, 1) /* Receive data flush request */ 111*4fb37aeaSArnaud Minier FIELD(RQR, MMRQ, 2, 1) /* Mute mode request */ 112*4fb37aeaSArnaud Minier FIELD(RQR, SBKRQ, 1, 1) /* Send break request */ 113*4fb37aeaSArnaud Minier FIELD(RQR, ABBRRQ, 0, 1) /* Auto baud rate request */ 114*4fb37aeaSArnaud Minier REG32(ISR, 0x1C) 115*4fb37aeaSArnaud Minier /* TCBGT only for STM32L475xx/476xx/486xx devices */ 116*4fb37aeaSArnaud Minier FIELD(ISR, REACK, 22, 1) /* Receive enable acknowledge flag */ 117*4fb37aeaSArnaud Minier FIELD(ISR, TEACK, 21, 1) /* Transmit enable acknowledge flag */ 118*4fb37aeaSArnaud Minier FIELD(ISR, WUF, 20, 1) /* Wakeup from Stop mode flag */ 119*4fb37aeaSArnaud Minier FIELD(ISR, RWU, 19, 1) /* Receiver wakeup from Mute mode */ 120*4fb37aeaSArnaud Minier FIELD(ISR, SBKF, 18, 1) /* Send break flag */ 121*4fb37aeaSArnaud Minier FIELD(ISR, CMF, 17, 1) /* Character match flag */ 122*4fb37aeaSArnaud Minier FIELD(ISR, BUSY, 16, 1) /* Busy flag */ 123*4fb37aeaSArnaud Minier FIELD(ISR, ABRF, 15, 1) /* Auto Baud rate flag */ 124*4fb37aeaSArnaud Minier FIELD(ISR, ABRE, 14, 1) /* Auto Baud rate error */ 125*4fb37aeaSArnaud Minier FIELD(ISR, EOBF, 12, 1) /* End of block flag */ 126*4fb37aeaSArnaud Minier FIELD(ISR, RTOF, 11, 1) /* Receiver timeout */ 127*4fb37aeaSArnaud Minier FIELD(ISR, CTS, 10, 1) /* CTS flag */ 128*4fb37aeaSArnaud Minier FIELD(ISR, CTSIF, 9, 1) /* CTS interrupt flag */ 129*4fb37aeaSArnaud Minier FIELD(ISR, LBDF, 8, 1) /* LIN break detection flag */ 130*4fb37aeaSArnaud Minier FIELD(ISR, TXE, 7, 1) /* Transmit data register empty */ 131*4fb37aeaSArnaud Minier FIELD(ISR, TC, 6, 1) /* Transmission complete */ 132*4fb37aeaSArnaud Minier FIELD(ISR, RXNE, 5, 1) /* Read data register not empty */ 133*4fb37aeaSArnaud Minier FIELD(ISR, IDLE, 4, 1) /* Idle line detected */ 134*4fb37aeaSArnaud Minier FIELD(ISR, ORE, 3, 1) /* Overrun error */ 135*4fb37aeaSArnaud Minier FIELD(ISR, NF, 2, 1) /* START bit Noise detection flag */ 136*4fb37aeaSArnaud Minier FIELD(ISR, FE, 1, 1) /* Framing Error */ 137*4fb37aeaSArnaud Minier FIELD(ISR, PE, 0, 1) /* Parity Error */ 138*4fb37aeaSArnaud Minier REG32(ICR, 0x20) 139*4fb37aeaSArnaud Minier FIELD(ICR, WUCF, 20, 1) /* Wakeup from Stop mode clear flag */ 140*4fb37aeaSArnaud Minier FIELD(ICR, CMCF, 17, 1) /* Character match clear flag */ 141*4fb37aeaSArnaud Minier FIELD(ICR, EOBCF, 12, 1) /* End of block clear flag */ 142*4fb37aeaSArnaud Minier FIELD(ICR, RTOCF, 11, 1) /* Receiver timeout clear flag */ 143*4fb37aeaSArnaud Minier FIELD(ICR, CTSCF, 9, 1) /* CTS clear flag */ 144*4fb37aeaSArnaud Minier FIELD(ICR, LBDCF, 8, 1) /* LIN break detection clear flag */ 145*4fb37aeaSArnaud Minier /* TCBGTCF only on STM32L496xx/4A6xx devices */ 146*4fb37aeaSArnaud Minier FIELD(ICR, TCCF, 6, 1) /* Transmission complete clear flag */ 147*4fb37aeaSArnaud Minier FIELD(ICR, IDLECF, 4, 1) /* Idle line detected clear flag */ 148*4fb37aeaSArnaud Minier FIELD(ICR, ORECF, 3, 1) /* Overrun error clear flag */ 149*4fb37aeaSArnaud Minier FIELD(ICR, NCF, 2, 1) /* Noise detected clear flag */ 150*4fb37aeaSArnaud Minier FIELD(ICR, FECF, 1, 1) /* Framing error clear flag */ 151*4fb37aeaSArnaud Minier FIELD(ICR, PECF, 0, 1) /* Parity error clear flag */ 152*4fb37aeaSArnaud Minier REG32(RDR, 0x24) 153*4fb37aeaSArnaud Minier FIELD(RDR, RDR, 0, 9) 154*4fb37aeaSArnaud Minier REG32(TDR, 0x28) 155*4fb37aeaSArnaud Minier FIELD(TDR, TDR, 0, 9) 156*4fb37aeaSArnaud Minier 157*4fb37aeaSArnaud Minier static void stm32l4x5_usart_base_reset_hold(Object *obj, ResetType type) 158*4fb37aeaSArnaud Minier { 159*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseState *s = STM32L4X5_USART_BASE(obj); 160*4fb37aeaSArnaud Minier 161*4fb37aeaSArnaud Minier s->cr1 = 0x00000000; 162*4fb37aeaSArnaud Minier s->cr2 = 0x00000000; 163*4fb37aeaSArnaud Minier s->cr3 = 0x00000000; 164*4fb37aeaSArnaud Minier s->brr = 0x00000000; 165*4fb37aeaSArnaud Minier s->gtpr = 0x00000000; 166*4fb37aeaSArnaud Minier s->rtor = 0x00000000; 167*4fb37aeaSArnaud Minier s->isr = 0x020000C0; 168*4fb37aeaSArnaud Minier s->rdr = 0x00000000; 169*4fb37aeaSArnaud Minier s->tdr = 0x00000000; 170*4fb37aeaSArnaud Minier } 171*4fb37aeaSArnaud Minier 172*4fb37aeaSArnaud Minier static uint64_t stm32l4x5_usart_base_read(void *opaque, hwaddr addr, 173*4fb37aeaSArnaud Minier unsigned int size) 174*4fb37aeaSArnaud Minier { 175*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseState *s = opaque; 176*4fb37aeaSArnaud Minier uint64_t retvalue = 0; 177*4fb37aeaSArnaud Minier 178*4fb37aeaSArnaud Minier switch (addr) { 179*4fb37aeaSArnaud Minier case A_CR1: 180*4fb37aeaSArnaud Minier retvalue = s->cr1; 181*4fb37aeaSArnaud Minier break; 182*4fb37aeaSArnaud Minier case A_CR2: 183*4fb37aeaSArnaud Minier retvalue = s->cr2; 184*4fb37aeaSArnaud Minier break; 185*4fb37aeaSArnaud Minier case A_CR3: 186*4fb37aeaSArnaud Minier retvalue = s->cr3; 187*4fb37aeaSArnaud Minier break; 188*4fb37aeaSArnaud Minier case A_BRR: 189*4fb37aeaSArnaud Minier retvalue = FIELD_EX32(s->brr, BRR, BRR); 190*4fb37aeaSArnaud Minier break; 191*4fb37aeaSArnaud Minier case A_GTPR: 192*4fb37aeaSArnaud Minier retvalue = s->gtpr; 193*4fb37aeaSArnaud Minier break; 194*4fb37aeaSArnaud Minier case A_RTOR: 195*4fb37aeaSArnaud Minier retvalue = s->rtor; 196*4fb37aeaSArnaud Minier break; 197*4fb37aeaSArnaud Minier case A_RQR: 198*4fb37aeaSArnaud Minier /* RQR is a write only register */ 199*4fb37aeaSArnaud Minier retvalue = 0x00000000; 200*4fb37aeaSArnaud Minier break; 201*4fb37aeaSArnaud Minier case A_ISR: 202*4fb37aeaSArnaud Minier retvalue = s->isr; 203*4fb37aeaSArnaud Minier break; 204*4fb37aeaSArnaud Minier case A_ICR: 205*4fb37aeaSArnaud Minier /* ICR is a clear register */ 206*4fb37aeaSArnaud Minier retvalue = 0x00000000; 207*4fb37aeaSArnaud Minier break; 208*4fb37aeaSArnaud Minier case A_RDR: 209*4fb37aeaSArnaud Minier retvalue = FIELD_EX32(s->rdr, RDR, RDR); 210*4fb37aeaSArnaud Minier /* Reset RXNE flag */ 211*4fb37aeaSArnaud Minier s->isr &= ~R_ISR_RXNE_MASK; 212*4fb37aeaSArnaud Minier break; 213*4fb37aeaSArnaud Minier case A_TDR: 214*4fb37aeaSArnaud Minier retvalue = FIELD_EX32(s->tdr, TDR, TDR); 215*4fb37aeaSArnaud Minier break; 216*4fb37aeaSArnaud Minier default: 217*4fb37aeaSArnaud Minier qemu_log_mask(LOG_GUEST_ERROR, 218*4fb37aeaSArnaud Minier "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr); 219*4fb37aeaSArnaud Minier break; 220*4fb37aeaSArnaud Minier } 221*4fb37aeaSArnaud Minier 222*4fb37aeaSArnaud Minier trace_stm32l4x5_usart_read(addr, retvalue); 223*4fb37aeaSArnaud Minier 224*4fb37aeaSArnaud Minier return retvalue; 225*4fb37aeaSArnaud Minier } 226*4fb37aeaSArnaud Minier 227*4fb37aeaSArnaud Minier static void stm32l4x5_usart_base_write(void *opaque, hwaddr addr, 228*4fb37aeaSArnaud Minier uint64_t val64, unsigned int size) 229*4fb37aeaSArnaud Minier { 230*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseState *s = opaque; 231*4fb37aeaSArnaud Minier const uint32_t value = val64; 232*4fb37aeaSArnaud Minier 233*4fb37aeaSArnaud Minier trace_stm32l4x5_usart_write(addr, value); 234*4fb37aeaSArnaud Minier 235*4fb37aeaSArnaud Minier switch (addr) { 236*4fb37aeaSArnaud Minier case A_CR1: 237*4fb37aeaSArnaud Minier s->cr1 = value; 238*4fb37aeaSArnaud Minier return; 239*4fb37aeaSArnaud Minier case A_CR2: 240*4fb37aeaSArnaud Minier s->cr2 = value; 241*4fb37aeaSArnaud Minier return; 242*4fb37aeaSArnaud Minier case A_CR3: 243*4fb37aeaSArnaud Minier s->cr3 = value; 244*4fb37aeaSArnaud Minier return; 245*4fb37aeaSArnaud Minier case A_BRR: 246*4fb37aeaSArnaud Minier s->brr = value; 247*4fb37aeaSArnaud Minier return; 248*4fb37aeaSArnaud Minier case A_GTPR: 249*4fb37aeaSArnaud Minier s->gtpr = value; 250*4fb37aeaSArnaud Minier return; 251*4fb37aeaSArnaud Minier case A_RTOR: 252*4fb37aeaSArnaud Minier s->rtor = value; 253*4fb37aeaSArnaud Minier return; 254*4fb37aeaSArnaud Minier case A_RQR: 255*4fb37aeaSArnaud Minier return; 256*4fb37aeaSArnaud Minier case A_ISR: 257*4fb37aeaSArnaud Minier qemu_log_mask(LOG_GUEST_ERROR, 258*4fb37aeaSArnaud Minier "%s: ISR is read only !\n", __func__); 259*4fb37aeaSArnaud Minier return; 260*4fb37aeaSArnaud Minier case A_ICR: 261*4fb37aeaSArnaud Minier /* Clear the status flags */ 262*4fb37aeaSArnaud Minier s->isr &= ~value; 263*4fb37aeaSArnaud Minier return; 264*4fb37aeaSArnaud Minier case A_RDR: 265*4fb37aeaSArnaud Minier qemu_log_mask(LOG_GUEST_ERROR, 266*4fb37aeaSArnaud Minier "%s: RDR is read only !\n", __func__); 267*4fb37aeaSArnaud Minier return; 268*4fb37aeaSArnaud Minier case A_TDR: 269*4fb37aeaSArnaud Minier s->tdr = value; 270*4fb37aeaSArnaud Minier return; 271*4fb37aeaSArnaud Minier default: 272*4fb37aeaSArnaud Minier qemu_log_mask(LOG_GUEST_ERROR, 273*4fb37aeaSArnaud Minier "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr); 274*4fb37aeaSArnaud Minier } 275*4fb37aeaSArnaud Minier } 276*4fb37aeaSArnaud Minier 277*4fb37aeaSArnaud Minier static const MemoryRegionOps stm32l4x5_usart_base_ops = { 278*4fb37aeaSArnaud Minier .read = stm32l4x5_usart_base_read, 279*4fb37aeaSArnaud Minier .write = stm32l4x5_usart_base_write, 280*4fb37aeaSArnaud Minier .endianness = DEVICE_NATIVE_ENDIAN, 281*4fb37aeaSArnaud Minier .valid = { 282*4fb37aeaSArnaud Minier .max_access_size = 4, 283*4fb37aeaSArnaud Minier .min_access_size = 4, 284*4fb37aeaSArnaud Minier .unaligned = false 285*4fb37aeaSArnaud Minier }, 286*4fb37aeaSArnaud Minier .impl = { 287*4fb37aeaSArnaud Minier .max_access_size = 4, 288*4fb37aeaSArnaud Minier .min_access_size = 4, 289*4fb37aeaSArnaud Minier .unaligned = false 290*4fb37aeaSArnaud Minier }, 291*4fb37aeaSArnaud Minier }; 292*4fb37aeaSArnaud Minier 293*4fb37aeaSArnaud Minier static Property stm32l4x5_usart_base_properties[] = { 294*4fb37aeaSArnaud Minier DEFINE_PROP_CHR("chardev", Stm32l4x5UsartBaseState, chr), 295*4fb37aeaSArnaud Minier DEFINE_PROP_END_OF_LIST(), 296*4fb37aeaSArnaud Minier }; 297*4fb37aeaSArnaud Minier 298*4fb37aeaSArnaud Minier static void stm32l4x5_usart_base_init(Object *obj) 299*4fb37aeaSArnaud Minier { 300*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseState *s = STM32L4X5_USART_BASE(obj); 301*4fb37aeaSArnaud Minier 302*4fb37aeaSArnaud Minier sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq); 303*4fb37aeaSArnaud Minier 304*4fb37aeaSArnaud Minier memory_region_init_io(&s->mmio, obj, &stm32l4x5_usart_base_ops, s, 305*4fb37aeaSArnaud Minier TYPE_STM32L4X5_USART_BASE, 0x400); 306*4fb37aeaSArnaud Minier sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); 307*4fb37aeaSArnaud Minier 308*4fb37aeaSArnaud Minier s->clk = qdev_init_clock_in(DEVICE(s), "clk", NULL, s, 0); 309*4fb37aeaSArnaud Minier } 310*4fb37aeaSArnaud Minier 311*4fb37aeaSArnaud Minier static const VMStateDescription vmstate_stm32l4x5_usart_base = { 312*4fb37aeaSArnaud Minier .name = TYPE_STM32L4X5_USART_BASE, 313*4fb37aeaSArnaud Minier .version_id = 1, 314*4fb37aeaSArnaud Minier .minimum_version_id = 1, 315*4fb37aeaSArnaud Minier .fields = (VMStateField[]) { 316*4fb37aeaSArnaud Minier VMSTATE_UINT32(cr1, Stm32l4x5UsartBaseState), 317*4fb37aeaSArnaud Minier VMSTATE_UINT32(cr2, Stm32l4x5UsartBaseState), 318*4fb37aeaSArnaud Minier VMSTATE_UINT32(cr3, Stm32l4x5UsartBaseState), 319*4fb37aeaSArnaud Minier VMSTATE_UINT32(brr, Stm32l4x5UsartBaseState), 320*4fb37aeaSArnaud Minier VMSTATE_UINT32(gtpr, Stm32l4x5UsartBaseState), 321*4fb37aeaSArnaud Minier VMSTATE_UINT32(rtor, Stm32l4x5UsartBaseState), 322*4fb37aeaSArnaud Minier VMSTATE_UINT32(isr, Stm32l4x5UsartBaseState), 323*4fb37aeaSArnaud Minier VMSTATE_UINT32(rdr, Stm32l4x5UsartBaseState), 324*4fb37aeaSArnaud Minier VMSTATE_UINT32(tdr, Stm32l4x5UsartBaseState), 325*4fb37aeaSArnaud Minier VMSTATE_CLOCK(clk, Stm32l4x5UsartBaseState), 326*4fb37aeaSArnaud Minier VMSTATE_END_OF_LIST() 327*4fb37aeaSArnaud Minier } 328*4fb37aeaSArnaud Minier }; 329*4fb37aeaSArnaud Minier 330*4fb37aeaSArnaud Minier 331*4fb37aeaSArnaud Minier static void stm32l4x5_usart_base_realize(DeviceState *dev, Error **errp) 332*4fb37aeaSArnaud Minier { 333*4fb37aeaSArnaud Minier ERRP_GUARD(); 334*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseState *s = STM32L4X5_USART_BASE(dev); 335*4fb37aeaSArnaud Minier if (!clock_has_source(s->clk)) { 336*4fb37aeaSArnaud Minier error_setg(errp, "USART clock must be wired up by SoC code"); 337*4fb37aeaSArnaud Minier return; 338*4fb37aeaSArnaud Minier } 339*4fb37aeaSArnaud Minier } 340*4fb37aeaSArnaud Minier 341*4fb37aeaSArnaud Minier static void stm32l4x5_usart_base_class_init(ObjectClass *klass, void *data) 342*4fb37aeaSArnaud Minier { 343*4fb37aeaSArnaud Minier DeviceClass *dc = DEVICE_CLASS(klass); 344*4fb37aeaSArnaud Minier ResettableClass *rc = RESETTABLE_CLASS(klass); 345*4fb37aeaSArnaud Minier 346*4fb37aeaSArnaud Minier rc->phases.hold = stm32l4x5_usart_base_reset_hold; 347*4fb37aeaSArnaud Minier device_class_set_props(dc, stm32l4x5_usart_base_properties); 348*4fb37aeaSArnaud Minier dc->realize = stm32l4x5_usart_base_realize; 349*4fb37aeaSArnaud Minier dc->vmsd = &vmstate_stm32l4x5_usart_base; 350*4fb37aeaSArnaud Minier } 351*4fb37aeaSArnaud Minier 352*4fb37aeaSArnaud Minier static void stm32l4x5_usart_class_init(ObjectClass *oc, void *data) 353*4fb37aeaSArnaud Minier { 354*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseClass *subc = STM32L4X5_USART_BASE_CLASS(oc); 355*4fb37aeaSArnaud Minier 356*4fb37aeaSArnaud Minier subc->type = STM32L4x5_USART; 357*4fb37aeaSArnaud Minier } 358*4fb37aeaSArnaud Minier 359*4fb37aeaSArnaud Minier static void stm32l4x5_uart_class_init(ObjectClass *oc, void *data) 360*4fb37aeaSArnaud Minier { 361*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseClass *subc = STM32L4X5_USART_BASE_CLASS(oc); 362*4fb37aeaSArnaud Minier 363*4fb37aeaSArnaud Minier subc->type = STM32L4x5_UART; 364*4fb37aeaSArnaud Minier } 365*4fb37aeaSArnaud Minier 366*4fb37aeaSArnaud Minier static void stm32l4x5_lpuart_class_init(ObjectClass *oc, void *data) 367*4fb37aeaSArnaud Minier { 368*4fb37aeaSArnaud Minier Stm32l4x5UsartBaseClass *subc = STM32L4X5_USART_BASE_CLASS(oc); 369*4fb37aeaSArnaud Minier 370*4fb37aeaSArnaud Minier subc->type = STM32L4x5_LPUART; 371*4fb37aeaSArnaud Minier } 372*4fb37aeaSArnaud Minier 373*4fb37aeaSArnaud Minier static const TypeInfo stm32l4x5_usart_types[] = { 374*4fb37aeaSArnaud Minier { 375*4fb37aeaSArnaud Minier .name = TYPE_STM32L4X5_USART_BASE, 376*4fb37aeaSArnaud Minier .parent = TYPE_SYS_BUS_DEVICE, 377*4fb37aeaSArnaud Minier .instance_size = sizeof(Stm32l4x5UsartBaseState), 378*4fb37aeaSArnaud Minier .instance_init = stm32l4x5_usart_base_init, 379*4fb37aeaSArnaud Minier .class_init = stm32l4x5_usart_base_class_init, 380*4fb37aeaSArnaud Minier .abstract = true, 381*4fb37aeaSArnaud Minier }, { 382*4fb37aeaSArnaud Minier .name = TYPE_STM32L4X5_USART, 383*4fb37aeaSArnaud Minier .parent = TYPE_STM32L4X5_USART_BASE, 384*4fb37aeaSArnaud Minier .class_init = stm32l4x5_usart_class_init, 385*4fb37aeaSArnaud Minier }, { 386*4fb37aeaSArnaud Minier .name = TYPE_STM32L4X5_UART, 387*4fb37aeaSArnaud Minier .parent = TYPE_STM32L4X5_USART_BASE, 388*4fb37aeaSArnaud Minier .class_init = stm32l4x5_uart_class_init, 389*4fb37aeaSArnaud Minier }, { 390*4fb37aeaSArnaud Minier .name = TYPE_STM32L4X5_LPUART, 391*4fb37aeaSArnaud Minier .parent = TYPE_STM32L4X5_USART_BASE, 392*4fb37aeaSArnaud Minier .class_init = stm32l4x5_lpuart_class_init, 393*4fb37aeaSArnaud Minier } 394*4fb37aeaSArnaud Minier }; 395*4fb37aeaSArnaud Minier 396*4fb37aeaSArnaud Minier DEFINE_TYPES(stm32l4x5_usart_types) 397