2 * QEMU USB OHCI Emulation
3 * Copyright (c) 2004 Gianni Tedesco
4 * Copyright (c) 2006 CodeSourcery
5 * Copyright (c) 2006 Openedhand Ltd.
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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
25 * o Handle unrecoverable errors properly
26 * o BIOS work to boot from USB storage
30 #include "qemu-timer.h"
35 #include "qdev-addr.h"
38 /* Dump packet contents. */
39 //#define DEBUG_PACKET
41 /* This causes frames to occur 1000x slower */
42 //#define OHCI_TIME_WARP 1
45 #define DPRINTF printf
50 /* Number of Downstream Ports on the root hub. */
52 #define OHCI_MAX_PORTS 15
54 static int64_t usb_frame_time
;
55 static int64_t usb_bit_time
;
57 typedef struct OHCIPort
{
73 /* Control partition */
78 /* memory pointer partition */
80 uint32_t ctrl_head
, ctrl_cur
;
81 uint32_t bulk_head
, bulk_cur
;
86 /* Frame counter partition */
91 uint16_t frame_number
;
96 /* Root Hub partition */
97 uint32_t rhdesc_a
, rhdesc_b
;
99 OHCIPort rhport
[OHCI_MAX_PORTS
];
101 /* PXA27x Non-OHCI events */
107 /* SM501 local memory offset */
108 target_phys_addr_t localmem_base
;
110 /* Active packets. */
112 USBPacket usb_packet
;
113 uint8_t usb_buf
[8192];
119 /* Host Controller Communications Area */
126 static void ohci_bus_stop(OHCIState
*ohci
);
128 /* Bitfields for the first word of an Endpoint Desciptor. */
129 #define OHCI_ED_FA_SHIFT 0
130 #define OHCI_ED_FA_MASK (0x7f<<OHCI_ED_FA_SHIFT)
131 #define OHCI_ED_EN_SHIFT 7
132 #define OHCI_ED_EN_MASK (0xf<<OHCI_ED_EN_SHIFT)
133 #define OHCI_ED_D_SHIFT 11
134 #define OHCI_ED_D_MASK (3<<OHCI_ED_D_SHIFT)
135 #define OHCI_ED_S (1<<13)
136 #define OHCI_ED_K (1<<14)
137 #define OHCI_ED_F (1<<15)
138 #define OHCI_ED_MPS_SHIFT 16
139 #define OHCI_ED_MPS_MASK (0x7ff<<OHCI_ED_MPS_SHIFT)
141 /* Flags in the head field of an Endpoint Desciptor. */
145 /* Bitfields for the first word of a Transfer Desciptor. */
146 #define OHCI_TD_R (1<<18)
147 #define OHCI_TD_DP_SHIFT 19
148 #define OHCI_TD_DP_MASK (3<<OHCI_TD_DP_SHIFT)
149 #define OHCI_TD_DI_SHIFT 21
150 #define OHCI_TD_DI_MASK (7<<OHCI_TD_DI_SHIFT)
151 #define OHCI_TD_T0 (1<<24)
152 #define OHCI_TD_T1 (1<<24)
153 #define OHCI_TD_EC_SHIFT 26
154 #define OHCI_TD_EC_MASK (3<<OHCI_TD_EC_SHIFT)
155 #define OHCI_TD_CC_SHIFT 28
156 #define OHCI_TD_CC_MASK (0xf<<OHCI_TD_CC_SHIFT)
158 /* Bitfields for the first word of an Isochronous Transfer Desciptor. */
159 /* CC & DI - same as in the General Transfer Desciptor */
160 #define OHCI_TD_SF_SHIFT 0
161 #define OHCI_TD_SF_MASK (0xffff<<OHCI_TD_SF_SHIFT)
162 #define OHCI_TD_FC_SHIFT 24
163 #define OHCI_TD_FC_MASK (7<<OHCI_TD_FC_SHIFT)
165 /* Isochronous Transfer Desciptor - Offset / PacketStatusWord */
166 #define OHCI_TD_PSW_CC_SHIFT 12
167 #define OHCI_TD_PSW_CC_MASK (0xf<<OHCI_TD_PSW_CC_SHIFT)
168 #define OHCI_TD_PSW_SIZE_SHIFT 0
169 #define OHCI_TD_PSW_SIZE_MASK (0xfff<<OHCI_TD_PSW_SIZE_SHIFT)
171 #define OHCI_PAGE_MASK 0xfffff000
172 #define OHCI_OFFSET_MASK 0xfff
174 #define OHCI_DPTR_MASK 0xfffffff0
176 #define OHCI_BM(val, field) \
177 (((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT)
179 #define OHCI_SET_BM(val, field, newval) do { \
180 val &= ~OHCI_##field##_MASK; \
181 val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \
184 /* endpoint descriptor */
192 /* General transfer descriptor */
200 /* Isochronous transfer descriptor */
209 #define USB_HZ 12000000
211 /* OHCI Local stuff */
212 #define OHCI_CTL_CBSR ((1<<0)|(1<<1))
213 #define OHCI_CTL_PLE (1<<2)
214 #define OHCI_CTL_IE (1<<3)
215 #define OHCI_CTL_CLE (1<<4)
216 #define OHCI_CTL_BLE (1<<5)
217 #define OHCI_CTL_HCFS ((1<<6)|(1<<7))
218 #define OHCI_USB_RESET 0x00
219 #define OHCI_USB_RESUME 0x40
220 #define OHCI_USB_OPERATIONAL 0x80
221 #define OHCI_USB_SUSPEND 0xc0
222 #define OHCI_CTL_IR (1<<8)
223 #define OHCI_CTL_RWC (1<<9)
224 #define OHCI_CTL_RWE (1<<10)
226 #define OHCI_STATUS_HCR (1<<0)
227 #define OHCI_STATUS_CLF (1<<1)
228 #define OHCI_STATUS_BLF (1<<2)
229 #define OHCI_STATUS_OCR (1<<3)
230 #define OHCI_STATUS_SOC ((1<<6)|(1<<7))
232 #define OHCI_INTR_SO (1<<0) /* Scheduling overrun */
233 #define OHCI_INTR_WD (1<<1) /* HcDoneHead writeback */
234 #define OHCI_INTR_SF (1<<2) /* Start of frame */
235 #define OHCI_INTR_RD (1<<3) /* Resume detect */
236 #define OHCI_INTR_UE (1<<4) /* Unrecoverable error */
237 #define OHCI_INTR_FNO (1<<5) /* Frame number overflow */
238 #define OHCI_INTR_RHSC (1<<6) /* Root hub status change */
239 #define OHCI_INTR_OC (1<<30) /* Ownership change */
240 #define OHCI_INTR_MIE (1<<31) /* Master Interrupt Enable */
242 #define OHCI_HCCA_SIZE 0x100
243 #define OHCI_HCCA_MASK 0xffffff00
245 #define OHCI_EDPTR_MASK 0xfffffff0
247 #define OHCI_FMI_FI 0x00003fff
248 #define OHCI_FMI_FSMPS 0xffff0000
249 #define OHCI_FMI_FIT 0x80000000
251 #define OHCI_FR_RT (1<<31)
253 #define OHCI_LS_THRESH 0x628
255 #define OHCI_RHA_RW_MASK 0x00000000 /* Mask of supported features. */
256 #define OHCI_RHA_PSM (1<<8)
257 #define OHCI_RHA_NPS (1<<9)
258 #define OHCI_RHA_DT (1<<10)
259 #define OHCI_RHA_OCPM (1<<11)
260 #define OHCI_RHA_NOCP (1<<12)
261 #define OHCI_RHA_POTPGT_MASK 0xff000000
263 #define OHCI_RHS_LPS (1<<0)
264 #define OHCI_RHS_OCI (1<<1)
265 #define OHCI_RHS_DRWE (1<<15)
266 #define OHCI_RHS_LPSC (1<<16)
267 #define OHCI_RHS_OCIC (1<<17)
268 #define OHCI_RHS_CRWE (1<<31)
270 #define OHCI_PORT_CCS (1<<0)
271 #define OHCI_PORT_PES (1<<1)
272 #define OHCI_PORT_PSS (1<<2)
273 #define OHCI_PORT_POCI (1<<3)
274 #define OHCI_PORT_PRS (1<<4)
275 #define OHCI_PORT_PPS (1<<8)
276 #define OHCI_PORT_LSDA (1<<9)
277 #define OHCI_PORT_CSC (1<<16)
278 #define OHCI_PORT_PESC (1<<17)
279 #define OHCI_PORT_PSSC (1<<18)
280 #define OHCI_PORT_OCIC (1<<19)
281 #define OHCI_PORT_PRSC (1<<20)
282 #define OHCI_PORT_WTC (OHCI_PORT_CSC|OHCI_PORT_PESC|OHCI_PORT_PSSC \
283 |OHCI_PORT_OCIC|OHCI_PORT_PRSC)
285 #define OHCI_TD_DIR_SETUP 0x0
286 #define OHCI_TD_DIR_OUT 0x1
287 #define OHCI_TD_DIR_IN 0x2
288 #define OHCI_TD_DIR_RESERVED 0x3
290 #define OHCI_CC_NOERROR 0x0
291 #define OHCI_CC_CRC 0x1
292 #define OHCI_CC_BITSTUFFING 0x2
293 #define OHCI_CC_DATATOGGLEMISMATCH 0x3
294 #define OHCI_CC_STALL 0x4
295 #define OHCI_CC_DEVICENOTRESPONDING 0x5
296 #define OHCI_CC_PIDCHECKFAILURE 0x6
297 #define OHCI_CC_UNDEXPETEDPID 0x7
298 #define OHCI_CC_DATAOVERRUN 0x8
299 #define OHCI_CC_DATAUNDERRUN 0x9
300 #define OHCI_CC_BUFFEROVERRUN 0xc
301 #define OHCI_CC_BUFFERUNDERRUN 0xd
303 #define OHCI_HRESET_FSBIR (1 << 0)
305 /* Update IRQ levels */
306 static inline void ohci_intr_update(OHCIState
*ohci
)
310 if ((ohci
->intr
& OHCI_INTR_MIE
) &&
311 (ohci
->intr_status
& ohci
->intr
))
314 qemu_set_irq(ohci
->irq
, level
);
317 /* Set an interrupt */
318 static inline void ohci_set_interrupt(OHCIState
*ohci
, uint32_t intr
)
320 ohci
->intr_status
|= intr
;
321 ohci_intr_update(ohci
);
324 /* Attach or detach a device on a root hub port. */
325 static void ohci_attach(USBPort
*port1
, USBDevice
*dev
)
327 OHCIState
*s
= port1
->opaque
;
328 OHCIPort
*port
= &s
->rhport
[port1
->index
];
329 uint32_t old_state
= port
->ctrl
;
332 if (port
->port
.dev
) {
333 usb_attach(port1
, NULL
);
335 /* set connect status */
336 port
->ctrl
|= OHCI_PORT_CCS
| OHCI_PORT_CSC
;
339 if (dev
->speed
== USB_SPEED_LOW
)
340 port
->ctrl
|= OHCI_PORT_LSDA
;
342 port
->ctrl
&= ~OHCI_PORT_LSDA
;
343 port
->port
.dev
= dev
;
345 /* notify of remote-wakeup */
346 if ((s
->ctl
& OHCI_CTL_HCFS
) == OHCI_USB_SUSPEND
)
347 ohci_set_interrupt(s
, OHCI_INTR_RD
);
349 /* send the attach message */
350 usb_send_msg(dev
, USB_MSG_ATTACH
);
351 DPRINTF("usb-ohci: Attached port %d\n", port1
->index
);
353 /* set connect status */
354 if (port
->ctrl
& OHCI_PORT_CCS
) {
355 port
->ctrl
&= ~OHCI_PORT_CCS
;
356 port
->ctrl
|= OHCI_PORT_CSC
;
359 if (port
->ctrl
& OHCI_PORT_PES
) {
360 port
->ctrl
&= ~OHCI_PORT_PES
;
361 port
->ctrl
|= OHCI_PORT_PESC
;
363 dev
= port
->port
.dev
;
365 /* send the detach message */
366 usb_send_msg(dev
, USB_MSG_DETACH
);
368 port
->port
.dev
= NULL
;
369 DPRINTF("usb-ohci: Detached port %d\n", port1
->index
);
372 if (old_state
!= port
->ctrl
)
373 ohci_set_interrupt(s
, OHCI_INTR_RHSC
);
376 /* Reset the controller */
377 static void ohci_reset(void *opaque
)
379 OHCIState
*ohci
= opaque
;
387 ohci
->intr_status
= 0;
388 ohci
->intr
= OHCI_INTR_MIE
;
391 ohci
->ctrl_head
= ohci
->ctrl_cur
= 0;
392 ohci
->bulk_head
= ohci
->bulk_cur
= 0;
395 ohci
->done_count
= 7;
397 /* FSMPS is marked TBD in OCHI 1.0, what gives ffs?
398 * I took the value linux sets ...
400 ohci
->fsmps
= 0x2778;
404 ohci
->frame_number
= 0;
406 ohci
->lst
= OHCI_LS_THRESH
;
408 ohci
->rhdesc_a
= OHCI_RHA_NPS
| ohci
->num_ports
;
409 ohci
->rhdesc_b
= 0x0; /* Impl. specific */
412 for (i
= 0; i
< ohci
->num_ports
; i
++)
414 port
= &ohci
->rhport
[i
];
417 ohci_attach(&port
->port
, port
->port
.dev
);
419 if (ohci
->async_td
) {
420 usb_cancel_packet(&ohci
->usb_packet
);
423 DPRINTF("usb-ohci: Reset %s\n", ohci
->name
);
426 /* Get an array of dwords from main memory */
427 static inline int get_dwords(OHCIState
*ohci
,
428 uint32_t addr
, uint32_t *buf
, int num
)
432 addr
+= ohci
->localmem_base
;
434 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
435 cpu_physical_memory_rw(addr
, (uint8_t *)buf
, sizeof(*buf
), 0);
436 *buf
= le32_to_cpu(*buf
);
442 /* Put an array of dwords in to main memory */
443 static inline int put_dwords(OHCIState
*ohci
,
444 uint32_t addr
, uint32_t *buf
, int num
)
448 addr
+= ohci
->localmem_base
;
450 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
451 uint32_t tmp
= cpu_to_le32(*buf
);
452 cpu_physical_memory_rw(addr
, (uint8_t *)&tmp
, sizeof(tmp
), 1);
458 /* Get an array of words from main memory */
459 static inline int get_words(OHCIState
*ohci
,
460 uint32_t addr
, uint16_t *buf
, int num
)
464 addr
+= ohci
->localmem_base
;
466 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
467 cpu_physical_memory_rw(addr
, (uint8_t *)buf
, sizeof(*buf
), 0);
468 *buf
= le16_to_cpu(*buf
);
474 /* Put an array of words in to main memory */
475 static inline int put_words(OHCIState
*ohci
,
476 uint32_t addr
, uint16_t *buf
, int num
)
480 addr
+= ohci
->localmem_base
;
482 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
483 uint16_t tmp
= cpu_to_le16(*buf
);
484 cpu_physical_memory_rw(addr
, (uint8_t *)&tmp
, sizeof(tmp
), 1);
490 static inline int ohci_read_ed(OHCIState
*ohci
,
491 uint32_t addr
, struct ohci_ed
*ed
)
493 return get_dwords(ohci
, addr
, (uint32_t *)ed
, sizeof(*ed
) >> 2);
496 static inline int ohci_read_td(OHCIState
*ohci
,
497 uint32_t addr
, struct ohci_td
*td
)
499 return get_dwords(ohci
, addr
, (uint32_t *)td
, sizeof(*td
) >> 2);
502 static inline int ohci_read_iso_td(OHCIState
*ohci
,
503 uint32_t addr
, struct ohci_iso_td
*td
)
505 return (get_dwords(ohci
, addr
, (uint32_t *)td
, 4) &&
506 get_words(ohci
, addr
+ 16, td
->offset
, 8));
509 static inline int ohci_read_hcca(OHCIState
*ohci
,
510 uint32_t addr
, struct ohci_hcca
*hcca
)
512 cpu_physical_memory_rw(addr
+ ohci
->localmem_base
,
513 (uint8_t *)hcca
, sizeof(*hcca
), 0);
517 static inline int ohci_put_ed(OHCIState
*ohci
,
518 uint32_t addr
, struct ohci_ed
*ed
)
520 return put_dwords(ohci
, addr
, (uint32_t *)ed
, sizeof(*ed
) >> 2);
523 static inline int ohci_put_td(OHCIState
*ohci
,
524 uint32_t addr
, struct ohci_td
*td
)
526 return put_dwords(ohci
, addr
, (uint32_t *)td
, sizeof(*td
) >> 2);
529 static inline int ohci_put_iso_td(OHCIState
*ohci
,
530 uint32_t addr
, struct ohci_iso_td
*td
)
532 return (put_dwords(ohci
, addr
, (uint32_t *)td
, 4) &&
533 put_words(ohci
, addr
+ 16, td
->offset
, 8));
536 static inline int ohci_put_hcca(OHCIState
*ohci
,
537 uint32_t addr
, struct ohci_hcca
*hcca
)
539 cpu_physical_memory_rw(addr
+ ohci
->localmem_base
,
540 (uint8_t *)hcca
, sizeof(*hcca
), 1);
544 /* Read/Write the contents of a TD from/to main memory. */
545 static void ohci_copy_td(OHCIState
*ohci
, struct ohci_td
*td
,
546 uint8_t *buf
, int len
, int write
)
552 n
= 0x1000 - (ptr
& 0xfff);
555 cpu_physical_memory_rw(ptr
+ ohci
->localmem_base
, buf
, n
, write
);
558 ptr
= td
->be
& ~0xfffu
;
560 cpu_physical_memory_rw(ptr
+ ohci
->localmem_base
, buf
, len
- n
, write
);
563 /* Read/Write the contents of an ISO TD from/to main memory. */
564 static void ohci_copy_iso_td(OHCIState
*ohci
,
565 uint32_t start_addr
, uint32_t end_addr
,
566 uint8_t *buf
, int len
, int write
)
572 n
= 0x1000 - (ptr
& 0xfff);
575 cpu_physical_memory_rw(ptr
+ ohci
->localmem_base
, buf
, n
, write
);
578 ptr
= end_addr
& ~0xfffu
;
580 cpu_physical_memory_rw(ptr
+ ohci
->localmem_base
, buf
, len
- n
, write
);
583 static void ohci_process_lists(OHCIState
*ohci
, int completion
);
585 static void ohci_async_complete_packet(USBPacket
*packet
, void *opaque
)
587 OHCIState
*ohci
= opaque
;
589 DPRINTF("Async packet complete\n");
591 ohci
->async_complete
= 1;
592 ohci_process_lists(ohci
, 1);
595 #define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
597 static int ohci_service_iso_td(OHCIState
*ohci
, struct ohci_ed
*ed
,
603 const char *str
= NULL
;
609 struct ohci_iso_td iso_td
;
611 uint16_t starting_frame
;
612 int16_t relative_frame_number
;
614 uint32_t start_offset
, next_offset
, end_offset
= 0;
615 uint32_t start_addr
, end_addr
;
617 addr
= ed
->head
& OHCI_DPTR_MASK
;
619 if (!ohci_read_iso_td(ohci
, addr
, &iso_td
)) {
620 printf("usb-ohci: ISO_TD read error at %x\n", addr
);
624 starting_frame
= OHCI_BM(iso_td
.flags
, TD_SF
);
625 frame_count
= OHCI_BM(iso_td
.flags
, TD_FC
);
626 relative_frame_number
= USUB(ohci
->frame_number
, starting_frame
);
629 printf("--- ISO_TD ED head 0x%.8x tailp 0x%.8x\n"
630 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
631 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
632 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
633 "frame_number 0x%.8x starting_frame 0x%.8x\n"
634 "frame_count 0x%.8x relative %d\n"
635 "di 0x%.8x cc 0x%.8x\n",
636 ed
->head
& OHCI_DPTR_MASK
, ed
->tail
& OHCI_DPTR_MASK
,
637 iso_td
.flags
, iso_td
.bp
, iso_td
.next
, iso_td
.be
,
638 iso_td
.offset
[0], iso_td
.offset
[1], iso_td
.offset
[2], iso_td
.offset
[3],
639 iso_td
.offset
[4], iso_td
.offset
[5], iso_td
.offset
[6], iso_td
.offset
[7],
640 ohci
->frame_number
, starting_frame
,
641 frame_count
, relative_frame_number
,
642 OHCI_BM(iso_td
.flags
, TD_DI
), OHCI_BM(iso_td
.flags
, TD_CC
));
645 if (relative_frame_number
< 0) {
646 DPRINTF("usb-ohci: ISO_TD R=%d < 0\n", relative_frame_number
);
648 } else if (relative_frame_number
> frame_count
) {
649 /* ISO TD expired - retire the TD to the Done Queue and continue with
650 the next ISO TD of the same ED */
651 DPRINTF("usb-ohci: ISO_TD R=%d > FC=%d\n", relative_frame_number
,
653 OHCI_SET_BM(iso_td
.flags
, TD_CC
, OHCI_CC_DATAOVERRUN
);
654 ed
->head
&= ~OHCI_DPTR_MASK
;
655 ed
->head
|= (iso_td
.next
& OHCI_DPTR_MASK
);
656 iso_td
.next
= ohci
->done
;
658 i
= OHCI_BM(iso_td
.flags
, TD_DI
);
659 if (i
< ohci
->done_count
)
660 ohci
->done_count
= i
;
661 ohci_put_iso_td(ohci
, addr
, &iso_td
);
665 dir
= OHCI_BM(ed
->flags
, ED_D
);
673 case OHCI_TD_DIR_OUT
:
679 case OHCI_TD_DIR_SETUP
:
683 pid
= USB_TOKEN_SETUP
;
686 printf("usb-ohci: Bad direction %d\n", dir
);
690 if (!iso_td
.bp
|| !iso_td
.be
) {
691 printf("usb-ohci: ISO_TD bp 0x%.8x be 0x%.8x\n", iso_td
.bp
, iso_td
.be
);
695 start_offset
= iso_td
.offset
[relative_frame_number
];
696 next_offset
= iso_td
.offset
[relative_frame_number
+ 1];
698 if (!(OHCI_BM(start_offset
, TD_PSW_CC
) & 0xe) ||
699 ((relative_frame_number
< frame_count
) &&
700 !(OHCI_BM(next_offset
, TD_PSW_CC
) & 0xe))) {
701 printf("usb-ohci: ISO_TD cc != not accessed 0x%.8x 0x%.8x\n",
702 start_offset
, next_offset
);
706 if ((relative_frame_number
< frame_count
) && (start_offset
> next_offset
)) {
707 printf("usb-ohci: ISO_TD start_offset=0x%.8x > next_offset=0x%.8x\n",
708 start_offset
, next_offset
);
712 if ((start_offset
& 0x1000) == 0) {
713 start_addr
= (iso_td
.bp
& OHCI_PAGE_MASK
) |
714 (start_offset
& OHCI_OFFSET_MASK
);
716 start_addr
= (iso_td
.be
& OHCI_PAGE_MASK
) |
717 (start_offset
& OHCI_OFFSET_MASK
);
720 if (relative_frame_number
< frame_count
) {
721 end_offset
= next_offset
- 1;
722 if ((end_offset
& 0x1000) == 0) {
723 end_addr
= (iso_td
.bp
& OHCI_PAGE_MASK
) |
724 (end_offset
& OHCI_OFFSET_MASK
);
726 end_addr
= (iso_td
.be
& OHCI_PAGE_MASK
) |
727 (end_offset
& OHCI_OFFSET_MASK
);
730 /* Last packet in the ISO TD */
731 end_addr
= iso_td
.be
;
734 if ((start_addr
& OHCI_PAGE_MASK
) != (end_addr
& OHCI_PAGE_MASK
)) {
735 len
= (end_addr
& OHCI_OFFSET_MASK
) + 0x1001
736 - (start_addr
& OHCI_OFFSET_MASK
);
738 len
= end_addr
- start_addr
+ 1;
741 if (len
&& dir
!= OHCI_TD_DIR_IN
) {
742 ohci_copy_iso_td(ohci
, start_addr
, end_addr
, ohci
->usb_buf
, len
, 0);
746 ret
= ohci
->usb_packet
.len
;
749 for (i
= 0; i
< ohci
->num_ports
; i
++) {
750 dev
= ohci
->rhport
[i
].port
.dev
;
751 if ((ohci
->rhport
[i
].ctrl
& OHCI_PORT_PES
) == 0)
753 ohci
->usb_packet
.pid
= pid
;
754 ohci
->usb_packet
.devaddr
= OHCI_BM(ed
->flags
, ED_FA
);
755 ohci
->usb_packet
.devep
= OHCI_BM(ed
->flags
, ED_EN
);
756 ohci
->usb_packet
.data
= ohci
->usb_buf
;
757 ohci
->usb_packet
.len
= len
;
758 ohci
->usb_packet
.complete_cb
= ohci_async_complete_packet
;
759 ohci
->usb_packet
.complete_opaque
= ohci
;
760 ret
= dev
->info
->handle_packet(dev
, &ohci
->usb_packet
);
761 if (ret
!= USB_RET_NODEV
)
765 if (ret
== USB_RET_ASYNC
) {
771 printf("so 0x%.8x eo 0x%.8x\nsa 0x%.8x ea 0x%.8x\ndir %s len %zu ret %d\n",
772 start_offset
, end_offset
, start_addr
, end_addr
, str
, len
, ret
);
776 if (dir
== OHCI_TD_DIR_IN
&& ret
>= 0 && ret
<= len
) {
777 /* IN transfer succeeded */
778 ohci_copy_iso_td(ohci
, start_addr
, end_addr
, ohci
->usb_buf
, ret
, 1);
779 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
781 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
, ret
);
782 } else if (dir
== OHCI_TD_DIR_OUT
&& ret
== len
) {
783 /* OUT transfer succeeded */
784 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
786 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
, 0);
788 if (ret
> (ssize_t
) len
) {
789 printf("usb-ohci: DataOverrun %d > %zu\n", ret
, len
);
790 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
791 OHCI_CC_DATAOVERRUN
);
792 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
794 } else if (ret
>= 0) {
795 printf("usb-ohci: DataUnderrun %d\n", ret
);
796 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
797 OHCI_CC_DATAUNDERRUN
);
801 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
802 OHCI_CC_DEVICENOTRESPONDING
);
803 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
808 printf("usb-ohci: got NAK/STALL %d\n", ret
);
809 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
811 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
815 printf("usb-ohci: Bad device response %d\n", ret
);
816 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
817 OHCI_CC_UNDEXPETEDPID
);
823 if (relative_frame_number
== frame_count
) {
824 /* Last data packet of ISO TD - retire the TD to the Done Queue */
825 OHCI_SET_BM(iso_td
.flags
, TD_CC
, OHCI_CC_NOERROR
);
826 ed
->head
&= ~OHCI_DPTR_MASK
;
827 ed
->head
|= (iso_td
.next
& OHCI_DPTR_MASK
);
828 iso_td
.next
= ohci
->done
;
830 i
= OHCI_BM(iso_td
.flags
, TD_DI
);
831 if (i
< ohci
->done_count
)
832 ohci
->done_count
= i
;
834 ohci_put_iso_td(ohci
, addr
, &iso_td
);
838 /* Service a transport descriptor.
839 Returns nonzero to terminate processing of this endpoint. */
841 static int ohci_service_td(OHCIState
*ohci
, struct ohci_ed
*ed
)
846 const char *str
= NULL
;
857 addr
= ed
->head
& OHCI_DPTR_MASK
;
858 /* See if this TD has already been submitted to the device. */
859 completion
= (addr
== ohci
->async_td
);
860 if (completion
&& !ohci
->async_complete
) {
862 DPRINTF("Skipping async TD\n");
866 if (!ohci_read_td(ohci
, addr
, &td
)) {
867 fprintf(stderr
, "usb-ohci: TD read error at %x\n", addr
);
871 dir
= OHCI_BM(ed
->flags
, ED_D
);
873 case OHCI_TD_DIR_OUT
:
878 dir
= OHCI_BM(td
.flags
, TD_DP
);
889 case OHCI_TD_DIR_OUT
:
895 case OHCI_TD_DIR_SETUP
:
899 pid
= USB_TOKEN_SETUP
;
902 fprintf(stderr
, "usb-ohci: Bad direction\n");
905 if (td
.cbp
&& td
.be
) {
906 if ((td
.cbp
& 0xfffff000) != (td
.be
& 0xfffff000)) {
907 len
= (td
.be
& 0xfff) + 0x1001 - (td
.cbp
& 0xfff);
909 len
= (td
.be
- td
.cbp
) + 1;
912 if (len
&& dir
!= OHCI_TD_DIR_IN
&& !completion
) {
913 ohci_copy_td(ohci
, &td
, ohci
->usb_buf
, len
, 0);
917 flag_r
= (td
.flags
& OHCI_TD_R
) != 0;
919 DPRINTF(" TD @ 0x%.8x %" PRId64
" bytes %s r=%d cbp=0x%.8x be=0x%.8x\n",
920 addr
, (int64_t)len
, str
, flag_r
, td
.cbp
, td
.be
);
922 if (len
> 0 && dir
!= OHCI_TD_DIR_IN
) {
924 for (i
= 0; i
< len
; i
++)
925 printf(" %.2x", ohci
->usb_buf
[i
]);
930 ret
= ohci
->usb_packet
.len
;
932 ohci
->async_complete
= 0;
935 for (i
= 0; i
< ohci
->num_ports
; i
++) {
936 dev
= ohci
->rhport
[i
].port
.dev
;
937 if ((ohci
->rhport
[i
].ctrl
& OHCI_PORT_PES
) == 0)
940 if (ohci
->async_td
) {
941 /* ??? The hardware should allow one active packet per
942 endpoint. We only allow one active packet per controller.
943 This should be sufficient as long as devices respond in a
947 DPRINTF("Too many pending packets\n");
951 ohci
->usb_packet
.pid
= pid
;
952 ohci
->usb_packet
.devaddr
= OHCI_BM(ed
->flags
, ED_FA
);
953 ohci
->usb_packet
.devep
= OHCI_BM(ed
->flags
, ED_EN
);
954 ohci
->usb_packet
.data
= ohci
->usb_buf
;
955 ohci
->usb_packet
.len
= len
;
956 ohci
->usb_packet
.complete_cb
= ohci_async_complete_packet
;
957 ohci
->usb_packet
.complete_opaque
= ohci
;
958 ret
= dev
->info
->handle_packet(dev
, &ohci
->usb_packet
);
959 if (ret
!= USB_RET_NODEV
)
963 DPRINTF("ret=%d\n", ret
);
965 if (ret
== USB_RET_ASYNC
) {
966 ohci
->async_td
= addr
;
971 if (dir
== OHCI_TD_DIR_IN
) {
972 ohci_copy_td(ohci
, &td
, ohci
->usb_buf
, ret
, 1);
975 for (i
= 0; i
< ret
; i
++)
976 printf(" %.2x", ohci
->usb_buf
[i
]);
985 if (ret
== len
|| (dir
== OHCI_TD_DIR_IN
&& ret
>= 0 && flag_r
)) {
986 /* Transmission succeeded. */
991 if ((td
.cbp
& 0xfff) + ret
> 0xfff) {
993 td
.cbp
|= td
.be
& ~0xfff;
996 td
.flags
|= OHCI_TD_T1
;
997 td
.flags
^= OHCI_TD_T0
;
998 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_NOERROR
);
999 OHCI_SET_BM(td
.flags
, TD_EC
, 0);
1001 ed
->head
&= ~OHCI_ED_C
;
1002 if (td
.flags
& OHCI_TD_T0
)
1003 ed
->head
|= OHCI_ED_C
;
1006 DPRINTF("usb-ohci: Underrun\n");
1007 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DATAUNDERRUN
);
1011 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DEVICENOTRESPONDING
);
1013 DPRINTF("usb-ohci: got NAK\n");
1016 DPRINTF("usb-ohci: got STALL\n");
1017 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_STALL
);
1019 case USB_RET_BABBLE
:
1020 DPRINTF("usb-ohci: got BABBLE\n");
1021 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DATAOVERRUN
);
1024 fprintf(stderr
, "usb-ohci: Bad device response %d\n", ret
);
1025 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_UNDEXPETEDPID
);
1026 OHCI_SET_BM(td
.flags
, TD_EC
, 3);
1030 ed
->head
|= OHCI_ED_H
;
1033 /* Retire this TD */
1034 ed
->head
&= ~OHCI_DPTR_MASK
;
1035 ed
->head
|= td
.next
& OHCI_DPTR_MASK
;
1036 td
.next
= ohci
->done
;
1038 i
= OHCI_BM(td
.flags
, TD_DI
);
1039 if (i
< ohci
->done_count
)
1040 ohci
->done_count
= i
;
1041 ohci_put_td(ohci
, addr
, &td
);
1042 return OHCI_BM(td
.flags
, TD_CC
) != OHCI_CC_NOERROR
;
1045 /* Service an endpoint list. Returns nonzero if active TD were found. */
1046 static int ohci_service_ed_list(OHCIState
*ohci
, uint32_t head
, int completion
)
1058 for (cur
= head
; cur
; cur
= next_ed
) {
1059 if (!ohci_read_ed(ohci
, cur
, &ed
)) {
1060 fprintf(stderr
, "usb-ohci: ED read error at %x\n", cur
);
1064 next_ed
= ed
.next
& OHCI_DPTR_MASK
;
1066 if ((ed
.head
& OHCI_ED_H
) || (ed
.flags
& OHCI_ED_K
)) {
1068 /* Cancel pending packets for ED that have been paused. */
1069 addr
= ed
.head
& OHCI_DPTR_MASK
;
1070 if (ohci
->async_td
&& addr
== ohci
->async_td
) {
1071 usb_cancel_packet(&ohci
->usb_packet
);
1077 while ((ed
.head
& OHCI_DPTR_MASK
) != ed
.tail
) {
1079 DPRINTF("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u "
1080 "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur
,
1081 OHCI_BM(ed
.flags
, ED_FA
), OHCI_BM(ed
.flags
, ED_EN
),
1082 OHCI_BM(ed
.flags
, ED_D
), (ed
.flags
& OHCI_ED_S
)!= 0,
1083 (ed
.flags
& OHCI_ED_K
) != 0, (ed
.flags
& OHCI_ED_F
) != 0,
1084 OHCI_BM(ed
.flags
, ED_MPS
), (ed
.head
& OHCI_ED_H
) != 0,
1085 (ed
.head
& OHCI_ED_C
) != 0, ed
.head
& OHCI_DPTR_MASK
,
1086 ed
.tail
& OHCI_DPTR_MASK
, ed
.next
& OHCI_DPTR_MASK
);
1090 if ((ed
.flags
& OHCI_ED_F
) == 0) {
1091 if (ohci_service_td(ohci
, &ed
))
1094 /* Handle isochronous endpoints */
1095 if (ohci_service_iso_td(ohci
, &ed
, completion
))
1100 ohci_put_ed(ohci
, cur
, &ed
);
1106 /* Generate a SOF event, and set a timer for EOF */
1107 static void ohci_sof(OHCIState
*ohci
)
1109 ohci
->sof_time
= qemu_get_clock(vm_clock
);
1110 qemu_mod_timer(ohci
->eof_timer
, ohci
->sof_time
+ usb_frame_time
);
1111 ohci_set_interrupt(ohci
, OHCI_INTR_SF
);
1114 /* Process Control and Bulk lists. */
1115 static void ohci_process_lists(OHCIState
*ohci
, int completion
)
1117 if ((ohci
->ctl
& OHCI_CTL_CLE
) && (ohci
->status
& OHCI_STATUS_CLF
)) {
1118 if (ohci
->ctrl_cur
&& ohci
->ctrl_cur
!= ohci
->ctrl_head
) {
1119 DPRINTF("usb-ohci: head %x, cur %x\n",
1120 ohci
->ctrl_head
, ohci
->ctrl_cur
);
1122 if (!ohci_service_ed_list(ohci
, ohci
->ctrl_head
, completion
)) {
1124 ohci
->status
&= ~OHCI_STATUS_CLF
;
1128 if ((ohci
->ctl
& OHCI_CTL_BLE
) && (ohci
->status
& OHCI_STATUS_BLF
)) {
1129 if (!ohci_service_ed_list(ohci
, ohci
->bulk_head
, completion
)) {
1131 ohci
->status
&= ~OHCI_STATUS_BLF
;
1136 /* Do frame processing on frame boundary */
1137 static void ohci_frame_boundary(void *opaque
)
1139 OHCIState
*ohci
= opaque
;
1140 struct ohci_hcca hcca
;
1142 ohci_read_hcca(ohci
, ohci
->hcca
, &hcca
);
1144 /* Process all the lists at the end of the frame */
1145 if (ohci
->ctl
& OHCI_CTL_PLE
) {
1148 n
= ohci
->frame_number
& 0x1f;
1149 ohci_service_ed_list(ohci
, le32_to_cpu(hcca
.intr
[n
]), 0);
1152 /* Cancel all pending packets if either of the lists has been disabled. */
1153 if (ohci
->async_td
&&
1154 ohci
->old_ctl
& (~ohci
->ctl
) & (OHCI_CTL_BLE
| OHCI_CTL_CLE
)) {
1155 usb_cancel_packet(&ohci
->usb_packet
);
1158 ohci
->old_ctl
= ohci
->ctl
;
1159 ohci_process_lists(ohci
, 0);
1161 /* Frame boundary, so do EOF stuf here */
1162 ohci
->frt
= ohci
->fit
;
1164 /* Increment frame number and take care of endianness. */
1165 ohci
->frame_number
= (ohci
->frame_number
+ 1) & 0xffff;
1166 hcca
.frame
= cpu_to_le16(ohci
->frame_number
);
1168 if (ohci
->done_count
== 0 && !(ohci
->intr_status
& OHCI_INTR_WD
)) {
1171 if (ohci
->intr
& ohci
->intr_status
)
1173 hcca
.done
= cpu_to_le32(ohci
->done
);
1175 ohci
->done_count
= 7;
1176 ohci_set_interrupt(ohci
, OHCI_INTR_WD
);
1179 if (ohci
->done_count
!= 7 && ohci
->done_count
!= 0)
1182 /* Do SOF stuff here */
1185 /* Writeback HCCA */
1186 ohci_put_hcca(ohci
, ohci
->hcca
, &hcca
);
1189 /* Start sending SOF tokens across the USB bus, lists are processed in
1192 static int ohci_bus_start(OHCIState
*ohci
)
1194 ohci
->eof_timer
= qemu_new_timer(vm_clock
,
1195 ohci_frame_boundary
,
1198 if (ohci
->eof_timer
== NULL
) {
1199 fprintf(stderr
, "usb-ohci: %s: qemu_new_timer failed\n", ohci
->name
);
1200 /* TODO: Signal unrecoverable error */
1204 DPRINTF("usb-ohci: %s: USB Operational\n", ohci
->name
);
1211 /* Stop sending SOF tokens on the bus */
1212 static void ohci_bus_stop(OHCIState
*ohci
)
1214 if (ohci
->eof_timer
)
1215 qemu_del_timer(ohci
->eof_timer
);
1216 ohci
->eof_timer
= NULL
;
1219 /* Sets a flag in a port status register but only set it if the port is
1220 * connected, if not set ConnectStatusChange flag. If flag is enabled
1223 static int ohci_port_set_if_connected(OHCIState
*ohci
, int i
, uint32_t val
)
1227 /* writing a 0 has no effect */
1231 /* If CurrentConnectStatus is cleared we set
1232 * ConnectStatusChange
1234 if (!(ohci
->rhport
[i
].ctrl
& OHCI_PORT_CCS
)) {
1235 ohci
->rhport
[i
].ctrl
|= OHCI_PORT_CSC
;
1236 if (ohci
->rhstatus
& OHCI_RHS_DRWE
) {
1237 /* TODO: CSC is a wakeup event */
1242 if (ohci
->rhport
[i
].ctrl
& val
)
1246 ohci
->rhport
[i
].ctrl
|= val
;
1251 /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
1252 static void ohci_set_frame_interval(OHCIState
*ohci
, uint16_t val
)
1256 if (val
!= ohci
->fi
) {
1257 DPRINTF("usb-ohci: %s: FrameInterval = 0x%x (%u)\n",
1258 ohci
->name
, ohci
->fi
, ohci
->fi
);
1264 static void ohci_port_power(OHCIState
*ohci
, int i
, int p
)
1267 ohci
->rhport
[i
].ctrl
|= OHCI_PORT_PPS
;
1269 ohci
->rhport
[i
].ctrl
&= ~(OHCI_PORT_PPS
|
1276 /* Set HcControlRegister */
1277 static void ohci_set_ctl(OHCIState
*ohci
, uint32_t val
)
1282 old_state
= ohci
->ctl
& OHCI_CTL_HCFS
;
1284 new_state
= ohci
->ctl
& OHCI_CTL_HCFS
;
1286 /* no state change */
1287 if (old_state
== new_state
)
1290 switch (new_state
) {
1291 case OHCI_USB_OPERATIONAL
:
1292 ohci_bus_start(ohci
);
1294 case OHCI_USB_SUSPEND
:
1295 ohci_bus_stop(ohci
);
1296 DPRINTF("usb-ohci: %s: USB Suspended\n", ohci
->name
);
1298 case OHCI_USB_RESUME
:
1299 DPRINTF("usb-ohci: %s: USB Resume\n", ohci
->name
);
1301 case OHCI_USB_RESET
:
1303 DPRINTF("usb-ohci: %s: USB Reset\n", ohci
->name
);
1308 static uint32_t ohci_get_frame_remaining(OHCIState
*ohci
)
1313 if ((ohci
->ctl
& OHCI_CTL_HCFS
) != OHCI_USB_OPERATIONAL
)
1314 return (ohci
->frt
<< 31);
1316 /* Being in USB operational state guarnatees sof_time was
1319 tks
= qemu_get_clock(vm_clock
) - ohci
->sof_time
;
1321 /* avoid muldiv if possible */
1322 if (tks
>= usb_frame_time
)
1323 return (ohci
->frt
<< 31);
1325 tks
= muldiv64(1, tks
, usb_bit_time
);
1326 fr
= (uint16_t)(ohci
->fi
- tks
);
1328 return (ohci
->frt
<< 31) | fr
;
1332 /* Set root hub status */
1333 static void ohci_set_hub_status(OHCIState
*ohci
, uint32_t val
)
1337 old_state
= ohci
->rhstatus
;
1339 /* write 1 to clear OCIC */
1340 if (val
& OHCI_RHS_OCIC
)
1341 ohci
->rhstatus
&= ~OHCI_RHS_OCIC
;
1343 if (val
& OHCI_RHS_LPS
) {
1346 for (i
= 0; i
< ohci
->num_ports
; i
++)
1347 ohci_port_power(ohci
, i
, 0);
1348 DPRINTF("usb-ohci: powered down all ports\n");
1351 if (val
& OHCI_RHS_LPSC
) {
1354 for (i
= 0; i
< ohci
->num_ports
; i
++)
1355 ohci_port_power(ohci
, i
, 1);
1356 DPRINTF("usb-ohci: powered up all ports\n");
1359 if (val
& OHCI_RHS_DRWE
)
1360 ohci
->rhstatus
|= OHCI_RHS_DRWE
;
1362 if (val
& OHCI_RHS_CRWE
)
1363 ohci
->rhstatus
&= ~OHCI_RHS_DRWE
;
1365 if (old_state
!= ohci
->rhstatus
)
1366 ohci_set_interrupt(ohci
, OHCI_INTR_RHSC
);
1369 /* Set root hub port status */
1370 static void ohci_port_set_status(OHCIState
*ohci
, int portnum
, uint32_t val
)
1375 port
= &ohci
->rhport
[portnum
];
1376 old_state
= port
->ctrl
;
1378 /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
1379 if (val
& OHCI_PORT_WTC
)
1380 port
->ctrl
&= ~(val
& OHCI_PORT_WTC
);
1382 if (val
& OHCI_PORT_CCS
)
1383 port
->ctrl
&= ~OHCI_PORT_PES
;
1385 ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PES
);
1387 if (ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PSS
)) {
1388 DPRINTF("usb-ohci: port %d: SUSPEND\n", portnum
);
1391 if (ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PRS
)) {
1392 DPRINTF("usb-ohci: port %d: RESET\n", portnum
);
1393 usb_send_msg(port
->port
.dev
, USB_MSG_RESET
);
1394 port
->ctrl
&= ~OHCI_PORT_PRS
;
1395 /* ??? Should this also set OHCI_PORT_PESC. */
1396 port
->ctrl
|= OHCI_PORT_PES
| OHCI_PORT_PRSC
;
1399 /* Invert order here to ensure in ambiguous case, device is
1402 if (val
& OHCI_PORT_LSDA
)
1403 ohci_port_power(ohci
, portnum
, 0);
1404 if (val
& OHCI_PORT_PPS
)
1405 ohci_port_power(ohci
, portnum
, 1);
1407 if (old_state
!= port
->ctrl
)
1408 ohci_set_interrupt(ohci
, OHCI_INTR_RHSC
);
1413 static uint32_t ohci_mem_read(void *ptr
, target_phys_addr_t addr
)
1415 OHCIState
*ohci
= ptr
;
1420 /* Only aligned reads are allowed on OHCI */
1422 fprintf(stderr
, "usb-ohci: Mis-aligned read\n");
1424 } else if (addr
>= 0x54 && addr
< 0x54 + ohci
->num_ports
* 4) {
1425 /* HcRhPortStatus */
1426 retval
= ohci
->rhport
[(addr
- 0x54) >> 2].ctrl
| OHCI_PORT_PPS
;
1428 switch (addr
>> 2) {
1429 case 0: /* HcRevision */
1433 case 1: /* HcControl */
1437 case 2: /* HcCommandStatus */
1438 retval
= ohci
->status
;
1441 case 3: /* HcInterruptStatus */
1442 retval
= ohci
->intr_status
;
1445 case 4: /* HcInterruptEnable */
1446 case 5: /* HcInterruptDisable */
1447 retval
= ohci
->intr
;
1450 case 6: /* HcHCCA */
1451 retval
= ohci
->hcca
;
1454 case 7: /* HcPeriodCurrentED */
1455 retval
= ohci
->per_cur
;
1458 case 8: /* HcControlHeadED */
1459 retval
= ohci
->ctrl_head
;
1462 case 9: /* HcControlCurrentED */
1463 retval
= ohci
->ctrl_cur
;
1466 case 10: /* HcBulkHeadED */
1467 retval
= ohci
->bulk_head
;
1470 case 11: /* HcBulkCurrentED */
1471 retval
= ohci
->bulk_cur
;
1474 case 12: /* HcDoneHead */
1475 retval
= ohci
->done
;
1478 case 13: /* HcFmInterretval */
1479 retval
= (ohci
->fit
<< 31) | (ohci
->fsmps
<< 16) | (ohci
->fi
);
1482 case 14: /* HcFmRemaining */
1483 retval
= ohci_get_frame_remaining(ohci
);
1486 case 15: /* HcFmNumber */
1487 retval
= ohci
->frame_number
;
1490 case 16: /* HcPeriodicStart */
1491 retval
= ohci
->pstart
;
1494 case 17: /* HcLSThreshold */
1498 case 18: /* HcRhDescriptorA */
1499 retval
= ohci
->rhdesc_a
;
1502 case 19: /* HcRhDescriptorB */
1503 retval
= ohci
->rhdesc_b
;
1506 case 20: /* HcRhStatus */
1507 retval
= ohci
->rhstatus
;
1510 /* PXA27x specific registers */
1511 case 24: /* HcStatus */
1512 retval
= ohci
->hstatus
& ohci
->hmask
;
1515 case 25: /* HcHReset */
1516 retval
= ohci
->hreset
;
1519 case 26: /* HcHInterruptEnable */
1520 retval
= ohci
->hmask
;
1523 case 27: /* HcHInterruptTest */
1524 retval
= ohci
->htest
;
1528 fprintf(stderr
, "ohci_read: Bad offset %x\n", (int)addr
);
1529 retval
= 0xffffffff;
1533 #ifdef TARGET_WORDS_BIGENDIAN
1534 retval
= bswap32(retval
);
1539 static void ohci_mem_write(void *ptr
, target_phys_addr_t addr
, uint32_t val
)
1541 OHCIState
*ohci
= ptr
;
1545 #ifdef TARGET_WORDS_BIGENDIAN
1549 /* Only aligned reads are allowed on OHCI */
1551 fprintf(stderr
, "usb-ohci: Mis-aligned write\n");
1555 if (addr
>= 0x54 && addr
< 0x54 + ohci
->num_ports
* 4) {
1556 /* HcRhPortStatus */
1557 ohci_port_set_status(ohci
, (addr
- 0x54) >> 2, val
);
1561 switch (addr
>> 2) {
1562 case 1: /* HcControl */
1563 ohci_set_ctl(ohci
, val
);
1566 case 2: /* HcCommandStatus */
1567 /* SOC is read-only */
1568 val
= (val
& ~OHCI_STATUS_SOC
);
1570 /* Bits written as '0' remain unchanged in the register */
1571 ohci
->status
|= val
;
1573 if (ohci
->status
& OHCI_STATUS_HCR
)
1577 case 3: /* HcInterruptStatus */
1578 ohci
->intr_status
&= ~val
;
1579 ohci_intr_update(ohci
);
1582 case 4: /* HcInterruptEnable */
1584 ohci_intr_update(ohci
);
1587 case 5: /* HcInterruptDisable */
1589 ohci_intr_update(ohci
);
1592 case 6: /* HcHCCA */
1593 ohci
->hcca
= val
& OHCI_HCCA_MASK
;
1596 case 8: /* HcControlHeadED */
1597 ohci
->ctrl_head
= val
& OHCI_EDPTR_MASK
;
1600 case 9: /* HcControlCurrentED */
1601 ohci
->ctrl_cur
= val
& OHCI_EDPTR_MASK
;
1604 case 10: /* HcBulkHeadED */
1605 ohci
->bulk_head
= val
& OHCI_EDPTR_MASK
;
1608 case 11: /* HcBulkCurrentED */
1609 ohci
->bulk_cur
= val
& OHCI_EDPTR_MASK
;
1612 case 13: /* HcFmInterval */
1613 ohci
->fsmps
= (val
& OHCI_FMI_FSMPS
) >> 16;
1614 ohci
->fit
= (val
& OHCI_FMI_FIT
) >> 31;
1615 ohci_set_frame_interval(ohci
, val
);
1618 case 15: /* HcFmNumber */
1621 case 16: /* HcPeriodicStart */
1622 ohci
->pstart
= val
& 0xffff;
1625 case 17: /* HcLSThreshold */
1626 ohci
->lst
= val
& 0xffff;
1629 case 18: /* HcRhDescriptorA */
1630 ohci
->rhdesc_a
&= ~OHCI_RHA_RW_MASK
;
1631 ohci
->rhdesc_a
|= val
& OHCI_RHA_RW_MASK
;
1634 case 19: /* HcRhDescriptorB */
1637 case 20: /* HcRhStatus */
1638 ohci_set_hub_status(ohci
, val
);
1641 /* PXA27x specific registers */
1642 case 24: /* HcStatus */
1643 ohci
->hstatus
&= ~(val
& ohci
->hmask
);
1645 case 25: /* HcHReset */
1646 ohci
->hreset
= val
& ~OHCI_HRESET_FSBIR
;
1647 if (val
& OHCI_HRESET_FSBIR
)
1651 case 26: /* HcHInterruptEnable */
1655 case 27: /* HcHInterruptTest */
1660 fprintf(stderr
, "ohci_write: Bad offset %x\n", (int)addr
);
1665 /* Only dword reads are defined on OHCI register space */
1666 static CPUReadMemoryFunc
* const ohci_readfn
[3]={
1672 /* Only dword writes are defined on OHCI register space */
1673 static CPUWriteMemoryFunc
* const ohci_writefn
[3]={
1679 static void usb_ohci_init(OHCIState
*ohci
, DeviceState
*dev
,
1680 int num_ports
, uint32_t localmem_base
)
1684 if (usb_frame_time
== 0) {
1685 #ifdef OHCI_TIME_WARP
1686 usb_frame_time
= get_ticks_per_sec();
1687 usb_bit_time
= muldiv64(1, get_ticks_per_sec(), USB_HZ
/1000);
1689 usb_frame_time
= muldiv64(1, get_ticks_per_sec(), 1000);
1690 if (get_ticks_per_sec() >= USB_HZ
) {
1691 usb_bit_time
= muldiv64(1, get_ticks_per_sec(), USB_HZ
);
1696 DPRINTF("usb-ohci: usb_bit_time=%" PRId64
" usb_frame_time=%" PRId64
"\n",
1697 usb_frame_time
, usb_bit_time
);
1700 ohci
->mem
= cpu_register_io_memory(ohci_readfn
, ohci_writefn
, ohci
);
1701 ohci
->localmem_base
= localmem_base
;
1703 ohci
->name
= dev
->info
->name
;
1705 usb_bus_new(&ohci
->bus
, dev
);
1706 ohci
->num_ports
= num_ports
;
1707 for (i
= 0; i
< num_ports
; i
++) {
1708 usb_register_port(&ohci
->bus
, &ohci
->rhport
[i
].port
, ohci
, i
, ohci_attach
);
1712 qemu_register_reset(ohci_reset
, ohci
);
1720 static void ohci_mapfunc(PCIDevice
*pci_dev
, int i
,
1721 pcibus_t addr
, pcibus_t size
, int type
)
1723 OHCIPCIState
*ohci
= DO_UPCAST(OHCIPCIState
, pci_dev
, pci_dev
);
1724 cpu_register_physical_memory(addr
, size
, ohci
->state
.mem
);
1727 static int usb_ohci_initfn_pci(struct PCIDevice
*dev
)
1729 OHCIPCIState
*ohci
= DO_UPCAST(OHCIPCIState
, pci_dev
, dev
);
1732 pci_config_set_vendor_id(ohci
->pci_dev
.config
, PCI_VENDOR_ID_APPLE
);
1733 pci_config_set_device_id(ohci
->pci_dev
.config
,
1734 PCI_DEVICE_ID_APPLE_IPID_USB
);
1735 ohci
->pci_dev
.config
[PCI_CLASS_PROG
] = 0x10; /* OHCI */
1736 pci_config_set_class(ohci
->pci_dev
.config
, PCI_CLASS_SERIAL_USB
);
1737 /* TODO: RST# value should be 0. */
1738 ohci
->pci_dev
.config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
1740 usb_ohci_init(&ohci
->state
, &dev
->qdev
, num_ports
, 0);
1741 ohci
->state
.irq
= ohci
->pci_dev
.irq
[0];
1743 /* TODO: avoid cast below by using dev */
1744 pci_register_bar((struct PCIDevice
*)ohci
, 0, 256,
1745 PCI_BASE_ADDRESS_SPACE_MEMORY
, ohci_mapfunc
);
1749 void usb_ohci_init_pci(struct PCIBus
*bus
, int devfn
)
1751 pci_create_simple(bus
, devfn
, "pci-ohci");
1755 SysBusDevice busdev
;
1758 target_phys_addr_t dma_offset
;
1761 static int ohci_init_pxa(SysBusDevice
*dev
)
1763 OHCISysBusState
*s
= FROM_SYSBUS(OHCISysBusState
, dev
);
1765 usb_ohci_init(&s
->ohci
, &dev
->qdev
, s
->num_ports
, s
->dma_offset
);
1766 sysbus_init_irq(dev
, &s
->ohci
.irq
);
1767 sysbus_init_mmio(dev
, 0x1000, s
->ohci
.mem
);
1772 static PCIDeviceInfo ohci_pci_info
= {
1773 .qdev
.name
= "pci-ohci",
1774 .qdev
.desc
= "Apple USB Controller",
1775 .qdev
.size
= sizeof(OHCIPCIState
),
1776 .init
= usb_ohci_initfn_pci
,
1779 static SysBusDeviceInfo ohci_sysbus_info
= {
1780 .init
= ohci_init_pxa
,
1781 .qdev
.name
= "sysbus-ohci",
1782 .qdev
.desc
= "OHCI USB Controller",
1783 .qdev
.size
= sizeof(OHCISysBusState
),
1784 .qdev
.props
= (Property
[]) {
1785 DEFINE_PROP_UINT32("num-ports", OHCISysBusState
, num_ports
, 3),
1786 DEFINE_PROP_TADDR("dma-offset", OHCISysBusState
, dma_offset
, 3),
1787 DEFINE_PROP_END_OF_LIST(),
1791 static void ohci_register(void)
1793 pci_qdev_register(&ohci_pci_info
);
1794 sysbus_register_withprop(&ohci_sysbus_info
);
1796 device_init(ohci_register
);