Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU USB EHCI Emulation |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or(at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "hw/usb/hcd-ehci.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 19 | #include "qemu/range.h" |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 20 | |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 21 | typedef struct EHCIPCIInfo { |
| 22 | const char *name; |
| 23 | uint16_t vendor_id; |
| 24 | uint16_t device_id; |
| 25 | uint8_t revision; |
Gerd Hoffmann | ec56214 | 2014-08-29 14:40:08 +0200 | [diff] [blame] | 26 | bool companion; |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 27 | } EHCIPCIInfo; |
| 28 | |
Markus Armbruster | 9af21db | 2015-01-19 15:52:30 +0100 | [diff] [blame] | 29 | static void usb_ehci_pci_realize(PCIDevice *dev, Error **errp) |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 30 | { |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 31 | EHCIPCIState *i = PCI_EHCI(dev); |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 32 | EHCIState *s = &i->ehci; |
| 33 | uint8_t *pci_conf = dev->config; |
| 34 | |
| 35 | pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20); |
| 36 | |
| 37 | /* capabilities pointer */ |
| 38 | pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00); |
| 39 | /* pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50); */ |
| 40 | |
| 41 | pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */ |
| 42 | pci_set_byte(&pci_conf[PCI_MIN_GNT], 0); |
| 43 | pci_set_byte(&pci_conf[PCI_MAX_LAT], 0); |
| 44 | |
| 45 | /* pci_conf[0x50] = 0x01; *//* power management caps */ |
| 46 | |
| 47 | pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); /* release # (2.1.4) */ |
| 48 | pci_set_byte(&pci_conf[0x61], 0x20); /* frame length adjustment (2.1.5) */ |
| 49 | pci_set_word(&pci_conf[0x62], 0x00); /* port wake up capability (2.1.6) */ |
| 50 | |
| 51 | pci_conf[0x64] = 0x00; |
| 52 | pci_conf[0x65] = 0x00; |
| 53 | pci_conf[0x66] = 0x00; |
| 54 | pci_conf[0x67] = 0x00; |
| 55 | pci_conf[0x68] = 0x01; |
| 56 | pci_conf[0x69] = 0x00; |
| 57 | pci_conf[0x6a] = 0x00; |
| 58 | pci_conf[0x6b] = 0x00; /* USBLEGSUP */ |
| 59 | pci_conf[0x6c] = 0x00; |
| 60 | pci_conf[0x6d] = 0x00; |
| 61 | pci_conf[0x6e] = 0x00; |
| 62 | pci_conf[0x6f] = 0xc0; /* USBLEFCTLSTS */ |
| 63 | |
Marcel Apfelbaum | 9e64f8a | 2013-10-07 10:36:39 +0300 | [diff] [blame] | 64 | s->irq = pci_allocate_irq(dev); |
Paolo Bonzini | df32fd1 | 2013-04-10 18:15:49 +0200 | [diff] [blame] | 65 | s->as = pci_get_address_space(dev); |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 66 | |
Andreas Färber | 08f4c90 | 2013-06-06 15:41:09 +0200 | [diff] [blame] | 67 | usb_ehci_realize(s, DEVICE(dev), NULL); |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 68 | pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem); |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Andreas Färber | d4614cc | 2013-06-06 15:41:10 +0200 | [diff] [blame] | 71 | static void usb_ehci_pci_init(Object *obj) |
| 72 | { |
Gerd Hoffmann | ec56214 | 2014-08-29 14:40:08 +0200 | [diff] [blame] | 73 | DeviceClass *dc = OBJECT_GET_CLASS(DeviceClass, obj, TYPE_DEVICE); |
Andreas Färber | d4614cc | 2013-06-06 15:41:10 +0200 | [diff] [blame] | 74 | EHCIPCIState *i = PCI_EHCI(obj); |
| 75 | EHCIState *s = &i->ehci; |
| 76 | |
| 77 | s->caps[0x09] = 0x68; /* EECP */ |
| 78 | |
| 79 | s->capsbase = 0x00; |
| 80 | s->opregbase = 0x20; |
Kuo-Jung Su | cc8d6a8 | 2013-06-06 15:41:12 +0200 | [diff] [blame] | 81 | s->portscbase = 0x44; |
| 82 | s->portnr = NB_PORTS; |
Andreas Färber | d4614cc | 2013-06-06 15:41:10 +0200 | [diff] [blame] | 83 | |
Gerd Hoffmann | ec56214 | 2014-08-29 14:40:08 +0200 | [diff] [blame] | 84 | if (!dc->hotpluggable) { |
| 85 | s->companion_enable = true; |
| 86 | } |
| 87 | |
Andreas Färber | d4614cc | 2013-06-06 15:41:10 +0200 | [diff] [blame] | 88 | usb_ehci_init(s, DEVICE(obj)); |
| 89 | } |
| 90 | |
Gonglei | 96e1492 | 2014-06-04 16:31:52 +0800 | [diff] [blame] | 91 | static void usb_ehci_pci_exit(PCIDevice *dev) |
| 92 | { |
| 93 | EHCIPCIState *i = PCI_EHCI(dev); |
| 94 | EHCIState *s = &i->ehci; |
| 95 | |
| 96 | usb_ehci_unrealize(s, DEVICE(dev), NULL); |
| 97 | |
Markus Armbruster | 012aef0 | 2015-08-26 14:02:53 +0200 | [diff] [blame] | 98 | g_free(s->irq); |
| 99 | s->irq = NULL; |
Gonglei | 96e1492 | 2014-06-04 16:31:52 +0800 | [diff] [blame] | 100 | } |
| 101 | |
Gonglei | 4e289b1 | 2015-03-18 17:33:47 +0800 | [diff] [blame] | 102 | static void usb_ehci_pci_reset(DeviceState *dev) |
| 103 | { |
| 104 | PCIDevice *pci_dev = PCI_DEVICE(dev); |
| 105 | EHCIPCIState *i = PCI_EHCI(pci_dev); |
| 106 | EHCIState *s = &i->ehci; |
| 107 | |
| 108 | ehci_reset(s); |
| 109 | } |
| 110 | |
Gerd Hoffmann | 55903f1 | 2012-11-15 13:07:49 +0100 | [diff] [blame] | 111 | static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr, |
| 112 | uint32_t val, int l) |
| 113 | { |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 114 | EHCIPCIState *i = PCI_EHCI(dev); |
Gerd Hoffmann | 55903f1 | 2012-11-15 13:07:49 +0100 | [diff] [blame] | 115 | bool busmaster; |
| 116 | |
| 117 | pci_default_write_config(dev, addr, val, l); |
| 118 | |
| 119 | if (!range_covers_byte(addr, l, PCI_COMMAND)) { |
| 120 | return; |
| 121 | } |
| 122 | busmaster = pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_MASTER; |
Paolo Bonzini | df32fd1 | 2013-04-10 18:15:49 +0200 | [diff] [blame] | 123 | i->ehci.as = busmaster ? pci_get_address_space(dev) : &address_space_memory; |
Gerd Hoffmann | 55903f1 | 2012-11-15 13:07:49 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 126 | static Property ehci_pci_properties[] = { |
| 127 | DEFINE_PROP_UINT32("maxframes", EHCIPCIState, ehci.maxframes, 128), |
| 128 | DEFINE_PROP_END_OF_LIST(), |
| 129 | }; |
| 130 | |
| 131 | static const VMStateDescription vmstate_ehci_pci = { |
| 132 | .name = "ehci", |
| 133 | .version_id = 2, |
| 134 | .minimum_version_id = 1, |
Juan Quintela | 6e3d652 | 2014-04-16 13:31:26 +0200 | [diff] [blame] | 135 | .fields = (VMStateField[]) { |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 136 | VMSTATE_PCI_DEVICE(pcidev, EHCIPCIState), |
| 137 | VMSTATE_STRUCT(ehci, EHCIPCIState, 2, vmstate_ehci, EHCIState), |
Gerd Hoffmann | 9d15304 | 2012-11-08 10:14:46 +0100 | [diff] [blame] | 138 | VMSTATE_END_OF_LIST() |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 139 | } |
| 140 | }; |
| 141 | |
| 142 | static void ehci_class_init(ObjectClass *klass, void *data) |
| 143 | { |
| 144 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 145 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
| 146 | |
Markus Armbruster | 9af21db | 2015-01-19 15:52:30 +0100 | [diff] [blame] | 147 | k->realize = usb_ehci_pci_realize; |
Gonglei | 96e1492 | 2014-06-04 16:31:52 +0800 | [diff] [blame] | 148 | k->exit = usb_ehci_pci_exit; |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 149 | k->class_id = PCI_CLASS_SERIAL_USB; |
Gerd Hoffmann | 55903f1 | 2012-11-15 13:07:49 +0100 | [diff] [blame] | 150 | k->config_write = usb_ehci_pci_write_config; |
Gerd Hoffmann | 9d15304 | 2012-11-08 10:14:46 +0100 | [diff] [blame] | 151 | dc->vmsd = &vmstate_ehci_pci; |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 152 | dc->props = ehci_pci_properties; |
Gonglei | 4e289b1 | 2015-03-18 17:33:47 +0800 | [diff] [blame] | 153 | dc->reset = usb_ehci_pci_reset; |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 156 | static const TypeInfo ehci_pci_type_info = { |
| 157 | .name = TYPE_PCI_EHCI, |
| 158 | .parent = TYPE_PCI_DEVICE, |
| 159 | .instance_size = sizeof(EHCIPCIState), |
Andreas Färber | d4614cc | 2013-06-06 15:41:10 +0200 | [diff] [blame] | 160 | .instance_init = usb_ehci_pci_init, |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 161 | .abstract = true, |
| 162 | .class_init = ehci_class_init, |
| 163 | }; |
| 164 | |
| 165 | static void ehci_data_class_init(ObjectClass *klass, void *data) |
| 166 | { |
| 167 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 168 | DeviceClass *dc = DEVICE_CLASS(klass); |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 169 | EHCIPCIInfo *i = data; |
| 170 | |
| 171 | k->vendor_id = i->vendor_id; |
| 172 | k->device_id = i->device_id; |
| 173 | k->revision = i->revision; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 174 | set_bit(DEVICE_CATEGORY_USB, dc->categories); |
Gerd Hoffmann | ec56214 | 2014-08-29 14:40:08 +0200 | [diff] [blame] | 175 | if (i->companion) { |
| 176 | dc->hotpluggable = false; |
| 177 | } |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 180 | static struct EHCIPCIInfo ehci_pci_info[] = { |
| 181 | { |
| 182 | .name = "usb-ehci", |
| 183 | .vendor_id = PCI_VENDOR_ID_INTEL, |
| 184 | .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */ |
| 185 | .revision = 0x10, |
| 186 | },{ |
Gerd Hoffmann | ba07630 | 2012-10-30 13:17:46 +0100 | [diff] [blame] | 187 | .name = "ich9-usb-ehci1", /* 00:1d.7 */ |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 188 | .vendor_id = PCI_VENDOR_ID_INTEL, |
| 189 | .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1, |
| 190 | .revision = 0x03, |
Gerd Hoffmann | ec56214 | 2014-08-29 14:40:08 +0200 | [diff] [blame] | 191 | .companion = true, |
Gerd Hoffmann | ba07630 | 2012-10-30 13:17:46 +0100 | [diff] [blame] | 192 | },{ |
| 193 | .name = "ich9-usb-ehci2", /* 00:1a.7 */ |
| 194 | .vendor_id = PCI_VENDOR_ID_INTEL, |
| 195 | .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2, |
| 196 | .revision = 0x03, |
Gerd Hoffmann | ec56214 | 2014-08-29 14:40:08 +0200 | [diff] [blame] | 197 | .companion = true, |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 198 | } |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | static void ehci_pci_register_types(void) |
| 202 | { |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 203 | TypeInfo ehci_type_info = { |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 204 | .parent = TYPE_PCI_EHCI, |
| 205 | .class_init = ehci_data_class_init, |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 206 | }; |
| 207 | int i; |
| 208 | |
Andreas Färber | 5aa3ca9 | 2012-12-16 04:49:43 +0100 | [diff] [blame] | 209 | type_register_static(&ehci_pci_type_info); |
| 210 | |
Gerd Hoffmann | df01318 | 2012-10-30 12:53:17 +0100 | [diff] [blame] | 211 | for (i = 0; i < ARRAY_SIZE(ehci_pci_info); i++) { |
| 212 | ehci_type_info.name = ehci_pci_info[i].name; |
| 213 | ehci_type_info.class_data = ehci_pci_info + i; |
| 214 | type_register(&ehci_type_info); |
| 215 | } |
Gerd Hoffmann | 0bf96f9 | 2012-10-30 12:20:06 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | type_init(ehci_pci_register_types) |
Gerd Hoffmann | bb4d2b2 | 2012-10-30 13:18:36 +0100 | [diff] [blame] | 219 | |
| 220 | struct ehci_companions { |
| 221 | const char *name; |
| 222 | int func; |
| 223 | int port; |
| 224 | }; |
| 225 | |
| 226 | static const struct ehci_companions ich9_1d[] = { |
| 227 | { .name = "ich9-usb-uhci1", .func = 0, .port = 0 }, |
| 228 | { .name = "ich9-usb-uhci2", .func = 1, .port = 2 }, |
| 229 | { .name = "ich9-usb-uhci3", .func = 2, .port = 4 }, |
| 230 | }; |
| 231 | |
| 232 | static const struct ehci_companions ich9_1a[] = { |
| 233 | { .name = "ich9-usb-uhci4", .func = 0, .port = 0 }, |
| 234 | { .name = "ich9-usb-uhci5", .func = 1, .port = 2 }, |
| 235 | { .name = "ich9-usb-uhci6", .func = 2, .port = 4 }, |
| 236 | }; |
| 237 | |
| 238 | int ehci_create_ich9_with_companions(PCIBus *bus, int slot) |
| 239 | { |
| 240 | const struct ehci_companions *comp; |
| 241 | PCIDevice *ehci, *uhci; |
| 242 | BusState *usbbus; |
| 243 | const char *name; |
| 244 | int i; |
| 245 | |
| 246 | switch (slot) { |
| 247 | case 0x1d: |
| 248 | name = "ich9-usb-ehci1"; |
| 249 | comp = ich9_1d; |
| 250 | break; |
| 251 | case 0x1a: |
| 252 | name = "ich9-usb-ehci2"; |
| 253 | comp = ich9_1a; |
| 254 | break; |
| 255 | default: |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | ehci = pci_create_multifunction(bus, PCI_DEVFN(slot, 7), true, name); |
| 260 | qdev_init_nofail(&ehci->qdev); |
| 261 | usbbus = QLIST_FIRST(&ehci->qdev.child_bus); |
| 262 | |
| 263 | for (i = 0; i < 3; i++) { |
| 264 | uhci = pci_create_multifunction(bus, PCI_DEVFN(slot, comp[i].func), |
| 265 | true, comp[i].name); |
| 266 | qdev_prop_set_string(&uhci->qdev, "masterbus", usbbus->name); |
| 267 | qdev_prop_set_uint32(&uhci->qdev, "firstport", comp[i].port); |
| 268 | qdev_init_nofail(&uhci->qdev); |
| 269 | } |
| 270 | return 0; |
| 271 | } |