10bf96f94SGerd Hoffmann /*
20bf96f94SGerd Hoffmann * QEMU USB EHCI Emulation
30bf96f94SGerd Hoffmann *
40bf96f94SGerd Hoffmann * This library is free software; you can redistribute it and/or
50bf96f94SGerd Hoffmann * modify it under the terms of the GNU Lesser General Public
60bf96f94SGerd Hoffmann * License as published by the Free Software Foundation; either
775a49fc6SThomas Huth * version 2.1 of the License, or (at your option) any later version.
80bf96f94SGerd Hoffmann *
90bf96f94SGerd Hoffmann * This library is distributed in the hope that it will be useful,
100bf96f94SGerd Hoffmann * but WITHOUT ANY WARRANTY; without even the implied warranty of
110bf96f94SGerd Hoffmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
120bf96f94SGerd Hoffmann * Lesser General Public License for more details.
130bf96f94SGerd Hoffmann *
1475a49fc6SThomas Huth * You should have received a copy of the GNU Lesser General Public License
150bf96f94SGerd Hoffmann * along with this program; if not, see <http://www.gnu.org/licenses/>.
160bf96f94SGerd Hoffmann */
170bf96f94SGerd Hoffmann
18e532b2e0SPeter Maydell #include "qemu/osdep.h"
19a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
200bf96f94SGerd Hoffmann #include "hw/usb/hcd-ehci.h"
21d6454270SMarkus Armbruster #include "migration/vmstate.h"
220b8fa32fSMarkus Armbruster #include "qemu/module.h"
231de7afc9SPaolo Bonzini #include "qemu/range.h"
240bf96f94SGerd Hoffmann
25df013187SGerd Hoffmann typedef struct EHCIPCIInfo {
26df013187SGerd Hoffmann const char *name;
27df013187SGerd Hoffmann uint16_t vendor_id;
28df013187SGerd Hoffmann uint16_t device_id;
29df013187SGerd Hoffmann uint8_t revision;
30ec56214fSGerd Hoffmann bool companion;
31df013187SGerd Hoffmann } EHCIPCIInfo;
32df013187SGerd Hoffmann
usb_ehci_pci_realize(PCIDevice * dev,Error ** errp)339af21dbeSMarkus Armbruster static void usb_ehci_pci_realize(PCIDevice *dev, Error **errp)
340bf96f94SGerd Hoffmann {
355aa3ca9fSAndreas Färber EHCIPCIState *i = PCI_EHCI(dev);
360bf96f94SGerd Hoffmann EHCIState *s = &i->ehci;
370bf96f94SGerd Hoffmann uint8_t *pci_conf = dev->config;
380bf96f94SGerd Hoffmann
390bf96f94SGerd Hoffmann pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20);
400bf96f94SGerd Hoffmann
410bf96f94SGerd Hoffmann /* capabilities pointer */
420bf96f94SGerd Hoffmann pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00);
430bf96f94SGerd Hoffmann /* pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50); */
440bf96f94SGerd Hoffmann
450bf96f94SGerd Hoffmann pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */
460bf96f94SGerd Hoffmann pci_set_byte(&pci_conf[PCI_MIN_GNT], 0);
470bf96f94SGerd Hoffmann pci_set_byte(&pci_conf[PCI_MAX_LAT], 0);
480bf96f94SGerd Hoffmann
490bf96f94SGerd Hoffmann /* pci_conf[0x50] = 0x01; *//* power management caps */
500bf96f94SGerd Hoffmann
510bf96f94SGerd Hoffmann pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); /* release # (2.1.4) */
520bf96f94SGerd Hoffmann pci_set_byte(&pci_conf[0x61], 0x20); /* frame length adjustment (2.1.5) */
530bf96f94SGerd Hoffmann pci_set_word(&pci_conf[0x62], 0x00); /* port wake up capability (2.1.6) */
540bf96f94SGerd Hoffmann
550bf96f94SGerd Hoffmann pci_conf[0x64] = 0x00;
560bf96f94SGerd Hoffmann pci_conf[0x65] = 0x00;
570bf96f94SGerd Hoffmann pci_conf[0x66] = 0x00;
580bf96f94SGerd Hoffmann pci_conf[0x67] = 0x00;
590bf96f94SGerd Hoffmann pci_conf[0x68] = 0x01;
600bf96f94SGerd Hoffmann pci_conf[0x69] = 0x00;
610bf96f94SGerd Hoffmann pci_conf[0x6a] = 0x00;
620bf96f94SGerd Hoffmann pci_conf[0x6b] = 0x00; /* USBLEGSUP */
630bf96f94SGerd Hoffmann pci_conf[0x6c] = 0x00;
640bf96f94SGerd Hoffmann pci_conf[0x6d] = 0x00;
650bf96f94SGerd Hoffmann pci_conf[0x6e] = 0x00;
660bf96f94SGerd Hoffmann pci_conf[0x6f] = 0xc0; /* USBLEFCTLSTS */
670bf96f94SGerd Hoffmann
689e64f8a3SMarcel Apfelbaum s->irq = pci_allocate_irq(dev);
69df32fd1cSPaolo Bonzini s->as = pci_get_address_space(dev);
700bf96f94SGerd Hoffmann
7108f4c90bSAndreas Färber usb_ehci_realize(s, DEVICE(dev), NULL);
720bf96f94SGerd Hoffmann pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem);
730bf96f94SGerd Hoffmann }
740bf96f94SGerd Hoffmann
usb_ehci_pci_init(Object * obj)75d4614cc3SAndreas Färber static void usb_ehci_pci_init(Object *obj)
76d4614cc3SAndreas Färber {
774c030dd0SPhilippe Mathieu-Daudé DeviceClass *dc = DEVICE_GET_CLASS(obj);
78d4614cc3SAndreas Färber EHCIPCIState *i = PCI_EHCI(obj);
79d4614cc3SAndreas Färber EHCIState *s = &i->ehci;
80d4614cc3SAndreas Färber
81d4614cc3SAndreas Färber s->caps[0x09] = 0x68; /* EECP */
82d4614cc3SAndreas Färber
83d4614cc3SAndreas Färber s->capsbase = 0x00;
84d4614cc3SAndreas Färber s->opregbase = 0x20;
85cc8d6a84SKuo-Jung Su s->portscbase = 0x44;
861c83f366SPhilippe Mathieu-Daudé s->portnr = EHCI_PORTS;
87d4614cc3SAndreas Färber
88ec56214fSGerd Hoffmann if (!dc->hotpluggable) {
89ec56214fSGerd Hoffmann s->companion_enable = true;
90ec56214fSGerd Hoffmann }
91ec56214fSGerd Hoffmann
92d4614cc3SAndreas Färber usb_ehci_init(s, DEVICE(obj));
93d4614cc3SAndreas Färber }
94d4614cc3SAndreas Färber
usb_ehci_pci_finalize(Object * obj)95d710e1e7SLi Qiang static void usb_ehci_pci_finalize(Object *obj)
96d710e1e7SLi Qiang {
97d710e1e7SLi Qiang EHCIPCIState *i = PCI_EHCI(obj);
98d710e1e7SLi Qiang EHCIState *s = &i->ehci;
99d710e1e7SLi Qiang
100d710e1e7SLi Qiang usb_ehci_finalize(s);
101d710e1e7SLi Qiang }
102d710e1e7SLi Qiang
usb_ehci_pci_exit(PCIDevice * dev)10396e14926SGonglei static void usb_ehci_pci_exit(PCIDevice *dev)
10496e14926SGonglei {
10596e14926SGonglei EHCIPCIState *i = PCI_EHCI(dev);
10696e14926SGonglei EHCIState *s = &i->ehci;
10796e14926SGonglei
108b69c3c21SMarkus Armbruster usb_ehci_unrealize(s, DEVICE(dev));
10996e14926SGonglei
11096e14926SGonglei g_free(s->irq);
11196e14926SGonglei s->irq = NULL;
11296e14926SGonglei }
11396e14926SGonglei
usb_ehci_pci_reset(DeviceState * dev)1144e289b1bSGonglei static void usb_ehci_pci_reset(DeviceState *dev)
1154e289b1bSGonglei {
1164e289b1bSGonglei PCIDevice *pci_dev = PCI_DEVICE(dev);
1174e289b1bSGonglei EHCIPCIState *i = PCI_EHCI(pci_dev);
1184e289b1bSGonglei EHCIState *s = &i->ehci;
1194e289b1bSGonglei
1204e289b1bSGonglei ehci_reset(s);
1214e289b1bSGonglei }
1224e289b1bSGonglei
usb_ehci_pci_write_config(PCIDevice * dev,uint32_t addr,uint32_t val,int l)12355903f1dSGerd Hoffmann static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr,
12455903f1dSGerd Hoffmann uint32_t val, int l)
12555903f1dSGerd Hoffmann {
1265aa3ca9fSAndreas Färber EHCIPCIState *i = PCI_EHCI(dev);
12755903f1dSGerd Hoffmann bool busmaster;
12855903f1dSGerd Hoffmann
12955903f1dSGerd Hoffmann pci_default_write_config(dev, addr, val, l);
13055903f1dSGerd Hoffmann
13155903f1dSGerd Hoffmann if (!range_covers_byte(addr, l, PCI_COMMAND)) {
13255903f1dSGerd Hoffmann return;
13355903f1dSGerd Hoffmann }
13455903f1dSGerd Hoffmann busmaster = pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_MASTER;
135df32fd1cSPaolo Bonzini i->ehci.as = busmaster ? pci_get_address_space(dev) : &address_space_memory;
13655903f1dSGerd Hoffmann }
13755903f1dSGerd Hoffmann
1380bf96f94SGerd Hoffmann static Property ehci_pci_properties[] = {
1390bf96f94SGerd Hoffmann DEFINE_PROP_UINT32("maxframes", EHCIPCIState, ehci.maxframes, 128),
1400bf96f94SGerd Hoffmann DEFINE_PROP_END_OF_LIST(),
1410bf96f94SGerd Hoffmann };
1420bf96f94SGerd Hoffmann
1430bf96f94SGerd Hoffmann static const VMStateDescription vmstate_ehci_pci = {
1440bf96f94SGerd Hoffmann .name = "ehci",
1450bf96f94SGerd Hoffmann .version_id = 2,
1460bf96f94SGerd Hoffmann .minimum_version_id = 1,
1473abedf29SRichard Henderson .fields = (const VMStateField[]) {
1480bf96f94SGerd Hoffmann VMSTATE_PCI_DEVICE(pcidev, EHCIPCIState),
1490bf96f94SGerd Hoffmann VMSTATE_STRUCT(ehci, EHCIPCIState, 2, vmstate_ehci, EHCIState),
1509d153047SGerd Hoffmann VMSTATE_END_OF_LIST()
1510bf96f94SGerd Hoffmann }
1520bf96f94SGerd Hoffmann };
1530bf96f94SGerd Hoffmann
ehci_class_init(ObjectClass * klass,void * data)1540bf96f94SGerd Hoffmann static void ehci_class_init(ObjectClass *klass, void *data)
1550bf96f94SGerd Hoffmann {
1560bf96f94SGerd Hoffmann DeviceClass *dc = DEVICE_CLASS(klass);
1570bf96f94SGerd Hoffmann PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1580bf96f94SGerd Hoffmann
1599af21dbeSMarkus Armbruster k->realize = usb_ehci_pci_realize;
16096e14926SGonglei k->exit = usb_ehci_pci_exit;
1610bf96f94SGerd Hoffmann k->class_id = PCI_CLASS_SERIAL_USB;
16255903f1dSGerd Hoffmann k->config_write = usb_ehci_pci_write_config;
1639d153047SGerd Hoffmann dc->vmsd = &vmstate_ehci_pci;
1644f67d30bSMarc-André Lureau device_class_set_props(dc, ehci_pci_properties);
165*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, usb_ehci_pci_reset);
1660bf96f94SGerd Hoffmann }
1670bf96f94SGerd Hoffmann
1685aa3ca9fSAndreas Färber static const TypeInfo ehci_pci_type_info = {
1695aa3ca9fSAndreas Färber .name = TYPE_PCI_EHCI,
1705aa3ca9fSAndreas Färber .parent = TYPE_PCI_DEVICE,
1715aa3ca9fSAndreas Färber .instance_size = sizeof(EHCIPCIState),
172d4614cc3SAndreas Färber .instance_init = usb_ehci_pci_init,
173d710e1e7SLi Qiang .instance_finalize = usb_ehci_pci_finalize,
1745aa3ca9fSAndreas Färber .abstract = true,
1755aa3ca9fSAndreas Färber .class_init = ehci_class_init,
176fd3b02c8SEduardo Habkost .interfaces = (InterfaceInfo[]) {
177fd3b02c8SEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE },
178fd3b02c8SEduardo Habkost { },
179fd3b02c8SEduardo Habkost },
1805aa3ca9fSAndreas Färber };
1815aa3ca9fSAndreas Färber
ehci_data_class_init(ObjectClass * klass,void * data)1825aa3ca9fSAndreas Färber static void ehci_data_class_init(ObjectClass *klass, void *data)
1835aa3ca9fSAndreas Färber {
1845aa3ca9fSAndreas Färber PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
185125ee0edSMarcel Apfelbaum DeviceClass *dc = DEVICE_CLASS(klass);
1865aa3ca9fSAndreas Färber EHCIPCIInfo *i = data;
1875aa3ca9fSAndreas Färber
1885aa3ca9fSAndreas Färber k->vendor_id = i->vendor_id;
1895aa3ca9fSAndreas Färber k->device_id = i->device_id;
1905aa3ca9fSAndreas Färber k->revision = i->revision;
191125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_USB, dc->categories);
192ec56214fSGerd Hoffmann if (i->companion) {
193ec56214fSGerd Hoffmann dc->hotpluggable = false;
194ec56214fSGerd Hoffmann }
1955aa3ca9fSAndreas Färber }
1965aa3ca9fSAndreas Färber
197df013187SGerd Hoffmann static struct EHCIPCIInfo ehci_pci_info[] = {
1980bf96f94SGerd Hoffmann {
199df013187SGerd Hoffmann .name = "usb-ehci",
200df013187SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL,
201df013187SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */
202df013187SGerd Hoffmann .revision = 0x10,
203df013187SGerd Hoffmann },{
204ba07630cSGerd Hoffmann .name = "ich9-usb-ehci1", /* 00:1d.7 */
205df013187SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL,
206df013187SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1,
207df013187SGerd Hoffmann .revision = 0x03,
208ec56214fSGerd Hoffmann .companion = true,
209ba07630cSGerd Hoffmann },{
210ba07630cSGerd Hoffmann .name = "ich9-usb-ehci2", /* 00:1a.7 */
211ba07630cSGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL,
212ba07630cSGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2,
213ba07630cSGerd Hoffmann .revision = 0x03,
214ec56214fSGerd Hoffmann .companion = true,
215df013187SGerd Hoffmann }
2160bf96f94SGerd Hoffmann };
2170bf96f94SGerd Hoffmann
ehci_pci_register_types(void)2180bf96f94SGerd Hoffmann static void ehci_pci_register_types(void)
2190bf96f94SGerd Hoffmann {
220df013187SGerd Hoffmann TypeInfo ehci_type_info = {
2215aa3ca9fSAndreas Färber .parent = TYPE_PCI_EHCI,
2225aa3ca9fSAndreas Färber .class_init = ehci_data_class_init,
223df013187SGerd Hoffmann };
224df013187SGerd Hoffmann int i;
225df013187SGerd Hoffmann
2265aa3ca9fSAndreas Färber type_register_static(&ehci_pci_type_info);
2275aa3ca9fSAndreas Färber
228df013187SGerd Hoffmann for (i = 0; i < ARRAY_SIZE(ehci_pci_info); i++) {
229df013187SGerd Hoffmann ehci_type_info.name = ehci_pci_info[i].name;
230df013187SGerd Hoffmann ehci_type_info.class_data = ehci_pci_info + i;
231df013187SGerd Hoffmann type_register(&ehci_type_info);
232df013187SGerd Hoffmann }
2330bf96f94SGerd Hoffmann }
2340bf96f94SGerd Hoffmann
2350bf96f94SGerd Hoffmann type_init(ehci_pci_register_types)
236