4 * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "hw/char/stm32f2xx_usart.h"
29 #ifndef STM_USART_ERR_DEBUG
30 #define STM_USART_ERR_DEBUG 0
33 #define DB_PRINT_L(lvl, fmt, args...) do { \
34 if (STM_USART_ERR_DEBUG >= lvl) { \
35 qemu_log("%s: " fmt, __func__, ## args); \
39 #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
41 static int stm32f2xx_usart_can_receive(void *opaque
)
43 STM32F2XXUsartState
*s
= opaque
;
45 if (!(s
->usart_sr
& USART_SR_RXNE
)) {
52 static void stm32f2xx_usart_receive(void *opaque
, const uint8_t *buf
, int size
)
54 STM32F2XXUsartState
*s
= opaque
;
58 if (!(s
->usart_cr1
& USART_CR1_UE
&& s
->usart_cr1
& USART_CR1_RE
)) {
59 /* USART not enabled - drop the chars */
60 DB_PRINT("Dropping the chars\n");
64 s
->usart_sr
|= USART_SR_RXNE
;
66 if (s
->usart_cr1
& USART_CR1_RXNEIE
) {
67 qemu_set_irq(s
->irq
, 1);
70 DB_PRINT("Receiving: %c\n", s
->usart_dr
);
73 static void stm32f2xx_usart_reset(DeviceState
*dev
)
75 STM32F2XXUsartState
*s
= STM32F2XX_USART(dev
);
77 s
->usart_sr
= USART_SR_RESET
;
78 s
->usart_dr
= 0x00000000;
79 s
->usart_brr
= 0x00000000;
80 s
->usart_cr1
= 0x00000000;
81 s
->usart_cr2
= 0x00000000;
82 s
->usart_cr3
= 0x00000000;
83 s
->usart_gtpr
= 0x00000000;
85 qemu_set_irq(s
->irq
, 0);
88 static uint64_t stm32f2xx_usart_read(void *opaque
, hwaddr addr
,
91 STM32F2XXUsartState
*s
= opaque
;
94 DB_PRINT("Read 0x%"HWADDR_PRIx
"\n", addr
);
98 retvalue
= s
->usart_sr
;
99 s
->usart_sr
&= ~USART_SR_TC
;
101 qemu_chr_accept_input(s
->chr
);
105 DB_PRINT("Value: 0x%" PRIx32
", %c\n", s
->usart_dr
, (char) s
->usart_dr
);
106 s
->usart_sr
|= USART_SR_TXE
;
107 s
->usart_sr
&= ~USART_SR_RXNE
;
109 qemu_chr_accept_input(s
->chr
);
111 qemu_set_irq(s
->irq
, 0);
112 return s
->usart_dr
& 0x3FF;
122 return s
->usart_gtpr
;
124 qemu_log_mask(LOG_GUEST_ERROR
,
125 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, addr
);
132 static void stm32f2xx_usart_write(void *opaque
, hwaddr addr
,
133 uint64_t val64
, unsigned int size
)
135 STM32F2XXUsartState
*s
= opaque
;
136 uint32_t value
= val64
;
139 DB_PRINT("Write 0x%" PRIx32
", 0x%"HWADDR_PRIx
"\n", value
, addr
);
143 if (value
<= 0x3FF) {
146 s
->usart_sr
&= value
;
148 if (!(s
->usart_sr
& USART_SR_RXNE
)) {
149 qemu_set_irq(s
->irq
, 0);
153 if (value
< 0xF000) {
156 /* XXX this blocks entire thread. Rewrite to use
157 * qemu_chr_fe_write and background I/O callbacks */
158 qemu_chr_fe_write_all(s
->chr
, &ch
, 1);
160 s
->usart_sr
|= USART_SR_TC
;
161 s
->usart_sr
&= ~USART_SR_TXE
;
165 s
->usart_brr
= value
;
168 s
->usart_cr1
= value
;
169 if (s
->usart_cr1
& USART_CR1_RXNEIE
&&
170 s
->usart_sr
& USART_SR_RXNE
) {
171 qemu_set_irq(s
->irq
, 1);
175 s
->usart_cr2
= value
;
178 s
->usart_cr3
= value
;
181 s
->usart_gtpr
= value
;
184 qemu_log_mask(LOG_GUEST_ERROR
,
185 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, addr
);
189 static const MemoryRegionOps stm32f2xx_usart_ops
= {
190 .read
= stm32f2xx_usart_read
,
191 .write
= stm32f2xx_usart_write
,
192 .endianness
= DEVICE_NATIVE_ENDIAN
,
195 static Property stm32f2xx_usart_properties
[] = {
196 DEFINE_PROP_CHR("chardev", STM32F2XXUsartState
, chr
),
197 DEFINE_PROP_END_OF_LIST(),
200 static void stm32f2xx_usart_init(Object
*obj
)
202 STM32F2XXUsartState
*s
= STM32F2XX_USART(obj
);
204 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
206 memory_region_init_io(&s
->mmio
, obj
, &stm32f2xx_usart_ops
, s
,
207 TYPE_STM32F2XX_USART
, 0x2000);
208 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->mmio
);
211 static void stm32f2xx_usart_realize(DeviceState
*dev
, Error
**errp
)
213 STM32F2XXUsartState
*s
= STM32F2XX_USART(dev
);
216 qemu_chr_add_handlers(s
->chr
, stm32f2xx_usart_can_receive
,
217 stm32f2xx_usart_receive
, NULL
, s
);
221 static void stm32f2xx_usart_class_init(ObjectClass
*klass
, void *data
)
223 DeviceClass
*dc
= DEVICE_CLASS(klass
);
225 dc
->reset
= stm32f2xx_usart_reset
;
226 dc
->props
= stm32f2xx_usart_properties
;
227 dc
->realize
= stm32f2xx_usart_realize
;
230 static const TypeInfo stm32f2xx_usart_info
= {
231 .name
= TYPE_STM32F2XX_USART
,
232 .parent
= TYPE_SYS_BUS_DEVICE
,
233 .instance_size
= sizeof(STM32F2XXUsartState
),
234 .instance_init
= stm32f2xx_usart_init
,
235 .class_init
= stm32f2xx_usart_class_init
,
238 static void stm32f2xx_usart_register_types(void)
240 type_register_static(&stm32f2xx_usart_info
);
243 type_init(stm32f2xx_usart_register_types
)