1558df83dSJean-Christophe Dubois /* 2558df83dSJean-Christophe Dubois * Copyright (c) 2013 Jean-Christophe Dubois <jcd@tribudubois.net> 3558df83dSJean-Christophe Dubois * 4558df83dSJean-Christophe Dubois * i.MX31 SOC emulation. 5558df83dSJean-Christophe Dubois * 6558df83dSJean-Christophe Dubois * Based on hw/arm/fsl-imx31.c 7558df83dSJean-Christophe Dubois * 8558df83dSJean-Christophe Dubois * This program is free software; you can redistribute it and/or modify it 9558df83dSJean-Christophe Dubois * under the terms of the GNU General Public License as published by the 10558df83dSJean-Christophe Dubois * Free Software Foundation; either version 2 of the License, or 11558df83dSJean-Christophe Dubois * (at your option) any later version. 12558df83dSJean-Christophe Dubois * 13558df83dSJean-Christophe Dubois * This program is distributed in the hope that it will be useful, but WITHOUT 14558df83dSJean-Christophe Dubois * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15558df83dSJean-Christophe Dubois * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16558df83dSJean-Christophe Dubois * for more details. 17558df83dSJean-Christophe Dubois * 18558df83dSJean-Christophe Dubois * You should have received a copy of the GNU General Public License along 19558df83dSJean-Christophe Dubois * with this program; if not, see <http://www.gnu.org/licenses/>. 20558df83dSJean-Christophe Dubois */ 21558df83dSJean-Christophe Dubois 22558df83dSJean-Christophe Dubois #include "hw/arm/fsl-imx31.h" 23558df83dSJean-Christophe Dubois #include "sysemu/sysemu.h" 24558df83dSJean-Christophe Dubois #include "exec/address-spaces.h" 25558df83dSJean-Christophe Dubois #include "hw/boards.h" 26558df83dSJean-Christophe Dubois #include "sysemu/char.h" 27558df83dSJean-Christophe Dubois 28558df83dSJean-Christophe Dubois static void fsl_imx31_init(Object *obj) 29558df83dSJean-Christophe Dubois { 30558df83dSJean-Christophe Dubois FslIMX31State *s = FSL_IMX31(obj); 31558df83dSJean-Christophe Dubois int i; 32558df83dSJean-Christophe Dubois 33558df83dSJean-Christophe Dubois object_initialize(&s->cpu, sizeof(s->cpu), "arm1136-" TYPE_ARM_CPU); 34558df83dSJean-Christophe Dubois 35558df83dSJean-Christophe Dubois object_initialize(&s->avic, sizeof(s->avic), TYPE_IMX_AVIC); 36558df83dSJean-Christophe Dubois qdev_set_parent_bus(DEVICE(&s->avic), sysbus_get_default()); 37558df83dSJean-Christophe Dubois 38558df83dSJean-Christophe Dubois object_initialize(&s->ccm, sizeof(s->ccm), TYPE_IMX_CCM); 39558df83dSJean-Christophe Dubois qdev_set_parent_bus(DEVICE(&s->ccm), sysbus_get_default()); 40558df83dSJean-Christophe Dubois 41558df83dSJean-Christophe Dubois for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) { 42558df83dSJean-Christophe Dubois object_initialize(&s->uart[i], sizeof(s->uart[i]), TYPE_IMX_SERIAL); 43558df83dSJean-Christophe Dubois qdev_set_parent_bus(DEVICE(&s->uart[i]), sysbus_get_default()); 44558df83dSJean-Christophe Dubois } 45558df83dSJean-Christophe Dubois 46558df83dSJean-Christophe Dubois object_initialize(&s->gpt, sizeof(s->gpt), TYPE_IMX_GPT); 47558df83dSJean-Christophe Dubois qdev_set_parent_bus(DEVICE(&s->gpt), sysbus_get_default()); 48558df83dSJean-Christophe Dubois 49558df83dSJean-Christophe Dubois for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) { 50558df83dSJean-Christophe Dubois object_initialize(&s->epit[i], sizeof(s->epit[i]), TYPE_IMX_EPIT); 51558df83dSJean-Christophe Dubois qdev_set_parent_bus(DEVICE(&s->epit[i]), sysbus_get_default()); 52558df83dSJean-Christophe Dubois } 53*d4e26d10SJean-Christophe Dubois 54*d4e26d10SJean-Christophe Dubois for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) { 55*d4e26d10SJean-Christophe Dubois object_initialize(&s->i2c[i], sizeof(s->i2c[i]), TYPE_IMX_I2C); 56*d4e26d10SJean-Christophe Dubois qdev_set_parent_bus(DEVICE(&s->i2c[i]), sysbus_get_default()); 57*d4e26d10SJean-Christophe Dubois } 58558df83dSJean-Christophe Dubois } 59558df83dSJean-Christophe Dubois 60558df83dSJean-Christophe Dubois static void fsl_imx31_realize(DeviceState *dev, Error **errp) 61558df83dSJean-Christophe Dubois { 62558df83dSJean-Christophe Dubois FslIMX31State *s = FSL_IMX31(dev); 63558df83dSJean-Christophe Dubois uint16_t i; 64558df83dSJean-Christophe Dubois Error *err = NULL; 65558df83dSJean-Christophe Dubois 66558df83dSJean-Christophe Dubois object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err); 67558df83dSJean-Christophe Dubois if (err) { 68558df83dSJean-Christophe Dubois error_propagate(errp, err); 69558df83dSJean-Christophe Dubois return; 70558df83dSJean-Christophe Dubois } 71558df83dSJean-Christophe Dubois 72558df83dSJean-Christophe Dubois object_property_set_bool(OBJECT(&s->avic), true, "realized", &err); 73558df83dSJean-Christophe Dubois if (err) { 74558df83dSJean-Christophe Dubois error_propagate(errp, err); 75558df83dSJean-Christophe Dubois return; 76558df83dSJean-Christophe Dubois } 77558df83dSJean-Christophe Dubois sysbus_mmio_map(SYS_BUS_DEVICE(&s->avic), 0, FSL_IMX31_AVIC_ADDR); 78558df83dSJean-Christophe Dubois sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 0, 79558df83dSJean-Christophe Dubois qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ)); 80558df83dSJean-Christophe Dubois sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 1, 81558df83dSJean-Christophe Dubois qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ)); 82558df83dSJean-Christophe Dubois 83558df83dSJean-Christophe Dubois object_property_set_bool(OBJECT(&s->ccm), true, "realized", &err); 84558df83dSJean-Christophe Dubois if (err) { 85558df83dSJean-Christophe Dubois error_propagate(errp, err); 86558df83dSJean-Christophe Dubois return; 87558df83dSJean-Christophe Dubois } 88558df83dSJean-Christophe Dubois sysbus_mmio_map(SYS_BUS_DEVICE(&s->ccm), 0, FSL_IMX31_CCM_ADDR); 89558df83dSJean-Christophe Dubois 90558df83dSJean-Christophe Dubois /* Initialize all UARTS */ 91558df83dSJean-Christophe Dubois for (i = 0; i < FSL_IMX31_NUM_UARTS; i++) { 92558df83dSJean-Christophe Dubois static const struct { 93558df83dSJean-Christophe Dubois hwaddr addr; 94558df83dSJean-Christophe Dubois unsigned int irq; 95558df83dSJean-Christophe Dubois } serial_table[FSL_IMX31_NUM_UARTS] = { 96558df83dSJean-Christophe Dubois { FSL_IMX31_UART1_ADDR, FSL_IMX31_UART1_IRQ }, 97558df83dSJean-Christophe Dubois { FSL_IMX31_UART2_ADDR, FSL_IMX31_UART2_IRQ }, 98558df83dSJean-Christophe Dubois }; 99558df83dSJean-Christophe Dubois 100558df83dSJean-Christophe Dubois if (i < MAX_SERIAL_PORTS) { 101558df83dSJean-Christophe Dubois CharDriverState *chr; 102558df83dSJean-Christophe Dubois 103558df83dSJean-Christophe Dubois chr = serial_hds[i]; 104558df83dSJean-Christophe Dubois 105558df83dSJean-Christophe Dubois if (!chr) { 106558df83dSJean-Christophe Dubois char label[20]; 107558df83dSJean-Christophe Dubois snprintf(label, sizeof(label), "imx31.uart%d", i); 108558df83dSJean-Christophe Dubois chr = qemu_chr_new(label, "null", NULL); 109558df83dSJean-Christophe Dubois } 110558df83dSJean-Christophe Dubois 111558df83dSJean-Christophe Dubois qdev_prop_set_chr(DEVICE(&s->uart[i]), "chardev", chr); 112558df83dSJean-Christophe Dubois } 113558df83dSJean-Christophe Dubois 114558df83dSJean-Christophe Dubois object_property_set_bool(OBJECT(&s->uart[i]), true, "realized", &err); 115558df83dSJean-Christophe Dubois if (err) { 116558df83dSJean-Christophe Dubois error_propagate(errp, err); 117558df83dSJean-Christophe Dubois return; 118558df83dSJean-Christophe Dubois } 119558df83dSJean-Christophe Dubois 120558df83dSJean-Christophe Dubois sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart[i]), 0, serial_table[i].addr); 121558df83dSJean-Christophe Dubois sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart[i]), 0, 122558df83dSJean-Christophe Dubois qdev_get_gpio_in(DEVICE(&s->avic), 123558df83dSJean-Christophe Dubois serial_table[i].irq)); 124558df83dSJean-Christophe Dubois } 125558df83dSJean-Christophe Dubois 126558df83dSJean-Christophe Dubois s->gpt.ccm = DEVICE(&s->ccm); 127558df83dSJean-Christophe Dubois 128558df83dSJean-Christophe Dubois object_property_set_bool(OBJECT(&s->gpt), true, "realized", &err); 129558df83dSJean-Christophe Dubois if (err) { 130558df83dSJean-Christophe Dubois error_propagate(errp, err); 131558df83dSJean-Christophe Dubois return; 132558df83dSJean-Christophe Dubois } 133558df83dSJean-Christophe Dubois 134558df83dSJean-Christophe Dubois sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpt), 0, FSL_IMX31_GPT_ADDR); 135558df83dSJean-Christophe Dubois sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpt), 0, 136558df83dSJean-Christophe Dubois qdev_get_gpio_in(DEVICE(&s->avic), FSL_IMX31_GPT_IRQ)); 137558df83dSJean-Christophe Dubois 138558df83dSJean-Christophe Dubois /* Initialize all EPIT timers */ 139558df83dSJean-Christophe Dubois for (i = 0; i < FSL_IMX31_NUM_EPITS; i++) { 140558df83dSJean-Christophe Dubois static const struct { 141558df83dSJean-Christophe Dubois hwaddr addr; 142558df83dSJean-Christophe Dubois unsigned int irq; 143558df83dSJean-Christophe Dubois } epit_table[FSL_IMX31_NUM_EPITS] = { 144558df83dSJean-Christophe Dubois { FSL_IMX31_EPIT1_ADDR, FSL_IMX31_EPIT1_IRQ }, 145558df83dSJean-Christophe Dubois { FSL_IMX31_EPIT2_ADDR, FSL_IMX31_EPIT2_IRQ }, 146558df83dSJean-Christophe Dubois }; 147558df83dSJean-Christophe Dubois 148558df83dSJean-Christophe Dubois s->epit[i].ccm = DEVICE(&s->ccm); 149558df83dSJean-Christophe Dubois 150558df83dSJean-Christophe Dubois object_property_set_bool(OBJECT(&s->epit[i]), true, "realized", &err); 151558df83dSJean-Christophe Dubois if (err) { 152558df83dSJean-Christophe Dubois error_propagate(errp, err); 153558df83dSJean-Christophe Dubois return; 154558df83dSJean-Christophe Dubois } 155558df83dSJean-Christophe Dubois 156558df83dSJean-Christophe Dubois sysbus_mmio_map(SYS_BUS_DEVICE(&s->epit[i]), 0, epit_table[i].addr); 157558df83dSJean-Christophe Dubois sysbus_connect_irq(SYS_BUS_DEVICE(&s->epit[i]), 0, 158558df83dSJean-Christophe Dubois qdev_get_gpio_in(DEVICE(&s->avic), 159558df83dSJean-Christophe Dubois epit_table[i].irq)); 160558df83dSJean-Christophe Dubois } 161558df83dSJean-Christophe Dubois 162*d4e26d10SJean-Christophe Dubois /* Initialize all I2C */ 163*d4e26d10SJean-Christophe Dubois for (i = 0; i < FSL_IMX31_NUM_I2CS; i++) { 164*d4e26d10SJean-Christophe Dubois static const struct { 165*d4e26d10SJean-Christophe Dubois hwaddr addr; 166*d4e26d10SJean-Christophe Dubois unsigned int irq; 167*d4e26d10SJean-Christophe Dubois } i2c_table[FSL_IMX31_NUM_I2CS] = { 168*d4e26d10SJean-Christophe Dubois { FSL_IMX31_I2C1_ADDR, FSL_IMX31_I2C1_IRQ }, 169*d4e26d10SJean-Christophe Dubois { FSL_IMX31_I2C2_ADDR, FSL_IMX31_I2C2_IRQ }, 170*d4e26d10SJean-Christophe Dubois { FSL_IMX31_I2C3_ADDR, FSL_IMX31_I2C3_IRQ } 171*d4e26d10SJean-Christophe Dubois }; 172*d4e26d10SJean-Christophe Dubois 173*d4e26d10SJean-Christophe Dubois /* Initialize the I2C */ 174*d4e26d10SJean-Christophe Dubois object_property_set_bool(OBJECT(&s->i2c[i]), true, "realized", &err); 175*d4e26d10SJean-Christophe Dubois if (err) { 176*d4e26d10SJean-Christophe Dubois error_propagate(errp, err); 177*d4e26d10SJean-Christophe Dubois return; 178*d4e26d10SJean-Christophe Dubois } 179*d4e26d10SJean-Christophe Dubois /* Map I2C memory */ 180*d4e26d10SJean-Christophe Dubois sysbus_mmio_map(SYS_BUS_DEVICE(&s->i2c[i]), 0, i2c_table[i].addr); 181*d4e26d10SJean-Christophe Dubois /* Connect I2C IRQ to PIC */ 182*d4e26d10SJean-Christophe Dubois sysbus_connect_irq(SYS_BUS_DEVICE(&s->i2c[i]), 0, 183*d4e26d10SJean-Christophe Dubois qdev_get_gpio_in(DEVICE(&s->avic), 184*d4e26d10SJean-Christophe Dubois i2c_table[i].irq)); 185*d4e26d10SJean-Christophe Dubois } 186*d4e26d10SJean-Christophe Dubois 187558df83dSJean-Christophe Dubois /* On a real system, the first 16k is a `secure boot rom' */ 188558df83dSJean-Christophe Dubois memory_region_init_rom_device(&s->secure_rom, NULL, NULL, NULL, 189558df83dSJean-Christophe Dubois "imx31.secure_rom", 190558df83dSJean-Christophe Dubois FSL_IMX31_SECURE_ROM_SIZE, &err); 191558df83dSJean-Christophe Dubois if (err) { 192558df83dSJean-Christophe Dubois error_propagate(errp, err); 193558df83dSJean-Christophe Dubois return; 194558df83dSJean-Christophe Dubois } 195558df83dSJean-Christophe Dubois memory_region_add_subregion(get_system_memory(), FSL_IMX31_SECURE_ROM_ADDR, 196558df83dSJean-Christophe Dubois &s->secure_rom); 197558df83dSJean-Christophe Dubois 198558df83dSJean-Christophe Dubois /* There is also a 16k ROM */ 199558df83dSJean-Christophe Dubois memory_region_init_rom_device(&s->rom, NULL, NULL, NULL, "imx31.rom", 200558df83dSJean-Christophe Dubois FSL_IMX31_ROM_SIZE, &err); 201558df83dSJean-Christophe Dubois if (err) { 202558df83dSJean-Christophe Dubois error_propagate(errp, err); 203558df83dSJean-Christophe Dubois return; 204558df83dSJean-Christophe Dubois } 205558df83dSJean-Christophe Dubois memory_region_add_subregion(get_system_memory(), FSL_IMX31_ROM_ADDR, 206558df83dSJean-Christophe Dubois &s->rom); 207558df83dSJean-Christophe Dubois 208558df83dSJean-Christophe Dubois /* initialize internal RAM (16 KB) */ 209558df83dSJean-Christophe Dubois memory_region_init_ram(&s->iram, NULL, "imx31.iram", FSL_IMX31_IRAM_SIZE, 210558df83dSJean-Christophe Dubois &err); 211558df83dSJean-Christophe Dubois if (err) { 212558df83dSJean-Christophe Dubois error_propagate(errp, err); 213558df83dSJean-Christophe Dubois return; 214558df83dSJean-Christophe Dubois } 215558df83dSJean-Christophe Dubois memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ADDR, 216558df83dSJean-Christophe Dubois &s->iram); 217558df83dSJean-Christophe Dubois vmstate_register_ram_global(&s->iram); 218558df83dSJean-Christophe Dubois 219558df83dSJean-Christophe Dubois /* internal RAM (16 KB) is aliased over 256 MB - 16 KB */ 220558df83dSJean-Christophe Dubois memory_region_init_alias(&s->iram_alias, NULL, "imx31.iram_alias", 221558df83dSJean-Christophe Dubois &s->iram, 0, FSL_IMX31_IRAM_ALIAS_SIZE); 222558df83dSJean-Christophe Dubois memory_region_add_subregion(get_system_memory(), FSL_IMX31_IRAM_ALIAS_ADDR, 223558df83dSJean-Christophe Dubois &s->iram_alias); 224558df83dSJean-Christophe Dubois } 225558df83dSJean-Christophe Dubois 226558df83dSJean-Christophe Dubois static void fsl_imx31_class_init(ObjectClass *oc, void *data) 227558df83dSJean-Christophe Dubois { 228558df83dSJean-Christophe Dubois DeviceClass *dc = DEVICE_CLASS(oc); 229558df83dSJean-Christophe Dubois 230558df83dSJean-Christophe Dubois dc->realize = fsl_imx31_realize; 231558df83dSJean-Christophe Dubois } 232558df83dSJean-Christophe Dubois 233558df83dSJean-Christophe Dubois static const TypeInfo fsl_imx31_type_info = { 234558df83dSJean-Christophe Dubois .name = TYPE_FSL_IMX31, 235558df83dSJean-Christophe Dubois .parent = TYPE_DEVICE, 236558df83dSJean-Christophe Dubois .instance_size = sizeof(FslIMX31State), 237558df83dSJean-Christophe Dubois .instance_init = fsl_imx31_init, 238558df83dSJean-Christophe Dubois .class_init = fsl_imx31_class_init, 239558df83dSJean-Christophe Dubois }; 240558df83dSJean-Christophe Dubois 241558df83dSJean-Christophe Dubois static void fsl_imx31_register_types(void) 242558df83dSJean-Christophe Dubois { 243558df83dSJean-Christophe Dubois type_register_static(&fsl_imx31_type_info); 244558df83dSJean-Christophe Dubois } 245558df83dSJean-Christophe Dubois 246558df83dSJean-Christophe Dubois type_init(fsl_imx31_register_types) 247