19158fa54Sliguang /* 29158fa54Sliguang * Allwinner A10 SoC emulation 39158fa54Sliguang * 49158fa54Sliguang * Copyright (C) 2013 Li Guang 59158fa54Sliguang * Written by Li Guang <lig.fnst@cn.fujitsu.com> 69158fa54Sliguang * 79158fa54Sliguang * This program is free software; you can redistribute it and/or modify it 89158fa54Sliguang * under the terms of the GNU General Public License as published by the 99158fa54Sliguang * Free Software Foundation; either version 2 of the License, or 109158fa54Sliguang * (at your option) any later version. 119158fa54Sliguang * 129158fa54Sliguang * This program is distributed in the hope that it will be useful, but WITHOUT 139158fa54Sliguang * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 149158fa54Sliguang * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 159158fa54Sliguang * for more details. 169158fa54Sliguang */ 179158fa54Sliguang 1812b16722SPeter Maydell #include "qemu/osdep.h" 195a720b1eSMarkus Armbruster #include "exec/address-spaces.h" 20da34e65cSMarkus Armbruster #include "qapi/error.h" 210b8fa32fSMarkus Armbruster #include "qemu/module.h" 224771d756SPaolo Bonzini #include "cpu.h" 239158fa54Sliguang #include "hw/sysbus.h" 249158fa54Sliguang #include "hw/arm/allwinner-a10.h" 25ead07aa4SPhilippe Mathieu-Daudé #include "hw/misc/unimp.h" 2646517dd4SMarkus Armbruster #include "sysemu/sysemu.h" 277abc8cabSGuenter Roeck #include "hw/boards.h" 287abc8cabSGuenter Roeck #include "hw/usb/hcd-ohci.h" 299158fa54Sliguang 3082e48382SNiek Linnenbank #define AW_A10_MMC0_BASE 0x01c0f000 317f0ec989SPhilippe Mathieu-Daudé #define AW_A10_PIC_REG_BASE 0x01c20400 327f0ec989SPhilippe Mathieu-Daudé #define AW_A10_PIT_REG_BASE 0x01c20c00 337f0ec989SPhilippe Mathieu-Daudé #define AW_A10_UART0_REG_BASE 0x01c28000 347f0ec989SPhilippe Mathieu-Daudé #define AW_A10_EMAC_BASE 0x01c0b000 357abc8cabSGuenter Roeck #define AW_A10_EHCI_BASE 0x01c14000 367abc8cabSGuenter Roeck #define AW_A10_OHCI_BASE 0x01c14400 377f0ec989SPhilippe Mathieu-Daudé #define AW_A10_SATA_BASE 0x01c18000 38a9ad9e73SNiek Linnenbank #define AW_A10_RTC_BASE 0x01c20d00 397f0ec989SPhilippe Mathieu-Daudé 409158fa54Sliguang static void aw_a10_init(Object *obj) 419158fa54Sliguang { 429158fa54Sliguang AwA10State *s = AW_A10(obj); 439158fa54Sliguang 44cf3fccfaSThomas Huth object_initialize_child(obj, "cpu", &s->cpu, sizeof(s->cpu), 458a863c81SPhilippe Mathieu-Daudé ARM_CPU_TYPE_NAME("cortex-a8"), 468a863c81SPhilippe Mathieu-Daudé &error_abort, NULL); 479158fa54Sliguang 48cf3fccfaSThomas Huth sysbus_init_child_obj(obj, "intc", &s->intc, sizeof(s->intc), 49cf3fccfaSThomas Huth TYPE_AW_A10_PIC); 509158fa54Sliguang 51cf3fccfaSThomas Huth sysbus_init_child_obj(obj, "timer", &s->timer, sizeof(s->timer), 52cf3fccfaSThomas Huth TYPE_AW_A10_PIT); 53db7dfd4cSBeniamino Galvani 54cf3fccfaSThomas Huth sysbus_init_child_obj(obj, "emac", &s->emac, sizeof(s->emac), TYPE_AW_EMAC); 55dca62576SPeter Crosthwaite 56cf3fccfaSThomas Huth sysbus_init_child_obj(obj, "sata", &s->sata, sizeof(s->sata), 57cf3fccfaSThomas Huth TYPE_ALLWINNER_AHCI); 587abc8cabSGuenter Roeck 597abc8cabSGuenter Roeck if (machine_usb(current_machine)) { 607abc8cabSGuenter Roeck int i; 617abc8cabSGuenter Roeck 627abc8cabSGuenter Roeck for (i = 0; i < AW_A10_NUM_USB; i++) { 637abc8cabSGuenter Roeck sysbus_init_child_obj(obj, "ehci[*]", OBJECT(&s->ehci[i]), 647abc8cabSGuenter Roeck sizeof(s->ehci[i]), TYPE_PLATFORM_EHCI); 657abc8cabSGuenter Roeck sysbus_init_child_obj(obj, "ohci[*]", OBJECT(&s->ohci[i]), 667abc8cabSGuenter Roeck sizeof(s->ohci[i]), TYPE_SYSBUS_OHCI); 677abc8cabSGuenter Roeck } 687abc8cabSGuenter Roeck } 6982e48382SNiek Linnenbank 7082e48382SNiek Linnenbank sysbus_init_child_obj(obj, "mmc0", &s->mmc0, sizeof(s->mmc0), 7182e48382SNiek Linnenbank TYPE_AW_SDHOST_SUN4I); 72a9ad9e73SNiek Linnenbank 73a9ad9e73SNiek Linnenbank sysbus_init_child_obj(obj, "rtc", &s->rtc, sizeof(s->rtc), 74a9ad9e73SNiek Linnenbank TYPE_AW_RTC_SUN4I); 759158fa54Sliguang } 769158fa54Sliguang 779158fa54Sliguang static void aw_a10_realize(DeviceState *dev, Error **errp) 789158fa54Sliguang { 799158fa54Sliguang AwA10State *s = AW_A10(dev); 809158fa54Sliguang SysBusDevice *sysbusdev; 819158fa54Sliguang Error *err = NULL; 829158fa54Sliguang 839158fa54Sliguang object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err); 849158fa54Sliguang if (err != NULL) { 859158fa54Sliguang error_propagate(errp, err); 869158fa54Sliguang return; 879158fa54Sliguang } 889158fa54Sliguang 899158fa54Sliguang object_property_set_bool(OBJECT(&s->intc), true, "realized", &err); 909158fa54Sliguang if (err != NULL) { 919158fa54Sliguang error_propagate(errp, err); 929158fa54Sliguang return; 939158fa54Sliguang } 949158fa54Sliguang sysbusdev = SYS_BUS_DEVICE(&s->intc); 959158fa54Sliguang sysbus_mmio_map(sysbusdev, 0, AW_A10_PIC_REG_BASE); 96af4ba4edSPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 0, 97af4ba4edSPhilippe Mathieu-Daudé qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ)); 98af4ba4edSPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 1, 99af4ba4edSPhilippe Mathieu-Daudé qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ)); 100f8a865d3SPhilippe Mathieu-Daudé qdev_pass_gpios(DEVICE(&s->intc), dev, NULL); 1019158fa54Sliguang 1029158fa54Sliguang object_property_set_bool(OBJECT(&s->timer), true, "realized", &err); 1039158fa54Sliguang if (err != NULL) { 1049158fa54Sliguang error_propagate(errp, err); 1059158fa54Sliguang return; 1069158fa54Sliguang } 1079158fa54Sliguang sysbusdev = SYS_BUS_DEVICE(&s->timer); 1089158fa54Sliguang sysbus_mmio_map(sysbusdev, 0, AW_A10_PIT_REG_BASE); 109f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 0, qdev_get_gpio_in(dev, 22)); 110f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 1, qdev_get_gpio_in(dev, 23)); 111f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 2, qdev_get_gpio_in(dev, 24)); 112f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 3, qdev_get_gpio_in(dev, 25)); 113f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 4, qdev_get_gpio_in(dev, 67)); 114f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 5, qdev_get_gpio_in(dev, 68)); 1159158fa54Sliguang 116ead07aa4SPhilippe Mathieu-Daudé memory_region_init_ram(&s->sram_a, OBJECT(dev), "sram A", 48 * KiB, 117ead07aa4SPhilippe Mathieu-Daudé &error_fatal); 118ead07aa4SPhilippe Mathieu-Daudé memory_region_add_subregion(get_system_memory(), 0x00000000, &s->sram_a); 119ead07aa4SPhilippe Mathieu-Daudé create_unimplemented_device("a10-sram-ctrl", 0x01c00000, 4 * KiB); 120ead07aa4SPhilippe Mathieu-Daudé 1218aabc543SThomas Huth /* FIXME use qdev NIC properties instead of nd_table[] */ 1228aabc543SThomas Huth if (nd_table[0].used) { 1238aabc543SThomas Huth qemu_check_nic_model(&nd_table[0], TYPE_AW_EMAC); 1248aabc543SThomas Huth qdev_set_nic_properties(DEVICE(&s->emac), &nd_table[0]); 1258aabc543SThomas Huth } 126db7dfd4cSBeniamino Galvani object_property_set_bool(OBJECT(&s->emac), true, "realized", &err); 127db7dfd4cSBeniamino Galvani if (err != NULL) { 128db7dfd4cSBeniamino Galvani error_propagate(errp, err); 129db7dfd4cSBeniamino Galvani return; 130db7dfd4cSBeniamino Galvani } 131db7dfd4cSBeniamino Galvani sysbusdev = SYS_BUS_DEVICE(&s->emac); 132db7dfd4cSBeniamino Galvani sysbus_mmio_map(sysbusdev, 0, AW_A10_EMAC_BASE); 133f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(sysbusdev, 0, qdev_get_gpio_in(dev, 55)); 134db7dfd4cSBeniamino Galvani 135dca62576SPeter Crosthwaite object_property_set_bool(OBJECT(&s->sata), true, "realized", &err); 136dca62576SPeter Crosthwaite if (err) { 137dca62576SPeter Crosthwaite error_propagate(errp, err); 138dca62576SPeter Crosthwaite return; 139dca62576SPeter Crosthwaite } 140dca62576SPeter Crosthwaite sysbus_mmio_map(SYS_BUS_DEVICE(&s->sata), 0, AW_A10_SATA_BASE); 141f8a865d3SPhilippe Mathieu-Daudé sysbus_connect_irq(SYS_BUS_DEVICE(&s->sata), 0, qdev_get_gpio_in(dev, 56)); 142dca62576SPeter Crosthwaite 1439bca0edbSPeter Maydell /* FIXME use a qdev chardev prop instead of serial_hd() */ 144f8a865d3SPhilippe Mathieu-Daudé serial_mm_init(get_system_memory(), AW_A10_UART0_REG_BASE, 2, 145f8a865d3SPhilippe Mathieu-Daudé qdev_get_gpio_in(dev, 1), 1469bca0edbSPeter Maydell 115200, serial_hd(0), DEVICE_NATIVE_ENDIAN); 1477abc8cabSGuenter Roeck 1487abc8cabSGuenter Roeck if (machine_usb(current_machine)) { 1497abc8cabSGuenter Roeck int i; 1507abc8cabSGuenter Roeck 1517abc8cabSGuenter Roeck for (i = 0; i < AW_A10_NUM_USB; i++) { 1527abc8cabSGuenter Roeck char bus[16]; 1537abc8cabSGuenter Roeck 1547abc8cabSGuenter Roeck sprintf(bus, "usb-bus.%d", i); 1557abc8cabSGuenter Roeck 1567abc8cabSGuenter Roeck object_property_set_bool(OBJECT(&s->ehci[i]), true, 1577abc8cabSGuenter Roeck "companion-enable", &error_fatal); 1587abc8cabSGuenter Roeck object_property_set_bool(OBJECT(&s->ehci[i]), true, "realized", 1597abc8cabSGuenter Roeck &error_fatal); 1607abc8cabSGuenter Roeck sysbus_mmio_map(SYS_BUS_DEVICE(&s->ehci[i]), 0, 1617abc8cabSGuenter Roeck AW_A10_EHCI_BASE + i * 0x8000); 1627abc8cabSGuenter Roeck sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[i]), 0, 1637abc8cabSGuenter Roeck qdev_get_gpio_in(dev, 39 + i)); 1647abc8cabSGuenter Roeck 1657abc8cabSGuenter Roeck object_property_set_str(OBJECT(&s->ohci[i]), bus, "masterbus", 1667abc8cabSGuenter Roeck &error_fatal); 1677abc8cabSGuenter Roeck object_property_set_bool(OBJECT(&s->ohci[i]), true, "realized", 1687abc8cabSGuenter Roeck &error_fatal); 1697abc8cabSGuenter Roeck sysbus_mmio_map(SYS_BUS_DEVICE(&s->ohci[i]), 0, 1707abc8cabSGuenter Roeck AW_A10_OHCI_BASE + i * 0x8000); 1717abc8cabSGuenter Roeck sysbus_connect_irq(SYS_BUS_DEVICE(&s->ohci[i]), 0, 1727abc8cabSGuenter Roeck qdev_get_gpio_in(dev, 64 + i)); 1737abc8cabSGuenter Roeck } 1747abc8cabSGuenter Roeck } 17582e48382SNiek Linnenbank 17682e48382SNiek Linnenbank /* SD/MMC */ 17782e48382SNiek Linnenbank qdev_init_nofail(DEVICE(&s->mmc0)); 17882e48382SNiek Linnenbank sysbus_mmio_map(SYS_BUS_DEVICE(&s->mmc0), 0, AW_A10_MMC0_BASE); 17982e48382SNiek Linnenbank sysbus_connect_irq(SYS_BUS_DEVICE(&s->mmc0), 0, qdev_get_gpio_in(dev, 32)); 18082e48382SNiek Linnenbank object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->mmc0), 181*d2623129SMarkus Armbruster "sd-bus"); 182a9ad9e73SNiek Linnenbank 183a9ad9e73SNiek Linnenbank /* RTC */ 184a9ad9e73SNiek Linnenbank qdev_init_nofail(DEVICE(&s->rtc)); 185a9ad9e73SNiek Linnenbank sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->rtc), 0, AW_A10_RTC_BASE, 10); 1869158fa54Sliguang } 1879158fa54Sliguang 1889158fa54Sliguang static void aw_a10_class_init(ObjectClass *oc, void *data) 1899158fa54Sliguang { 1909158fa54Sliguang DeviceClass *dc = DEVICE_CLASS(oc); 1919158fa54Sliguang 1929158fa54Sliguang dc->realize = aw_a10_realize; 1938aabc543SThomas Huth /* Reason: Uses serial_hds and nd_table in realize function */ 194dc89a180SThomas Huth dc->user_creatable = false; 1959158fa54Sliguang } 1969158fa54Sliguang 1979158fa54Sliguang static const TypeInfo aw_a10_type_info = { 1989158fa54Sliguang .name = TYPE_AW_A10, 1999158fa54Sliguang .parent = TYPE_DEVICE, 2009158fa54Sliguang .instance_size = sizeof(AwA10State), 2019158fa54Sliguang .instance_init = aw_a10_init, 2029158fa54Sliguang .class_init = aw_a10_class_init, 2039158fa54Sliguang }; 2049158fa54Sliguang 2059158fa54Sliguang static void aw_a10_register_types(void) 2069158fa54Sliguang { 2079158fa54Sliguang type_register_static(&aw_a10_type_info); 2089158fa54Sliguang } 2099158fa54Sliguang 2109158fa54Sliguang type_init(aw_a10_register_types) 211