1673b2d42SJoel Stanley /* 2673b2d42SJoel Stanley * Nordic Semiconductor nRF51 SoC 3673b2d42SJoel Stanley * http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.1.pdf 4673b2d42SJoel Stanley * 5673b2d42SJoel Stanley * Copyright 2018 Joel Stanley <joel@jms.id.au> 6673b2d42SJoel Stanley * 7673b2d42SJoel Stanley * This code is licensed under the GPL version 2 or later. See 8673b2d42SJoel Stanley * the COPYING file in the top-level directory. 9673b2d42SJoel Stanley */ 10673b2d42SJoel Stanley 11673b2d42SJoel Stanley #include "qemu/osdep.h" 12673b2d42SJoel Stanley #include "qapi/error.h" 13673b2d42SJoel Stanley #include "qemu-common.h" 14673b2d42SJoel Stanley #include "hw/arm/arm.h" 15673b2d42SJoel Stanley #include "hw/sysbus.h" 16673b2d42SJoel Stanley #include "hw/boards.h" 17673b2d42SJoel Stanley #include "hw/devices.h" 18673b2d42SJoel Stanley #include "hw/misc/unimp.h" 19673b2d42SJoel Stanley #include "exec/address-spaces.h" 20673b2d42SJoel Stanley #include "sysemu/sysemu.h" 21673b2d42SJoel Stanley #include "qemu/log.h" 22673b2d42SJoel Stanley #include "cpu.h" 23673b2d42SJoel Stanley 24673b2d42SJoel Stanley #include "hw/arm/nrf51_soc.h" 25673b2d42SJoel Stanley 26673b2d42SJoel Stanley #define IOMEM_BASE 0x40000000 27673b2d42SJoel Stanley #define IOMEM_SIZE 0x20000000 28673b2d42SJoel Stanley 29673b2d42SJoel Stanley #define FICR_BASE 0x10000000 30673b2d42SJoel Stanley #define FICR_SIZE 0x000000fc 31673b2d42SJoel Stanley 32673b2d42SJoel Stanley #define FLASH_BASE 0x00000000 33673b2d42SJoel Stanley #define SRAM_BASE 0x20000000 34673b2d42SJoel Stanley 35673b2d42SJoel Stanley #define PRIVATE_BASE 0xF0000000 36673b2d42SJoel Stanley #define PRIVATE_SIZE 0x10000000 37673b2d42SJoel Stanley 38673b2d42SJoel Stanley /* 39673b2d42SJoel Stanley * The size and base is for the NRF51822 part. If other parts 40673b2d42SJoel Stanley * are supported in the future, add a sub-class of NRF51SoC for 41673b2d42SJoel Stanley * the specific variants 42673b2d42SJoel Stanley */ 43673b2d42SJoel Stanley #define NRF51822_FLASH_SIZE (256 * 1024) 44673b2d42SJoel Stanley #define NRF51822_SRAM_SIZE (16 * 1024) 45673b2d42SJoel Stanley 46*b0014913SJulia Suvorova #define BASE_TO_IRQ(base) ((base >> 12) & 0x1F) 47*b0014913SJulia Suvorova 48673b2d42SJoel Stanley static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp) 49673b2d42SJoel Stanley { 50673b2d42SJoel Stanley NRF51State *s = NRF51_SOC(dev_soc); 51*b0014913SJulia Suvorova MemoryRegion *mr; 52673b2d42SJoel Stanley Error *err = NULL; 53673b2d42SJoel Stanley 54673b2d42SJoel Stanley if (!s->board_memory) { 55673b2d42SJoel Stanley error_setg(errp, "memory property was not set"); 56673b2d42SJoel Stanley return; 57673b2d42SJoel Stanley } 58673b2d42SJoel Stanley 59673b2d42SJoel Stanley object_property_set_link(OBJECT(&s->cpu), OBJECT(&s->container), "memory", 60673b2d42SJoel Stanley &err); 61673b2d42SJoel Stanley if (err) { 62673b2d42SJoel Stanley error_propagate(errp, err); 63673b2d42SJoel Stanley return; 64673b2d42SJoel Stanley } 65673b2d42SJoel Stanley object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err); 66673b2d42SJoel Stanley if (err) { 67673b2d42SJoel Stanley error_propagate(errp, err); 68673b2d42SJoel Stanley return; 69673b2d42SJoel Stanley } 70673b2d42SJoel Stanley 71673b2d42SJoel Stanley memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1); 72673b2d42SJoel Stanley 73673b2d42SJoel Stanley memory_region_init_rom(&s->flash, OBJECT(s), "nrf51.flash", s->flash_size, 74673b2d42SJoel Stanley &err); 75673b2d42SJoel Stanley if (err) { 76673b2d42SJoel Stanley error_propagate(errp, err); 77673b2d42SJoel Stanley return; 78673b2d42SJoel Stanley } 79673b2d42SJoel Stanley memory_region_add_subregion(&s->container, FLASH_BASE, &s->flash); 80673b2d42SJoel Stanley 81673b2d42SJoel Stanley memory_region_init_ram(&s->sram, NULL, "nrf51.sram", s->sram_size, &err); 82673b2d42SJoel Stanley if (err) { 83673b2d42SJoel Stanley error_propagate(errp, err); 84673b2d42SJoel Stanley return; 85673b2d42SJoel Stanley } 86673b2d42SJoel Stanley memory_region_add_subregion(&s->container, SRAM_BASE, &s->sram); 87673b2d42SJoel Stanley 88*b0014913SJulia Suvorova /* UART */ 89*b0014913SJulia Suvorova object_property_set_bool(OBJECT(&s->uart), true, "realized", &err); 90*b0014913SJulia Suvorova if (err) { 91*b0014913SJulia Suvorova error_propagate(errp, err); 92*b0014913SJulia Suvorova return; 93*b0014913SJulia Suvorova } 94*b0014913SJulia Suvorova mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->uart), 0); 95*b0014913SJulia Suvorova memory_region_add_subregion_overlap(&s->container, UART_BASE, mr, 0); 96*b0014913SJulia Suvorova sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart), 0, 97*b0014913SJulia Suvorova qdev_get_gpio_in(DEVICE(&s->cpu), 98*b0014913SJulia Suvorova BASE_TO_IRQ(UART_BASE))); 99*b0014913SJulia Suvorova 100673b2d42SJoel Stanley create_unimplemented_device("nrf51_soc.io", IOMEM_BASE, IOMEM_SIZE); 101673b2d42SJoel Stanley create_unimplemented_device("nrf51_soc.ficr", FICR_BASE, FICR_SIZE); 102673b2d42SJoel Stanley create_unimplemented_device("nrf51_soc.private", 103673b2d42SJoel Stanley PRIVATE_BASE, PRIVATE_SIZE); 104673b2d42SJoel Stanley } 105673b2d42SJoel Stanley 106673b2d42SJoel Stanley static void nrf51_soc_init(Object *obj) 107673b2d42SJoel Stanley { 108673b2d42SJoel Stanley NRF51State *s = NRF51_SOC(obj); 109673b2d42SJoel Stanley 110673b2d42SJoel Stanley memory_region_init(&s->container, obj, "nrf51-container", UINT64_MAX); 111673b2d42SJoel Stanley 112673b2d42SJoel Stanley sysbus_init_child_obj(OBJECT(s), "armv6m", OBJECT(&s->cpu), sizeof(s->cpu), 113673b2d42SJoel Stanley TYPE_ARMV7M); 114673b2d42SJoel Stanley qdev_prop_set_string(DEVICE(&s->cpu), "cpu-type", 115673b2d42SJoel Stanley ARM_CPU_TYPE_NAME("cortex-m0")); 116673b2d42SJoel Stanley qdev_prop_set_uint32(DEVICE(&s->cpu), "num-irq", 32); 117*b0014913SJulia Suvorova 118*b0014913SJulia Suvorova sysbus_init_child_obj(obj, "uart", &s->uart, sizeof(s->uart), 119*b0014913SJulia Suvorova TYPE_NRF51_UART); 120*b0014913SJulia Suvorova object_property_add_alias(obj, "serial0", OBJECT(&s->uart), "chardev", 121*b0014913SJulia Suvorova &error_abort); 122673b2d42SJoel Stanley } 123673b2d42SJoel Stanley 124673b2d42SJoel Stanley static Property nrf51_soc_properties[] = { 125673b2d42SJoel Stanley DEFINE_PROP_LINK("memory", NRF51State, board_memory, TYPE_MEMORY_REGION, 126673b2d42SJoel Stanley MemoryRegion *), 127673b2d42SJoel Stanley DEFINE_PROP_UINT32("sram-size", NRF51State, sram_size, NRF51822_SRAM_SIZE), 128673b2d42SJoel Stanley DEFINE_PROP_UINT32("flash-size", NRF51State, flash_size, 129673b2d42SJoel Stanley NRF51822_FLASH_SIZE), 130673b2d42SJoel Stanley DEFINE_PROP_END_OF_LIST(), 131673b2d42SJoel Stanley }; 132673b2d42SJoel Stanley 133673b2d42SJoel Stanley static void nrf51_soc_class_init(ObjectClass *klass, void *data) 134673b2d42SJoel Stanley { 135673b2d42SJoel Stanley DeviceClass *dc = DEVICE_CLASS(klass); 136673b2d42SJoel Stanley 137673b2d42SJoel Stanley dc->realize = nrf51_soc_realize; 138673b2d42SJoel Stanley dc->props = nrf51_soc_properties; 139673b2d42SJoel Stanley } 140673b2d42SJoel Stanley 141673b2d42SJoel Stanley static const TypeInfo nrf51_soc_info = { 142673b2d42SJoel Stanley .name = TYPE_NRF51_SOC, 143673b2d42SJoel Stanley .parent = TYPE_SYS_BUS_DEVICE, 144673b2d42SJoel Stanley .instance_size = sizeof(NRF51State), 145673b2d42SJoel Stanley .instance_init = nrf51_soc_init, 146673b2d42SJoel Stanley .class_init = nrf51_soc_class_init, 147673b2d42SJoel Stanley }; 148673b2d42SJoel Stanley 149673b2d42SJoel Stanley static void nrf51_soc_types(void) 150673b2d42SJoel Stanley { 151673b2d42SJoel Stanley type_register_static(&nrf51_soc_info); 152673b2d42SJoel Stanley } 153673b2d42SJoel Stanley type_init(nrf51_soc_types) 154