]> git.proxmox.com Git - qemu.git/blame - hw/usb/hcd-ehci-pci.c
usb/hcd-ehci: Split off instance_init from realize
[qemu.git] / hw / usb / hcd-ehci-pci.c
CommitLineData
0bf96f94
GH
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"
1de7afc9 19#include "qemu/range.h"
0bf96f94 20
df013187
GH
21typedef struct EHCIPCIInfo {
22 const char *name;
23 uint16_t vendor_id;
24 uint16_t device_id;
25 uint8_t revision;
26} EHCIPCIInfo;
27
0bf96f94
GH
28static int usb_ehci_pci_initfn(PCIDevice *dev)
29{
5aa3ca9f 30 EHCIPCIState *i = PCI_EHCI(dev);
0bf96f94
GH
31 EHCIState *s = &i->ehci;
32 uint8_t *pci_conf = dev->config;
33
34 pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20);
35
36 /* capabilities pointer */
37 pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00);
38 /* pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50); */
39
40 pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */
41 pci_set_byte(&pci_conf[PCI_MIN_GNT], 0);
42 pci_set_byte(&pci_conf[PCI_MAX_LAT], 0);
43
44 /* pci_conf[0x50] = 0x01; *//* power management caps */
45
46 pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); /* release # (2.1.4) */
47 pci_set_byte(&pci_conf[0x61], 0x20); /* frame length adjustment (2.1.5) */
48 pci_set_word(&pci_conf[0x62], 0x00); /* port wake up capability (2.1.6) */
49
50 pci_conf[0x64] = 0x00;
51 pci_conf[0x65] = 0x00;
52 pci_conf[0x66] = 0x00;
53 pci_conf[0x67] = 0x00;
54 pci_conf[0x68] = 0x01;
55 pci_conf[0x69] = 0x00;
56 pci_conf[0x6a] = 0x00;
57 pci_conf[0x6b] = 0x00; /* USBLEGSUP */
58 pci_conf[0x6c] = 0x00;
59 pci_conf[0x6d] = 0x00;
60 pci_conf[0x6e] = 0x00;
61 pci_conf[0x6f] = 0xc0; /* USBLEFCTLSTS */
62
0bf96f94 63 s->irq = dev->irq[3];
df32fd1c 64 s->as = pci_get_address_space(dev);
0bf96f94 65
08f4c90b 66 usb_ehci_realize(s, DEVICE(dev), NULL);
0bf96f94
GH
67 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem);
68
69 return 0;
70}
71
d4614cc3
AF
72static void usb_ehci_pci_init(Object *obj)
73{
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;
81
82 usb_ehci_init(s, DEVICE(obj));
83}
84
55903f1d
GH
85static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr,
86 uint32_t val, int l)
87{
5aa3ca9f 88 EHCIPCIState *i = PCI_EHCI(dev);
55903f1d
GH
89 bool busmaster;
90
91 pci_default_write_config(dev, addr, val, l);
92
93 if (!range_covers_byte(addr, l, PCI_COMMAND)) {
94 return;
95 }
96 busmaster = pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_MASTER;
df32fd1c 97 i->ehci.as = busmaster ? pci_get_address_space(dev) : &address_space_memory;
55903f1d
GH
98}
99
0bf96f94
GH
100static Property ehci_pci_properties[] = {
101 DEFINE_PROP_UINT32("maxframes", EHCIPCIState, ehci.maxframes, 128),
102 DEFINE_PROP_END_OF_LIST(),
103};
104
105static const VMStateDescription vmstate_ehci_pci = {
106 .name = "ehci",
107 .version_id = 2,
108 .minimum_version_id = 1,
109 .fields = (VMStateField[]) {
110 VMSTATE_PCI_DEVICE(pcidev, EHCIPCIState),
111 VMSTATE_STRUCT(ehci, EHCIPCIState, 2, vmstate_ehci, EHCIState),
9d153047 112 VMSTATE_END_OF_LIST()
0bf96f94
GH
113 }
114};
115
116static void ehci_class_init(ObjectClass *klass, void *data)
117{
118 DeviceClass *dc = DEVICE_CLASS(klass);
119 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
120
121 k->init = usb_ehci_pci_initfn;
0bf96f94 122 k->class_id = PCI_CLASS_SERIAL_USB;
55903f1d 123 k->config_write = usb_ehci_pci_write_config;
6c2d1c32 124 k->no_hotplug = 1;
9d153047 125 dc->vmsd = &vmstate_ehci_pci;
0bf96f94
GH
126 dc->props = ehci_pci_properties;
127}
128
5aa3ca9f
AF
129static const TypeInfo ehci_pci_type_info = {
130 .name = TYPE_PCI_EHCI,
131 .parent = TYPE_PCI_DEVICE,
132 .instance_size = sizeof(EHCIPCIState),
d4614cc3 133 .instance_init = usb_ehci_pci_init,
5aa3ca9f
AF
134 .abstract = true,
135 .class_init = ehci_class_init,
136};
137
138static void ehci_data_class_init(ObjectClass *klass, void *data)
139{
140 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
141 EHCIPCIInfo *i = data;
142
143 k->vendor_id = i->vendor_id;
144 k->device_id = i->device_id;
145 k->revision = i->revision;
146}
147
df013187
GH
148static struct EHCIPCIInfo ehci_pci_info[] = {
149 {
150 .name = "usb-ehci",
151 .vendor_id = PCI_VENDOR_ID_INTEL,
152 .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */
153 .revision = 0x10,
154 },{
ba07630c 155 .name = "ich9-usb-ehci1", /* 00:1d.7 */
df013187
GH
156 .vendor_id = PCI_VENDOR_ID_INTEL,
157 .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1,
158 .revision = 0x03,
ba07630c
GH
159 },{
160 .name = "ich9-usb-ehci2", /* 00:1a.7 */
161 .vendor_id = PCI_VENDOR_ID_INTEL,
162 .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2,
163 .revision = 0x03,
df013187 164 }
0bf96f94
GH
165};
166
167static void ehci_pci_register_types(void)
168{
df013187 169 TypeInfo ehci_type_info = {
5aa3ca9f
AF
170 .parent = TYPE_PCI_EHCI,
171 .class_init = ehci_data_class_init,
df013187
GH
172 };
173 int i;
174
5aa3ca9f
AF
175 type_register_static(&ehci_pci_type_info);
176
df013187
GH
177 for (i = 0; i < ARRAY_SIZE(ehci_pci_info); i++) {
178 ehci_type_info.name = ehci_pci_info[i].name;
179 ehci_type_info.class_data = ehci_pci_info + i;
180 type_register(&ehci_type_info);
181 }
0bf96f94
GH
182}
183
184type_init(ehci_pci_register_types)
bb4d2b2f
GH
185
186struct ehci_companions {
187 const char *name;
188 int func;
189 int port;
190};
191
192static const struct ehci_companions ich9_1d[] = {
193 { .name = "ich9-usb-uhci1", .func = 0, .port = 0 },
194 { .name = "ich9-usb-uhci2", .func = 1, .port = 2 },
195 { .name = "ich9-usb-uhci3", .func = 2, .port = 4 },
196};
197
198static const struct ehci_companions ich9_1a[] = {
199 { .name = "ich9-usb-uhci4", .func = 0, .port = 0 },
200 { .name = "ich9-usb-uhci5", .func = 1, .port = 2 },
201 { .name = "ich9-usb-uhci6", .func = 2, .port = 4 },
202};
203
204int ehci_create_ich9_with_companions(PCIBus *bus, int slot)
205{
206 const struct ehci_companions *comp;
207 PCIDevice *ehci, *uhci;
208 BusState *usbbus;
209 const char *name;
210 int i;
211
212 switch (slot) {
213 case 0x1d:
214 name = "ich9-usb-ehci1";
215 comp = ich9_1d;
216 break;
217 case 0x1a:
218 name = "ich9-usb-ehci2";
219 comp = ich9_1a;
220 break;
221 default:
222 return -1;
223 }
224
225 ehci = pci_create_multifunction(bus, PCI_DEVFN(slot, 7), true, name);
226 qdev_init_nofail(&ehci->qdev);
227 usbbus = QLIST_FIRST(&ehci->qdev.child_bus);
228
229 for (i = 0; i < 3; i++) {
230 uhci = pci_create_multifunction(bus, PCI_DEVFN(slot, comp[i].func),
231 true, comp[i].name);
232 qdev_prop_set_string(&uhci->qdev, "masterbus", usbbus->name);
233 qdev_prop_set_uint32(&uhci->qdev, "firstport", comp[i].port);
234 qdev_init_nofail(&uhci->qdev);
235 }
236 return 0;
237}