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/sysbus.h"
29 #include "hw/adc/stm32f2xx_adc.h"
31 #ifndef STM_ADC_ERR_DEBUG
32 #define STM_ADC_ERR_DEBUG 0
35 #define DB_PRINT_L(lvl, fmt, args...) do { \
36 if (STM_ADC_ERR_DEBUG >= lvl) { \
37 qemu_log("%s: " fmt, __func__, ## args); \
41 #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
43 static void stm32f2xx_adc_reset(DeviceState
*dev
)
45 STM32F2XXADCState
*s
= STM32F2XX_ADC(dev
);
47 s
->adc_sr
= 0x00000000;
48 s
->adc_cr1
= 0x00000000;
49 s
->adc_cr2
= 0x00000000;
50 s
->adc_smpr1
= 0x00000000;
51 s
->adc_smpr2
= 0x00000000;
52 s
->adc_jofr
[0] = 0x00000000;
53 s
->adc_jofr
[1] = 0x00000000;
54 s
->adc_jofr
[2] = 0x00000000;
55 s
->adc_jofr
[3] = 0x00000000;
56 s
->adc_htr
= 0x00000FFF;
57 s
->adc_ltr
= 0x00000000;
58 s
->adc_sqr1
= 0x00000000;
59 s
->adc_sqr2
= 0x00000000;
60 s
->adc_sqr3
= 0x00000000;
61 s
->adc_jsqr
= 0x00000000;
62 s
->adc_jdr
[0] = 0x00000000;
63 s
->adc_jdr
[1] = 0x00000000;
64 s
->adc_jdr
[2] = 0x00000000;
65 s
->adc_jdr
[3] = 0x00000000;
66 s
->adc_dr
= 0x00000000;
69 static uint32_t stm32f2xx_adc_generate_value(STM32F2XXADCState
*s
)
71 /* Attempts to fake some ADC values */
72 s
->adc_dr
= s
->adc_dr
+ 7;
74 switch ((s
->adc_cr1
& ADC_CR1_RES
) >> 24) {
92 if (s
->adc_cr2
& ADC_CR2_ALIGN
) {
93 return (s
->adc_dr
<< 1) & 0xFFF0;
99 static uint64_t stm32f2xx_adc_read(void *opaque
, hwaddr addr
,
102 STM32F2XXADCState
*s
= opaque
;
104 DB_PRINT("Address: 0x%" HWADDR_PRIx
"\n", addr
);
106 if (addr
>= ADC_COMMON_ADDRESS
) {
107 qemu_log_mask(LOG_UNIMP
,
108 "%s: ADC Common Register Unsupported\n", __func__
);
117 return s
->adc_cr2
& 0xFFFFFFF;
126 qemu_log_mask(LOG_UNIMP
, "%s: " \
127 "Injection ADC is not implemented, the registers are " \
128 "included for compatibility\n", __func__
);
129 return s
->adc_jofr
[(addr
- ADC_JOFR1
) / 4];
141 qemu_log_mask(LOG_UNIMP
, "%s: " \
142 "Injection ADC is not implemented, the registers are " \
143 "included for compatibility\n", __func__
);
149 qemu_log_mask(LOG_UNIMP
, "%s: " \
150 "Injection ADC is not implemented, the registers are " \
151 "included for compatibility\n", __func__
);
152 return s
->adc_jdr
[(addr
- ADC_JDR1
) / 4] -
153 s
->adc_jofr
[(addr
- ADC_JDR1
) / 4];
155 if ((s
->adc_cr2
& ADC_CR2_ADON
) && (s
->adc_cr2
& ADC_CR2_SWSTART
)) {
156 s
->adc_cr2
^= ADC_CR2_SWSTART
;
157 return stm32f2xx_adc_generate_value(s
);
162 qemu_log_mask(LOG_GUEST_ERROR
,
163 "%s: Bad offset 0x%" HWADDR_PRIx
"\n", __func__
, addr
);
169 static void stm32f2xx_adc_write(void *opaque
, hwaddr addr
,
170 uint64_t val64
, unsigned int size
)
172 STM32F2XXADCState
*s
= opaque
;
173 uint32_t value
= (uint32_t) val64
;
175 DB_PRINT("Address: 0x%" HWADDR_PRIx
", Value: 0x%x\n",
179 qemu_log_mask(LOG_UNIMP
,
180 "%s: ADC Common Register Unsupported\n", __func__
);
185 s
->adc_sr
&= (value
& 0x3F);
194 s
->adc_smpr1
= value
;
197 s
->adc_smpr2
= value
;
203 s
->adc_jofr
[(addr
- ADC_JOFR1
) / 4] = (value
& 0xFFF);
204 qemu_log_mask(LOG_UNIMP
, "%s: " \
205 "Injection ADC is not implemented, the registers are " \
206 "included for compatibility\n", __func__
);
225 qemu_log_mask(LOG_UNIMP
, "%s: " \
226 "Injection ADC is not implemented, the registers are " \
227 "included for compatibility\n", __func__
);
233 s
->adc_jdr
[(addr
- ADC_JDR1
) / 4] = value
;
234 qemu_log_mask(LOG_UNIMP
, "%s: " \
235 "Injection ADC is not implemented, the registers are " \
236 "included for compatibility\n", __func__
);
239 qemu_log_mask(LOG_GUEST_ERROR
,
240 "%s: Bad offset 0x%" HWADDR_PRIx
"\n", __func__
, addr
);
244 static const MemoryRegionOps stm32f2xx_adc_ops
= {
245 .read
= stm32f2xx_adc_read
,
246 .write
= stm32f2xx_adc_write
,
247 .endianness
= DEVICE_NATIVE_ENDIAN
,
250 static const VMStateDescription vmstate_stm32f2xx_adc
= {
251 .name
= TYPE_STM32F2XX_ADC
,
253 .minimum_version_id
= 1,
254 .fields
= (VMStateField
[]) {
255 VMSTATE_UINT32(adc_sr
, STM32F2XXADCState
),
256 VMSTATE_UINT32(adc_cr1
, STM32F2XXADCState
),
257 VMSTATE_UINT32(adc_cr2
, STM32F2XXADCState
),
258 VMSTATE_UINT32(adc_smpr1
, STM32F2XXADCState
),
259 VMSTATE_UINT32(adc_smpr2
, STM32F2XXADCState
),
260 VMSTATE_UINT32_ARRAY(adc_jofr
, STM32F2XXADCState
, 4),
261 VMSTATE_UINT32(adc_htr
, STM32F2XXADCState
),
262 VMSTATE_UINT32(adc_ltr
, STM32F2XXADCState
),
263 VMSTATE_UINT32(adc_sqr1
, STM32F2XXADCState
),
264 VMSTATE_UINT32(adc_sqr2
, STM32F2XXADCState
),
265 VMSTATE_UINT32(adc_sqr3
, STM32F2XXADCState
),
266 VMSTATE_UINT32(adc_jsqr
, STM32F2XXADCState
),
267 VMSTATE_UINT32_ARRAY(adc_jdr
, STM32F2XXADCState
, 4),
268 VMSTATE_UINT32(adc_dr
, STM32F2XXADCState
),
269 VMSTATE_END_OF_LIST()
273 static void stm32f2xx_adc_init(Object
*obj
)
275 STM32F2XXADCState
*s
= STM32F2XX_ADC(obj
);
277 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
279 memory_region_init_io(&s
->mmio
, obj
, &stm32f2xx_adc_ops
, s
,
280 TYPE_STM32F2XX_ADC
, 0xFF);
281 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->mmio
);
284 static void stm32f2xx_adc_class_init(ObjectClass
*klass
, void *data
)
286 DeviceClass
*dc
= DEVICE_CLASS(klass
);
288 dc
->reset
= stm32f2xx_adc_reset
;
289 dc
->vmsd
= &vmstate_stm32f2xx_adc
;
292 static const TypeInfo stm32f2xx_adc_info
= {
293 .name
= TYPE_STM32F2XX_ADC
,
294 .parent
= TYPE_SYS_BUS_DEVICE
,
295 .instance_size
= sizeof(STM32F2XXADCState
),
296 .instance_init
= stm32f2xx_adc_init
,
297 .class_init
= stm32f2xx_adc_class_init
,
300 static void stm32f2xx_adc_register_types(void)
302 type_register_static(&stm32f2xx_adc_info
);
305 type_init(stm32f2xx_adc_register_types
)