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"
32 #include "hw/pci/pci.h"
33 #include "hw/sysbus.h"
34 #include "hw/qdev-dma.h"
37 /* Dump packet contents. */
38 //#define DEBUG_PACKET
40 /* This causes frames to occur 1000x slower */
41 //#define OHCI_TIME_WARP 1
44 #define DPRINTF printf
49 /* Number of Downstream Ports on the root hub. */
51 #define OHCI_MAX_PORTS 15
53 static int64_t usb_frame_time
;
54 static int64_t usb_bit_time
;
56 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 dma_addr_t localmem_base
;
110 /* Active packets. */
112 USBPacket usb_packet
;
113 uint8_t usb_buf
[8192];
119 /* Host Controller Communications Area */
125 #define HCCA_WRITEBACK_OFFSET offsetof(struct ohci_hcca, frame)
126 #define HCCA_WRITEBACK_SIZE 8 /* frame, pad, done */
128 #define ED_WBACK_OFFSET offsetof(struct ohci_ed, head)
129 #define ED_WBACK_SIZE 4
131 static void ohci_bus_stop(OHCIState
*ohci
);
132 static void ohci_async_cancel_device(OHCIState
*ohci
, USBDevice
*dev
);
134 /* Bitfields for the first word of an Endpoint Desciptor. */
135 #define OHCI_ED_FA_SHIFT 0
136 #define OHCI_ED_FA_MASK (0x7f<<OHCI_ED_FA_SHIFT)
137 #define OHCI_ED_EN_SHIFT 7
138 #define OHCI_ED_EN_MASK (0xf<<OHCI_ED_EN_SHIFT)
139 #define OHCI_ED_D_SHIFT 11
140 #define OHCI_ED_D_MASK (3<<OHCI_ED_D_SHIFT)
141 #define OHCI_ED_S (1<<13)
142 #define OHCI_ED_K (1<<14)
143 #define OHCI_ED_F (1<<15)
144 #define OHCI_ED_MPS_SHIFT 16
145 #define OHCI_ED_MPS_MASK (0x7ff<<OHCI_ED_MPS_SHIFT)
147 /* Flags in the head field of an Endpoint Desciptor. */
151 /* Bitfields for the first word of a Transfer Desciptor. */
152 #define OHCI_TD_R (1<<18)
153 #define OHCI_TD_DP_SHIFT 19
154 #define OHCI_TD_DP_MASK (3<<OHCI_TD_DP_SHIFT)
155 #define OHCI_TD_DI_SHIFT 21
156 #define OHCI_TD_DI_MASK (7<<OHCI_TD_DI_SHIFT)
157 #define OHCI_TD_T0 (1<<24)
158 #define OHCI_TD_T1 (1<<25)
159 #define OHCI_TD_EC_SHIFT 26
160 #define OHCI_TD_EC_MASK (3<<OHCI_TD_EC_SHIFT)
161 #define OHCI_TD_CC_SHIFT 28
162 #define OHCI_TD_CC_MASK (0xf<<OHCI_TD_CC_SHIFT)
164 /* Bitfields for the first word of an Isochronous Transfer Desciptor. */
165 /* CC & DI - same as in the General Transfer Desciptor */
166 #define OHCI_TD_SF_SHIFT 0
167 #define OHCI_TD_SF_MASK (0xffff<<OHCI_TD_SF_SHIFT)
168 #define OHCI_TD_FC_SHIFT 24
169 #define OHCI_TD_FC_MASK (7<<OHCI_TD_FC_SHIFT)
171 /* Isochronous Transfer Desciptor - Offset / PacketStatusWord */
172 #define OHCI_TD_PSW_CC_SHIFT 12
173 #define OHCI_TD_PSW_CC_MASK (0xf<<OHCI_TD_PSW_CC_SHIFT)
174 #define OHCI_TD_PSW_SIZE_SHIFT 0
175 #define OHCI_TD_PSW_SIZE_MASK (0xfff<<OHCI_TD_PSW_SIZE_SHIFT)
177 #define OHCI_PAGE_MASK 0xfffff000
178 #define OHCI_OFFSET_MASK 0xfff
180 #define OHCI_DPTR_MASK 0xfffffff0
182 #define OHCI_BM(val, field) \
183 (((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT)
185 #define OHCI_SET_BM(val, field, newval) do { \
186 val &= ~OHCI_##field##_MASK; \
187 val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \
190 /* endpoint descriptor */
198 /* General transfer descriptor */
206 /* Isochronous transfer descriptor */
215 #define USB_HZ 12000000
217 /* OHCI Local stuff */
218 #define OHCI_CTL_CBSR ((1<<0)|(1<<1))
219 #define OHCI_CTL_PLE (1<<2)
220 #define OHCI_CTL_IE (1<<3)
221 #define OHCI_CTL_CLE (1<<4)
222 #define OHCI_CTL_BLE (1<<5)
223 #define OHCI_CTL_HCFS ((1<<6)|(1<<7))
224 #define OHCI_USB_RESET 0x00
225 #define OHCI_USB_RESUME 0x40
226 #define OHCI_USB_OPERATIONAL 0x80
227 #define OHCI_USB_SUSPEND 0xc0
228 #define OHCI_CTL_IR (1<<8)
229 #define OHCI_CTL_RWC (1<<9)
230 #define OHCI_CTL_RWE (1<<10)
232 #define OHCI_STATUS_HCR (1<<0)
233 #define OHCI_STATUS_CLF (1<<1)
234 #define OHCI_STATUS_BLF (1<<2)
235 #define OHCI_STATUS_OCR (1<<3)
236 #define OHCI_STATUS_SOC ((1<<6)|(1<<7))
238 #define OHCI_INTR_SO (1<<0) /* Scheduling overrun */
239 #define OHCI_INTR_WD (1<<1) /* HcDoneHead writeback */
240 #define OHCI_INTR_SF (1<<2) /* Start of frame */
241 #define OHCI_INTR_RD (1<<3) /* Resume detect */
242 #define OHCI_INTR_UE (1<<4) /* Unrecoverable error */
243 #define OHCI_INTR_FNO (1<<5) /* Frame number overflow */
244 #define OHCI_INTR_RHSC (1<<6) /* Root hub status change */
245 #define OHCI_INTR_OC (1<<30) /* Ownership change */
246 #define OHCI_INTR_MIE (1<<31) /* Master Interrupt Enable */
248 #define OHCI_HCCA_SIZE 0x100
249 #define OHCI_HCCA_MASK 0xffffff00
251 #define OHCI_EDPTR_MASK 0xfffffff0
253 #define OHCI_FMI_FI 0x00003fff
254 #define OHCI_FMI_FSMPS 0xffff0000
255 #define OHCI_FMI_FIT 0x80000000
257 #define OHCI_FR_RT (1<<31)
259 #define OHCI_LS_THRESH 0x628
261 #define OHCI_RHA_RW_MASK 0x00000000 /* Mask of supported features. */
262 #define OHCI_RHA_PSM (1<<8)
263 #define OHCI_RHA_NPS (1<<9)
264 #define OHCI_RHA_DT (1<<10)
265 #define OHCI_RHA_OCPM (1<<11)
266 #define OHCI_RHA_NOCP (1<<12)
267 #define OHCI_RHA_POTPGT_MASK 0xff000000
269 #define OHCI_RHS_LPS (1<<0)
270 #define OHCI_RHS_OCI (1<<1)
271 #define OHCI_RHS_DRWE (1<<15)
272 #define OHCI_RHS_LPSC (1<<16)
273 #define OHCI_RHS_OCIC (1<<17)
274 #define OHCI_RHS_CRWE (1<<31)
276 #define OHCI_PORT_CCS (1<<0)
277 #define OHCI_PORT_PES (1<<1)
278 #define OHCI_PORT_PSS (1<<2)
279 #define OHCI_PORT_POCI (1<<3)
280 #define OHCI_PORT_PRS (1<<4)
281 #define OHCI_PORT_PPS (1<<8)
282 #define OHCI_PORT_LSDA (1<<9)
283 #define OHCI_PORT_CSC (1<<16)
284 #define OHCI_PORT_PESC (1<<17)
285 #define OHCI_PORT_PSSC (1<<18)
286 #define OHCI_PORT_OCIC (1<<19)
287 #define OHCI_PORT_PRSC (1<<20)
288 #define OHCI_PORT_WTC (OHCI_PORT_CSC|OHCI_PORT_PESC|OHCI_PORT_PSSC \
289 |OHCI_PORT_OCIC|OHCI_PORT_PRSC)
291 #define OHCI_TD_DIR_SETUP 0x0
292 #define OHCI_TD_DIR_OUT 0x1
293 #define OHCI_TD_DIR_IN 0x2
294 #define OHCI_TD_DIR_RESERVED 0x3
296 #define OHCI_CC_NOERROR 0x0
297 #define OHCI_CC_CRC 0x1
298 #define OHCI_CC_BITSTUFFING 0x2
299 #define OHCI_CC_DATATOGGLEMISMATCH 0x3
300 #define OHCI_CC_STALL 0x4
301 #define OHCI_CC_DEVICENOTRESPONDING 0x5
302 #define OHCI_CC_PIDCHECKFAILURE 0x6
303 #define OHCI_CC_UNDEXPETEDPID 0x7
304 #define OHCI_CC_DATAOVERRUN 0x8
305 #define OHCI_CC_DATAUNDERRUN 0x9
306 #define OHCI_CC_BUFFEROVERRUN 0xc
307 #define OHCI_CC_BUFFERUNDERRUN 0xd
309 #define OHCI_HRESET_FSBIR (1 << 0)
311 /* Update IRQ levels */
312 static inline void ohci_intr_update(OHCIState
*ohci
)
316 if ((ohci
->intr
& OHCI_INTR_MIE
) &&
317 (ohci
->intr_status
& ohci
->intr
))
320 qemu_set_irq(ohci
->irq
, level
);
323 /* Set an interrupt */
324 static inline void ohci_set_interrupt(OHCIState
*ohci
, uint32_t intr
)
326 ohci
->intr_status
|= intr
;
327 ohci_intr_update(ohci
);
330 /* Attach or detach a device on a root hub port. */
331 static void ohci_attach(USBPort
*port1
)
333 OHCIState
*s
= port1
->opaque
;
334 OHCIPort
*port
= &s
->rhport
[port1
->index
];
335 uint32_t old_state
= port
->ctrl
;
337 /* set connect status */
338 port
->ctrl
|= OHCI_PORT_CCS
| OHCI_PORT_CSC
;
341 if (port
->port
.dev
->speed
== USB_SPEED_LOW
) {
342 port
->ctrl
|= OHCI_PORT_LSDA
;
344 port
->ctrl
&= ~OHCI_PORT_LSDA
;
347 /* notify of remote-wakeup */
348 if ((s
->ctl
& OHCI_CTL_HCFS
) == OHCI_USB_SUSPEND
) {
349 ohci_set_interrupt(s
, OHCI_INTR_RD
);
352 DPRINTF("usb-ohci: Attached port %d\n", port1
->index
);
354 if (old_state
!= port
->ctrl
) {
355 ohci_set_interrupt(s
, OHCI_INTR_RHSC
);
359 static void ohci_detach(USBPort
*port1
)
361 OHCIState
*s
= port1
->opaque
;
362 OHCIPort
*port
= &s
->rhport
[port1
->index
];
363 uint32_t old_state
= port
->ctrl
;
365 ohci_async_cancel_device(s
, port1
->dev
);
367 /* set connect status */
368 if (port
->ctrl
& OHCI_PORT_CCS
) {
369 port
->ctrl
&= ~OHCI_PORT_CCS
;
370 port
->ctrl
|= OHCI_PORT_CSC
;
373 if (port
->ctrl
& OHCI_PORT_PES
) {
374 port
->ctrl
&= ~OHCI_PORT_PES
;
375 port
->ctrl
|= OHCI_PORT_PESC
;
377 DPRINTF("usb-ohci: Detached port %d\n", port1
->index
);
379 if (old_state
!= port
->ctrl
) {
380 ohci_set_interrupt(s
, OHCI_INTR_RHSC
);
384 static void ohci_wakeup(USBPort
*port1
)
386 OHCIState
*s
= port1
->opaque
;
387 OHCIPort
*port
= &s
->rhport
[port1
->index
];
389 if (port
->ctrl
& OHCI_PORT_PSS
) {
390 DPRINTF("usb-ohci: port %d: wakeup\n", port1
->index
);
391 port
->ctrl
|= OHCI_PORT_PSSC
;
392 port
->ctrl
&= ~OHCI_PORT_PSS
;
393 intr
= OHCI_INTR_RHSC
;
395 /* Note that the controller can be suspended even if this port is not */
396 if ((s
->ctl
& OHCI_CTL_HCFS
) == OHCI_USB_SUSPEND
) {
397 DPRINTF("usb-ohci: remote-wakeup: SUSPEND->RESUME\n");
398 /* This is the one state transition the controller can do by itself */
399 s
->ctl
&= ~OHCI_CTL_HCFS
;
400 s
->ctl
|= OHCI_USB_RESUME
;
401 /* In suspend mode only ResumeDetected is possible, not RHSC:
402 * see the OHCI spec 5.1.2.3.
406 ohci_set_interrupt(s
, intr
);
409 static void ohci_child_detach(USBPort
*port1
, USBDevice
*child
)
411 OHCIState
*s
= port1
->opaque
;
413 ohci_async_cancel_device(s
, child
);
416 static USBDevice
*ohci_find_device(OHCIState
*ohci
, uint8_t addr
)
421 for (i
= 0; i
< ohci
->num_ports
; i
++) {
422 if ((ohci
->rhport
[i
].ctrl
& OHCI_PORT_PES
) == 0) {
425 dev
= usb_find_device(&ohci
->rhport
[i
].port
, addr
);
433 static void ohci_stop_endpoints(OHCIState
*ohci
)
438 for (i
= 0; i
< ohci
->num_ports
; i
++) {
439 dev
= ohci
->rhport
[i
].port
.dev
;
440 if (dev
&& dev
->attached
) {
441 usb_device_ep_stopped(dev
, &dev
->ep_ctl
);
442 for (j
= 0; j
< USB_MAX_ENDPOINTS
; j
++) {
443 usb_device_ep_stopped(dev
, &dev
->ep_in
[j
]);
444 usb_device_ep_stopped(dev
, &dev
->ep_out
[j
]);
450 /* Reset the controller */
451 static void ohci_reset(void *opaque
)
453 OHCIState
*ohci
= opaque
;
461 ohci
->intr_status
= 0;
462 ohci
->intr
= OHCI_INTR_MIE
;
465 ohci
->ctrl_head
= ohci
->ctrl_cur
= 0;
466 ohci
->bulk_head
= ohci
->bulk_cur
= 0;
469 ohci
->done_count
= 7;
471 /* FSMPS is marked TBD in OCHI 1.0, what gives ffs?
472 * I took the value linux sets ...
474 ohci
->fsmps
= 0x2778;
478 ohci
->frame_number
= 0;
480 ohci
->lst
= OHCI_LS_THRESH
;
482 ohci
->rhdesc_a
= OHCI_RHA_NPS
| ohci
->num_ports
;
483 ohci
->rhdesc_b
= 0x0; /* Impl. specific */
486 for (i
= 0; i
< ohci
->num_ports
; i
++)
488 port
= &ohci
->rhport
[i
];
490 if (port
->port
.dev
&& port
->port
.dev
->attached
) {
491 usb_port_reset(&port
->port
);
494 if (ohci
->async_td
) {
495 usb_cancel_packet(&ohci
->usb_packet
);
498 ohci_stop_endpoints(ohci
);
499 DPRINTF("usb-ohci: Reset %s\n", ohci
->name
);
502 /* Get an array of dwords from main memory */
503 static inline int get_dwords(OHCIState
*ohci
,
504 dma_addr_t addr
, uint32_t *buf
, int num
)
508 addr
+= ohci
->localmem_base
;
510 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
511 dma_memory_read(ohci
->dma
, addr
, buf
, sizeof(*buf
));
512 *buf
= le32_to_cpu(*buf
);
518 /* Put an array of dwords in to main memory */
519 static inline int put_dwords(OHCIState
*ohci
,
520 dma_addr_t addr
, uint32_t *buf
, int num
)
524 addr
+= ohci
->localmem_base
;
526 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
527 uint32_t tmp
= cpu_to_le32(*buf
);
528 dma_memory_write(ohci
->dma
, addr
, &tmp
, sizeof(tmp
));
534 /* Get an array of words from main memory */
535 static inline int get_words(OHCIState
*ohci
,
536 dma_addr_t addr
, uint16_t *buf
, int num
)
540 addr
+= ohci
->localmem_base
;
542 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
543 dma_memory_read(ohci
->dma
, addr
, buf
, sizeof(*buf
));
544 *buf
= le16_to_cpu(*buf
);
550 /* Put an array of words in to main memory */
551 static inline int put_words(OHCIState
*ohci
,
552 dma_addr_t addr
, uint16_t *buf
, int num
)
556 addr
+= ohci
->localmem_base
;
558 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
559 uint16_t tmp
= cpu_to_le16(*buf
);
560 dma_memory_write(ohci
->dma
, addr
, &tmp
, sizeof(tmp
));
566 static inline int ohci_read_ed(OHCIState
*ohci
,
567 dma_addr_t addr
, struct ohci_ed
*ed
)
569 return get_dwords(ohci
, addr
, (uint32_t *)ed
, sizeof(*ed
) >> 2);
572 static inline int ohci_read_td(OHCIState
*ohci
,
573 dma_addr_t addr
, struct ohci_td
*td
)
575 return get_dwords(ohci
, addr
, (uint32_t *)td
, sizeof(*td
) >> 2);
578 static inline int ohci_read_iso_td(OHCIState
*ohci
,
579 dma_addr_t addr
, struct ohci_iso_td
*td
)
581 return (get_dwords(ohci
, addr
, (uint32_t *)td
, 4) &&
582 get_words(ohci
, addr
+ 16, td
->offset
, 8));
585 static inline int ohci_read_hcca(OHCIState
*ohci
,
586 dma_addr_t addr
, struct ohci_hcca
*hcca
)
588 dma_memory_read(ohci
->dma
, addr
+ ohci
->localmem_base
, hcca
, sizeof(*hcca
));
592 static inline int ohci_put_ed(OHCIState
*ohci
,
593 dma_addr_t addr
, struct ohci_ed
*ed
)
595 /* ed->tail is under control of the HCD.
596 * Since just ed->head is changed by HC, just write back this
599 return put_dwords(ohci
, addr
+ ED_WBACK_OFFSET
,
600 (uint32_t *)((char *)ed
+ ED_WBACK_OFFSET
),
604 static inline int ohci_put_td(OHCIState
*ohci
,
605 dma_addr_t addr
, struct ohci_td
*td
)
607 return put_dwords(ohci
, addr
, (uint32_t *)td
, sizeof(*td
) >> 2);
610 static inline int ohci_put_iso_td(OHCIState
*ohci
,
611 dma_addr_t addr
, struct ohci_iso_td
*td
)
613 return (put_dwords(ohci
, addr
, (uint32_t *)td
, 4) &&
614 put_words(ohci
, addr
+ 16, td
->offset
, 8));
617 static inline int ohci_put_hcca(OHCIState
*ohci
,
618 dma_addr_t addr
, struct ohci_hcca
*hcca
)
620 dma_memory_write(ohci
->dma
,
621 addr
+ ohci
->localmem_base
+ HCCA_WRITEBACK_OFFSET
,
622 (char *)hcca
+ HCCA_WRITEBACK_OFFSET
,
623 HCCA_WRITEBACK_SIZE
);
627 /* Read/Write the contents of a TD from/to main memory. */
628 static void ohci_copy_td(OHCIState
*ohci
, struct ohci_td
*td
,
629 uint8_t *buf
, int len
, DMADirection dir
)
634 n
= 0x1000 - (ptr
& 0xfff);
637 dma_memory_rw(ohci
->dma
, ptr
+ ohci
->localmem_base
, buf
, n
, dir
);
640 ptr
= td
->be
& ~0xfffu
;
642 dma_memory_rw(ohci
->dma
, ptr
+ ohci
->localmem_base
, buf
, len
- n
, dir
);
645 /* Read/Write the contents of an ISO TD from/to main memory. */
646 static void ohci_copy_iso_td(OHCIState
*ohci
,
647 uint32_t start_addr
, uint32_t end_addr
,
648 uint8_t *buf
, int len
, DMADirection dir
)
653 n
= 0x1000 - (ptr
& 0xfff);
656 dma_memory_rw(ohci
->dma
, ptr
+ ohci
->localmem_base
, buf
, n
, dir
);
659 ptr
= end_addr
& ~0xfffu
;
661 dma_memory_rw(ohci
->dma
, ptr
+ ohci
->localmem_base
, buf
, len
- n
, dir
);
664 static void ohci_process_lists(OHCIState
*ohci
, int completion
);
666 static void ohci_async_complete_packet(USBPort
*port
, USBPacket
*packet
)
668 OHCIState
*ohci
= container_of(packet
, OHCIState
, usb_packet
);
670 DPRINTF("Async packet complete\n");
672 ohci
->async_complete
= 1;
673 ohci_process_lists(ohci
, 1);
676 #define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
678 static int ohci_service_iso_td(OHCIState
*ohci
, struct ohci_ed
*ed
,
684 const char *str
= NULL
;
691 struct ohci_iso_td iso_td
;
693 uint16_t starting_frame
;
694 int16_t relative_frame_number
;
696 uint32_t start_offset
, next_offset
, end_offset
= 0;
697 uint32_t start_addr
, end_addr
;
699 addr
= ed
->head
& OHCI_DPTR_MASK
;
701 if (!ohci_read_iso_td(ohci
, addr
, &iso_td
)) {
702 printf("usb-ohci: ISO_TD read error at %x\n", addr
);
706 starting_frame
= OHCI_BM(iso_td
.flags
, TD_SF
);
707 frame_count
= OHCI_BM(iso_td
.flags
, TD_FC
);
708 relative_frame_number
= USUB(ohci
->frame_number
, starting_frame
);
711 printf("--- ISO_TD ED head 0x%.8x tailp 0x%.8x\n"
712 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
713 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
714 "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
715 "frame_number 0x%.8x starting_frame 0x%.8x\n"
716 "frame_count 0x%.8x relative %d\n"
717 "di 0x%.8x cc 0x%.8x\n",
718 ed
->head
& OHCI_DPTR_MASK
, ed
->tail
& OHCI_DPTR_MASK
,
719 iso_td
.flags
, iso_td
.bp
, iso_td
.next
, iso_td
.be
,
720 iso_td
.offset
[0], iso_td
.offset
[1], iso_td
.offset
[2], iso_td
.offset
[3],
721 iso_td
.offset
[4], iso_td
.offset
[5], iso_td
.offset
[6], iso_td
.offset
[7],
722 ohci
->frame_number
, starting_frame
,
723 frame_count
, relative_frame_number
,
724 OHCI_BM(iso_td
.flags
, TD_DI
), OHCI_BM(iso_td
.flags
, TD_CC
));
727 if (relative_frame_number
< 0) {
728 DPRINTF("usb-ohci: ISO_TD R=%d < 0\n", relative_frame_number
);
730 } else if (relative_frame_number
> frame_count
) {
731 /* ISO TD expired - retire the TD to the Done Queue and continue with
732 the next ISO TD of the same ED */
733 DPRINTF("usb-ohci: ISO_TD R=%d > FC=%d\n", relative_frame_number
,
735 OHCI_SET_BM(iso_td
.flags
, TD_CC
, OHCI_CC_DATAOVERRUN
);
736 ed
->head
&= ~OHCI_DPTR_MASK
;
737 ed
->head
|= (iso_td
.next
& OHCI_DPTR_MASK
);
738 iso_td
.next
= ohci
->done
;
740 i
= OHCI_BM(iso_td
.flags
, TD_DI
);
741 if (i
< ohci
->done_count
)
742 ohci
->done_count
= i
;
743 ohci_put_iso_td(ohci
, addr
, &iso_td
);
747 dir
= OHCI_BM(ed
->flags
, ED_D
);
755 case OHCI_TD_DIR_OUT
:
761 case OHCI_TD_DIR_SETUP
:
765 pid
= USB_TOKEN_SETUP
;
768 printf("usb-ohci: Bad direction %d\n", dir
);
772 if (!iso_td
.bp
|| !iso_td
.be
) {
773 printf("usb-ohci: ISO_TD bp 0x%.8x be 0x%.8x\n", iso_td
.bp
, iso_td
.be
);
777 start_offset
= iso_td
.offset
[relative_frame_number
];
778 next_offset
= iso_td
.offset
[relative_frame_number
+ 1];
780 if (!(OHCI_BM(start_offset
, TD_PSW_CC
) & 0xe) ||
781 ((relative_frame_number
< frame_count
) &&
782 !(OHCI_BM(next_offset
, TD_PSW_CC
) & 0xe))) {
783 printf("usb-ohci: ISO_TD cc != not accessed 0x%.8x 0x%.8x\n",
784 start_offset
, next_offset
);
788 if ((relative_frame_number
< frame_count
) && (start_offset
> next_offset
)) {
789 printf("usb-ohci: ISO_TD start_offset=0x%.8x > next_offset=0x%.8x\n",
790 start_offset
, next_offset
);
794 if ((start_offset
& 0x1000) == 0) {
795 start_addr
= (iso_td
.bp
& OHCI_PAGE_MASK
) |
796 (start_offset
& OHCI_OFFSET_MASK
);
798 start_addr
= (iso_td
.be
& OHCI_PAGE_MASK
) |
799 (start_offset
& OHCI_OFFSET_MASK
);
802 if (relative_frame_number
< frame_count
) {
803 end_offset
= next_offset
- 1;
804 if ((end_offset
& 0x1000) == 0) {
805 end_addr
= (iso_td
.bp
& OHCI_PAGE_MASK
) |
806 (end_offset
& OHCI_OFFSET_MASK
);
808 end_addr
= (iso_td
.be
& OHCI_PAGE_MASK
) |
809 (end_offset
& OHCI_OFFSET_MASK
);
812 /* Last packet in the ISO TD */
813 end_addr
= iso_td
.be
;
816 if ((start_addr
& OHCI_PAGE_MASK
) != (end_addr
& OHCI_PAGE_MASK
)) {
817 len
= (end_addr
& OHCI_OFFSET_MASK
) + 0x1001
818 - (start_addr
& OHCI_OFFSET_MASK
);
820 len
= end_addr
- start_addr
+ 1;
823 if (len
&& dir
!= OHCI_TD_DIR_IN
) {
824 ohci_copy_iso_td(ohci
, start_addr
, end_addr
, ohci
->usb_buf
, len
,
825 DMA_DIRECTION_TO_DEVICE
);
829 bool int_req
= relative_frame_number
== frame_count
&&
830 OHCI_BM(iso_td
.flags
, TD_DI
) == 0;
831 dev
= ohci_find_device(ohci
, OHCI_BM(ed
->flags
, ED_FA
));
832 ep
= usb_ep_get(dev
, pid
, OHCI_BM(ed
->flags
, ED_EN
));
833 usb_packet_setup(&ohci
->usb_packet
, pid
, ep
, 0, addr
, false, int_req
);
834 usb_packet_addbuf(&ohci
->usb_packet
, ohci
->usb_buf
, len
);
835 usb_handle_packet(dev
, &ohci
->usb_packet
);
836 if (ohci
->usb_packet
.status
== USB_RET_ASYNC
) {
837 usb_device_flush_ep_queue(dev
, ep
);
841 if (ohci
->usb_packet
.status
== USB_RET_SUCCESS
) {
842 ret
= ohci
->usb_packet
.actual_length
;
844 ret
= ohci
->usb_packet
.status
;
848 printf("so 0x%.8x eo 0x%.8x\nsa 0x%.8x ea 0x%.8x\ndir %s len %zu ret %d\n",
849 start_offset
, end_offset
, start_addr
, end_addr
, str
, len
, ret
);
853 if (dir
== OHCI_TD_DIR_IN
&& ret
>= 0 && ret
<= len
) {
854 /* IN transfer succeeded */
855 ohci_copy_iso_td(ohci
, start_addr
, end_addr
, ohci
->usb_buf
, ret
,
856 DMA_DIRECTION_FROM_DEVICE
);
857 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
859 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
, ret
);
860 } else if (dir
== OHCI_TD_DIR_OUT
&& ret
== len
) {
861 /* OUT transfer succeeded */
862 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
864 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
, 0);
866 if (ret
> (ssize_t
) len
) {
867 printf("usb-ohci: DataOverrun %d > %zu\n", ret
, len
);
868 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
869 OHCI_CC_DATAOVERRUN
);
870 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
872 } else if (ret
>= 0) {
873 printf("usb-ohci: DataUnderrun %d\n", ret
);
874 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
875 OHCI_CC_DATAUNDERRUN
);
878 case USB_RET_IOERROR
:
880 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
881 OHCI_CC_DEVICENOTRESPONDING
);
882 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
887 printf("usb-ohci: got NAK/STALL %d\n", ret
);
888 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
890 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
894 printf("usb-ohci: Bad device response %d\n", ret
);
895 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
896 OHCI_CC_UNDEXPETEDPID
);
902 if (relative_frame_number
== frame_count
) {
903 /* Last data packet of ISO TD - retire the TD to the Done Queue */
904 OHCI_SET_BM(iso_td
.flags
, TD_CC
, OHCI_CC_NOERROR
);
905 ed
->head
&= ~OHCI_DPTR_MASK
;
906 ed
->head
|= (iso_td
.next
& OHCI_DPTR_MASK
);
907 iso_td
.next
= ohci
->done
;
909 i
= OHCI_BM(iso_td
.flags
, TD_DI
);
910 if (i
< ohci
->done_count
)
911 ohci
->done_count
= i
;
913 ohci_put_iso_td(ohci
, addr
, &iso_td
);
917 /* Service a transport descriptor.
918 Returns nonzero to terminate processing of this endpoint. */
920 static int ohci_service_td(OHCIState
*ohci
, struct ohci_ed
*ed
)
923 size_t len
= 0, pktlen
= 0;
925 const char *str
= NULL
;
937 addr
= ed
->head
& OHCI_DPTR_MASK
;
938 /* See if this TD has already been submitted to the device. */
939 completion
= (addr
== ohci
->async_td
);
940 if (completion
&& !ohci
->async_complete
) {
942 DPRINTF("Skipping async TD\n");
946 if (!ohci_read_td(ohci
, addr
, &td
)) {
947 fprintf(stderr
, "usb-ohci: TD read error at %x\n", addr
);
951 dir
= OHCI_BM(ed
->flags
, ED_D
);
953 case OHCI_TD_DIR_OUT
:
958 dir
= OHCI_BM(td
.flags
, TD_DP
);
969 case OHCI_TD_DIR_OUT
:
975 case OHCI_TD_DIR_SETUP
:
979 pid
= USB_TOKEN_SETUP
;
982 fprintf(stderr
, "usb-ohci: Bad direction\n");
985 if (td
.cbp
&& td
.be
) {
986 if ((td
.cbp
& 0xfffff000) != (td
.be
& 0xfffff000)) {
987 len
= (td
.be
& 0xfff) + 0x1001 - (td
.cbp
& 0xfff);
989 len
= (td
.be
- td
.cbp
) + 1;
993 if (len
&& dir
!= OHCI_TD_DIR_IN
) {
994 /* The endpoint may not allow us to transfer it all now */
995 pktlen
= (ed
->flags
& OHCI_ED_MPS_MASK
) >> OHCI_ED_MPS_SHIFT
;
1000 ohci_copy_td(ohci
, &td
, ohci
->usb_buf
, pktlen
,
1001 DMA_DIRECTION_TO_DEVICE
);
1006 flag_r
= (td
.flags
& OHCI_TD_R
) != 0;
1008 DPRINTF(" TD @ 0x%.8x %" PRId64
" of %" PRId64
1009 " bytes %s r=%d cbp=0x%.8x be=0x%.8x\n",
1010 addr
, (int64_t)pktlen
, (int64_t)len
, str
, flag_r
, td
.cbp
, td
.be
);
1012 if (pktlen
> 0 && dir
!= OHCI_TD_DIR_IN
) {
1014 for (i
= 0; i
< pktlen
; i
++) {
1015 printf(" %.2x", ohci
->usb_buf
[i
]);
1022 ohci
->async_complete
= 0;
1024 if (ohci
->async_td
) {
1025 /* ??? The hardware should allow one active packet per
1026 endpoint. We only allow one active packet per controller.
1027 This should be sufficient as long as devices respond in a
1031 DPRINTF("Too many pending packets\n");
1035 dev
= ohci_find_device(ohci
, OHCI_BM(ed
->flags
, ED_FA
));
1036 ep
= usb_ep_get(dev
, pid
, OHCI_BM(ed
->flags
, ED_EN
));
1037 usb_packet_setup(&ohci
->usb_packet
, pid
, ep
, 0, addr
, !flag_r
,
1038 OHCI_BM(td
.flags
, TD_DI
) == 0);
1039 usb_packet_addbuf(&ohci
->usb_packet
, ohci
->usb_buf
, pktlen
);
1040 usb_handle_packet(dev
, &ohci
->usb_packet
);
1042 DPRINTF("status=%d\n", ohci
->usb_packet
.status
);
1044 if (ohci
->usb_packet
.status
== USB_RET_ASYNC
) {
1045 usb_device_flush_ep_queue(dev
, ep
);
1046 ohci
->async_td
= addr
;
1050 if (ohci
->usb_packet
.status
== USB_RET_SUCCESS
) {
1051 ret
= ohci
->usb_packet
.actual_length
;
1053 ret
= ohci
->usb_packet
.status
;
1057 if (dir
== OHCI_TD_DIR_IN
) {
1058 ohci_copy_td(ohci
, &td
, ohci
->usb_buf
, ret
,
1059 DMA_DIRECTION_FROM_DEVICE
);
1062 for (i
= 0; i
< ret
; i
++)
1063 printf(" %.2x", ohci
->usb_buf
[i
]);
1072 if (ret
== pktlen
|| (dir
== OHCI_TD_DIR_IN
&& ret
>= 0 && flag_r
)) {
1073 /* Transmission succeeded. */
1077 if ((td
.cbp
& 0xfff) + ret
> 0xfff) {
1078 td
.cbp
= (td
.be
& ~0xfff) + ((td
.cbp
+ ret
) & 0xfff);
1083 td
.flags
|= OHCI_TD_T1
;
1084 td
.flags
^= OHCI_TD_T0
;
1085 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_NOERROR
);
1086 OHCI_SET_BM(td
.flags
, TD_EC
, 0);
1088 if ((dir
!= OHCI_TD_DIR_IN
) && (ret
!= len
)) {
1089 /* Partial packet transfer: TD not ready to retire yet */
1090 goto exit_no_retire
;
1093 /* Setting ED_C is part of the TD retirement process */
1094 ed
->head
&= ~OHCI_ED_C
;
1095 if (td
.flags
& OHCI_TD_T0
)
1096 ed
->head
|= OHCI_ED_C
;
1099 DPRINTF("usb-ohci: Underrun\n");
1100 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DATAUNDERRUN
);
1103 case USB_RET_IOERROR
:
1105 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DEVICENOTRESPONDING
);
1107 DPRINTF("usb-ohci: got NAK\n");
1110 DPRINTF("usb-ohci: got STALL\n");
1111 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_STALL
);
1113 case USB_RET_BABBLE
:
1114 DPRINTF("usb-ohci: got BABBLE\n");
1115 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DATAOVERRUN
);
1118 fprintf(stderr
, "usb-ohci: Bad device response %d\n", ret
);
1119 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_UNDEXPETEDPID
);
1120 OHCI_SET_BM(td
.flags
, TD_EC
, 3);
1124 ed
->head
|= OHCI_ED_H
;
1127 /* Retire this TD */
1128 ed
->head
&= ~OHCI_DPTR_MASK
;
1129 ed
->head
|= td
.next
& OHCI_DPTR_MASK
;
1130 td
.next
= ohci
->done
;
1132 i
= OHCI_BM(td
.flags
, TD_DI
);
1133 if (i
< ohci
->done_count
)
1134 ohci
->done_count
= i
;
1136 ohci_put_td(ohci
, addr
, &td
);
1137 return OHCI_BM(td
.flags
, TD_CC
) != OHCI_CC_NOERROR
;
1140 /* Service an endpoint list. Returns nonzero if active TD were found. */
1141 static int ohci_service_ed_list(OHCIState
*ohci
, uint32_t head
, int completion
)
1153 for (cur
= head
; cur
; cur
= next_ed
) {
1154 if (!ohci_read_ed(ohci
, cur
, &ed
)) {
1155 fprintf(stderr
, "usb-ohci: ED read error at %x\n", cur
);
1159 next_ed
= ed
.next
& OHCI_DPTR_MASK
;
1161 if ((ed
.head
& OHCI_ED_H
) || (ed
.flags
& OHCI_ED_K
)) {
1163 /* Cancel pending packets for ED that have been paused. */
1164 addr
= ed
.head
& OHCI_DPTR_MASK
;
1165 if (ohci
->async_td
&& addr
== ohci
->async_td
) {
1166 usb_cancel_packet(&ohci
->usb_packet
);
1168 usb_device_ep_stopped(ohci
->usb_packet
.ep
->dev
,
1169 ohci
->usb_packet
.ep
);
1174 while ((ed
.head
& OHCI_DPTR_MASK
) != ed
.tail
) {
1176 DPRINTF("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u "
1177 "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur
,
1178 OHCI_BM(ed
.flags
, ED_FA
), OHCI_BM(ed
.flags
, ED_EN
),
1179 OHCI_BM(ed
.flags
, ED_D
), (ed
.flags
& OHCI_ED_S
)!= 0,
1180 (ed
.flags
& OHCI_ED_K
) != 0, (ed
.flags
& OHCI_ED_F
) != 0,
1181 OHCI_BM(ed
.flags
, ED_MPS
), (ed
.head
& OHCI_ED_H
) != 0,
1182 (ed
.head
& OHCI_ED_C
) != 0, ed
.head
& OHCI_DPTR_MASK
,
1183 ed
.tail
& OHCI_DPTR_MASK
, ed
.next
& OHCI_DPTR_MASK
);
1187 if ((ed
.flags
& OHCI_ED_F
) == 0) {
1188 if (ohci_service_td(ohci
, &ed
))
1191 /* Handle isochronous endpoints */
1192 if (ohci_service_iso_td(ohci
, &ed
, completion
))
1197 ohci_put_ed(ohci
, cur
, &ed
);
1203 /* Generate a SOF event, and set a timer for EOF */
1204 static void ohci_sof(OHCIState
*ohci
)
1206 ohci
->sof_time
= qemu_get_clock_ns(vm_clock
);
1207 qemu_mod_timer(ohci
->eof_timer
, ohci
->sof_time
+ usb_frame_time
);
1208 ohci_set_interrupt(ohci
, OHCI_INTR_SF
);
1211 /* Process Control and Bulk lists. */
1212 static void ohci_process_lists(OHCIState
*ohci
, int completion
)
1214 if ((ohci
->ctl
& OHCI_CTL_CLE
) && (ohci
->status
& OHCI_STATUS_CLF
)) {
1215 if (ohci
->ctrl_cur
&& ohci
->ctrl_cur
!= ohci
->ctrl_head
) {
1216 DPRINTF("usb-ohci: head %x, cur %x\n",
1217 ohci
->ctrl_head
, ohci
->ctrl_cur
);
1219 if (!ohci_service_ed_list(ohci
, ohci
->ctrl_head
, completion
)) {
1221 ohci
->status
&= ~OHCI_STATUS_CLF
;
1225 if ((ohci
->ctl
& OHCI_CTL_BLE
) && (ohci
->status
& OHCI_STATUS_BLF
)) {
1226 if (!ohci_service_ed_list(ohci
, ohci
->bulk_head
, completion
)) {
1228 ohci
->status
&= ~OHCI_STATUS_BLF
;
1233 /* Do frame processing on frame boundary */
1234 static void ohci_frame_boundary(void *opaque
)
1236 OHCIState
*ohci
= opaque
;
1237 struct ohci_hcca hcca
;
1239 ohci_read_hcca(ohci
, ohci
->hcca
, &hcca
);
1241 /* Process all the lists at the end of the frame */
1242 if (ohci
->ctl
& OHCI_CTL_PLE
) {
1245 n
= ohci
->frame_number
& 0x1f;
1246 ohci_service_ed_list(ohci
, le32_to_cpu(hcca
.intr
[n
]), 0);
1249 /* Cancel all pending packets if either of the lists has been disabled. */
1250 if (ohci
->old_ctl
& (~ohci
->ctl
) & (OHCI_CTL_BLE
| OHCI_CTL_CLE
)) {
1251 if (ohci
->async_td
) {
1252 usb_cancel_packet(&ohci
->usb_packet
);
1255 ohci_stop_endpoints(ohci
);
1257 ohci
->old_ctl
= ohci
->ctl
;
1258 ohci_process_lists(ohci
, 0);
1260 /* Frame boundary, so do EOF stuf here */
1261 ohci
->frt
= ohci
->fit
;
1263 /* Increment frame number and take care of endianness. */
1264 ohci
->frame_number
= (ohci
->frame_number
+ 1) & 0xffff;
1265 hcca
.frame
= cpu_to_le16(ohci
->frame_number
);
1267 if (ohci
->done_count
== 0 && !(ohci
->intr_status
& OHCI_INTR_WD
)) {
1270 if (ohci
->intr
& ohci
->intr_status
)
1272 hcca
.done
= cpu_to_le32(ohci
->done
);
1274 ohci
->done_count
= 7;
1275 ohci_set_interrupt(ohci
, OHCI_INTR_WD
);
1278 if (ohci
->done_count
!= 7 && ohci
->done_count
!= 0)
1281 /* Do SOF stuff here */
1284 /* Writeback HCCA */
1285 ohci_put_hcca(ohci
, ohci
->hcca
, &hcca
);
1288 /* Start sending SOF tokens across the USB bus, lists are processed in
1291 static int ohci_bus_start(OHCIState
*ohci
)
1293 ohci
->eof_timer
= qemu_new_timer_ns(vm_clock
,
1294 ohci_frame_boundary
,
1297 if (ohci
->eof_timer
== NULL
) {
1298 fprintf(stderr
, "usb-ohci: %s: qemu_new_timer_ns failed\n", ohci
->name
);
1299 /* TODO: Signal unrecoverable error */
1303 DPRINTF("usb-ohci: %s: USB Operational\n", ohci
->name
);
1310 /* Stop sending SOF tokens on the bus */
1311 static void ohci_bus_stop(OHCIState
*ohci
)
1313 if (ohci
->eof_timer
)
1314 qemu_del_timer(ohci
->eof_timer
);
1315 ohci
->eof_timer
= NULL
;
1318 /* Sets a flag in a port status register but only set it if the port is
1319 * connected, if not set ConnectStatusChange flag. If flag is enabled
1322 static int ohci_port_set_if_connected(OHCIState
*ohci
, int i
, uint32_t val
)
1326 /* writing a 0 has no effect */
1330 /* If CurrentConnectStatus is cleared we set
1331 * ConnectStatusChange
1333 if (!(ohci
->rhport
[i
].ctrl
& OHCI_PORT_CCS
)) {
1334 ohci
->rhport
[i
].ctrl
|= OHCI_PORT_CSC
;
1335 if (ohci
->rhstatus
& OHCI_RHS_DRWE
) {
1336 /* TODO: CSC is a wakeup event */
1341 if (ohci
->rhport
[i
].ctrl
& val
)
1345 ohci
->rhport
[i
].ctrl
|= val
;
1350 /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
1351 static void ohci_set_frame_interval(OHCIState
*ohci
, uint16_t val
)
1355 if (val
!= ohci
->fi
) {
1356 DPRINTF("usb-ohci: %s: FrameInterval = 0x%x (%u)\n",
1357 ohci
->name
, ohci
->fi
, ohci
->fi
);
1363 static void ohci_port_power(OHCIState
*ohci
, int i
, int p
)
1366 ohci
->rhport
[i
].ctrl
|= OHCI_PORT_PPS
;
1368 ohci
->rhport
[i
].ctrl
&= ~(OHCI_PORT_PPS
|
1375 /* Set HcControlRegister */
1376 static void ohci_set_ctl(OHCIState
*ohci
, uint32_t val
)
1381 old_state
= ohci
->ctl
& OHCI_CTL_HCFS
;
1383 new_state
= ohci
->ctl
& OHCI_CTL_HCFS
;
1385 /* no state change */
1386 if (old_state
== new_state
)
1389 switch (new_state
) {
1390 case OHCI_USB_OPERATIONAL
:
1391 ohci_bus_start(ohci
);
1393 case OHCI_USB_SUSPEND
:
1394 ohci_bus_stop(ohci
);
1395 DPRINTF("usb-ohci: %s: USB Suspended\n", ohci
->name
);
1397 case OHCI_USB_RESUME
:
1398 DPRINTF("usb-ohci: %s: USB Resume\n", ohci
->name
);
1400 case OHCI_USB_RESET
:
1402 DPRINTF("usb-ohci: %s: USB Reset\n", ohci
->name
);
1407 static uint32_t ohci_get_frame_remaining(OHCIState
*ohci
)
1412 if ((ohci
->ctl
& OHCI_CTL_HCFS
) != OHCI_USB_OPERATIONAL
)
1413 return (ohci
->frt
<< 31);
1415 /* Being in USB operational state guarnatees sof_time was
1418 tks
= qemu_get_clock_ns(vm_clock
) - ohci
->sof_time
;
1420 /* avoid muldiv if possible */
1421 if (tks
>= usb_frame_time
)
1422 return (ohci
->frt
<< 31);
1424 tks
= muldiv64(1, tks
, usb_bit_time
);
1425 fr
= (uint16_t)(ohci
->fi
- tks
);
1427 return (ohci
->frt
<< 31) | fr
;
1431 /* Set root hub status */
1432 static void ohci_set_hub_status(OHCIState
*ohci
, uint32_t val
)
1436 old_state
= ohci
->rhstatus
;
1438 /* write 1 to clear OCIC */
1439 if (val
& OHCI_RHS_OCIC
)
1440 ohci
->rhstatus
&= ~OHCI_RHS_OCIC
;
1442 if (val
& OHCI_RHS_LPS
) {
1445 for (i
= 0; i
< ohci
->num_ports
; i
++)
1446 ohci_port_power(ohci
, i
, 0);
1447 DPRINTF("usb-ohci: powered down all ports\n");
1450 if (val
& OHCI_RHS_LPSC
) {
1453 for (i
= 0; i
< ohci
->num_ports
; i
++)
1454 ohci_port_power(ohci
, i
, 1);
1455 DPRINTF("usb-ohci: powered up all ports\n");
1458 if (val
& OHCI_RHS_DRWE
)
1459 ohci
->rhstatus
|= OHCI_RHS_DRWE
;
1461 if (val
& OHCI_RHS_CRWE
)
1462 ohci
->rhstatus
&= ~OHCI_RHS_DRWE
;
1464 if (old_state
!= ohci
->rhstatus
)
1465 ohci_set_interrupt(ohci
, OHCI_INTR_RHSC
);
1468 /* Set root hub port status */
1469 static void ohci_port_set_status(OHCIState
*ohci
, int portnum
, uint32_t val
)
1474 port
= &ohci
->rhport
[portnum
];
1475 old_state
= port
->ctrl
;
1477 /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
1478 if (val
& OHCI_PORT_WTC
)
1479 port
->ctrl
&= ~(val
& OHCI_PORT_WTC
);
1481 if (val
& OHCI_PORT_CCS
)
1482 port
->ctrl
&= ~OHCI_PORT_PES
;
1484 ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PES
);
1486 if (ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PSS
)) {
1487 DPRINTF("usb-ohci: port %d: SUSPEND\n", portnum
);
1490 if (ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PRS
)) {
1491 DPRINTF("usb-ohci: port %d: RESET\n", portnum
);
1492 usb_device_reset(port
->port
.dev
);
1493 port
->ctrl
&= ~OHCI_PORT_PRS
;
1494 /* ??? Should this also set OHCI_PORT_PESC. */
1495 port
->ctrl
|= OHCI_PORT_PES
| OHCI_PORT_PRSC
;
1498 /* Invert order here to ensure in ambiguous case, device is
1501 if (val
& OHCI_PORT_LSDA
)
1502 ohci_port_power(ohci
, portnum
, 0);
1503 if (val
& OHCI_PORT_PPS
)
1504 ohci_port_power(ohci
, portnum
, 1);
1506 if (old_state
!= port
->ctrl
)
1507 ohci_set_interrupt(ohci
, OHCI_INTR_RHSC
);
1510 static uint64_t ohci_mem_read(void *opaque
,
1514 OHCIState
*ohci
= opaque
;
1517 /* Only aligned reads are allowed on OHCI */
1519 fprintf(stderr
, "usb-ohci: Mis-aligned read\n");
1521 } else if (addr
>= 0x54 && addr
< 0x54 + ohci
->num_ports
* 4) {
1522 /* HcRhPortStatus */
1523 retval
= ohci
->rhport
[(addr
- 0x54) >> 2].ctrl
| OHCI_PORT_PPS
;
1525 switch (addr
>> 2) {
1526 case 0: /* HcRevision */
1530 case 1: /* HcControl */
1534 case 2: /* HcCommandStatus */
1535 retval
= ohci
->status
;
1538 case 3: /* HcInterruptStatus */
1539 retval
= ohci
->intr_status
;
1542 case 4: /* HcInterruptEnable */
1543 case 5: /* HcInterruptDisable */
1544 retval
= ohci
->intr
;
1547 case 6: /* HcHCCA */
1548 retval
= ohci
->hcca
;
1551 case 7: /* HcPeriodCurrentED */
1552 retval
= ohci
->per_cur
;
1555 case 8: /* HcControlHeadED */
1556 retval
= ohci
->ctrl_head
;
1559 case 9: /* HcControlCurrentED */
1560 retval
= ohci
->ctrl_cur
;
1563 case 10: /* HcBulkHeadED */
1564 retval
= ohci
->bulk_head
;
1567 case 11: /* HcBulkCurrentED */
1568 retval
= ohci
->bulk_cur
;
1571 case 12: /* HcDoneHead */
1572 retval
= ohci
->done
;
1575 case 13: /* HcFmInterretval */
1576 retval
= (ohci
->fit
<< 31) | (ohci
->fsmps
<< 16) | (ohci
->fi
);
1579 case 14: /* HcFmRemaining */
1580 retval
= ohci_get_frame_remaining(ohci
);
1583 case 15: /* HcFmNumber */
1584 retval
= ohci
->frame_number
;
1587 case 16: /* HcPeriodicStart */
1588 retval
= ohci
->pstart
;
1591 case 17: /* HcLSThreshold */
1595 case 18: /* HcRhDescriptorA */
1596 retval
= ohci
->rhdesc_a
;
1599 case 19: /* HcRhDescriptorB */
1600 retval
= ohci
->rhdesc_b
;
1603 case 20: /* HcRhStatus */
1604 retval
= ohci
->rhstatus
;
1607 /* PXA27x specific registers */
1608 case 24: /* HcStatus */
1609 retval
= ohci
->hstatus
& ohci
->hmask
;
1612 case 25: /* HcHReset */
1613 retval
= ohci
->hreset
;
1616 case 26: /* HcHInterruptEnable */
1617 retval
= ohci
->hmask
;
1620 case 27: /* HcHInterruptTest */
1621 retval
= ohci
->htest
;
1625 fprintf(stderr
, "ohci_read: Bad offset %x\n", (int)addr
);
1626 retval
= 0xffffffff;
1633 static void ohci_mem_write(void *opaque
,
1638 OHCIState
*ohci
= opaque
;
1640 /* Only aligned reads are allowed on OHCI */
1642 fprintf(stderr
, "usb-ohci: Mis-aligned write\n");
1646 if (addr
>= 0x54 && addr
< 0x54 + ohci
->num_ports
* 4) {
1647 /* HcRhPortStatus */
1648 ohci_port_set_status(ohci
, (addr
- 0x54) >> 2, val
);
1652 switch (addr
>> 2) {
1653 case 1: /* HcControl */
1654 ohci_set_ctl(ohci
, val
);
1657 case 2: /* HcCommandStatus */
1658 /* SOC is read-only */
1659 val
= (val
& ~OHCI_STATUS_SOC
);
1661 /* Bits written as '0' remain unchanged in the register */
1662 ohci
->status
|= val
;
1664 if (ohci
->status
& OHCI_STATUS_HCR
)
1668 case 3: /* HcInterruptStatus */
1669 ohci
->intr_status
&= ~val
;
1670 ohci_intr_update(ohci
);
1673 case 4: /* HcInterruptEnable */
1675 ohci_intr_update(ohci
);
1678 case 5: /* HcInterruptDisable */
1680 ohci_intr_update(ohci
);
1683 case 6: /* HcHCCA */
1684 ohci
->hcca
= val
& OHCI_HCCA_MASK
;
1687 case 7: /* HcPeriodCurrentED */
1688 /* Ignore writes to this read-only register, Linux does them */
1691 case 8: /* HcControlHeadED */
1692 ohci
->ctrl_head
= val
& OHCI_EDPTR_MASK
;
1695 case 9: /* HcControlCurrentED */
1696 ohci
->ctrl_cur
= val
& OHCI_EDPTR_MASK
;
1699 case 10: /* HcBulkHeadED */
1700 ohci
->bulk_head
= val
& OHCI_EDPTR_MASK
;
1703 case 11: /* HcBulkCurrentED */
1704 ohci
->bulk_cur
= val
& OHCI_EDPTR_MASK
;
1707 case 13: /* HcFmInterval */
1708 ohci
->fsmps
= (val
& OHCI_FMI_FSMPS
) >> 16;
1709 ohci
->fit
= (val
& OHCI_FMI_FIT
) >> 31;
1710 ohci_set_frame_interval(ohci
, val
);
1713 case 15: /* HcFmNumber */
1716 case 16: /* HcPeriodicStart */
1717 ohci
->pstart
= val
& 0xffff;
1720 case 17: /* HcLSThreshold */
1721 ohci
->lst
= val
& 0xffff;
1724 case 18: /* HcRhDescriptorA */
1725 ohci
->rhdesc_a
&= ~OHCI_RHA_RW_MASK
;
1726 ohci
->rhdesc_a
|= val
& OHCI_RHA_RW_MASK
;
1729 case 19: /* HcRhDescriptorB */
1732 case 20: /* HcRhStatus */
1733 ohci_set_hub_status(ohci
, val
);
1736 /* PXA27x specific registers */
1737 case 24: /* HcStatus */
1738 ohci
->hstatus
&= ~(val
& ohci
->hmask
);
1741 case 25: /* HcHReset */
1742 ohci
->hreset
= val
& ~OHCI_HRESET_FSBIR
;
1743 if (val
& OHCI_HRESET_FSBIR
)
1747 case 26: /* HcHInterruptEnable */
1751 case 27: /* HcHInterruptTest */
1756 fprintf(stderr
, "ohci_write: Bad offset %x\n", (int)addr
);
1761 static void ohci_async_cancel_device(OHCIState
*ohci
, USBDevice
*dev
)
1763 if (ohci
->async_td
&&
1764 usb_packet_is_inflight(&ohci
->usb_packet
) &&
1765 ohci
->usb_packet
.ep
->dev
== dev
) {
1766 usb_cancel_packet(&ohci
->usb_packet
);
1771 static const MemoryRegionOps ohci_mem_ops
= {
1772 .read
= ohci_mem_read
,
1773 .write
= ohci_mem_write
,
1774 .endianness
= DEVICE_LITTLE_ENDIAN
,
1777 static USBPortOps ohci_port_ops
= {
1778 .attach
= ohci_attach
,
1779 .detach
= ohci_detach
,
1780 .child_detach
= ohci_child_detach
,
1781 .wakeup
= ohci_wakeup
,
1782 .complete
= ohci_async_complete_packet
,
1785 static USBBusOps ohci_bus_ops
= {
1788 static int usb_ohci_init(OHCIState
*ohci
, DeviceState
*dev
,
1789 int num_ports
, dma_addr_t localmem_base
,
1790 char *masterbus
, uint32_t firstport
,
1797 if (usb_frame_time
== 0) {
1798 #ifdef OHCI_TIME_WARP
1799 usb_frame_time
= get_ticks_per_sec();
1800 usb_bit_time
= muldiv64(1, get_ticks_per_sec(), USB_HZ
/1000);
1802 usb_frame_time
= muldiv64(1, get_ticks_per_sec(), 1000);
1803 if (get_ticks_per_sec() >= USB_HZ
) {
1804 usb_bit_time
= muldiv64(1, get_ticks_per_sec(), USB_HZ
);
1809 DPRINTF("usb-ohci: usb_bit_time=%" PRId64
" usb_frame_time=%" PRId64
"\n",
1810 usb_frame_time
, usb_bit_time
);
1813 ohci
->num_ports
= num_ports
;
1815 USBPort
*ports
[OHCI_MAX_PORTS
];
1816 for(i
= 0; i
< num_ports
; i
++) {
1817 ports
[i
] = &ohci
->rhport
[i
].port
;
1819 if (usb_register_companion(masterbus
, ports
, num_ports
,
1820 firstport
, ohci
, &ohci_port_ops
,
1821 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
) != 0) {
1825 usb_bus_new(&ohci
->bus
, &ohci_bus_ops
, dev
);
1826 for (i
= 0; i
< num_ports
; i
++) {
1827 usb_register_port(&ohci
->bus
, &ohci
->rhport
[i
].port
,
1828 ohci
, i
, &ohci_port_ops
,
1829 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
);
1833 memory_region_init_io(&ohci
->mem
, &ohci_mem_ops
, ohci
, "ohci", 256);
1834 ohci
->localmem_base
= localmem_base
;
1836 ohci
->name
= object_get_typename(OBJECT(dev
));
1837 usb_packet_init(&ohci
->usb_packet
);
1840 qemu_register_reset(ohci_reset
, ohci
);
1853 static int usb_ohci_initfn_pci(struct PCIDevice
*dev
)
1855 OHCIPCIState
*ohci
= DO_UPCAST(OHCIPCIState
, pci_dev
, dev
);
1857 ohci
->pci_dev
.config
[PCI_CLASS_PROG
] = 0x10; /* OHCI */
1858 ohci
->pci_dev
.config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin A */
1860 if (usb_ohci_init(&ohci
->state
, &dev
->qdev
, ohci
->num_ports
, 0,
1861 ohci
->masterbus
, ohci
->firstport
,
1862 pci_dma_context(dev
)) != 0) {
1865 ohci
->state
.irq
= ohci
->pci_dev
.irq
[0];
1867 /* TODO: avoid cast below by using dev */
1868 pci_register_bar(&ohci
->pci_dev
, 0, 0, &ohci
->state
.mem
);
1873 SysBusDevice busdev
;
1876 dma_addr_t dma_offset
;
1879 static int ohci_init_pxa(SysBusDevice
*dev
)
1881 OHCISysBusState
*s
= FROM_SYSBUS(OHCISysBusState
, dev
);
1883 /* Cannot fail as we pass NULL for masterbus */
1884 usb_ohci_init(&s
->ohci
, &dev
->qdev
, s
->num_ports
, s
->dma_offset
, NULL
, 0,
1885 &dma_context_memory
);
1886 sysbus_init_irq(dev
, &s
->ohci
.irq
);
1887 sysbus_init_mmio(dev
, &s
->ohci
.mem
);
1892 static Property ohci_pci_properties
[] = {
1893 DEFINE_PROP_STRING("masterbus", OHCIPCIState
, masterbus
),
1894 DEFINE_PROP_UINT32("num-ports", OHCIPCIState
, num_ports
, 3),
1895 DEFINE_PROP_UINT32("firstport", OHCIPCIState
, firstport
, 0),
1896 DEFINE_PROP_END_OF_LIST(),
1899 static void ohci_pci_class_init(ObjectClass
*klass
, void *data
)
1901 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1902 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1904 k
->init
= usb_ohci_initfn_pci
;
1905 k
->vendor_id
= PCI_VENDOR_ID_APPLE
;
1906 k
->device_id
= PCI_DEVICE_ID_APPLE_IPID_USB
;
1907 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1909 dc
->desc
= "Apple USB Controller";
1910 dc
->props
= ohci_pci_properties
;
1913 static const TypeInfo ohci_pci_info
= {
1915 .parent
= TYPE_PCI_DEVICE
,
1916 .instance_size
= sizeof(OHCIPCIState
),
1917 .class_init
= ohci_pci_class_init
,
1920 static Property ohci_sysbus_properties
[] = {
1921 DEFINE_PROP_UINT32("num-ports", OHCISysBusState
, num_ports
, 3),
1922 DEFINE_PROP_DMAADDR("dma-offset", OHCISysBusState
, dma_offset
, 3),
1923 DEFINE_PROP_END_OF_LIST(),
1926 static void ohci_sysbus_class_init(ObjectClass
*klass
, void *data
)
1928 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1929 SysBusDeviceClass
*sbc
= SYS_BUS_DEVICE_CLASS(klass
);
1931 sbc
->init
= ohci_init_pxa
;
1932 dc
->desc
= "OHCI USB Controller";
1933 dc
->props
= ohci_sysbus_properties
;
1936 static const TypeInfo ohci_sysbus_info
= {
1937 .name
= "sysbus-ohci",
1938 .parent
= TYPE_SYS_BUS_DEVICE
,
1939 .instance_size
= sizeof(OHCISysBusState
),
1940 .class_init
= ohci_sysbus_class_init
,
1943 static void ohci_register_types(void)
1945 type_register_static(&ohci_pci_info
);
1946 type_register_static(&ohci_sysbus_info
);
1949 type_init(ohci_register_types
)