xref: /qemu/hw/arm/stm32f100_soc.c (revision 4a04655c)
10f76debdSAlexandre Iooss /*
20f76debdSAlexandre Iooss  * STM32F100 SoC
30f76debdSAlexandre Iooss  *
40f76debdSAlexandre Iooss  * Copyright (c) 2021 Alexandre Iooss <erdnaxe@crans.org>
50f76debdSAlexandre Iooss  * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
60f76debdSAlexandre Iooss  *
70f76debdSAlexandre Iooss  * Permission is hereby granted, free of charge, to any person obtaining a copy
80f76debdSAlexandre Iooss  * of this software and associated documentation files (the "Software"), to deal
90f76debdSAlexandre Iooss  * in the Software without restriction, including without limitation the rights
100f76debdSAlexandre Iooss  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
110f76debdSAlexandre Iooss  * copies of the Software, and to permit persons to whom the Software is
120f76debdSAlexandre Iooss  * furnished to do so, subject to the following conditions:
130f76debdSAlexandre Iooss  *
140f76debdSAlexandre Iooss  * The above copyright notice and this permission notice shall be included in
150f76debdSAlexandre Iooss  * all copies or substantial portions of the Software.
160f76debdSAlexandre Iooss  *
170f76debdSAlexandre Iooss  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
180f76debdSAlexandre Iooss  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
190f76debdSAlexandre Iooss  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
200f76debdSAlexandre Iooss  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
210f76debdSAlexandre Iooss  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
220f76debdSAlexandre Iooss  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
230f76debdSAlexandre Iooss  * THE SOFTWARE.
240f76debdSAlexandre Iooss  */
250f76debdSAlexandre Iooss 
260f76debdSAlexandre Iooss #include "qemu/osdep.h"
270f76debdSAlexandre Iooss #include "qapi/error.h"
280f76debdSAlexandre Iooss #include "qemu/module.h"
290f76debdSAlexandre Iooss #include "hw/arm/boot.h"
300f76debdSAlexandre Iooss #include "exec/address-spaces.h"
310f76debdSAlexandre Iooss #include "hw/arm/stm32f100_soc.h"
320f76debdSAlexandre Iooss #include "hw/qdev-properties.h"
33b5ff0c61SPeter Maydell #include "hw/qdev-clock.h"
340f76debdSAlexandre Iooss #include "hw/misc/unimp.h"
350f76debdSAlexandre Iooss #include "sysemu/sysemu.h"
360f76debdSAlexandre Iooss 
370f76debdSAlexandre Iooss /* stm32f100_soc implementation is derived from stm32f205_soc */
380f76debdSAlexandre Iooss 
390f76debdSAlexandre Iooss static const uint32_t usart_addr[STM_NUM_USARTS] = { 0x40013800, 0x40004400,
400f76debdSAlexandre Iooss     0x40004800 };
410f76debdSAlexandre Iooss static const uint32_t spi_addr[STM_NUM_SPIS] = { 0x40013000, 0x40003800 };
420f76debdSAlexandre Iooss 
430f76debdSAlexandre Iooss static const int usart_irq[STM_NUM_USARTS] = {37, 38, 39};
440f76debdSAlexandre Iooss static const int spi_irq[STM_NUM_SPIS] = {35, 36};
450f76debdSAlexandre Iooss 
stm32f100_soc_initfn(Object * obj)460f76debdSAlexandre Iooss static void stm32f100_soc_initfn(Object *obj)
470f76debdSAlexandre Iooss {
480f76debdSAlexandre Iooss     STM32F100State *s = STM32F100_SOC(obj);
490f76debdSAlexandre Iooss     int i;
500f76debdSAlexandre Iooss 
510f76debdSAlexandre Iooss     object_initialize_child(obj, "armv7m", &s->armv7m, TYPE_ARMV7M);
520f76debdSAlexandre Iooss 
530f76debdSAlexandre Iooss     for (i = 0; i < STM_NUM_USARTS; i++) {
540f76debdSAlexandre Iooss         object_initialize_child(obj, "usart[*]", &s->usart[i],
550f76debdSAlexandre Iooss                                 TYPE_STM32F2XX_USART);
560f76debdSAlexandre Iooss     }
570f76debdSAlexandre Iooss 
580f76debdSAlexandre Iooss     for (i = 0; i < STM_NUM_SPIS; i++) {
590f76debdSAlexandre Iooss         object_initialize_child(obj, "spi[*]", &s->spi[i], TYPE_STM32F2XX_SPI);
600f76debdSAlexandre Iooss     }
61b5ff0c61SPeter Maydell 
62b5ff0c61SPeter Maydell     s->sysclk = qdev_init_clock_in(DEVICE(s), "sysclk", NULL, NULL, 0);
63b5ff0c61SPeter Maydell     s->refclk = qdev_init_clock_in(DEVICE(s), "refclk", NULL, NULL, 0);
640f76debdSAlexandre Iooss }
650f76debdSAlexandre Iooss 
stm32f100_soc_realize(DeviceState * dev_soc,Error ** errp)660f76debdSAlexandre Iooss static void stm32f100_soc_realize(DeviceState *dev_soc, Error **errp)
670f76debdSAlexandre Iooss {
680f76debdSAlexandre Iooss     STM32F100State *s = STM32F100_SOC(dev_soc);
690f76debdSAlexandre Iooss     DeviceState *dev, *armv7m;
700f76debdSAlexandre Iooss     SysBusDevice *busdev;
710f76debdSAlexandre Iooss     int i;
720f76debdSAlexandre Iooss 
730f76debdSAlexandre Iooss     MemoryRegion *system_memory = get_system_memory();
740f76debdSAlexandre Iooss 
750f76debdSAlexandre Iooss     /*
76b5ff0c61SPeter Maydell      * We use s->refclk internally and only define it with qdev_init_clock_in()
77b5ff0c61SPeter Maydell      * so it is correctly parented and not leaked on an init/deinit; it is not
78b5ff0c61SPeter Maydell      * intended as an externally exposed clock.
79b5ff0c61SPeter Maydell      */
80b5ff0c61SPeter Maydell     if (clock_has_source(s->refclk)) {
81b5ff0c61SPeter Maydell         error_setg(errp, "refclk clock must not be wired up by the board code");
82b5ff0c61SPeter Maydell         return;
83b5ff0c61SPeter Maydell     }
84b5ff0c61SPeter Maydell 
85b5ff0c61SPeter Maydell     if (!clock_has_source(s->sysclk)) {
86b5ff0c61SPeter Maydell         error_setg(errp, "sysclk clock must be wired up by the board code");
87b5ff0c61SPeter Maydell         return;
88b5ff0c61SPeter Maydell     }
89b5ff0c61SPeter Maydell 
90b5ff0c61SPeter Maydell     /*
91b5ff0c61SPeter Maydell      * TODO: ideally we should model the SoC RCC and its ability to
92b5ff0c61SPeter Maydell      * change the sysclk frequency and define different sysclk sources.
93b5ff0c61SPeter Maydell      */
94b5ff0c61SPeter Maydell 
95b5ff0c61SPeter Maydell     /* The refclk always runs at frequency HCLK / 8 */
96b5ff0c61SPeter Maydell     clock_set_mul_div(s->refclk, 8, 1);
97b5ff0c61SPeter Maydell     clock_set_source(s->refclk, s->sysclk);
98b5ff0c61SPeter Maydell 
99b5ff0c61SPeter Maydell     /*
1000f76debdSAlexandre Iooss      * Init flash region
1010f76debdSAlexandre Iooss      * Flash starts at 0x08000000 and then is aliased to boot memory at 0x0
1020f76debdSAlexandre Iooss      */
103cabc613fSPeter Maydell     memory_region_init_rom(&s->flash, OBJECT(dev_soc), "STM32F100.flash",
1040f76debdSAlexandre Iooss                            FLASH_SIZE, &error_fatal);
105cabc613fSPeter Maydell     memory_region_init_alias(&s->flash_alias, OBJECT(dev_soc),
106cabc613fSPeter Maydell                              "STM32F100.flash.alias", &s->flash, 0, FLASH_SIZE);
107cabc613fSPeter Maydell     memory_region_add_subregion(system_memory, FLASH_BASE_ADDRESS, &s->flash);
108cabc613fSPeter Maydell     memory_region_add_subregion(system_memory, 0, &s->flash_alias);
1090f76debdSAlexandre Iooss 
1100f76debdSAlexandre Iooss     /* Init SRAM region */
111cabc613fSPeter Maydell     memory_region_init_ram(&s->sram, NULL, "STM32F100.sram", SRAM_SIZE,
1120f76debdSAlexandre Iooss                            &error_fatal);
113cabc613fSPeter Maydell     memory_region_add_subregion(system_memory, SRAM_BASE_ADDRESS, &s->sram);
1140f76debdSAlexandre Iooss 
1150f76debdSAlexandre Iooss     /* Init ARMv7m */
1160f76debdSAlexandre Iooss     armv7m = DEVICE(&s->armv7m);
1170f76debdSAlexandre Iooss     qdev_prop_set_uint32(armv7m, "num-irq", 61);
1184a04655cSSamuel Tardieu     qdev_prop_set_uint8(armv7m, "num-prio-bits", 4);
119d6528660SPhilippe Mathieu-Daudé     qdev_prop_set_string(armv7m, "cpu-type", ARM_CPU_TYPE_NAME("cortex-m3"));
1200f76debdSAlexandre Iooss     qdev_prop_set_bit(armv7m, "enable-bitband", true);
121b5ff0c61SPeter Maydell     qdev_connect_clock_in(armv7m, "cpuclk", s->sysclk);
122b5ff0c61SPeter Maydell     qdev_connect_clock_in(armv7m, "refclk", s->refclk);
1230f76debdSAlexandre Iooss     object_property_set_link(OBJECT(&s->armv7m), "memory",
1240f76debdSAlexandre Iooss                              OBJECT(get_system_memory()), &error_abort);
1250f76debdSAlexandre Iooss     if (!sysbus_realize(SYS_BUS_DEVICE(&s->armv7m), errp)) {
1260f76debdSAlexandre Iooss         return;
1270f76debdSAlexandre Iooss     }
1280f76debdSAlexandre Iooss 
1290f76debdSAlexandre Iooss     /* Attach UART (uses USART registers) and USART controllers */
1300f76debdSAlexandre Iooss     for (i = 0; i < STM_NUM_USARTS; i++) {
1310f76debdSAlexandre Iooss         dev = DEVICE(&(s->usart[i]));
1320f76debdSAlexandre Iooss         qdev_prop_set_chr(dev, "chardev", serial_hd(i));
1330f76debdSAlexandre Iooss         if (!sysbus_realize(SYS_BUS_DEVICE(&s->usart[i]), errp)) {
1340f76debdSAlexandre Iooss             return;
1350f76debdSAlexandre Iooss         }
1360f76debdSAlexandre Iooss         busdev = SYS_BUS_DEVICE(dev);
1370f76debdSAlexandre Iooss         sysbus_mmio_map(busdev, 0, usart_addr[i]);
1380f76debdSAlexandre Iooss         sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, usart_irq[i]));
1390f76debdSAlexandre Iooss     }
1400f76debdSAlexandre Iooss 
1410f76debdSAlexandre Iooss     /* SPI 1 and 2 */
1420f76debdSAlexandre Iooss     for (i = 0; i < STM_NUM_SPIS; i++) {
1430f76debdSAlexandre Iooss         dev = DEVICE(&(s->spi[i]));
1440f76debdSAlexandre Iooss         if (!sysbus_realize(SYS_BUS_DEVICE(&s->spi[i]), errp)) {
1450f76debdSAlexandre Iooss             return;
1460f76debdSAlexandre Iooss         }
1470f76debdSAlexandre Iooss         busdev = SYS_BUS_DEVICE(dev);
1480f76debdSAlexandre Iooss         sysbus_mmio_map(busdev, 0, spi_addr[i]);
1490f76debdSAlexandre Iooss         sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, spi_irq[i]));
1500f76debdSAlexandre Iooss     }
1510f76debdSAlexandre Iooss 
1520f76debdSAlexandre Iooss     create_unimplemented_device("timer[2]",  0x40000000, 0x400);
1530f76debdSAlexandre Iooss     create_unimplemented_device("timer[3]",  0x40000400, 0x400);
1540f76debdSAlexandre Iooss     create_unimplemented_device("timer[4]",  0x40000800, 0x400);
1550f76debdSAlexandre Iooss     create_unimplemented_device("timer[6]",  0x40001000, 0x400);
1560f76debdSAlexandre Iooss     create_unimplemented_device("timer[7]",  0x40001400, 0x400);
1570f76debdSAlexandre Iooss     create_unimplemented_device("RTC",       0x40002800, 0x400);
1580f76debdSAlexandre Iooss     create_unimplemented_device("WWDG",      0x40002C00, 0x400);
1590f76debdSAlexandre Iooss     create_unimplemented_device("IWDG",      0x40003000, 0x400);
1600f76debdSAlexandre Iooss     create_unimplemented_device("I2C1",      0x40005400, 0x400);
1610f76debdSAlexandre Iooss     create_unimplemented_device("I2C2",      0x40005800, 0x400);
1620f76debdSAlexandre Iooss     create_unimplemented_device("BKP",       0x40006C00, 0x400);
1630f76debdSAlexandre Iooss     create_unimplemented_device("PWR",       0x40007000, 0x400);
1640f76debdSAlexandre Iooss     create_unimplemented_device("DAC",       0x40007400, 0x400);
1650f76debdSAlexandre Iooss     create_unimplemented_device("CEC",       0x40007800, 0x400);
1660f76debdSAlexandre Iooss     create_unimplemented_device("AFIO",      0x40010000, 0x400);
1670f76debdSAlexandre Iooss     create_unimplemented_device("EXTI",      0x40010400, 0x400);
1680f76debdSAlexandre Iooss     create_unimplemented_device("GPIOA",     0x40010800, 0x400);
1690f76debdSAlexandre Iooss     create_unimplemented_device("GPIOB",     0x40010C00, 0x400);
1700f76debdSAlexandre Iooss     create_unimplemented_device("GPIOC",     0x40011000, 0x400);
1710f76debdSAlexandre Iooss     create_unimplemented_device("GPIOD",     0x40011400, 0x400);
1720f76debdSAlexandre Iooss     create_unimplemented_device("GPIOE",     0x40011800, 0x400);
1730f76debdSAlexandre Iooss     create_unimplemented_device("ADC1",      0x40012400, 0x400);
1740f76debdSAlexandre Iooss     create_unimplemented_device("timer[1]",  0x40012C00, 0x400);
1750f76debdSAlexandre Iooss     create_unimplemented_device("timer[15]", 0x40014000, 0x400);
1760f76debdSAlexandre Iooss     create_unimplemented_device("timer[16]", 0x40014400, 0x400);
1770f76debdSAlexandre Iooss     create_unimplemented_device("timer[17]", 0x40014800, 0x400);
1780f76debdSAlexandre Iooss     create_unimplemented_device("DMA",       0x40020000, 0x400);
1790f76debdSAlexandre Iooss     create_unimplemented_device("RCC",       0x40021000, 0x400);
1800f76debdSAlexandre Iooss     create_unimplemented_device("Flash Int", 0x40022000, 0x400);
1810f76debdSAlexandre Iooss     create_unimplemented_device("CRC",       0x40023000, 0x400);
1820f76debdSAlexandre Iooss }
1830f76debdSAlexandre Iooss 
stm32f100_soc_class_init(ObjectClass * klass,void * data)1840f76debdSAlexandre Iooss static void stm32f100_soc_class_init(ObjectClass *klass, void *data)
1850f76debdSAlexandre Iooss {
1860f76debdSAlexandre Iooss     DeviceClass *dc = DEVICE_CLASS(klass);
1870f76debdSAlexandre Iooss 
1880f76debdSAlexandre Iooss     dc->realize = stm32f100_soc_realize;
189d6528660SPhilippe Mathieu-Daudé     /* No vmstate or reset required: device has no internal state */
1900f76debdSAlexandre Iooss }
1910f76debdSAlexandre Iooss 
1920f76debdSAlexandre Iooss static const TypeInfo stm32f100_soc_info = {
1930f76debdSAlexandre Iooss     .name          = TYPE_STM32F100_SOC,
1940f76debdSAlexandre Iooss     .parent        = TYPE_SYS_BUS_DEVICE,
1950f76debdSAlexandre Iooss     .instance_size = sizeof(STM32F100State),
1960f76debdSAlexandre Iooss     .instance_init = stm32f100_soc_initfn,
1970f76debdSAlexandre Iooss     .class_init    = stm32f100_soc_class_init,
1980f76debdSAlexandre Iooss };
1990f76debdSAlexandre Iooss 
stm32f100_soc_types(void)2000f76debdSAlexandre Iooss static void stm32f100_soc_types(void)
2010f76debdSAlexandre Iooss {
2020f76debdSAlexandre Iooss     type_register_static(&stm32f100_soc_info);
2030f76debdSAlexandre Iooss }
2040f76debdSAlexandre Iooss 
2050f76debdSAlexandre Iooss type_init(stm32f100_soc_types)
206