xref: /qemu/hw/arm/allwinner-a10.c (revision 82e4838249b23c3fe20cee295f9c1b3e6abd68d1)
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 
30*82e48382SNiek 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
387f0ec989SPhilippe Mathieu-Daudé 
399158fa54Sliguang static void aw_a10_init(Object *obj)
409158fa54Sliguang {
419158fa54Sliguang     AwA10State *s = AW_A10(obj);
429158fa54Sliguang 
43cf3fccfaSThomas Huth     object_initialize_child(obj, "cpu", &s->cpu, sizeof(s->cpu),
448a863c81SPhilippe Mathieu-Daudé                             ARM_CPU_TYPE_NAME("cortex-a8"),
458a863c81SPhilippe Mathieu-Daudé                             &error_abort, NULL);
469158fa54Sliguang 
47cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "intc", &s->intc, sizeof(s->intc),
48cf3fccfaSThomas Huth                           TYPE_AW_A10_PIC);
499158fa54Sliguang 
50cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "timer", &s->timer, sizeof(s->timer),
51cf3fccfaSThomas Huth                           TYPE_AW_A10_PIT);
52db7dfd4cSBeniamino Galvani 
53cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "emac", &s->emac, sizeof(s->emac), TYPE_AW_EMAC);
54dca62576SPeter Crosthwaite 
55cf3fccfaSThomas Huth     sysbus_init_child_obj(obj, "sata", &s->sata, sizeof(s->sata),
56cf3fccfaSThomas Huth                           TYPE_ALLWINNER_AHCI);
577abc8cabSGuenter Roeck 
587abc8cabSGuenter Roeck     if (machine_usb(current_machine)) {
597abc8cabSGuenter Roeck         int i;
607abc8cabSGuenter Roeck 
617abc8cabSGuenter Roeck         for (i = 0; i < AW_A10_NUM_USB; i++) {
627abc8cabSGuenter Roeck             sysbus_init_child_obj(obj, "ehci[*]", OBJECT(&s->ehci[i]),
637abc8cabSGuenter Roeck                                   sizeof(s->ehci[i]), TYPE_PLATFORM_EHCI);
647abc8cabSGuenter Roeck             sysbus_init_child_obj(obj, "ohci[*]", OBJECT(&s->ohci[i]),
657abc8cabSGuenter Roeck                                   sizeof(s->ohci[i]), TYPE_SYSBUS_OHCI);
667abc8cabSGuenter Roeck         }
677abc8cabSGuenter Roeck     }
68*82e48382SNiek Linnenbank 
69*82e48382SNiek Linnenbank     sysbus_init_child_obj(obj, "mmc0", &s->mmc0, sizeof(s->mmc0),
70*82e48382SNiek Linnenbank                           TYPE_AW_SDHOST_SUN4I);
719158fa54Sliguang }
729158fa54Sliguang 
739158fa54Sliguang static void aw_a10_realize(DeviceState *dev, Error **errp)
749158fa54Sliguang {
759158fa54Sliguang     AwA10State *s = AW_A10(dev);
769158fa54Sliguang     SysBusDevice *sysbusdev;
779158fa54Sliguang     Error *err = NULL;
789158fa54Sliguang 
799158fa54Sliguang     object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
809158fa54Sliguang     if (err != NULL) {
819158fa54Sliguang         error_propagate(errp, err);
829158fa54Sliguang         return;
839158fa54Sliguang     }
849158fa54Sliguang 
859158fa54Sliguang     object_property_set_bool(OBJECT(&s->intc), true, "realized", &err);
869158fa54Sliguang     if (err != NULL) {
879158fa54Sliguang         error_propagate(errp, err);
889158fa54Sliguang         return;
899158fa54Sliguang     }
909158fa54Sliguang     sysbusdev = SYS_BUS_DEVICE(&s->intc);
919158fa54Sliguang     sysbus_mmio_map(sysbusdev, 0, AW_A10_PIC_REG_BASE);
92af4ba4edSPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 0,
93af4ba4edSPhilippe Mathieu-Daudé                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ));
94af4ba4edSPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 1,
95af4ba4edSPhilippe Mathieu-Daudé                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ));
96f8a865d3SPhilippe Mathieu-Daudé     qdev_pass_gpios(DEVICE(&s->intc), dev, NULL);
979158fa54Sliguang 
989158fa54Sliguang     object_property_set_bool(OBJECT(&s->timer), true, "realized", &err);
999158fa54Sliguang     if (err != NULL) {
1009158fa54Sliguang         error_propagate(errp, err);
1019158fa54Sliguang         return;
1029158fa54Sliguang     }
1039158fa54Sliguang     sysbusdev = SYS_BUS_DEVICE(&s->timer);
1049158fa54Sliguang     sysbus_mmio_map(sysbusdev, 0, AW_A10_PIT_REG_BASE);
105f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 0, qdev_get_gpio_in(dev, 22));
106f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 1, qdev_get_gpio_in(dev, 23));
107f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 2, qdev_get_gpio_in(dev, 24));
108f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 3, qdev_get_gpio_in(dev, 25));
109f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 4, qdev_get_gpio_in(dev, 67));
110f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 5, qdev_get_gpio_in(dev, 68));
1119158fa54Sliguang 
112ead07aa4SPhilippe Mathieu-Daudé     memory_region_init_ram(&s->sram_a, OBJECT(dev), "sram A", 48 * KiB,
113ead07aa4SPhilippe Mathieu-Daudé                            &error_fatal);
114ead07aa4SPhilippe Mathieu-Daudé     memory_region_add_subregion(get_system_memory(), 0x00000000, &s->sram_a);
115ead07aa4SPhilippe Mathieu-Daudé     create_unimplemented_device("a10-sram-ctrl", 0x01c00000, 4 * KiB);
116ead07aa4SPhilippe Mathieu-Daudé 
1178aabc543SThomas Huth     /* FIXME use qdev NIC properties instead of nd_table[] */
1188aabc543SThomas Huth     if (nd_table[0].used) {
1198aabc543SThomas Huth         qemu_check_nic_model(&nd_table[0], TYPE_AW_EMAC);
1208aabc543SThomas Huth         qdev_set_nic_properties(DEVICE(&s->emac), &nd_table[0]);
1218aabc543SThomas Huth     }
122db7dfd4cSBeniamino Galvani     object_property_set_bool(OBJECT(&s->emac), true, "realized", &err);
123db7dfd4cSBeniamino Galvani     if (err != NULL) {
124db7dfd4cSBeniamino Galvani         error_propagate(errp, err);
125db7dfd4cSBeniamino Galvani         return;
126db7dfd4cSBeniamino Galvani     }
127db7dfd4cSBeniamino Galvani     sysbusdev = SYS_BUS_DEVICE(&s->emac);
128db7dfd4cSBeniamino Galvani     sysbus_mmio_map(sysbusdev, 0, AW_A10_EMAC_BASE);
129f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(sysbusdev, 0, qdev_get_gpio_in(dev, 55));
130db7dfd4cSBeniamino Galvani 
131dca62576SPeter Crosthwaite     object_property_set_bool(OBJECT(&s->sata), true, "realized", &err);
132dca62576SPeter Crosthwaite     if (err) {
133dca62576SPeter Crosthwaite         error_propagate(errp, err);
134dca62576SPeter Crosthwaite         return;
135dca62576SPeter Crosthwaite     }
136dca62576SPeter Crosthwaite     sysbus_mmio_map(SYS_BUS_DEVICE(&s->sata), 0, AW_A10_SATA_BASE);
137f8a865d3SPhilippe Mathieu-Daudé     sysbus_connect_irq(SYS_BUS_DEVICE(&s->sata), 0, qdev_get_gpio_in(dev, 56));
138dca62576SPeter Crosthwaite 
1399bca0edbSPeter Maydell     /* FIXME use a qdev chardev prop instead of serial_hd() */
140f8a865d3SPhilippe Mathieu-Daudé     serial_mm_init(get_system_memory(), AW_A10_UART0_REG_BASE, 2,
141f8a865d3SPhilippe Mathieu-Daudé                    qdev_get_gpio_in(dev, 1),
1429bca0edbSPeter Maydell                    115200, serial_hd(0), DEVICE_NATIVE_ENDIAN);
1437abc8cabSGuenter Roeck 
1447abc8cabSGuenter Roeck     if (machine_usb(current_machine)) {
1457abc8cabSGuenter Roeck         int i;
1467abc8cabSGuenter Roeck 
1477abc8cabSGuenter Roeck         for (i = 0; i < AW_A10_NUM_USB; i++) {
1487abc8cabSGuenter Roeck             char bus[16];
1497abc8cabSGuenter Roeck 
1507abc8cabSGuenter Roeck             sprintf(bus, "usb-bus.%d", i);
1517abc8cabSGuenter Roeck 
1527abc8cabSGuenter Roeck             object_property_set_bool(OBJECT(&s->ehci[i]), true,
1537abc8cabSGuenter Roeck                                      "companion-enable", &error_fatal);
1547abc8cabSGuenter Roeck             object_property_set_bool(OBJECT(&s->ehci[i]), true, "realized",
1557abc8cabSGuenter Roeck                                      &error_fatal);
1567abc8cabSGuenter Roeck             sysbus_mmio_map(SYS_BUS_DEVICE(&s->ehci[i]), 0,
1577abc8cabSGuenter Roeck                             AW_A10_EHCI_BASE + i * 0x8000);
1587abc8cabSGuenter Roeck             sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[i]), 0,
1597abc8cabSGuenter Roeck                                qdev_get_gpio_in(dev, 39 + i));
1607abc8cabSGuenter Roeck 
1617abc8cabSGuenter Roeck             object_property_set_str(OBJECT(&s->ohci[i]), bus, "masterbus",
1627abc8cabSGuenter Roeck                                     &error_fatal);
1637abc8cabSGuenter Roeck             object_property_set_bool(OBJECT(&s->ohci[i]), true, "realized",
1647abc8cabSGuenter Roeck                                      &error_fatal);
1657abc8cabSGuenter Roeck             sysbus_mmio_map(SYS_BUS_DEVICE(&s->ohci[i]), 0,
1667abc8cabSGuenter Roeck                             AW_A10_OHCI_BASE + i * 0x8000);
1677abc8cabSGuenter Roeck             sysbus_connect_irq(SYS_BUS_DEVICE(&s->ohci[i]), 0,
1687abc8cabSGuenter Roeck                                qdev_get_gpio_in(dev, 64 + i));
1697abc8cabSGuenter Roeck         }
1707abc8cabSGuenter Roeck     }
171*82e48382SNiek Linnenbank 
172*82e48382SNiek Linnenbank     /* SD/MMC */
173*82e48382SNiek Linnenbank     qdev_init_nofail(DEVICE(&s->mmc0));
174*82e48382SNiek Linnenbank     sysbus_mmio_map(SYS_BUS_DEVICE(&s->mmc0), 0, AW_A10_MMC0_BASE);
175*82e48382SNiek Linnenbank     sysbus_connect_irq(SYS_BUS_DEVICE(&s->mmc0), 0, qdev_get_gpio_in(dev, 32));
176*82e48382SNiek Linnenbank     object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->mmc0),
177*82e48382SNiek Linnenbank                               "sd-bus", &error_abort);
1789158fa54Sliguang }
1799158fa54Sliguang 
1809158fa54Sliguang static void aw_a10_class_init(ObjectClass *oc, void *data)
1819158fa54Sliguang {
1829158fa54Sliguang     DeviceClass *dc = DEVICE_CLASS(oc);
1839158fa54Sliguang 
1849158fa54Sliguang     dc->realize = aw_a10_realize;
1858aabc543SThomas Huth     /* Reason: Uses serial_hds and nd_table in realize function */
186dc89a180SThomas Huth     dc->user_creatable = false;
1879158fa54Sliguang }
1889158fa54Sliguang 
1899158fa54Sliguang static const TypeInfo aw_a10_type_info = {
1909158fa54Sliguang     .name = TYPE_AW_A10,
1919158fa54Sliguang     .parent = TYPE_DEVICE,
1929158fa54Sliguang     .instance_size = sizeof(AwA10State),
1939158fa54Sliguang     .instance_init = aw_a10_init,
1949158fa54Sliguang     .class_init = aw_a10_class_init,
1959158fa54Sliguang };
1969158fa54Sliguang 
1979158fa54Sliguang static void aw_a10_register_types(void)
1989158fa54Sliguang {
1999158fa54Sliguang     type_register_static(&aw_a10_type_info);
2009158fa54Sliguang }
2019158fa54Sliguang 
2029158fa54Sliguang type_init(aw_a10_register_types)
203