Merge remote-tracking branch 'remotes/mcayland/tags/qemu-sparc-signed' into staging
[qemu/cris-port.git] / tests / usb-hcd-ehci-test.c
blobc990492835eed666fa6662b606d8810b63dc4bfd
1 /*
2 * QTest testcase for USB EHCI
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
10 #include <glib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include "libqtest.h"
14 #include "libqos/pci-pc.h"
15 #include "qemu/osdep.h"
16 #include "hw/usb/uhci-regs.h"
17 #include "hw/usb/ehci-regs.h"
19 struct qhc {
20 QPCIDevice *dev;
21 void *base;
24 static QPCIBus *pcibus;
25 static struct qhc uhci1;
26 static struct qhc uhci2;
27 static struct qhc uhci3;
28 static struct qhc ehci1;
30 /* helpers */
32 static void pci_init_one(struct qhc *hc, uint32_t devfn, int bar)
34 hc->dev = qpci_device_find(pcibus, devfn);
35 g_assert(hc->dev != NULL);
36 qpci_device_enable(hc->dev);
37 hc->base = qpci_iomap(hc->dev, bar, NULL);
38 g_assert(hc->base != NULL);
41 #if 0
42 static void uhci_port_update(struct qhc *hc, int port,
43 uint16_t set, uint16_t clear)
45 void *addr = hc->base + 0x10 + 2 * port;
46 uint16_t value;
48 value = qpci_io_readw(hc->dev, addr);
49 value |= set;
50 value &= ~clear;
51 qpci_io_writew(hc->dev, addr, value);
53 #endif
55 static void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
57 void *addr = hc->base + 0x10 + 2 * port;
58 uint16_t value = qpci_io_readw(hc->dev, addr);
59 uint16_t mask = ~(UHCI_PORT_WRITE_CLEAR | UHCI_PORT_RSVD1);
61 #if 0
62 fprintf(stderr, "%s: %d, have 0x%04x, want 0x%04x\n",
63 __func__, port, value & mask, expect & mask);
64 #endif
65 g_assert((value & mask) == (expect & mask));
68 static void ehci_port_test(struct qhc *hc, int port, uint32_t expect)
70 void *addr = hc->base + 0x64 + 4 * port;
71 uint32_t value = qpci_io_readl(hc->dev, addr);
72 uint16_t mask = ~(PORTSC_CSC | PORTSC_PEDC | PORTSC_OCC);
74 #if 0
75 fprintf(stderr, "%s: %d, have 0x%08x, want 0x%08x\n",
76 __func__, port, value & mask, expect & mask);
77 #endif
78 g_assert((value & mask) == (expect & mask));
81 /* tests */
83 static void pci_init(void)
85 if (pcibus) {
86 return;
88 pcibus = qpci_init_pc();
89 g_assert(pcibus != NULL);
91 pci_init_one(&uhci1, QPCI_DEVFN(0x1d, 0), 4);
92 pci_init_one(&uhci2, QPCI_DEVFN(0x1d, 1), 4);
93 pci_init_one(&uhci3, QPCI_DEVFN(0x1d, 2), 4);
94 pci_init_one(&ehci1, QPCI_DEVFN(0x1d, 7), 0);
97 static void pci_uhci_port_1(void)
99 g_assert(pcibus != NULL);
101 uhci_port_test(&uhci1, 0, UHCI_PORT_CCS); /* usb-tablet */
102 uhci_port_test(&uhci1, 1, UHCI_PORT_CCS); /* usb-storage */
103 uhci_port_test(&uhci2, 0, 0);
104 uhci_port_test(&uhci2, 1, 0);
105 uhci_port_test(&uhci3, 0, 0);
106 uhci_port_test(&uhci3, 1, 0);
109 static void pci_ehci_port_1(void)
111 int i;
113 g_assert(pcibus != NULL);
115 for (i = 0; i < 6; i++) {
116 ehci_port_test(&ehci1, i, PORTSC_POWNER | PORTSC_PPOWER);
120 static void pci_ehci_config(void)
122 /* hands over all ports from companion uhci to ehci */
123 qpci_io_writew(ehci1.dev, ehci1.base + 0x60, 1);
126 static void pci_uhci_port_2(void)
128 g_assert(pcibus != NULL);
130 uhci_port_test(&uhci1, 0, 0); /* usb-tablet, @ehci */
131 uhci_port_test(&uhci1, 1, 0); /* usb-storage, @ehci */
132 uhci_port_test(&uhci2, 0, 0);
133 uhci_port_test(&uhci2, 1, 0);
134 uhci_port_test(&uhci3, 0, 0);
135 uhci_port_test(&uhci3, 1, 0);
138 static void pci_ehci_port_2(void)
140 static uint32_t expect[] = {
141 PORTSC_PPOWER | PORTSC_CONNECT, /* usb-tablet */
142 PORTSC_PPOWER | PORTSC_CONNECT, /* usb-storage */
143 PORTSC_PPOWER,
144 PORTSC_PPOWER,
145 PORTSC_PPOWER,
146 PORTSC_PPOWER,
148 int i;
150 g_assert(pcibus != NULL);
152 for (i = 0; i < 6; i++) {
153 ehci_port_test(&ehci1, i, expect[i]);
157 int main(int argc, char **argv)
159 int ret;
161 g_test_init(&argc, &argv, NULL);
162 qtest_add_func("/ehci/pci/init", pci_init);
163 qtest_add_func("/ehci/pci/uhci-port-1", pci_uhci_port_1);
164 qtest_add_func("/ehci/pci/ehci-port-1", pci_ehci_port_1);
165 qtest_add_func("/ehci/pci/ehci-config", pci_ehci_config);
166 qtest_add_func("/ehci/pci/uhci-port-2", pci_uhci_port_2);
167 qtest_add_func("/ehci/pci/ehci-port-2", pci_ehci_port_2);
169 qtest_start("-machine q35 -device ich9-usb-ehci1,bus=pcie.0,addr=1d.7,"
170 "multifunction=on,id=ich9-ehci-1 "
171 "-device ich9-usb-uhci1,bus=pcie.0,addr=1d.0,"
172 "multifunction=on,masterbus=ich9-ehci-1.0,firstport=0 "
173 "-device ich9-usb-uhci2,bus=pcie.0,addr=1d.1,"
174 "multifunction=on,masterbus=ich9-ehci-1.0,firstport=2 "
175 "-device ich9-usb-uhci3,bus=pcie.0,addr=1d.2,"
176 "multifunction=on,masterbus=ich9-ehci-1.0,firstport=4 "
177 "-drive if=none,id=usbcdrom,media=cdrom "
178 "-device usb-tablet,bus=ich9-ehci-1.0,port=1,usb_version=1 "
179 "-device usb-storage,bus=ich9-ehci-1.0,port=2,drive=usbcdrom ");
180 ret = g_test_run();
182 qtest_end();
184 return ret;