]> Git Repo - qemu.git/blame - hw/usb-ohci.c
Merge remote-tracking branch 'mdroth/qga-pull-3-12-2012' into staging
[qemu.git] / hw / usb-ohci.c
CommitLineData
0d92ed30
PB
1/*
2 * QEMU USB OHCI Emulation
3 * Copyright (c) 2004 Gianni Tedesco
4 * Copyright (c) 2006 CodeSourcery
e24ad6f1 5 * Copyright (c) 2006 Openedhand Ltd.
0d92ed30
PB
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
8167ee88 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
0d92ed30
PB
19 *
20 * TODO:
21 * o Isochronous transfers
22 * o Allocate bandwidth in frames properly
23 * o Disable timers when nothing needs to be done, or remove timer usage
24 * all together.
25 * o Handle unrecoverable errors properly
26 * o BIOS work to boot from USB storage
27*/
28
87ecb68b
PB
29#include "hw.h"
30#include "qemu-timer.h"
31#include "usb.h"
32#include "pci.h"
18e08a55 33#include "usb-ohci.h"
61d3cf93
PB
34#include "sysbus.h"
35#include "qdev-addr.h"
0d92ed30
PB
36
37//#define DEBUG_OHCI
38/* Dump packet contents. */
39//#define DEBUG_PACKET
7bfe5777 40//#define DEBUG_ISOCH
0d92ed30
PB
41/* This causes frames to occur 1000x slower */
42//#define OHCI_TIME_WARP 1
43
44#ifdef DEBUG_OHCI
d0f2c4c6 45#define DPRINTF printf
0d92ed30 46#else
d0f2c4c6 47#define DPRINTF(...)
0d92ed30
PB
48#endif
49
50/* Number of Downstream Ports on the root hub. */
51
52#define OHCI_MAX_PORTS 15
53
54static int64_t usb_frame_time;
55static int64_t usb_bit_time;
56
57typedef struct OHCIPort {
58 USBPort port;
59 uint32_t ctrl;
60} OHCIPort;
61
62typedef struct {
b2317837 63 USBBus bus;
d537cf6c 64 qemu_irq irq;
6da48311 65 MemoryRegion mem;
0d92ed30 66 int num_ports;
e24ad6f1 67 const char *name;
0d92ed30
PB
68
69 QEMUTimer *eof_timer;
70 int64_t sof_time;
71
72 /* OHCI state */
73 /* Control partition */
74 uint32_t ctl, status;
75 uint32_t intr_status;
76 uint32_t intr;
77
78 /* memory pointer partition */
79 uint32_t hcca;
80 uint32_t ctrl_head, ctrl_cur;
81 uint32_t bulk_head, bulk_cur;
82 uint32_t per_cur;
83 uint32_t done;
84 int done_count;
85
86 /* Frame counter partition */
87 uint32_t fsmps:15;
88 uint32_t fit:1;
89 uint32_t fi:14;
90 uint32_t frt:1;
91 uint16_t frame_number;
92 uint16_t padding;
93 uint32_t pstart;
94 uint32_t lst;
95
96 /* Root Hub partition */
97 uint32_t rhdesc_a, rhdesc_b;
98 uint32_t rhstatus;
99 OHCIPort rhport[OHCI_MAX_PORTS];
4d611c9a 100
e24ad6f1
PB
101 /* PXA27x Non-OHCI events */
102 uint32_t hstatus;
103 uint32_t hmask;
104 uint32_t hreset;
105 uint32_t htest;
106
ac611340 107 /* SM501 local memory offset */
c227f099 108 target_phys_addr_t localmem_base;
ac611340 109
4d611c9a
PB
110 /* Active packets. */
111 uint32_t old_ctl;
112 USBPacket usb_packet;
113 uint8_t usb_buf[8192];
114 uint32_t async_td;
115 int async_complete;
116
0d92ed30
PB
117} OHCIState;
118
119/* Host Controller Communications Area */
120struct ohci_hcca {
121 uint32_t intr[32];
122 uint16_t frame, pad;
123 uint32_t done;
124};
125
73221b12 126static void ohci_bus_stop(OHCIState *ohci);
4706ab6c 127static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev);
73221b12 128
0d92ed30
PB
129/* Bitfields for the first word of an Endpoint Desciptor. */
130#define OHCI_ED_FA_SHIFT 0
131#define OHCI_ED_FA_MASK (0x7f<<OHCI_ED_FA_SHIFT)
132#define OHCI_ED_EN_SHIFT 7
133#define OHCI_ED_EN_MASK (0xf<<OHCI_ED_EN_SHIFT)
134#define OHCI_ED_D_SHIFT 11
135#define OHCI_ED_D_MASK (3<<OHCI_ED_D_SHIFT)
136#define OHCI_ED_S (1<<13)
137#define OHCI_ED_K (1<<14)
138#define OHCI_ED_F (1<<15)
7bfe5777
AZ
139#define OHCI_ED_MPS_SHIFT 16
140#define OHCI_ED_MPS_MASK (0x7ff<<OHCI_ED_MPS_SHIFT)
0d92ed30
PB
141
142/* Flags in the head field of an Endpoint Desciptor. */
143#define OHCI_ED_H 1
144#define OHCI_ED_C 2
145
146/* Bitfields for the first word of a Transfer Desciptor. */
147#define OHCI_TD_R (1<<18)
148#define OHCI_TD_DP_SHIFT 19
149#define OHCI_TD_DP_MASK (3<<OHCI_TD_DP_SHIFT)
150#define OHCI_TD_DI_SHIFT 21
151#define OHCI_TD_DI_MASK (7<<OHCI_TD_DI_SHIFT)
152#define OHCI_TD_T0 (1<<24)
8d11b78c 153#define OHCI_TD_T1 (1<<25)
0d92ed30
PB
154#define OHCI_TD_EC_SHIFT 26
155#define OHCI_TD_EC_MASK (3<<OHCI_TD_EC_SHIFT)
156#define OHCI_TD_CC_SHIFT 28
157#define OHCI_TD_CC_MASK (0xf<<OHCI_TD_CC_SHIFT)
158
7bfe5777
AZ
159/* Bitfields for the first word of an Isochronous Transfer Desciptor. */
160/* CC & DI - same as in the General Transfer Desciptor */
161#define OHCI_TD_SF_SHIFT 0
162#define OHCI_TD_SF_MASK (0xffff<<OHCI_TD_SF_SHIFT)
163#define OHCI_TD_FC_SHIFT 24
164#define OHCI_TD_FC_MASK (7<<OHCI_TD_FC_SHIFT)
165
166/* Isochronous Transfer Desciptor - Offset / PacketStatusWord */
167#define OHCI_TD_PSW_CC_SHIFT 12
168#define OHCI_TD_PSW_CC_MASK (0xf<<OHCI_TD_PSW_CC_SHIFT)
169#define OHCI_TD_PSW_SIZE_SHIFT 0
170#define OHCI_TD_PSW_SIZE_MASK (0xfff<<OHCI_TD_PSW_SIZE_SHIFT)
171
172#define OHCI_PAGE_MASK 0xfffff000
173#define OHCI_OFFSET_MASK 0xfff
174
0d92ed30
PB
175#define OHCI_DPTR_MASK 0xfffffff0
176
177#define OHCI_BM(val, field) \
178 (((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT)
179
180#define OHCI_SET_BM(val, field, newval) do { \
181 val &= ~OHCI_##field##_MASK; \
182 val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \
183 } while(0)
184
185/* endpoint descriptor */
186struct ohci_ed {
187 uint32_t flags;
188 uint32_t tail;
189 uint32_t head;
190 uint32_t next;
191};
192
193/* General transfer descriptor */
194struct ohci_td {
195 uint32_t flags;
196 uint32_t cbp;
197 uint32_t next;
198 uint32_t be;
199};
200
7bfe5777
AZ
201/* Isochronous transfer descriptor */
202struct ohci_iso_td {
203 uint32_t flags;
204 uint32_t bp;
205 uint32_t next;
206 uint32_t be;
207 uint16_t offset[8];
208};
209
0d92ed30
PB
210#define USB_HZ 12000000
211
212/* OHCI Local stuff */
213#define OHCI_CTL_CBSR ((1<<0)|(1<<1))
214#define OHCI_CTL_PLE (1<<2)
215#define OHCI_CTL_IE (1<<3)
216#define OHCI_CTL_CLE (1<<4)
217#define OHCI_CTL_BLE (1<<5)
218#define OHCI_CTL_HCFS ((1<<6)|(1<<7))
219#define OHCI_USB_RESET 0x00
220#define OHCI_USB_RESUME 0x40
221#define OHCI_USB_OPERATIONAL 0x80
222#define OHCI_USB_SUSPEND 0xc0
223#define OHCI_CTL_IR (1<<8)
224#define OHCI_CTL_RWC (1<<9)
225#define OHCI_CTL_RWE (1<<10)
226
227#define OHCI_STATUS_HCR (1<<0)
228#define OHCI_STATUS_CLF (1<<1)
229#define OHCI_STATUS_BLF (1<<2)
230#define OHCI_STATUS_OCR (1<<3)
231#define OHCI_STATUS_SOC ((1<<6)|(1<<7))
232
233#define OHCI_INTR_SO (1<<0) /* Scheduling overrun */
234#define OHCI_INTR_WD (1<<1) /* HcDoneHead writeback */
235#define OHCI_INTR_SF (1<<2) /* Start of frame */
236#define OHCI_INTR_RD (1<<3) /* Resume detect */
237#define OHCI_INTR_UE (1<<4) /* Unrecoverable error */
238#define OHCI_INTR_FNO (1<<5) /* Frame number overflow */
239#define OHCI_INTR_RHSC (1<<6) /* Root hub status change */
240#define OHCI_INTR_OC (1<<30) /* Ownership change */
241#define OHCI_INTR_MIE (1<<31) /* Master Interrupt Enable */
242
243#define OHCI_HCCA_SIZE 0x100
244#define OHCI_HCCA_MASK 0xffffff00
245
246#define OHCI_EDPTR_MASK 0xfffffff0
247
248#define OHCI_FMI_FI 0x00003fff
249#define OHCI_FMI_FSMPS 0xffff0000
250#define OHCI_FMI_FIT 0x80000000
251
252#define OHCI_FR_RT (1<<31)
253
254#define OHCI_LS_THRESH 0x628
255
256#define OHCI_RHA_RW_MASK 0x00000000 /* Mask of supported features. */
257#define OHCI_RHA_PSM (1<<8)
258#define OHCI_RHA_NPS (1<<9)
259#define OHCI_RHA_DT (1<<10)
260#define OHCI_RHA_OCPM (1<<11)
261#define OHCI_RHA_NOCP (1<<12)
262#define OHCI_RHA_POTPGT_MASK 0xff000000
263
264#define OHCI_RHS_LPS (1<<0)
265#define OHCI_RHS_OCI (1<<1)
266#define OHCI_RHS_DRWE (1<<15)
267#define OHCI_RHS_LPSC (1<<16)
268#define OHCI_RHS_OCIC (1<<17)
269#define OHCI_RHS_CRWE (1<<31)
270
271#define OHCI_PORT_CCS (1<<0)
272#define OHCI_PORT_PES (1<<1)
273#define OHCI_PORT_PSS (1<<2)
274#define OHCI_PORT_POCI (1<<3)
275#define OHCI_PORT_PRS (1<<4)
276#define OHCI_PORT_PPS (1<<8)
277#define OHCI_PORT_LSDA (1<<9)
278#define OHCI_PORT_CSC (1<<16)
279#define OHCI_PORT_PESC (1<<17)
280#define OHCI_PORT_PSSC (1<<18)
281#define OHCI_PORT_OCIC (1<<19)
282#define OHCI_PORT_PRSC (1<<20)
283#define OHCI_PORT_WTC (OHCI_PORT_CSC|OHCI_PORT_PESC|OHCI_PORT_PSSC \
284 |OHCI_PORT_OCIC|OHCI_PORT_PRSC)
285
286#define OHCI_TD_DIR_SETUP 0x0
287#define OHCI_TD_DIR_OUT 0x1
288#define OHCI_TD_DIR_IN 0x2
289#define OHCI_TD_DIR_RESERVED 0x3
290
291#define OHCI_CC_NOERROR 0x0
292#define OHCI_CC_CRC 0x1
293#define OHCI_CC_BITSTUFFING 0x2
294#define OHCI_CC_DATATOGGLEMISMATCH 0x3
295#define OHCI_CC_STALL 0x4
296#define OHCI_CC_DEVICENOTRESPONDING 0x5
297#define OHCI_CC_PIDCHECKFAILURE 0x6
298#define OHCI_CC_UNDEXPETEDPID 0x7
299#define OHCI_CC_DATAOVERRUN 0x8
300#define OHCI_CC_DATAUNDERRUN 0x9
301#define OHCI_CC_BUFFEROVERRUN 0xc
302#define OHCI_CC_BUFFERUNDERRUN 0xd
303
e24ad6f1
PB
304#define OHCI_HRESET_FSBIR (1 << 0)
305
61064870
PB
306/* Update IRQ levels */
307static inline void ohci_intr_update(OHCIState *ohci)
308{
309 int level = 0;
310
311 if ((ohci->intr & OHCI_INTR_MIE) &&
312 (ohci->intr_status & ohci->intr))
313 level = 1;
314
d537cf6c 315 qemu_set_irq(ohci->irq, level);
61064870
PB
316}
317
318/* Set an interrupt */
319static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr)
320{
321 ohci->intr_status |= intr;
322 ohci_intr_update(ohci);
323}
324
325/* Attach or detach a device on a root hub port. */
618c169b 326static void ohci_attach(USBPort *port1)
0d92ed30
PB
327{
328 OHCIState *s = port1->opaque;
329 OHCIPort *port = &s->rhport[port1->index];
3dc345d5 330 uint32_t old_state = port->ctrl;
0d92ed30 331
618c169b
GH
332 /* set connect status */
333 port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC;
334
335 /* update speed */
336 if (port->port.dev->speed == USB_SPEED_LOW) {
337 port->ctrl |= OHCI_PORT_LSDA;
0d92ed30 338 } else {
618c169b
GH
339 port->ctrl &= ~OHCI_PORT_LSDA;
340 }
341
342 /* notify of remote-wakeup */
343 if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
344 ohci_set_interrupt(s, OHCI_INTR_RD);
345 }
346
347 DPRINTF("usb-ohci: Attached port %d\n", port1->index);
3dc345d5
GH
348
349 if (old_state != port->ctrl) {
350 ohci_set_interrupt(s, OHCI_INTR_RHSC);
351 }
618c169b
GH
352}
353
354static void ohci_detach(USBPort *port1)
355{
356 OHCIState *s = port1->opaque;
357 OHCIPort *port = &s->rhport[port1->index];
358 uint32_t old_state = port->ctrl;
359
4706ab6c
HG
360 ohci_async_cancel_device(s, port1->dev);
361
618c169b
GH
362 /* set connect status */
363 if (port->ctrl & OHCI_PORT_CCS) {
364 port->ctrl &= ~OHCI_PORT_CCS;
365 port->ctrl |= OHCI_PORT_CSC;
366 }
367 /* disable port */
368 if (port->ctrl & OHCI_PORT_PES) {
369 port->ctrl &= ~OHCI_PORT_PES;
370 port->ctrl |= OHCI_PORT_PESC;
0d92ed30 371 }
618c169b 372 DPRINTF("usb-ohci: Detached port %d\n", port1->index);
61064870 373
3dc345d5 374 if (old_state != port->ctrl) {
61064870 375 ohci_set_interrupt(s, OHCI_INTR_RHSC);
3dc345d5 376 }
0d92ed30
PB
377}
378
d47e59b8 379static void ohci_wakeup(USBPort *port1)
9bba1eb1 380{
d47e59b8
HG
381 OHCIState *s = port1->opaque;
382 OHCIPort *port = &s->rhport[port1->index];
f3dc0051 383 uint32_t intr = 0;
9bba1eb1 384 if (port->ctrl & OHCI_PORT_PSS) {
d47e59b8 385 DPRINTF("usb-ohci: port %d: wakeup\n", port1->index);
9bba1eb1
PM
386 port->ctrl |= OHCI_PORT_PSSC;
387 port->ctrl &= ~OHCI_PORT_PSS;
f3dc0051 388 intr = OHCI_INTR_RHSC;
9bba1eb1 389 }
f3dc0051
PM
390 /* Note that the controller can be suspended even if this port is not */
391 if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
392 DPRINTF("usb-ohci: remote-wakeup: SUSPEND->RESUME\n");
393 /* This is the one state transition the controller can do by itself */
394 s->ctl &= ~OHCI_CTL_HCFS;
395 s->ctl |= OHCI_USB_RESUME;
396 /* In suspend mode only ResumeDetected is possible, not RHSC:
397 * see the OHCI spec 5.1.2.3.
398 */
399 intr = OHCI_INTR_RD;
400 }
401 ohci_set_interrupt(s, intr);
9bba1eb1
PM
402}
403
4706ab6c
HG
404static void ohci_child_detach(USBPort *port1, USBDevice *child)
405{
406 OHCIState *s = port1->opaque;
407
408 ohci_async_cancel_device(s, child);
409}
410
993048bb
GH
411static USBDevice *ohci_find_device(OHCIState *ohci, uint8_t addr)
412{
413 USBDevice *dev;
414 int i;
415
416 for (i = 0; i < ohci->num_ports; i++) {
417 if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0) {
418 continue;
419 }
420 dev = usb_find_device(&ohci->rhport[i].port, addr);
421 if (dev != NULL) {
422 return dev;
423 }
424 }
425 return NULL;
426}
427
0d92ed30 428/* Reset the controller */
73221b12 429static void ohci_reset(void *opaque)
0d92ed30 430{
73221b12 431 OHCIState *ohci = opaque;
0d92ed30
PB
432 OHCIPort *port;
433 int i;
434
73221b12 435 ohci_bus_stop(ohci);
0d92ed30 436 ohci->ctl = 0;
4d611c9a 437 ohci->old_ctl = 0;
0d92ed30
PB
438 ohci->status = 0;
439 ohci->intr_status = 0;
440 ohci->intr = OHCI_INTR_MIE;
441
442 ohci->hcca = 0;
443 ohci->ctrl_head = ohci->ctrl_cur = 0;
444 ohci->bulk_head = ohci->bulk_cur = 0;
445 ohci->per_cur = 0;
446 ohci->done = 0;
447 ohci->done_count = 7;
448
449 /* FSMPS is marked TBD in OCHI 1.0, what gives ffs?
450 * I took the value linux sets ...
451 */
452 ohci->fsmps = 0x2778;
453 ohci->fi = 0x2edf;
454 ohci->fit = 0;
455 ohci->frt = 0;
456 ohci->frame_number = 0;
457 ohci->pstart = 0;
458 ohci->lst = OHCI_LS_THRESH;
459
460 ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports;
461 ohci->rhdesc_b = 0x0; /* Impl. specific */
462 ohci->rhstatus = 0;
463
464 for (i = 0; i < ohci->num_ports; i++)
465 {
466 port = &ohci->rhport[i];
467 port->ctrl = 0;
891fb2cd 468 if (port->port.dev && port->port.dev->attached) {
d28f4e2d 469 usb_port_reset(&port->port);
618c169b 470 }
0d92ed30 471 }
4d611c9a
PB
472 if (ohci->async_td) {
473 usb_cancel_packet(&ohci->usb_packet);
474 ohci->async_td = 0;
475 }
d0f2c4c6 476 DPRINTF("usb-ohci: Reset %s\n", ohci->name);
0d92ed30
PB
477}
478
0d92ed30 479/* Get an array of dwords from main memory */
ac611340
AJ
480static inline int get_dwords(OHCIState *ohci,
481 uint32_t addr, uint32_t *buf, int num)
0d92ed30
PB
482{
483 int i;
484
ac611340
AJ
485 addr += ohci->localmem_base;
486
0d92ed30 487 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
54f7b4a3 488 cpu_physical_memory_read(addr, buf, sizeof(*buf));
0d92ed30
PB
489 *buf = le32_to_cpu(*buf);
490 }
491
492 return 1;
493}
494
495/* Put an array of dwords in to main memory */
ac611340
AJ
496static inline int put_dwords(OHCIState *ohci,
497 uint32_t addr, uint32_t *buf, int num)
0d92ed30
PB
498{
499 int i;
500
ac611340
AJ
501 addr += ohci->localmem_base;
502
0d92ed30
PB
503 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
504 uint32_t tmp = cpu_to_le32(*buf);
54f7b4a3 505 cpu_physical_memory_write(addr, &tmp, sizeof(tmp));
0d92ed30
PB
506 }
507
508 return 1;
509}
510
7bfe5777 511/* Get an array of words from main memory */
ac611340
AJ
512static inline int get_words(OHCIState *ohci,
513 uint32_t addr, uint16_t *buf, int num)
7bfe5777
AZ
514{
515 int i;
516
ac611340
AJ
517 addr += ohci->localmem_base;
518
7bfe5777 519 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
54f7b4a3 520 cpu_physical_memory_read(addr, buf, sizeof(*buf));
7bfe5777
AZ
521 *buf = le16_to_cpu(*buf);
522 }
523
524 return 1;
525}
526
527/* Put an array of words in to main memory */
ac611340
AJ
528static inline int put_words(OHCIState *ohci,
529 uint32_t addr, uint16_t *buf, int num)
7bfe5777
AZ
530{
531 int i;
532
ac611340
AJ
533 addr += ohci->localmem_base;
534
7bfe5777
AZ
535 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
536 uint16_t tmp = cpu_to_le16(*buf);
54f7b4a3 537 cpu_physical_memory_write(addr, &tmp, sizeof(tmp));
7bfe5777
AZ
538 }
539
540 return 1;
541}
542
ac611340
AJ
543static inline int ohci_read_ed(OHCIState *ohci,
544 uint32_t addr, struct ohci_ed *ed)
0d92ed30 545{
ac611340 546 return get_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
0d92ed30
PB
547}
548
ac611340
AJ
549static inline int ohci_read_td(OHCIState *ohci,
550 uint32_t addr, struct ohci_td *td)
0d92ed30 551{
ac611340 552 return get_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
0d92ed30
PB
553}
554
ac611340
AJ
555static inline int ohci_read_iso_td(OHCIState *ohci,
556 uint32_t addr, struct ohci_iso_td *td)
7bfe5777 557{
ac611340
AJ
558 return (get_dwords(ohci, addr, (uint32_t *)td, 4) &&
559 get_words(ohci, addr + 16, td->offset, 8));
7bfe5777
AZ
560}
561
ac611340
AJ
562static inline int ohci_read_hcca(OHCIState *ohci,
563 uint32_t addr, struct ohci_hcca *hcca)
0d92ed30 564{
54f7b4a3 565 cpu_physical_memory_read(addr + ohci->localmem_base, hcca, sizeof(*hcca));
ac611340 566 return 1;
0d92ed30
PB
567}
568
ac611340
AJ
569static inline int ohci_put_ed(OHCIState *ohci,
570 uint32_t addr, struct ohci_ed *ed)
0d92ed30 571{
ac611340 572 return put_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
0d92ed30
PB
573}
574
ac611340
AJ
575static inline int ohci_put_td(OHCIState *ohci,
576 uint32_t addr, struct ohci_td *td)
7bfe5777 577{
ac611340
AJ
578 return put_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
579}
580
581static inline int ohci_put_iso_td(OHCIState *ohci,
582 uint32_t addr, struct ohci_iso_td *td)
583{
584 return (put_dwords(ohci, addr, (uint32_t *)td, 4) &&
585 put_words(ohci, addr + 16, td->offset, 8));
586}
587
588static inline int ohci_put_hcca(OHCIState *ohci,
589 uint32_t addr, struct ohci_hcca *hcca)
590{
54f7b4a3 591 cpu_physical_memory_write(addr + ohci->localmem_base, hcca, sizeof(*hcca));
ac611340 592 return 1;
7bfe5777
AZ
593}
594
0d92ed30 595/* Read/Write the contents of a TD from/to main memory. */
ac611340
AJ
596static void ohci_copy_td(OHCIState *ohci, struct ohci_td *td,
597 uint8_t *buf, int len, int write)
0d92ed30
PB
598{
599 uint32_t ptr;
600 uint32_t n;
601
602 ptr = td->cbp;
603 n = 0x1000 - (ptr & 0xfff);
604 if (n > len)
605 n = len;
ac611340 606 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write);
0d92ed30
PB
607 if (n == len)
608 return;
609 ptr = td->be & ~0xfffu;
e6f3e5e0 610 buf += n;
ac611340 611 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write);
0d92ed30
PB
612}
613
7bfe5777 614/* Read/Write the contents of an ISO TD from/to main memory. */
ac611340
AJ
615static void ohci_copy_iso_td(OHCIState *ohci,
616 uint32_t start_addr, uint32_t end_addr,
7bfe5777
AZ
617 uint8_t *buf, int len, int write)
618{
619 uint32_t ptr;
620 uint32_t n;
4d611c9a 621
7bfe5777
AZ
622 ptr = start_addr;
623 n = 0x1000 - (ptr & 0xfff);
624 if (n > len)
625 n = len;
ac611340 626 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write);
7bfe5777
AZ
627 if (n == len)
628 return;
629 ptr = end_addr & ~0xfffu;
630 buf += n;
ac611340 631 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write);
7bfe5777
AZ
632}
633
634static void ohci_process_lists(OHCIState *ohci, int completion);
635
d47e59b8 636static void ohci_async_complete_packet(USBPort *port, USBPacket *packet)
4d611c9a 637{
9066df13 638 OHCIState *ohci = container_of(packet, OHCIState, usb_packet);
4d611c9a 639#ifdef DEBUG_PACKET
d0f2c4c6 640 DPRINTF("Async packet complete\n");
4d611c9a
PB
641#endif
642 ohci->async_complete = 1;
7bfe5777
AZ
643 ohci_process_lists(ohci, 1);
644}
645
646#define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
647
648static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
649 int completion)
650{
651 int dir;
652 size_t len = 0;
d4c4e6fd 653#ifdef DEBUG_ISOCH
7ccfb2eb 654 const char *str = NULL;
d4c4e6fd 655#endif
7bfe5777
AZ
656 int pid;
657 int ret;
658 int i;
659 USBDevice *dev;
079d0b7f 660 USBEndpoint *ep;
7bfe5777
AZ
661 struct ohci_iso_td iso_td;
662 uint32_t addr;
663 uint16_t starting_frame;
664 int16_t relative_frame_number;
665 int frame_count;
666 uint32_t start_offset, next_offset, end_offset = 0;
667 uint32_t start_addr, end_addr;
668
669 addr = ed->head & OHCI_DPTR_MASK;
670
ac611340 671 if (!ohci_read_iso_td(ohci, addr, &iso_td)) {
7bfe5777
AZ
672 printf("usb-ohci: ISO_TD read error at %x\n", addr);
673 return 0;
674 }
675
676 starting_frame = OHCI_BM(iso_td.flags, TD_SF);
677 frame_count = OHCI_BM(iso_td.flags, TD_FC);
678 relative_frame_number = USUB(ohci->frame_number, starting_frame);
679
680#ifdef DEBUG_ISOCH
681 printf("--- ISO_TD ED head 0x%.8x tailp 0x%.8x\n"
682 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
683 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
684 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
685 "frame_number 0x%.8x starting_frame 0x%.8x\n"
686 "frame_count 0x%.8x relative %d\n"
687 "di 0x%.8x cc 0x%.8x\n",
688 ed->head & OHCI_DPTR_MASK, ed->tail & OHCI_DPTR_MASK,
689 iso_td.flags, iso_td.bp, iso_td.next, iso_td.be,
690 iso_td.offset[0], iso_td.offset[1], iso_td.offset[2], iso_td.offset[3],
691 iso_td.offset[4], iso_td.offset[5], iso_td.offset[6], iso_td.offset[7],
692 ohci->frame_number, starting_frame,
693 frame_count, relative_frame_number,
694 OHCI_BM(iso_td.flags, TD_DI), OHCI_BM(iso_td.flags, TD_CC));
695#endif
696
697 if (relative_frame_number < 0) {
d0f2c4c6 698 DPRINTF("usb-ohci: ISO_TD R=%d < 0\n", relative_frame_number);
7bfe5777
AZ
699 return 1;
700 } else if (relative_frame_number > frame_count) {
701 /* ISO TD expired - retire the TD to the Done Queue and continue with
702 the next ISO TD of the same ED */
d0f2c4c6 703 DPRINTF("usb-ohci: ISO_TD R=%d > FC=%d\n", relative_frame_number,
7bfe5777
AZ
704 frame_count);
705 OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
706 ed->head &= ~OHCI_DPTR_MASK;
707 ed->head |= (iso_td.next & OHCI_DPTR_MASK);
708 iso_td.next = ohci->done;
709 ohci->done = addr;
710 i = OHCI_BM(iso_td.flags, TD_DI);
711 if (i < ohci->done_count)
712 ohci->done_count = i;
ac611340 713 ohci_put_iso_td(ohci, addr, &iso_td);
7bfe5777
AZ
714 return 0;
715 }
716
717 dir = OHCI_BM(ed->flags, ED_D);
718 switch (dir) {
719 case OHCI_TD_DIR_IN:
d4c4e6fd 720#ifdef DEBUG_ISOCH
7bfe5777 721 str = "in";
d4c4e6fd 722#endif
7bfe5777
AZ
723 pid = USB_TOKEN_IN;
724 break;
725 case OHCI_TD_DIR_OUT:
d4c4e6fd 726#ifdef DEBUG_ISOCH
7bfe5777 727 str = "out";
d4c4e6fd 728#endif
7bfe5777
AZ
729 pid = USB_TOKEN_OUT;
730 break;
731 case OHCI_TD_DIR_SETUP:
d4c4e6fd 732#ifdef DEBUG_ISOCH
7bfe5777 733 str = "setup";
d4c4e6fd 734#endif
7bfe5777
AZ
735 pid = USB_TOKEN_SETUP;
736 break;
737 default:
738 printf("usb-ohci: Bad direction %d\n", dir);
739 return 1;
740 }
741
742 if (!iso_td.bp || !iso_td.be) {
743 printf("usb-ohci: ISO_TD bp 0x%.8x be 0x%.8x\n", iso_td.bp, iso_td.be);
744 return 1;
745 }
746
747 start_offset = iso_td.offset[relative_frame_number];
748 next_offset = iso_td.offset[relative_frame_number + 1];
749
750 if (!(OHCI_BM(start_offset, TD_PSW_CC) & 0xe) ||
751 ((relative_frame_number < frame_count) &&
752 !(OHCI_BM(next_offset, TD_PSW_CC) & 0xe))) {
753 printf("usb-ohci: ISO_TD cc != not accessed 0x%.8x 0x%.8x\n",
754 start_offset, next_offset);
755 return 1;
756 }
757
758 if ((relative_frame_number < frame_count) && (start_offset > next_offset)) {
759 printf("usb-ohci: ISO_TD start_offset=0x%.8x > next_offset=0x%.8x\n",
760 start_offset, next_offset);
761 return 1;
762 }
763
764 if ((start_offset & 0x1000) == 0) {
765 start_addr = (iso_td.bp & OHCI_PAGE_MASK) |
766 (start_offset & OHCI_OFFSET_MASK);
767 } else {
768 start_addr = (iso_td.be & OHCI_PAGE_MASK) |
769 (start_offset & OHCI_OFFSET_MASK);
770 }
771
772 if (relative_frame_number < frame_count) {
773 end_offset = next_offset - 1;
774 if ((end_offset & 0x1000) == 0) {
775 end_addr = (iso_td.bp & OHCI_PAGE_MASK) |
776 (end_offset & OHCI_OFFSET_MASK);
777 } else {
778 end_addr = (iso_td.be & OHCI_PAGE_MASK) |
779 (end_offset & OHCI_OFFSET_MASK);
780 }
781 } else {
782 /* Last packet in the ISO TD */
783 end_addr = iso_td.be;
784 }
785
786 if ((start_addr & OHCI_PAGE_MASK) != (end_addr & OHCI_PAGE_MASK)) {
787 len = (end_addr & OHCI_OFFSET_MASK) + 0x1001
788 - (start_addr & OHCI_OFFSET_MASK);
789 } else {
790 len = end_addr - start_addr + 1;
791 }
792
793 if (len && dir != OHCI_TD_DIR_IN) {
ac611340 794 ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, len, 0);
7bfe5777
AZ
795 }
796
797 if (completion) {
4f4321c1 798 ret = ohci->usb_packet.result;
7bfe5777 799 } else {
079d0b7f
GH
800 dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
801 ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
802 usb_packet_setup(&ohci->usb_packet, pid, ep);
993048bb 803 usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, len);
993048bb 804 ret = usb_handle_packet(dev, &ohci->usb_packet);
7bfe5777
AZ
805 if (ret == USB_RET_ASYNC) {
806 return 1;
807 }
808 }
809
810#ifdef DEBUG_ISOCH
811 printf("so 0x%.8x eo 0x%.8x\nsa 0x%.8x ea 0x%.8x\ndir %s len %zu ret %d\n",
812 start_offset, end_offset, start_addr, end_addr, str, len, ret);
813#endif
814
815 /* Writeback */
816 if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) {
817 /* IN transfer succeeded */
ac611340 818 ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, ret, 1);
7bfe5777
AZ
819 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
820 OHCI_CC_NOERROR);
821 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret);
822 } else if (dir == OHCI_TD_DIR_OUT && ret == len) {
823 /* OUT transfer succeeded */
824 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
825 OHCI_CC_NOERROR);
826 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0);
827 } else {
87283515 828 if (ret > (ssize_t) len) {
7bfe5777
AZ
829 printf("usb-ohci: DataOverrun %d > %zu\n", ret, len);
830 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
831 OHCI_CC_DATAOVERRUN);
832 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
833 len);
834 } else if (ret >= 0) {
835 printf("usb-ohci: DataUnderrun %d\n", ret);
836 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
837 OHCI_CC_DATAUNDERRUN);
838 } else {
839 switch (ret) {
d61000a8 840 case USB_RET_IOERROR:
7bfe5777
AZ
841 case USB_RET_NODEV:
842 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
843 OHCI_CC_DEVICENOTRESPONDING);
844 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
845 0);
846 break;
847 case USB_RET_NAK:
848 case USB_RET_STALL:
849 printf("usb-ohci: got NAK/STALL %d\n", ret);
850 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
851 OHCI_CC_STALL);
852 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
853 0);
854 break;
855 default:
856 printf("usb-ohci: Bad device response %d\n", ret);
857 OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
858 OHCI_CC_UNDEXPETEDPID);
859 break;
860 }
861 }
862 }
863
864 if (relative_frame_number == frame_count) {
865 /* Last data packet of ISO TD - retire the TD to the Done Queue */
866 OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR);
867 ed->head &= ~OHCI_DPTR_MASK;
868 ed->head |= (iso_td.next & OHCI_DPTR_MASK);
869 iso_td.next = ohci->done;
870 ohci->done = addr;
871 i = OHCI_BM(iso_td.flags, TD_DI);
872 if (i < ohci->done_count)
873 ohci->done_count = i;
874 }
ac611340 875 ohci_put_iso_td(ohci, addr, &iso_td);
7bfe5777 876 return 1;
4d611c9a
PB
877}
878
0d92ed30
PB
879/* Service a transport descriptor.
880 Returns nonzero to terminate processing of this endpoint. */
881
882static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
883{
884 int dir;
905fb034 885 size_t len = 0, pktlen = 0;
d4c4e6fd 886#ifdef DEBUG_PACKET
7ccfb2eb 887 const char *str = NULL;
d4c4e6fd 888#endif
0d92ed30
PB
889 int pid;
890 int ret;
891 int i;
892 USBDevice *dev;
079d0b7f 893 USBEndpoint *ep;
0d92ed30
PB
894 struct ohci_td td;
895 uint32_t addr;
896 int flag_r;
4d611c9a 897 int completion;
0d92ed30
PB
898
899 addr = ed->head & OHCI_DPTR_MASK;
4d611c9a
PB
900 /* See if this TD has already been submitted to the device. */
901 completion = (addr == ohci->async_td);
902 if (completion && !ohci->async_complete) {
903#ifdef DEBUG_PACKET
d0f2c4c6 904 DPRINTF("Skipping async TD\n");
4d611c9a
PB
905#endif
906 return 1;
907 }
ac611340 908 if (!ohci_read_td(ohci, addr, &td)) {
0d92ed30
PB
909 fprintf(stderr, "usb-ohci: TD read error at %x\n", addr);
910 return 0;
911 }
912
913 dir = OHCI_BM(ed->flags, ED_D);
914 switch (dir) {
915 case OHCI_TD_DIR_OUT:
916 case OHCI_TD_DIR_IN:
917 /* Same value. */
918 break;
919 default:
920 dir = OHCI_BM(td.flags, TD_DP);
921 break;
922 }
923
924 switch (dir) {
925 case OHCI_TD_DIR_IN:
d4c4e6fd 926#ifdef DEBUG_PACKET
0d92ed30 927 str = "in";
d4c4e6fd 928#endif
0d92ed30
PB
929 pid = USB_TOKEN_IN;
930 break;
931 case OHCI_TD_DIR_OUT:
d4c4e6fd 932#ifdef DEBUG_PACKET
0d92ed30 933 str = "out";
d4c4e6fd 934#endif
0d92ed30
PB
935 pid = USB_TOKEN_OUT;
936 break;
937 case OHCI_TD_DIR_SETUP:
d4c4e6fd 938#ifdef DEBUG_PACKET
0d92ed30 939 str = "setup";
d4c4e6fd 940#endif
0d92ed30
PB
941 pid = USB_TOKEN_SETUP;
942 break;
943 default:
944 fprintf(stderr, "usb-ohci: Bad direction\n");
945 return 1;
946 }
947 if (td.cbp && td.be) {
e6f3e5e0
PB
948 if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) {
949 len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff);
950 } else {
951 len = (td.be - td.cbp) + 1;
952 }
953
905fb034
PM
954 pktlen = len;
955 if (len && dir != OHCI_TD_DIR_IN) {
956 /* The endpoint may not allow us to transfer it all now */
957 pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
958 if (pktlen > len) {
959 pktlen = len;
960 }
961 if (!completion) {
962 ohci_copy_td(ohci, &td, ohci->usb_buf, pktlen, 0);
963 }
0d92ed30
PB
964 }
965 }
966
967 flag_r = (td.flags & OHCI_TD_R) != 0;
968#ifdef DEBUG_PACKET
905fb034
PM
969 DPRINTF(" TD @ 0x%.8x %" PRId64 " of %" PRId64
970 " bytes %s r=%d cbp=0x%.8x be=0x%.8x\n",
971 addr, (int64_t)pktlen, (int64_t)len, str, flag_r, td.cbp, td.be);
0d92ed30 972
905fb034 973 if (pktlen > 0 && dir != OHCI_TD_DIR_IN) {
d0f2c4c6 974 DPRINTF(" data:");
905fb034 975 for (i = 0; i < pktlen; i++) {
4d611c9a 976 printf(" %.2x", ohci->usb_buf[i]);
905fb034 977 }
d0f2c4c6 978 DPRINTF("\n");
0d92ed30
PB
979 }
980#endif
4d611c9a 981 if (completion) {
4f4321c1 982 ret = ohci->usb_packet.result;
4d611c9a
PB
983 ohci->async_td = 0;
984 ohci->async_complete = 0;
985 } else {
993048bb
GH
986 if (ohci->async_td) {
987 /* ??? The hardware should allow one active packet per
988 endpoint. We only allow one active packet per controller.
989 This should be sufficient as long as devices respond in a
990 timely manner.
991 */
0d92ed30 992#ifdef DEBUG_PACKET
993048bb 993 DPRINTF("Too many pending packets\n");
0d92ed30 994#endif
993048bb 995 return 1;
4d611c9a 996 }
079d0b7f
GH
997 dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
998 ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
999 usb_packet_setup(&ohci->usb_packet, pid, ep);
993048bb 1000 usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, pktlen);
993048bb 1001 ret = usb_handle_packet(dev, &ohci->usb_packet);
4d611c9a 1002#ifdef DEBUG_PACKET
d0f2c4c6 1003 DPRINTF("ret=%d\n", ret);
4d611c9a
PB
1004#endif
1005 if (ret == USB_RET_ASYNC) {
1006 ohci->async_td = addr;
1007 return 1;
1008 }
1009 }
0d92ed30
PB
1010 if (ret >= 0) {
1011 if (dir == OHCI_TD_DIR_IN) {
ac611340 1012 ohci_copy_td(ohci, &td, ohci->usb_buf, ret, 1);
0d92ed30 1013#ifdef DEBUG_PACKET
d0f2c4c6 1014 DPRINTF(" data:");
0d92ed30 1015 for (i = 0; i < ret; i++)
4d611c9a 1016 printf(" %.2x", ohci->usb_buf[i]);
d0f2c4c6 1017 DPRINTF("\n");
0d92ed30
PB
1018#endif
1019 } else {
905fb034 1020 ret = pktlen;
0d92ed30
PB
1021 }
1022 }
1023
1024 /* Writeback */
905fb034 1025 if (ret == pktlen || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) {
0d92ed30
PB
1026 /* Transmission succeeded. */
1027 if (ret == len) {
1028 td.cbp = 0;
1029 } else {
0d92ed30 1030 if ((td.cbp & 0xfff) + ret > 0xfff) {
fd891c93
AG
1031 td.cbp = (td.be & ~0xfff) + ((td.cbp + ret) & 0xfff);
1032 } else {
1033 td.cbp += ret;
0d92ed30
PB
1034 }
1035 }
1036 td.flags |= OHCI_TD_T1;
1037 td.flags ^= OHCI_TD_T0;
1038 OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR);
1039 OHCI_SET_BM(td.flags, TD_EC, 0);
1040
905fb034
PM
1041 if ((dir != OHCI_TD_DIR_IN) && (ret != len)) {
1042 /* Partial packet transfer: TD not ready to retire yet */
1043 goto exit_no_retire;
1044 }
1045
1046 /* Setting ED_C is part of the TD retirement process */
0d92ed30
PB
1047 ed->head &= ~OHCI_ED_C;
1048 if (td.flags & OHCI_TD_T0)
1049 ed->head |= OHCI_ED_C;
1050 } else {
1051 if (ret >= 0) {
d0f2c4c6 1052 DPRINTF("usb-ohci: Underrun\n");
0d92ed30
PB
1053 OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
1054 } else {
1055 switch (ret) {
d61000a8 1056 case USB_RET_IOERROR:
0d92ed30
PB
1057 case USB_RET_NODEV:
1058 OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
1059 case USB_RET_NAK:
d0f2c4c6 1060 DPRINTF("usb-ohci: got NAK\n");
0d92ed30
PB
1061 return 1;
1062 case USB_RET_STALL:
d0f2c4c6 1063 DPRINTF("usb-ohci: got STALL\n");
0d92ed30
PB
1064 OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL);
1065 break;
1066 case USB_RET_BABBLE:
d0f2c4c6 1067 DPRINTF("usb-ohci: got BABBLE\n");
0d92ed30
PB
1068 OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
1069 break;
1070 default:
1071 fprintf(stderr, "usb-ohci: Bad device response %d\n", ret);
1072 OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID);
1073 OHCI_SET_BM(td.flags, TD_EC, 3);
1074 break;
1075 }
1076 }
1077 ed->head |= OHCI_ED_H;
1078 }
1079
1080 /* Retire this TD */
1081 ed->head &= ~OHCI_DPTR_MASK;
1082 ed->head |= td.next & OHCI_DPTR_MASK;
1083 td.next = ohci->done;
1084 ohci->done = addr;
1085 i = OHCI_BM(td.flags, TD_DI);
1086 if (i < ohci->done_count)
1087 ohci->done_count = i;
905fb034 1088exit_no_retire:
ac611340 1089 ohci_put_td(ohci, addr, &td);
0d92ed30
PB
1090 return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR;
1091}
1092
1093/* Service an endpoint list. Returns nonzero if active TD were found. */
7bfe5777 1094static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion)
0d92ed30
PB
1095{
1096 struct ohci_ed ed;
1097 uint32_t next_ed;
1098 uint32_t cur;
1099 int active;
1100
1101 active = 0;
1102
1103 if (head == 0)
1104 return 0;
1105
1106 for (cur = head; cur; cur = next_ed) {
ac611340 1107 if (!ohci_read_ed(ohci, cur, &ed)) {
0d92ed30
PB
1108 fprintf(stderr, "usb-ohci: ED read error at %x\n", cur);
1109 return 0;
1110 }
1111
1112 next_ed = ed.next & OHCI_DPTR_MASK;
1113
4d611c9a
PB
1114 if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) {
1115 uint32_t addr;
1116 /* Cancel pending packets for ED that have been paused. */
1117 addr = ed.head & OHCI_DPTR_MASK;
1118 if (ohci->async_td && addr == ohci->async_td) {
1119 usb_cancel_packet(&ohci->usb_packet);
1120 ohci->async_td = 0;
1121 }
0d92ed30 1122 continue;
4d611c9a 1123 }
0d92ed30 1124
0d92ed30
PB
1125 while ((ed.head & OHCI_DPTR_MASK) != ed.tail) {
1126#ifdef DEBUG_PACKET
d0f2c4c6 1127 DPRINTF("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u "
0d92ed30
PB
1128 "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur,
1129 OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN),
1130 OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0,
1131 (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0,
1132 OHCI_BM(ed.flags, ED_MPS), (ed.head & OHCI_ED_H) != 0,
1133 (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK,
1134 ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK);
1135#endif
1136 active = 1;
1137
7bfe5777
AZ
1138 if ((ed.flags & OHCI_ED_F) == 0) {
1139 if (ohci_service_td(ohci, &ed))
1140 break;
1141 } else {
1142 /* Handle isochronous endpoints */
1143 if (ohci_service_iso_td(ohci, &ed, completion))
1144 break;
1145 }
0d92ed30
PB
1146 }
1147
ac611340 1148 ohci_put_ed(ohci, cur, &ed);
0d92ed30
PB
1149 }
1150
1151 return active;
1152}
1153
1154/* Generate a SOF event, and set a timer for EOF */
1155static void ohci_sof(OHCIState *ohci)
1156{
74475455 1157 ohci->sof_time = qemu_get_clock_ns(vm_clock);
0d92ed30
PB
1158 qemu_mod_timer(ohci->eof_timer, ohci->sof_time + usb_frame_time);
1159 ohci_set_interrupt(ohci, OHCI_INTR_SF);
1160}
1161
4d611c9a 1162/* Process Control and Bulk lists. */
7bfe5777 1163static void ohci_process_lists(OHCIState *ohci, int completion)
4d611c9a
PB
1164{
1165 if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
6ad6135d
BS
1166 if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) {
1167 DPRINTF("usb-ohci: head %x, cur %x\n",
1168 ohci->ctrl_head, ohci->ctrl_cur);
1169 }
7bfe5777 1170 if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) {
4d611c9a
PB
1171 ohci->ctrl_cur = 0;
1172 ohci->status &= ~OHCI_STATUS_CLF;
1173 }
1174 }
1175
1176 if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
7bfe5777 1177 if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) {
4d611c9a
PB
1178 ohci->bulk_cur = 0;
1179 ohci->status &= ~OHCI_STATUS_BLF;
1180 }
1181 }
1182}
1183
0d92ed30
PB
1184/* Do frame processing on frame boundary */
1185static void ohci_frame_boundary(void *opaque)
1186{
1187 OHCIState *ohci = opaque;
1188 struct ohci_hcca hcca;
1189
ac611340 1190 ohci_read_hcca(ohci, ohci->hcca, &hcca);
0d92ed30
PB
1191
1192 /* Process all the lists at the end of the frame */
1193 if (ohci->ctl & OHCI_CTL_PLE) {
1194 int n;
1195
1196 n = ohci->frame_number & 0x1f;
7bfe5777 1197 ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]), 0);
0d92ed30 1198 }
0d92ed30 1199
4d611c9a
PB
1200 /* Cancel all pending packets if either of the lists has been disabled. */
1201 if (ohci->async_td &&
1202 ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
1203 usb_cancel_packet(&ohci->usb_packet);
1204 ohci->async_td = 0;
0d92ed30 1205 }
4d611c9a 1206 ohci->old_ctl = ohci->ctl;
7bfe5777 1207 ohci_process_lists(ohci, 0);
0d92ed30
PB
1208
1209 /* Frame boundary, so do EOF stuf here */
1210 ohci->frt = ohci->fit;
1211
1fa63e43 1212 /* Increment frame number and take care of endianness. */
0d92ed30 1213 ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
1fa63e43 1214 hcca.frame = cpu_to_le16(ohci->frame_number);
0d92ed30
PB
1215
1216 if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
1217 if (!ohci->done)
1218 abort();
1219 if (ohci->intr & ohci->intr_status)
1220 ohci->done |= 1;
1221 hcca.done = cpu_to_le32(ohci->done);
1222 ohci->done = 0;
1223 ohci->done_count = 7;
1224 ohci_set_interrupt(ohci, OHCI_INTR_WD);
1225 }
1226
1227 if (ohci->done_count != 7 && ohci->done_count != 0)
1228 ohci->done_count--;
1229
1230 /* Do SOF stuff here */
1231 ohci_sof(ohci);
1232
1233 /* Writeback HCCA */
ac611340 1234 ohci_put_hcca(ohci, ohci->hcca, &hcca);
0d92ed30
PB
1235}
1236
1237/* Start sending SOF tokens across the USB bus, lists are processed in
1238 * next frame
1239 */
1240static int ohci_bus_start(OHCIState *ohci)
1241{
74475455 1242 ohci->eof_timer = qemu_new_timer_ns(vm_clock,
0d92ed30
PB
1243 ohci_frame_boundary,
1244 ohci);
1245
1246 if (ohci->eof_timer == NULL) {
74475455 1247 fprintf(stderr, "usb-ohci: %s: qemu_new_timer_ns failed\n", ohci->name);
0d92ed30
PB
1248 /* TODO: Signal unrecoverable error */
1249 return 0;
1250 }
1251
d0f2c4c6 1252 DPRINTF("usb-ohci: %s: USB Operational\n", ohci->name);
0d92ed30
PB
1253
1254 ohci_sof(ohci);
1255
1256 return 1;
1257}
1258
1259/* Stop sending SOF tokens on the bus */
1260static void ohci_bus_stop(OHCIState *ohci)
1261{
1262 if (ohci->eof_timer)
1263 qemu_del_timer(ohci->eof_timer);
73221b12 1264 ohci->eof_timer = NULL;
0d92ed30
PB
1265}
1266
1267/* Sets a flag in a port status register but only set it if the port is
1268 * connected, if not set ConnectStatusChange flag. If flag is enabled
1269 * return 1.
1270 */
1271static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
1272{
1273 int ret = 1;
1274
1275 /* writing a 0 has no effect */
1276 if (val == 0)
1277 return 0;
1278
1279 /* If CurrentConnectStatus is cleared we set
1280 * ConnectStatusChange
1281 */
1282 if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
1283 ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
1284 if (ohci->rhstatus & OHCI_RHS_DRWE) {
1285 /* TODO: CSC is a wakeup event */
1286 }
1287 return 0;
1288 }
1289
1290 if (ohci->rhport[i].ctrl & val)
1291 ret = 0;
1292
1293 /* set the bit */
1294 ohci->rhport[i].ctrl |= val;
1295
1296 return ret;
1297}
1298
1299/* Set the frame interval - frame interval toggle is manipulated by the hcd only */
1300static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
1301{
1302 val &= OHCI_FMI_FI;
1303
1304 if (val != ohci->fi) {
d0f2c4c6 1305 DPRINTF("usb-ohci: %s: FrameInterval = 0x%x (%u)\n",
e24ad6f1 1306 ohci->name, ohci->fi, ohci->fi);
0d92ed30
PB
1307 }
1308
1309 ohci->fi = val;
1310}
1311
1312static void ohci_port_power(OHCIState *ohci, int i, int p)
1313{
1314 if (p) {
1315 ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
1316 } else {
1317 ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS|
1318 OHCI_PORT_CCS|
1319 OHCI_PORT_PSS|
1320 OHCI_PORT_PRS);
1321 }
1322}
1323
1324/* Set HcControlRegister */
1325static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
1326{
1327 uint32_t old_state;
1328 uint32_t new_state;
1329
1330 old_state = ohci->ctl & OHCI_CTL_HCFS;
1331 ohci->ctl = val;
1332 new_state = ohci->ctl & OHCI_CTL_HCFS;
1333
1334 /* no state change */
1335 if (old_state == new_state)
1336 return;
1337
1338 switch (new_state) {
1339 case OHCI_USB_OPERATIONAL:
1340 ohci_bus_start(ohci);
1341 break;
1342 case OHCI_USB_SUSPEND:
1343 ohci_bus_stop(ohci);
d0f2c4c6 1344 DPRINTF("usb-ohci: %s: USB Suspended\n", ohci->name);
0d92ed30
PB
1345 break;
1346 case OHCI_USB_RESUME:
d0f2c4c6 1347 DPRINTF("usb-ohci: %s: USB Resume\n", ohci->name);
0d92ed30
PB
1348 break;
1349 case OHCI_USB_RESET:
73221b12 1350 ohci_reset(ohci);
d0f2c4c6 1351 DPRINTF("usb-ohci: %s: USB Reset\n", ohci->name);
0d92ed30
PB
1352 break;
1353 }
1354}
1355
1356static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
1357{
1358 uint16_t fr;
1359 int64_t tks;
1360
1361 if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL)
1362 return (ohci->frt << 31);
1363
1364 /* Being in USB operational state guarnatees sof_time was
1365 * set already.
1366 */
74475455 1367 tks = qemu_get_clock_ns(vm_clock) - ohci->sof_time;
0d92ed30
PB
1368
1369 /* avoid muldiv if possible */
1370 if (tks >= usb_frame_time)
1371 return (ohci->frt << 31);
1372
1373 tks = muldiv64(1, tks, usb_bit_time);
1374 fr = (uint16_t)(ohci->fi - tks);
1375
1376 return (ohci->frt << 31) | fr;
1377}
1378
1379
1380/* Set root hub status */
1381static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
1382{
1383 uint32_t old_state;
1384
1385 old_state = ohci->rhstatus;
1386
1387 /* write 1 to clear OCIC */
1388 if (val & OHCI_RHS_OCIC)
1389 ohci->rhstatus &= ~OHCI_RHS_OCIC;
1390
1391 if (val & OHCI_RHS_LPS) {
1392 int i;
1393
1394 for (i = 0; i < ohci->num_ports; i++)
1395 ohci_port_power(ohci, i, 0);
d0f2c4c6 1396 DPRINTF("usb-ohci: powered down all ports\n");
0d92ed30
PB
1397 }
1398
1399 if (val & OHCI_RHS_LPSC) {
1400 int i;
1401
1402 for (i = 0; i < ohci->num_ports; i++)
1403 ohci_port_power(ohci, i, 1);
d0f2c4c6 1404 DPRINTF("usb-ohci: powered up all ports\n");
0d92ed30
PB
1405 }
1406
1407 if (val & OHCI_RHS_DRWE)
1408 ohci->rhstatus |= OHCI_RHS_DRWE;
1409
1410 if (val & OHCI_RHS_CRWE)
1411 ohci->rhstatus &= ~OHCI_RHS_DRWE;
1412
1413 if (old_state != ohci->rhstatus)
1414 ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
1415}
1416
1417/* Set root hub port status */
1418static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
1419{
1420 uint32_t old_state;
1421 OHCIPort *port;
1422
1423 port = &ohci->rhport[portnum];
1424 old_state = port->ctrl;
1425
1426 /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
1427 if (val & OHCI_PORT_WTC)
1428 port->ctrl &= ~(val & OHCI_PORT_WTC);
1429
1430 if (val & OHCI_PORT_CCS)
1431 port->ctrl &= ~OHCI_PORT_PES;
1432
1433 ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
1434
6ad6135d 1435 if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) {
d0f2c4c6 1436 DPRINTF("usb-ohci: port %d: SUSPEND\n", portnum);
6ad6135d 1437 }
0d92ed30
PB
1438
1439 if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
d0f2c4c6 1440 DPRINTF("usb-ohci: port %d: RESET\n", portnum);
d28f4e2d 1441 usb_device_reset(port->port.dev);
0d92ed30
PB
1442 port->ctrl &= ~OHCI_PORT_PRS;
1443 /* ??? Should this also set OHCI_PORT_PESC. */
1444 port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC;
1445 }
1446
1447 /* Invert order here to ensure in ambiguous case, device is
1448 * powered up...
1449 */
1450 if (val & OHCI_PORT_LSDA)
1451 ohci_port_power(ohci, portnum, 0);
1452 if (val & OHCI_PORT_PPS)
1453 ohci_port_power(ohci, portnum, 1);
1454
1455 if (old_state != port->ctrl)
1456 ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
1457
1458 return;
1459}
1460
6da48311
AK
1461static uint64_t ohci_mem_read(void *opaque,
1462 target_phys_addr_t addr,
1463 unsigned size)
0d92ed30 1464{
6da48311 1465 OHCIState *ohci = opaque;
65e1d81b 1466 uint32_t retval;
0d92ed30 1467
0d92ed30
PB
1468 /* Only aligned reads are allowed on OHCI */
1469 if (addr & 3) {
1470 fprintf(stderr, "usb-ohci: Mis-aligned read\n");
1471 return 0xffffffff;
65e1d81b 1472 } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
0d92ed30 1473 /* HcRhPortStatus */
65e1d81b
AJ
1474 retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
1475 } else {
1476 switch (addr >> 2) {
1477 case 0: /* HcRevision */
1478 retval = 0x10;
1479 break;
1480
1481 case 1: /* HcControl */
1482 retval = ohci->ctl;
1483 break;
1484
1485 case 2: /* HcCommandStatus */
1486 retval = ohci->status;
1487 break;
1488
1489 case 3: /* HcInterruptStatus */
1490 retval = ohci->intr_status;
1491 break;
1492
1493 case 4: /* HcInterruptEnable */
1494 case 5: /* HcInterruptDisable */
1495 retval = ohci->intr;
1496 break;
1497
1498 case 6: /* HcHCCA */
1499 retval = ohci->hcca;
1500 break;
1501
1502 case 7: /* HcPeriodCurrentED */
1503 retval = ohci->per_cur;
1504 break;
1505
1506 case 8: /* HcControlHeadED */
1507 retval = ohci->ctrl_head;
1508 break;
1509
1510 case 9: /* HcControlCurrentED */
1511 retval = ohci->ctrl_cur;
1512 break;
1513
1514 case 10: /* HcBulkHeadED */
1515 retval = ohci->bulk_head;
1516 break;
1517
1518 case 11: /* HcBulkCurrentED */
1519 retval = ohci->bulk_cur;
1520 break;
1521
1522 case 12: /* HcDoneHead */
1523 retval = ohci->done;
1524 break;
1525
1526 case 13: /* HcFmInterretval */
1527 retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
1528 break;
1529
1530 case 14: /* HcFmRemaining */
1531 retval = ohci_get_frame_remaining(ohci);
1532 break;
1533
1534 case 15: /* HcFmNumber */
1535 retval = ohci->frame_number;
1536 break;
1537
1538 case 16: /* HcPeriodicStart */
1539 retval = ohci->pstart;
1540 break;
1541
1542 case 17: /* HcLSThreshold */
1543 retval = ohci->lst;
1544 break;
1545
1546 case 18: /* HcRhDescriptorA */
1547 retval = ohci->rhdesc_a;
1548 break;
1549
1550 case 19: /* HcRhDescriptorB */
1551 retval = ohci->rhdesc_b;
1552 break;
1553
1554 case 20: /* HcRhStatus */
1555 retval = ohci->rhstatus;
1556 break;
1557
1558 /* PXA27x specific registers */
1559 case 24: /* HcStatus */
1560 retval = ohci->hstatus & ohci->hmask;
1561 break;
1562
1563 case 25: /* HcHReset */
1564 retval = ohci->hreset;
1565 break;
1566
1567 case 26: /* HcHInterruptEnable */
1568 retval = ohci->hmask;
1569 break;
1570
1571 case 27: /* HcHInterruptTest */
1572 retval = ohci->htest;
1573 break;
1574
1575 default:
1576 fprintf(stderr, "ohci_read: Bad offset %x\n", (int)addr);
1577 retval = 0xffffffff;
1578 }
0d92ed30
PB
1579 }
1580
65e1d81b 1581 return retval;
0d92ed30
PB
1582}
1583
6da48311
AK
1584static void ohci_mem_write(void *opaque,
1585 target_phys_addr_t addr,
1586 uint64_t val,
1587 unsigned size)
0d92ed30 1588{
6da48311 1589 OHCIState *ohci = opaque;
09564574 1590
0d92ed30
PB
1591 /* Only aligned reads are allowed on OHCI */
1592 if (addr & 3) {
1593 fprintf(stderr, "usb-ohci: Mis-aligned write\n");
1594 return;
1595 }
1596
1597 if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
1598 /* HcRhPortStatus */
1599 ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
1600 return;
1601 }
1602
1603 switch (addr >> 2) {
1604 case 1: /* HcControl */
1605 ohci_set_ctl(ohci, val);
1606 break;
1607
1608 case 2: /* HcCommandStatus */
1609 /* SOC is read-only */
1610 val = (val & ~OHCI_STATUS_SOC);
1611
1612 /* Bits written as '0' remain unchanged in the register */
1613 ohci->status |= val;
1614
1615 if (ohci->status & OHCI_STATUS_HCR)
1616 ohci_reset(ohci);
1617 break;
1618
1619 case 3: /* HcInterruptStatus */
1620 ohci->intr_status &= ~val;
1621 ohci_intr_update(ohci);
1622 break;
1623
1624 case 4: /* HcInterruptEnable */
1625 ohci->intr |= val;
1626 ohci_intr_update(ohci);
1627 break;
1628
1629 case 5: /* HcInterruptDisable */
1630 ohci->intr &= ~val;
1631 ohci_intr_update(ohci);
1632 break;
1633
1634 case 6: /* HcHCCA */
1635 ohci->hcca = val & OHCI_HCCA_MASK;
1636 break;
1637
4b0315d7
PM
1638 case 7: /* HcPeriodCurrentED */
1639 /* Ignore writes to this read-only register, Linux does them */
1640 break;
1641
0d92ed30
PB
1642 case 8: /* HcControlHeadED */
1643 ohci->ctrl_head = val & OHCI_EDPTR_MASK;
1644 break;
1645
1646 case 9: /* HcControlCurrentED */
1647 ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
1648 break;
1649
1650 case 10: /* HcBulkHeadED */
1651 ohci->bulk_head = val & OHCI_EDPTR_MASK;
1652 break;
1653
1654 case 11: /* HcBulkCurrentED */
1655 ohci->bulk_cur = val & OHCI_EDPTR_MASK;
1656 break;
1657
1658 case 13: /* HcFmInterval */
1659 ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
1660 ohci->fit = (val & OHCI_FMI_FIT) >> 31;
1661 ohci_set_frame_interval(ohci, val);
1662 break;
1663
7bfe5777
AZ
1664 case 15: /* HcFmNumber */
1665 break;
1666
0d92ed30
PB
1667 case 16: /* HcPeriodicStart */
1668 ohci->pstart = val & 0xffff;
1669 break;
1670
1671 case 17: /* HcLSThreshold */
1672 ohci->lst = val & 0xffff;
1673 break;
1674
1675 case 18: /* HcRhDescriptorA */
1676 ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
1677 ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
1678 break;
1679
1680 case 19: /* HcRhDescriptorB */
1681 break;
1682
1683 case 20: /* HcRhStatus */
1684 ohci_set_hub_status(ohci, val);
1685 break;
1686
e24ad6f1
PB
1687 /* PXA27x specific registers */
1688 case 24: /* HcStatus */
1689 ohci->hstatus &= ~(val & ohci->hmask);
1690
1691 case 25: /* HcHReset */
1692 ohci->hreset = val & ~OHCI_HRESET_FSBIR;
1693 if (val & OHCI_HRESET_FSBIR)
1694 ohci_reset(ohci);
1695 break;
1696
1697 case 26: /* HcHInterruptEnable */
1698 ohci->hmask = val;
1699 break;
1700
1701 case 27: /* HcHInterruptTest */
1702 ohci->htest = val;
1703 break;
1704
0d92ed30
PB
1705 default:
1706 fprintf(stderr, "ohci_write: Bad offset %x\n", (int)addr);
1707 break;
1708 }
1709}
1710
4706ab6c 1711static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev)
07771f6f 1712{
25d5de7d 1713 if (ohci->async_td &&
f53c398a
GH
1714 usb_packet_is_inflight(&ohci->usb_packet) &&
1715 ohci->usb_packet.ep->dev == dev) {
07771f6f
GH
1716 usb_cancel_packet(&ohci->usb_packet);
1717 ohci->async_td = 0;
1718 }
1719}
1720
6da48311
AK
1721static const MemoryRegionOps ohci_mem_ops = {
1722 .read = ohci_mem_read,
1723 .write = ohci_mem_write,
1724 .endianness = DEVICE_LITTLE_ENDIAN,
0d92ed30
PB
1725};
1726
0d86d2be
GH
1727static USBPortOps ohci_port_ops = {
1728 .attach = ohci_attach,
618c169b 1729 .detach = ohci_detach,
4706ab6c 1730 .child_detach = ohci_child_detach,
9bba1eb1 1731 .wakeup = ohci_wakeup,
13a9a0d3 1732 .complete = ohci_async_complete_packet,
0d86d2be
GH
1733};
1734
07771f6f 1735static USBBusOps ohci_bus_ops = {
07771f6f
GH
1736};
1737
9c9fc334
HG
1738static int usb_ohci_init(OHCIState *ohci, DeviceState *dev,
1739 int num_ports, uint32_t localmem_base,
1740 char *masterbus, uint32_t firstport)
0d92ed30 1741{
0d92ed30
PB
1742 int i;
1743
0d92ed30 1744 if (usb_frame_time == 0) {
eb38c52c 1745#ifdef OHCI_TIME_WARP
6ee093c9
JQ
1746 usb_frame_time = get_ticks_per_sec();
1747 usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ/1000);
0d92ed30 1748#else
6ee093c9
JQ
1749 usb_frame_time = muldiv64(1, get_ticks_per_sec(), 1000);
1750 if (get_ticks_per_sec() >= USB_HZ) {
1751 usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ);
0d92ed30
PB
1752 } else {
1753 usb_bit_time = 1;
1754 }
1755#endif
d0f2c4c6 1756 DPRINTF("usb-ohci: usb_bit_time=%" PRId64 " usb_frame_time=%" PRId64 "\n",
0d92ed30
PB
1757 usb_frame_time, usb_bit_time);
1758 }
1759
9c9fc334
HG
1760 ohci->num_ports = num_ports;
1761 if (masterbus) {
1762 USBPort *ports[OHCI_MAX_PORTS];
1763 for(i = 0; i < num_ports; i++) {
1764 ports[i] = &ohci->rhport[i].port;
1765 }
1766 if (usb_register_companion(masterbus, ports, num_ports,
1767 firstport, ohci, &ohci_port_ops,
1768 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1769 return -1;
1770 }
1771 } else {
1772 usb_bus_new(&ohci->bus, &ohci_bus_ops, dev);
1773 for (i = 0; i < num_ports; i++) {
1774 usb_register_port(&ohci->bus, &ohci->rhport[i].port,
1775 ohci, i, &ohci_port_ops,
1776 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1777 }
1778 }
1779
6da48311 1780 memory_region_init_io(&ohci->mem, &ohci_mem_ops, ohci, "ohci", 256);
ac611340 1781 ohci->localmem_base = localmem_base;
e24ad6f1 1782
f79f2bfc 1783 ohci->name = object_get_typename(OBJECT(dev));
4f4321c1 1784 usb_packet_init(&ohci->usb_packet);
e24ad6f1 1785
e24ad6f1 1786 ohci->async_td = 0;
a08d4367 1787 qemu_register_reset(ohci_reset, ohci);
9c9fc334
HG
1788
1789 return 0;
e24ad6f1
PB
1790}
1791
1792typedef struct {
1793 PCIDevice pci_dev;
1794 OHCIState state;
9c9fc334
HG
1795 char *masterbus;
1796 uint32_t num_ports;
1797 uint32_t firstport;
e24ad6f1
PB
1798} OHCIPCIState;
1799
5b19d9a2 1800static int usb_ohci_initfn_pci(struct PCIDevice *dev)
e24ad6f1 1801{
5b19d9a2 1802 OHCIPCIState *ohci = DO_UPCAST(OHCIPCIState, pci_dev, dev);
0d92ed30 1803
d74dbb94 1804 ohci->pci_dev.config[PCI_CLASS_PROG] = 0x10; /* OHCI */
817e0b6f 1805 ohci->pci_dev.config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
0d92ed30 1806
9c9fc334
HG
1807 if (usb_ohci_init(&ohci->state, &dev->qdev, ohci->num_ports, 0,
1808 ohci->masterbus, ohci->firstport) != 0) {
1809 return -1;
1810 }
61d3cf93 1811 ohci->state.irq = ohci->pci_dev.irq[0];
0d92ed30 1812
d74dbb94 1813 /* TODO: avoid cast below by using dev */
e824b2cc 1814 pci_register_bar(&ohci->pci_dev, 0, 0, &ohci->state.mem);
5b19d9a2
GH
1815 return 0;
1816}
1817
a67ba3b6 1818void usb_ohci_init_pci(struct PCIBus *bus, int devfn)
5b19d9a2 1819{
a67ba3b6 1820 pci_create_simple(bus, devfn, "pci-ohci");
e24ad6f1 1821}
0d92ed30 1822
61d3cf93
PB
1823typedef struct {
1824 SysBusDevice busdev;
1825 OHCIState ohci;
1826 uint32_t num_ports;
1827 target_phys_addr_t dma_offset;
1828} OHCISysBusState;
ac611340 1829
61d3cf93 1830static int ohci_init_pxa(SysBusDevice *dev)
ac611340 1831{
61d3cf93 1832 OHCISysBusState *s = FROM_SYSBUS(OHCISysBusState, dev);
ac611340 1833
9c9fc334
HG
1834 /* Cannot fail as we pass NULL for masterbus */
1835 usb_ohci_init(&s->ohci, &dev->qdev, s->num_ports, s->dma_offset, NULL, 0);
61d3cf93 1836 sysbus_init_irq(dev, &s->ohci.irq);
750ecd44 1837 sysbus_init_mmio(dev, &s->ohci.mem);
ac611340 1838
61d3cf93 1839 return 0;
ac611340
AJ
1840}
1841
40021f08
AL
1842static Property ohci_pci_properties[] = {
1843 DEFINE_PROP_STRING("masterbus", OHCIPCIState, masterbus),
1844 DEFINE_PROP_UINT32("num-ports", OHCIPCIState, num_ports, 3),
1845 DEFINE_PROP_UINT32("firstport", OHCIPCIState, firstport, 0),
1846 DEFINE_PROP_END_OF_LIST(),
1847};
1848
1849static void ohci_pci_class_init(ObjectClass *klass, void *data)
1850{
39bffca2 1851 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1852 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1853
1854 k->init = usb_ohci_initfn_pci;
1855 k->vendor_id = PCI_VENDOR_ID_APPLE;
1856 k->device_id = PCI_DEVICE_ID_APPLE_IPID_USB;
1857 k->class_id = PCI_CLASS_SERIAL_USB;
39bffca2
AL
1858 dc->desc = "Apple USB Controller";
1859 dc->props = ohci_pci_properties;
40021f08
AL
1860}
1861
39bffca2
AL
1862static TypeInfo ohci_pci_info = {
1863 .name = "pci-ohci",
1864 .parent = TYPE_PCI_DEVICE,
1865 .instance_size = sizeof(OHCIPCIState),
1866 .class_init = ohci_pci_class_init,
1867};
1868
1869static Property ohci_sysbus_properties[] = {
1870 DEFINE_PROP_UINT32("num-ports", OHCISysBusState, num_ports, 3),
1871 DEFINE_PROP_TADDR("dma-offset", OHCISysBusState, dma_offset, 3),
1872 DEFINE_PROP_END_OF_LIST(),
5b19d9a2
GH
1873};
1874
999e12bb
AL
1875static void ohci_sysbus_class_init(ObjectClass *klass, void *data)
1876{
39bffca2 1877 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
1878 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
1879
1880 sbc->init = ohci_init_pxa;
39bffca2
AL
1881 dc->desc = "OHCI USB Controller";
1882 dc->props = ohci_sysbus_properties;
999e12bb
AL
1883}
1884
39bffca2
AL
1885static TypeInfo ohci_sysbus_info = {
1886 .name = "sysbus-ohci",
1887 .parent = TYPE_SYS_BUS_DEVICE,
1888 .instance_size = sizeof(OHCISysBusState),
1889 .class_init = ohci_sysbus_class_init,
61d3cf93
PB
1890};
1891
83f7d43a 1892static void ohci_register_types(void)
5b19d9a2 1893{
39bffca2
AL
1894 type_register_static(&ohci_pci_info);
1895 type_register_static(&ohci_sysbus_info);
5b19d9a2 1896}
83f7d43a
AF
1897
1898type_init(ohci_register_types)
This page took 0.991127 seconds and 4 git commands to generate.