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 "qapi/error.h"
27 #include "qemu-common.h"
28 #include "hw/arm/arm.h"
29 #include "exec/address-spaces.h"
30 #include "hw/arm/stm32f205_soc.h"
32 /* At the moment only Timer 2 to 5 are modelled */
33 static const uint32_t timer_addr
[STM_NUM_TIMERS
] = { 0x40000000, 0x40000400,
34 0x40000800, 0x40000C00 };
35 static const uint32_t usart_addr
[STM_NUM_USARTS
] = { 0x40011000, 0x40004400,
36 0x40004800, 0x40004C00, 0x40005000, 0x40011400 };
37 static const uint32_t adc_addr
[STM_NUM_ADCS
] = { 0x40012000, 0x40012100,
39 static const uint32_t spi_addr
[STM_NUM_SPIS
] = { 0x40013000, 0x40003800,
42 static const int timer_irq
[STM_NUM_TIMERS
] = {28, 29, 30, 50};
43 static const int usart_irq
[STM_NUM_USARTS
] = {37, 38, 39, 52, 53, 71};
45 static const int spi_irq
[STM_NUM_SPIS
] = {35, 36, 51};
47 static void stm32f205_soc_initfn(Object
*obj
)
49 STM32F205State
*s
= STM32F205_SOC(obj
);
52 object_initialize(&s
->armv7m
, sizeof(s
->armv7m
), TYPE_ARMV7M
);
53 qdev_set_parent_bus(DEVICE(&s
->armv7m
), sysbus_get_default());
55 object_initialize(&s
->syscfg
, sizeof(s
->syscfg
), TYPE_STM32F2XX_SYSCFG
);
56 qdev_set_parent_bus(DEVICE(&s
->syscfg
), sysbus_get_default());
58 for (i
= 0; i
< STM_NUM_USARTS
; i
++) {
59 object_initialize(&s
->usart
[i
], sizeof(s
->usart
[i
]),
60 TYPE_STM32F2XX_USART
);
61 qdev_set_parent_bus(DEVICE(&s
->usart
[i
]), sysbus_get_default());
64 for (i
= 0; i
< STM_NUM_TIMERS
; i
++) {
65 object_initialize(&s
->timer
[i
], sizeof(s
->timer
[i
]),
66 TYPE_STM32F2XX_TIMER
);
67 qdev_set_parent_bus(DEVICE(&s
->timer
[i
]), sysbus_get_default());
70 s
->adc_irqs
= OR_IRQ(object_new(TYPE_OR_IRQ
));
72 for (i
= 0; i
< STM_NUM_ADCS
; i
++) {
73 object_initialize(&s
->adc
[i
], sizeof(s
->adc
[i
]),
75 qdev_set_parent_bus(DEVICE(&s
->adc
[i
]), sysbus_get_default());
78 for (i
= 0; i
< STM_NUM_SPIS
; i
++) {
79 object_initialize(&s
->spi
[i
], sizeof(s
->spi
[i
]),
81 qdev_set_parent_bus(DEVICE(&s
->spi
[i
]), sysbus_get_default());
85 static void stm32f205_soc_realize(DeviceState
*dev_soc
, Error
**errp
)
87 STM32F205State
*s
= STM32F205_SOC(dev_soc
);
88 DeviceState
*dev
, *armv7m
;
93 MemoryRegion
*system_memory
= get_system_memory();
94 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
95 MemoryRegion
*flash
= g_new(MemoryRegion
, 1);
96 MemoryRegion
*flash_alias
= g_new(MemoryRegion
, 1);
98 memory_region_init_ram(flash
, NULL
, "STM32F205.flash", FLASH_SIZE
,
100 memory_region_init_alias(flash_alias
, NULL
, "STM32F205.flash.alias",
101 flash
, 0, FLASH_SIZE
);
103 vmstate_register_ram_global(flash
);
105 memory_region_set_readonly(flash
, true);
106 memory_region_set_readonly(flash_alias
, true);
108 memory_region_add_subregion(system_memory
, FLASH_BASE_ADDRESS
, flash
);
109 memory_region_add_subregion(system_memory
, 0, flash_alias
);
111 memory_region_init_ram(sram
, NULL
, "STM32F205.sram", SRAM_SIZE
,
113 vmstate_register_ram_global(sram
);
114 memory_region_add_subregion(system_memory
, SRAM_BASE_ADDRESS
, sram
);
116 armv7m
= DEVICE(&s
->armv7m
);
117 qdev_prop_set_uint32(armv7m
, "num-irq", 96);
118 qdev_prop_set_string(armv7m
, "cpu-model", s
->cpu_model
);
119 object_property_set_link(OBJECT(&s
->armv7m
), OBJECT(get_system_memory()),
120 "memory", &error_abort
);
121 object_property_set_bool(OBJECT(&s
->armv7m
), true, "realized", &err
);
123 error_propagate(errp
, err
);
127 /* System configuration controller */
128 dev
= DEVICE(&s
->syscfg
);
129 object_property_set_bool(OBJECT(&s
->syscfg
), true, "realized", &err
);
131 error_propagate(errp
, err
);
134 busdev
= SYS_BUS_DEVICE(dev
);
135 sysbus_mmio_map(busdev
, 0, 0x40013800);
136 sysbus_connect_irq(busdev
, 0, qdev_get_gpio_in(armv7m
, 71));
138 /* Attach UART (uses USART registers) and USART controllers */
139 for (i
= 0; i
< STM_NUM_USARTS
; i
++) {
140 dev
= DEVICE(&(s
->usart
[i
]));
141 qdev_prop_set_chr(dev
, "chardev",
142 i
< MAX_SERIAL_PORTS
? serial_hds
[i
] : NULL
);
143 object_property_set_bool(OBJECT(&s
->usart
[i
]), true, "realized", &err
);
145 error_propagate(errp
, err
);
148 busdev
= SYS_BUS_DEVICE(dev
);
149 sysbus_mmio_map(busdev
, 0, usart_addr
[i
]);
150 sysbus_connect_irq(busdev
, 0, qdev_get_gpio_in(armv7m
, usart_irq
[i
]));
154 for (i
= 0; i
< STM_NUM_TIMERS
; i
++) {
155 dev
= DEVICE(&(s
->timer
[i
]));
156 qdev_prop_set_uint64(dev
, "clock-frequency", 1000000000);
157 object_property_set_bool(OBJECT(&s
->timer
[i
]), true, "realized", &err
);
159 error_propagate(errp
, err
);
162 busdev
= SYS_BUS_DEVICE(dev
);
163 sysbus_mmio_map(busdev
, 0, timer_addr
[i
]);
164 sysbus_connect_irq(busdev
, 0, qdev_get_gpio_in(armv7m
, timer_irq
[i
]));
168 object_property_set_int(OBJECT(s
->adc_irqs
), STM_NUM_ADCS
,
170 object_property_set_bool(OBJECT(s
->adc_irqs
), true, "realized", &err
);
172 error_propagate(errp
, err
);
175 qdev_connect_gpio_out(DEVICE(s
->adc_irqs
), 0,
176 qdev_get_gpio_in(armv7m
, ADC_IRQ
));
178 for (i
= 0; i
< STM_NUM_ADCS
; i
++) {
179 dev
= DEVICE(&(s
->adc
[i
]));
180 object_property_set_bool(OBJECT(&s
->adc
[i
]), true, "realized", &err
);
182 error_propagate(errp
, err
);
185 busdev
= SYS_BUS_DEVICE(dev
);
186 sysbus_mmio_map(busdev
, 0, adc_addr
[i
]);
187 sysbus_connect_irq(busdev
, 0,
188 qdev_get_gpio_in(DEVICE(s
->adc_irqs
), i
));
192 for (i
= 0; i
< STM_NUM_SPIS
; i
++) {
193 dev
= DEVICE(&(s
->spi
[i
]));
194 object_property_set_bool(OBJECT(&s
->spi
[i
]), true, "realized", &err
);
196 error_propagate(errp
, err
);
199 busdev
= SYS_BUS_DEVICE(dev
);
200 sysbus_mmio_map(busdev
, 0, spi_addr
[i
]);
201 sysbus_connect_irq(busdev
, 0, qdev_get_gpio_in(armv7m
, spi_irq
[i
]));
205 static Property stm32f205_soc_properties
[] = {
206 DEFINE_PROP_STRING("cpu-model", STM32F205State
, cpu_model
),
207 DEFINE_PROP_END_OF_LIST(),
210 static void stm32f205_soc_class_init(ObjectClass
*klass
, void *data
)
212 DeviceClass
*dc
= DEVICE_CLASS(klass
);
214 dc
->realize
= stm32f205_soc_realize
;
215 dc
->props
= stm32f205_soc_properties
;
218 static const TypeInfo stm32f205_soc_info
= {
219 .name
= TYPE_STM32F205_SOC
,
220 .parent
= TYPE_SYS_BUS_DEVICE
,
221 .instance_size
= sizeof(STM32F205State
),
222 .instance_init
= stm32f205_soc_initfn
,
223 .class_init
= stm32f205_soc_class_init
,
226 static void stm32f205_soc_types(void)
228 type_register_static(&stm32f205_soc_info
);
231 type_init(stm32f205_soc_types
)