2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 #include <ipxe/malloc.h>
35 #include <ipxe/init.h>
40 * USB Enhanced Host Controller Interface (EHCI) driver
45 * Construct error code from transfer descriptor status
47 * @v status Transfer descriptor status
50 * Bits 2-5 of the status code provide some indication as to the root
51 * cause of the error. We incorporate these into the error code as
52 * reported to usb_complete_err().
54 #define EIO_STATUS( status ) EUNIQ ( EINFO_EIO, ( ( (status) >> 2 ) & 0xf ) )
56 /******************************************************************************
60 ******************************************************************************
67 * @v regs MMIO registers
69 static void ehci_init ( struct ehci_device *ehci, void *regs ) {
74 /* Locate capability and operational registers */
76 caplength = readb ( ehci->cap + EHCI_CAP_CAPLENGTH );
77 ehci->op = ( ehci->cap + caplength );
78 DBGC2 ( ehci, "EHCI %s cap %08lx op %08lx\n", ehci->name,
79 virt_to_phys ( ehci->cap ), virt_to_phys ( ehci->op ) );
81 /* Read structural parameters */
82 hcsparams = readl ( ehci->cap + EHCI_CAP_HCSPARAMS );
83 ehci->ports = EHCI_HCSPARAMS_PORTS ( hcsparams );
84 DBGC ( ehci, "EHCI %s has %d ports\n", ehci->name, ehci->ports );
86 /* Read capability parameters 1 */
87 hccparams = readl ( ehci->cap + EHCI_CAP_HCCPARAMS );
88 ehci->addr64 = EHCI_HCCPARAMS_ADDR64 ( hccparams );
89 ehci->flsize = ( EHCI_HCCPARAMS_FLSIZE ( hccparams ) ?
90 EHCI_FLSIZE_SMALL : EHCI_FLSIZE_DEFAULT );
91 ehci->eecp = EHCI_HCCPARAMS_EECP ( hccparams );
92 DBGC2 ( ehci, "EHCI %s %d-bit flsize %d\n", ehci->name,
93 ( ehci->addr64 ? 64 : 32 ), ehci->flsize );
97 * Find extended capability
101 * @v id Capability ID
102 * @v offset Offset to previous extended capability instance, or zero
103 * @ret offset Offset to extended capability, or zero if not found
105 static unsigned int ehci_extended_capability ( struct ehci_device *ehci,
106 struct pci_device *pci,
108 unsigned int offset ) {
111 /* Locate the extended capability */
114 /* Locate first or next capability as applicable */
116 pci_read_config_dword ( pci, offset, &eecp );
117 offset = EHCI_EECP_NEXT ( eecp );
124 /* Check if this is the requested capability */
125 pci_read_config_dword ( pci, offset, &eecp );
126 if ( EHCI_EECP_ID ( eecp ) == id )
132 * Calculate buffer alignment
135 * @ret align Buffer alignment
137 * Determine alignment required for a buffer which must be aligned to
138 * at least EHCI_MIN_ALIGN and which must not cross a page boundary.
140 static inline size_t ehci_align ( size_t len ) {
143 /* Align to own length (rounded up to a power of two) */
144 align = ( 1 << fls ( len - 1 ) );
146 /* Round up to EHCI_MIN_ALIGN if needed */
147 if ( align < EHCI_MIN_ALIGN )
148 align = EHCI_MIN_ALIGN;
154 * Check control data structure reachability
156 * @v ehci EHCI device
157 * @v ptr Data structure pointer
158 * @ret rc Return status code
160 static int ehci_ctrl_reachable ( struct ehci_device *ehci, void *ptr ) {
161 physaddr_t phys = virt_to_phys ( ptr );
164 /* Always reachable in a 32-bit build */
165 if ( sizeof ( physaddr_t ) <= sizeof ( uint32_t ) )
168 /* Reachable only if control segment matches in a 64-bit build */
169 segment = ( ( ( uint64_t ) phys ) >> 32 );
170 if ( segment == ehci->ctrldssegment )
176 /******************************************************************************
180 ******************************************************************************
183 /** Prevent the release of ownership back to BIOS */
184 static int ehci_legacy_prevent_release;
187 * Initialise USB legacy support
189 * @v ehci EHCI device
192 static void ehci_legacy_init ( struct ehci_device *ehci,
193 struct pci_device *pci ) {
197 /* Locate USB legacy support capability (if present) */
198 legacy = ehci_extended_capability ( ehci, pci, EHCI_EECP_ID_LEGACY, 0 );
200 /* Not an error; capability may not be present */
201 DBGC ( ehci, "EHCI %s has no USB legacy support capability\n",
206 /* Check if legacy USB support is enabled */
207 pci_read_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_BIOS ), &bios );
208 if ( ! ( bios & EHCI_USBLEGSUP_BIOS_OWNED ) ) {
209 /* Not an error; already owned by OS */
210 DBGC ( ehci, "EHCI %s USB legacy support already disabled\n",
215 /* Record presence of USB legacy support capability */
216 ehci->legacy = legacy;
220 * Claim ownership from BIOS
222 * @v ehci EHCI device
225 static void ehci_legacy_claim ( struct ehci_device *ehci,
226 struct pci_device *pci ) {
227 unsigned int legacy = ehci->legacy;
232 /* Do nothing unless legacy support capability is present */
236 /* Claim ownership */
237 pci_write_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_OS ),
238 EHCI_USBLEGSUP_OS_OWNED );
240 /* Wait for BIOS to release ownership */
241 for ( i = 0 ; i < EHCI_USBLEGSUP_MAX_WAIT_MS ; i++ ) {
243 /* Check if BIOS has released ownership */
244 pci_read_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_BIOS ),
246 if ( ! ( bios & EHCI_USBLEGSUP_BIOS_OWNED ) ) {
247 DBGC ( ehci, "EHCI %s claimed ownership from BIOS\n",
249 pci_read_config_dword ( pci, ( legacy +
250 EHCI_USBLEGSUP_CTLSTS ),
253 DBGC ( ehci, "EHCI %s warning: BIOS retained "
254 "SMIs: %08x\n", ehci->name, ctlsts );
263 /* BIOS did not release ownership. Claim it forcibly by
264 * disabling all SMIs.
266 DBGC ( ehci, "EHCI %s could not claim ownership from BIOS: forcibly "
267 "disabling SMIs\n", ehci->name );
268 pci_write_config_dword ( pci, ( legacy + EHCI_USBLEGSUP_CTLSTS ), 0 );
272 * Release ownership back to BIOS
274 * @v ehci EHCI device
277 static void ehci_legacy_release ( struct ehci_device *ehci,
278 struct pci_device *pci ) {
280 /* Do nothing unless legacy support capability is present */
281 if ( ! ehci->legacy )
284 /* Do nothing if releasing ownership is prevented */
285 if ( ehci_legacy_prevent_release ) {
286 DBGC ( ehci, "EHCI %s not releasing ownership to BIOS\n",
291 /* Release ownership */
292 pci_write_config_byte ( pci, ( ehci->legacy + EHCI_USBLEGSUP_OS ), 0 );
293 DBGC ( ehci, "EHCI %s released ownership to BIOS\n", ehci->name );
296 /******************************************************************************
298 * Companion controllers
300 ******************************************************************************
304 * Poll child companion controllers
306 * @v ehci EHCI device
308 static void ehci_poll_companions ( struct ehci_device *ehci ) {
310 struct device_description *desc;
312 /* Poll any USB buses belonging to child companion controllers */
313 for_each_usb_bus ( bus ) {
315 /* Get underlying devices description */
316 desc = &bus->dev->desc;
318 /* Skip buses that are not PCI devices */
319 if ( desc->bus_type != BUS_TYPE_PCI )
322 /* Skip buses that are not part of the same PCI device */
323 if ( PCI_FIRST_FUNC ( desc->location ) !=
324 PCI_FIRST_FUNC ( ehci->bus->dev->desc.location ) )
327 /* Skip buses that are not UHCI or OHCI PCI devices */
328 if ( ( desc->class != PCI_CLASS ( PCI_CLASS_SERIAL,
329 PCI_CLASS_SERIAL_USB,
330 PCI_CLASS_SERIAL_USB_UHCI ))&&
331 ( desc->class != PCI_CLASS ( PCI_CLASS_SERIAL,
332 PCI_CLASS_SERIAL_USB,
333 PCI_CLASS_SERIAL_USB_OHCI ) ))
336 /* Poll child companion controller bus */
337 DBGC2 ( ehci, "EHCI %s polling companion %s\n",
338 ehci->name, bus->name );
344 * Locate EHCI companion controller
347 * @ret busdevfn EHCI companion controller bus:dev.fn (if any)
349 unsigned int ehci_companion ( struct pci_device *pci ) {
350 struct pci_device tmp;
351 unsigned int busdevfn;
354 /* Look for an EHCI function on the same PCI device */
355 busdevfn = pci->busdevfn;
356 while ( ++busdevfn <= PCI_LAST_FUNC ( pci->busdevfn ) ) {
357 pci_init ( &tmp, busdevfn );
358 if ( ( rc = pci_read_config ( &tmp ) ) != 0 )
360 if ( tmp.class == PCI_CLASS ( PCI_CLASS_SERIAL,
361 PCI_CLASS_SERIAL_USB,
362 PCI_CLASS_SERIAL_USB_EHCI ) )
369 /******************************************************************************
373 ******************************************************************************
379 * @v ehci EHCI device
381 static void ehci_run ( struct ehci_device *ehci ) {
384 /* Set run/stop bit */
385 usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
386 usbcmd &= ~EHCI_USBCMD_FLSIZE_MASK;
387 usbcmd |= ( EHCI_USBCMD_RUN | EHCI_USBCMD_FLSIZE ( ehci->flsize ) |
388 EHCI_USBCMD_PERIODIC | EHCI_USBCMD_ASYNC );
389 writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
395 * @v ehci EHCI device
396 * @ret rc Return status code
398 static int ehci_stop ( struct ehci_device *ehci ) {
403 /* Clear run/stop bit */
404 usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
405 usbcmd &= ~( EHCI_USBCMD_RUN | EHCI_USBCMD_PERIODIC |
407 writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
409 /* Wait for device to stop */
410 for ( i = 0 ; i < EHCI_STOP_MAX_WAIT_MS ; i++ ) {
412 /* Check if device is stopped */
413 usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
414 if ( usbsts & EHCI_USBSTS_HCH )
421 DBGC ( ehci, "EHCI %s timed out waiting for stop\n", ehci->name );
428 * @v ehci EHCI device
429 * @ret rc Return status code
431 static int ehci_reset ( struct ehci_device *ehci ) {
436 /* The EHCI specification states that resetting a running
437 * device may result in undefined behaviour, so try stopping
440 if ( ( rc = ehci_stop ( ehci ) ) != 0 ) {
441 /* Ignore errors and attempt to reset the device anyway */
445 writel ( EHCI_USBCMD_HCRST, ehci->op + EHCI_OP_USBCMD );
447 /* Wait for reset to complete */
448 for ( i = 0 ; i < EHCI_RESET_MAX_WAIT_MS ; i++ ) {
450 /* Check if reset is complete */
451 usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
452 if ( ! ( usbcmd & EHCI_USBCMD_HCRST ) )
459 DBGC ( ehci, "EHCI %s timed out waiting for reset\n", ehci->name );
463 /******************************************************************************
465 * Transfer descriptor rings
467 ******************************************************************************
471 * Allocate transfer descriptor ring
473 * @v ehci EHCI device
474 * @v ring Transfer descriptor ring
475 * @ret rc Return status code
477 static int ehci_ring_alloc ( struct ehci_device *ehci,
478 struct ehci_ring *ring ) {
479 struct ehci_transfer_descriptor *desc;
480 struct ehci_transfer_descriptor *next;
486 /* Initialise structure */
487 memset ( ring, 0, sizeof ( *ring ) );
489 /* Allocate I/O buffers */
490 ring->iobuf = zalloc ( EHCI_RING_COUNT * sizeof ( ring->iobuf[0] ) );
491 if ( ! ring->iobuf ) {
493 goto err_alloc_iobuf;
496 /* Allocate queue head */
497 ring->head = malloc_dma ( sizeof ( *ring->head ),
498 ehci_align ( sizeof ( *ring->head ) ) );
499 if ( ! ring->head ) {
501 goto err_alloc_queue;
503 if ( ( rc = ehci_ctrl_reachable ( ehci, ring->head ) ) != 0 ) {
504 DBGC ( ehci, "EHCI %s queue head unreachable\n", ehci->name );
505 goto err_unreachable_queue;
507 memset ( ring->head, 0, sizeof ( *ring->head ) );
509 /* Allocate transfer descriptors */
510 len = ( EHCI_RING_COUNT * sizeof ( ring->desc[0] ) );
511 ring->desc = malloc_dma ( len, sizeof ( ring->desc[0] ) );
512 if ( ! ring->desc ) {
516 memset ( ring->desc, 0, len );
518 /* Initialise transfer descriptors */
519 for ( i = 0 ; i < EHCI_RING_COUNT ; i++ ) {
520 desc = &ring->desc[i];
521 if ( ( rc = ehci_ctrl_reachable ( ehci, desc ) ) != 0 ) {
522 DBGC ( ehci, "EHCI %s descriptor unreachable\n",
524 goto err_unreachable_desc;
526 next = &ring->desc[ ( i + 1 ) % EHCI_RING_COUNT ];
527 link = virt_to_phys ( next );
528 desc->next = cpu_to_le32 ( link );
529 desc->alt = cpu_to_le32 ( link );
532 /* Initialise queue head */
533 link = virt_to_phys ( &ring->desc[0] );
534 ring->head->cache.next = cpu_to_le32 ( link );
538 err_unreachable_desc:
539 free_dma ( ring->desc, len );
541 err_unreachable_queue:
542 free_dma ( ring->head, sizeof ( *ring->head ) );
544 free ( ring->iobuf );
550 * Free transfer descriptor ring
552 * @v ring Transfer descriptor ring
554 static void ehci_ring_free ( struct ehci_ring *ring ) {
558 assert ( ehci_ring_fill ( ring ) == 0 );
559 for ( i = 0 ; i < EHCI_RING_COUNT ; i++ )
560 assert ( ring->iobuf[i] == NULL );
562 /* Free transfer descriptors */
563 free_dma ( ring->desc, ( EHCI_RING_COUNT * sizeof ( ring->desc[0] ) ) );
565 /* Free queue head */
566 free_dma ( ring->head, sizeof ( *ring->head ) );
568 /* Free I/O buffers */
569 free ( ring->iobuf );
573 * Enqueue transfer descriptors
575 * @v ehci EHCI device
576 * @v ring Transfer descriptor ring
577 * @v iobuf I/O buffer
579 * @v count Number of transfers
580 * @ret rc Return status code
582 static int ehci_enqueue ( struct ehci_device *ehci, struct ehci_ring *ring,
583 struct io_buffer *iobuf,
584 const struct ehci_transfer *xfer,
585 unsigned int count ) {
586 struct ehci_transfer_descriptor *desc;
597 assert ( iobuf != NULL );
598 assert ( count > 0 );
600 /* Fail if ring does not have sufficient space */
601 if ( ehci_ring_remaining ( ring ) < count )
604 /* Fail if any portion is unreachable */
605 for ( i = 0 ; i < count ; i++ ) {
606 phys = ( virt_to_phys ( xfer[i].data ) + xfer[i].len - 1 );
607 if ( ( phys > 0xffffffffUL ) && ( ! ehci->addr64 ) )
611 /* Enqueue each transfer, recording the I/O buffer with the last */
612 for ( ; count ; ring->prod++, xfer++ ) {
614 /* Populate descriptor header */
615 index = ( ring->prod % EHCI_RING_COUNT );
616 desc = &ring->desc[index];
617 toggle = ( xfer->flags & EHCI_FL_TOGGLE );
618 assert ( xfer->len <= EHCI_LEN_MASK );
619 assert ( EHCI_FL_TOGGLE == EHCI_LEN_TOGGLE );
620 desc->len = cpu_to_le16 ( xfer->len | toggle );
621 desc->flags = ( xfer->flags | EHCI_FL_CERR_MAX );
623 /* Populate buffer pointers */
626 for ( i = 0 ; len ; i++ ) {
628 /* Calculate length of this fragment */
629 phys = virt_to_phys ( data );
630 offset = ( phys & ( EHCI_PAGE_ALIGN - 1 ) );
631 frag_len = ( EHCI_PAGE_ALIGN - offset );
632 if ( frag_len > len )
636 assert ( ( i == 0 ) || ( offset == 0 ) );
637 assert ( i < ( sizeof ( desc->low ) /
638 sizeof ( desc->low[0] ) ) );
640 /* Populate buffer pointer */
641 desc->low[i] = cpu_to_le32 ( phys );
642 if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
644 cpu_to_le32 ( ((uint64_t) phys) >> 32 );
647 /* Move to next fragment */
652 /* Ensure everything is valid before activating descriptor */
654 desc->status = EHCI_STATUS_ACTIVE;
656 /* Record I/O buffer against last ring index */
658 ring->iobuf[index] = iobuf;
665 * Dequeue a transfer descriptor
667 * @v ring Transfer descriptor ring
668 * @ret iobuf I/O buffer (or NULL)
670 static struct io_buffer * ehci_dequeue ( struct ehci_ring *ring ) {
671 struct ehci_transfer_descriptor *desc;
672 struct io_buffer *iobuf;
673 unsigned int index = ( ring->cons % EHCI_RING_COUNT );
676 assert ( ehci_ring_fill ( ring ) > 0 );
678 /* Mark descriptor as inactive (and not halted) */
679 desc = &ring->desc[index];
682 /* Retrieve I/O buffer */
683 iobuf = ring->iobuf[index];
684 ring->iobuf[index] = NULL;
686 /* Update consumer counter */
692 /******************************************************************************
694 * Schedule management
696 ******************************************************************************
700 * Get link value for a queue head
702 * @v queue Queue head
703 * @ret link Link value
705 static inline uint32_t ehci_link_qh ( struct ehci_queue_head *queue ) {
707 return ( virt_to_phys ( queue ) | EHCI_LINK_TYPE_QH );
711 * (Re)build asynchronous schedule
713 * @v ehci EHCI device
715 static void ehci_async_schedule ( struct ehci_device *ehci ) {
716 struct ehci_endpoint *endpoint;
717 struct ehci_queue_head *queue;
720 /* Build schedule in reverse order of execution. Provided
721 * that we only ever add or remove single endpoints, this can
722 * safely run concurrently with hardware execution of the
725 link = ehci_link_qh ( ehci->head );
726 list_for_each_entry_reverse ( endpoint, &ehci->async, schedule ) {
727 queue = endpoint->ring.head;
728 queue->link = cpu_to_le32 ( link );
730 link = ehci_link_qh ( queue );
732 ehci->head->link = cpu_to_le32 ( link );
737 * Add endpoint to asynchronous schedule
739 * @v endpoint Endpoint
741 static void ehci_async_add ( struct ehci_endpoint *endpoint ) {
742 struct ehci_device *ehci = endpoint->ehci;
744 /* Add to end of schedule */
745 list_add_tail ( &endpoint->schedule, &ehci->async );
747 /* Rebuild schedule */
748 ehci_async_schedule ( ehci );
752 * Remove endpoint from asynchronous schedule
754 * @v endpoint Endpoint
755 * @ret rc Return status code
757 static int ehci_async_del ( struct ehci_endpoint *endpoint ) {
758 struct ehci_device *ehci = endpoint->ehci;
763 /* Remove from schedule */
764 list_check_contains_entry ( endpoint, &ehci->async, schedule );
765 list_del ( &endpoint->schedule );
767 /* Rebuild schedule */
768 ehci_async_schedule ( ehci );
770 /* Request notification when asynchronous schedule advances */
771 usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
772 usbcmd |= EHCI_USBCMD_ASYNC_ADVANCE;
773 writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
775 /* Wait for asynchronous schedule to advance */
776 for ( i = 0 ; i < EHCI_ASYNC_ADVANCE_MAX_WAIT_MS ; i++ ) {
778 /* Check for asynchronous schedule advancing */
779 usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
780 if ( usbsts & EHCI_USBSTS_ASYNC_ADVANCE ) {
781 usbsts &= ~EHCI_USBSTS_CHANGE;
782 usbsts |= EHCI_USBSTS_ASYNC_ADVANCE;
783 writel ( usbsts, ehci->op + EHCI_OP_USBSTS );
791 /* Bad things will probably happen now */
792 DBGC ( ehci, "EHCI %s timed out waiting for asynchronous schedule "
793 "to advance\n", ehci->name );
798 * (Re)build periodic schedule
800 * @v ehci EHCI device
802 static void ehci_periodic_schedule ( struct ehci_device *ehci ) {
803 struct ehci_endpoint *endpoint;
804 struct ehci_queue_head *queue;
807 unsigned int max_interval;
810 /* Build schedule in reverse order of execution. Provided
811 * that we only ever add or remove single endpoints, this can
812 * safely run concurrently with hardware execution of the
815 DBGCP ( ehci, "EHCI %s periodic schedule: ", ehci->name );
816 link = EHCI_LINK_TERMINATE;
817 list_for_each_entry_reverse ( endpoint, &ehci->periodic, schedule ) {
818 queue = endpoint->ring.head;
819 queue->link = cpu_to_le32 ( link );
821 DBGCP ( ehci, "%s%d",
822 ( ( link == EHCI_LINK_TERMINATE ) ? "" : "<-" ),
823 endpoint->ep->interval );
824 link = ehci_link_qh ( queue );
826 DBGCP ( ehci, "\n" );
828 /* Populate periodic frame list */
829 DBGCP ( ehci, "EHCI %s periodic frame list:", ehci->name );
830 frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
831 for ( i = 0 ; i < frames ; i++ ) {
833 /* Calculate maximum interval (in microframes) which
834 * may appear as part of this frame list.
837 /* Start of list: include all endpoints */
840 /* Calculate highest power-of-two frame interval */
841 max_interval = ( 1 << ( ffs ( i ) - 1 ) );
842 /* Convert to microframes */
844 /* Round up to nearest 2^n-1 */
845 max_interval = ( ( max_interval << 1 ) - 1 );
848 /* Find first endpoint in schedule satisfying this
849 * maximum interval constraint.
851 link = EHCI_LINK_TERMINATE;
852 list_for_each_entry ( endpoint, &ehci->periodic, schedule ) {
853 if ( endpoint->ep->interval <= max_interval ) {
854 queue = endpoint->ring.head;
855 link = ehci_link_qh ( queue );
856 DBGCP ( ehci, " %d:%d",
857 i, endpoint->ep->interval );
861 ehci->frame[i].link = cpu_to_le32 ( link );
864 DBGCP ( ehci, "\n" );
868 * Add endpoint to periodic schedule
870 * @v endpoint Endpoint
872 static void ehci_periodic_add ( struct ehci_endpoint *endpoint ) {
873 struct ehci_device *ehci = endpoint->ehci;
874 struct ehci_endpoint *before;
875 unsigned int interval = endpoint->ep->interval;
877 /* Find first endpoint with a smaller interval */
878 list_for_each_entry ( before, &ehci->periodic, schedule ) {
879 if ( before->ep->interval < interval )
882 list_add_tail ( &endpoint->schedule, &before->schedule );
884 /* Rebuild schedule */
885 ehci_periodic_schedule ( ehci );
889 * Remove endpoint from periodic schedule
891 * @v endpoint Endpoint
892 * @ret rc Return status code
894 static int ehci_periodic_del ( struct ehci_endpoint *endpoint ) {
895 struct ehci_device *ehci = endpoint->ehci;
897 /* Remove from schedule */
898 list_check_contains_entry ( endpoint, &ehci->periodic, schedule );
899 list_del ( &endpoint->schedule );
901 /* Rebuild schedule */
902 ehci_periodic_schedule ( ehci );
904 /* Delay for a whole USB frame (with a 100% safety margin) */
911 * Add endpoint to appropriate schedule
913 * @v endpoint Endpoint
915 static void ehci_schedule_add ( struct ehci_endpoint *endpoint ) {
916 struct usb_endpoint *ep = endpoint->ep;
917 unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
919 if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
920 ehci_periodic_add ( endpoint );
922 ehci_async_add ( endpoint );
927 * Remove endpoint from appropriate schedule
929 * @v endpoint Endpoint
930 * @ret rc Return status code
932 static int ehci_schedule_del ( struct ehci_endpoint *endpoint ) {
933 struct usb_endpoint *ep = endpoint->ep;
934 unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
936 if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
937 return ehci_periodic_del ( endpoint );
939 return ehci_async_del ( endpoint );
943 /******************************************************************************
945 * Endpoint operations
947 ******************************************************************************
951 * Determine endpoint characteristics
954 * @ret chr Endpoint characteristics
956 static uint32_t ehci_endpoint_characteristics ( struct usb_endpoint *ep ) {
957 struct usb_device *usb = ep->usb;
958 unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
961 /* Determine basic characteristics */
962 chr = ( EHCI_CHR_ADDRESS ( usb->address ) |
963 EHCI_CHR_ENDPOINT ( ep->address ) |
964 EHCI_CHR_MAX_LEN ( ep->mtu ) );
966 /* Control endpoints require manual control of the data toggle */
967 if ( attr == USB_ENDPOINT_ATTR_CONTROL )
968 chr |= EHCI_CHR_TOGGLE;
970 /* Determine endpoint speed */
971 if ( usb->port->speed == USB_SPEED_HIGH ) {
972 chr |= EHCI_CHR_EPS_HIGH;
974 if ( usb->port->speed == USB_SPEED_FULL ) {
975 chr |= EHCI_CHR_EPS_FULL;
977 chr |= EHCI_CHR_EPS_LOW;
979 if ( attr == USB_ENDPOINT_ATTR_CONTROL )
980 chr |= EHCI_CHR_CONTROL;
987 * Determine endpoint capabilities
990 * @ret cap Endpoint capabilities
992 static uint32_t ehci_endpoint_capabilities ( struct usb_endpoint *ep ) {
993 struct usb_device *usb = ep->usb;
994 struct usb_port *tt = usb_transaction_translator ( usb );
995 unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
999 /* Determine basic capabilities */
1000 cap = EHCI_CAP_MULT ( ep->burst + 1 );
1002 /* Determine interrupt schedule mask, if applicable */
1003 if ( ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) &&
1004 ( ( ep->interval != 0 ) /* avoid infinite loop */ ) ) {
1005 for ( i = 0 ; i < 8 /* microframes per frame */ ;
1006 i += ep->interval ) {
1007 cap |= EHCI_CAP_INTR_SCHED ( i );
1011 /* Set transaction translator hub address and port, if applicable */
1013 assert ( tt->hub->usb );
1014 cap |= ( EHCI_CAP_TT_HUB ( tt->hub->usb->address ) |
1015 EHCI_CAP_TT_PORT ( tt->address ) );
1016 if ( attr == USB_ENDPOINT_ATTR_INTERRUPT )
1017 cap |= EHCI_CAP_SPLIT_SCHED_DEFAULT;
1024 * Update endpoint characteristics and capabilities
1026 * @v ep USB endpoint
1028 static void ehci_endpoint_update ( struct usb_endpoint *ep ) {
1029 struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1030 struct ehci_queue_head *head;
1032 /* Update queue characteristics and capabilities */
1033 head = endpoint->ring.head;
1034 head->chr = cpu_to_le32 ( ehci_endpoint_characteristics ( ep ) );
1035 head->cap = cpu_to_le32 ( ehci_endpoint_capabilities ( ep ) );
1041 * @v ep USB endpoint
1042 * @ret rc Return status code
1044 static int ehci_endpoint_open ( struct usb_endpoint *ep ) {
1045 struct usb_device *usb = ep->usb;
1046 struct ehci_device *ehci = usb_get_hostdata ( usb );
1047 struct ehci_endpoint *endpoint;
1050 /* Allocate and initialise structure */
1051 endpoint = zalloc ( sizeof ( *endpoint ) );
1056 endpoint->ehci = ehci;
1058 usb_endpoint_set_hostdata ( ep, endpoint );
1060 /* Initialise descriptor ring */
1061 if ( ( rc = ehci_ring_alloc ( ehci, &endpoint->ring ) ) != 0 )
1062 goto err_ring_alloc;
1064 /* Update queue characteristics and capabilities */
1065 ehci_endpoint_update ( ep );
1067 /* Add to list of endpoints */
1068 list_add_tail ( &endpoint->list, &ehci->endpoints );
1070 /* Add to schedule */
1071 ehci_schedule_add ( endpoint );
1075 ehci_ring_free ( &endpoint->ring );
1085 * @v ep USB endpoint
1087 static void ehci_endpoint_close ( struct usb_endpoint *ep ) {
1088 struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1089 struct ehci_device *ehci = endpoint->ehci;
1090 struct usb_device *usb = ep->usb;
1091 struct io_buffer *iobuf;
1094 /* Remove from schedule */
1095 if ( ( rc = ehci_schedule_del ( endpoint ) ) != 0 ) {
1096 /* No way to prevent hardware from continuing to
1097 * access the memory, so leak it.
1099 DBGC ( ehci, "EHCI %s %s could not unschedule: %s\n",
1100 usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
1104 /* Cancel any incomplete transfers */
1105 while ( ehci_ring_fill ( &endpoint->ring ) ) {
1106 iobuf = ehci_dequeue ( &endpoint->ring );
1108 usb_complete_err ( ep, iobuf, -ECANCELED );
1111 /* Remove from list of endpoints */
1112 list_del ( &endpoint->list );
1114 /* Free descriptor ring */
1115 ehci_ring_free ( &endpoint->ring );
1124 * @v ep USB endpoint
1125 * @ret rc Return status code
1127 static int ehci_endpoint_reset ( struct usb_endpoint *ep ) {
1128 struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1129 struct ehci_ring *ring = &endpoint->ring;
1130 struct ehci_transfer_descriptor *cache = &ring->head->cache;
1134 assert ( ! ( cache->status & EHCI_STATUS_ACTIVE ) );
1135 assert ( cache->status & EHCI_STATUS_HALTED );
1137 /* Reset residual count */
1140 /* Reset data toggle */
1143 /* Prepare to restart at next unconsumed descriptor */
1144 link = virt_to_phys ( &ring->desc[ ring->cons % EHCI_RING_COUNT ] );
1145 cache->next = cpu_to_le32 ( link );
1157 * @v ep USB endpoint
1158 * @ret rc Return status code
1160 static int ehci_endpoint_mtu ( struct usb_endpoint *ep ) {
1162 /* Update endpoint characteristics and capabilities */
1163 ehci_endpoint_update ( ep );
1169 * Enqueue message transfer
1171 * @v ep USB endpoint
1172 * @v iobuf I/O buffer
1173 * @ret rc Return status code
1175 static int ehci_endpoint_message ( struct usb_endpoint *ep,
1176 struct io_buffer *iobuf ) {
1177 struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1178 struct ehci_device *ehci = endpoint->ehci;
1179 struct usb_setup_packet *packet;
1181 struct ehci_transfer xfers[3];
1182 struct ehci_transfer *xfer = xfers;
1186 /* Construct setup stage */
1187 assert ( iob_len ( iobuf ) >= sizeof ( *packet ) );
1188 packet = iobuf->data;
1189 iob_pull ( iobuf, sizeof ( *packet ) );
1190 xfer->data = packet;
1191 xfer->len = sizeof ( *packet );
1192 xfer->flags = EHCI_FL_PID_SETUP;
1195 /* Construct data stage, if applicable */
1196 len = iob_len ( iobuf );
1197 input = ( packet->request & cpu_to_le16 ( USB_DIR_IN ) );
1199 xfer->data = iobuf->data;
1201 xfer->flags = ( EHCI_FL_TOGGLE |
1202 ( input ? EHCI_FL_PID_IN : EHCI_FL_PID_OUT ) );
1206 /* Construct status stage */
1209 xfer->flags = ( EHCI_FL_TOGGLE | EHCI_FL_IOC |
1210 ( ( len && input ) ? EHCI_FL_PID_OUT : EHCI_FL_PID_IN));
1213 /* Enqueue transfer */
1214 if ( ( rc = ehci_enqueue ( ehci, &endpoint->ring, iobuf, xfers,
1215 ( xfer - xfers ) ) ) != 0 )
1222 * Enqueue stream transfer
1224 * @v ep USB endpoint
1225 * @v iobuf I/O buffer
1226 * @v terminate Terminate using a short packet
1227 * @ret rc Return status code
1229 static int ehci_endpoint_stream ( struct usb_endpoint *ep,
1230 struct io_buffer *iobuf, int terminate ) {
1231 struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1232 struct ehci_device *ehci = endpoint->ehci;
1233 unsigned int input = ( ep->address & USB_DIR_IN );
1234 struct ehci_transfer xfers[2];
1235 struct ehci_transfer *xfer = xfers;
1236 size_t len = iob_len ( iobuf );
1239 /* Create transfer */
1240 xfer->data = iobuf->data;
1242 xfer->flags = ( EHCI_FL_IOC |
1243 ( input ? EHCI_FL_PID_IN : EHCI_FL_PID_OUT ) );
1245 if ( terminate && ( ( len & ( ep->mtu - 1 ) ) == 0 ) ) {
1249 xfer->flags = ( EHCI_FL_IOC | EHCI_FL_PID_OUT );
1253 /* Enqueue transfer */
1254 if ( ( rc = ehci_enqueue ( ehci, &endpoint->ring, iobuf, xfers,
1255 ( xfer - xfers ) ) ) != 0 )
1262 * Poll for completions
1264 * @v endpoint Endpoint
1266 static void ehci_endpoint_poll ( struct ehci_endpoint *endpoint ) {
1267 struct ehci_device *ehci = endpoint->ehci;
1268 struct ehci_ring *ring = &endpoint->ring;
1269 struct ehci_transfer_descriptor *desc;
1270 struct usb_endpoint *ep = endpoint->ep;
1271 struct usb_device *usb = ep->usb;
1272 struct io_buffer *iobuf;
1274 unsigned int status;
1277 /* Consume all completed descriptors */
1278 while ( ehci_ring_fill ( &endpoint->ring ) ) {
1280 /* Stop if we reach an uncompleted descriptor */
1282 index = ( ring->cons % EHCI_RING_COUNT );
1283 desc = &ring->desc[index];
1284 status = desc->status;
1285 if ( status & EHCI_STATUS_ACTIVE )
1288 /* Consume this descriptor */
1289 iobuf = ehci_dequeue ( ring );
1291 /* If we have encountered an error, then consume all
1292 * remaining descriptors in this transaction, report
1293 * the error to the USB core, and stop further
1296 if ( status & EHCI_STATUS_HALTED ) {
1297 rc = -EIO_STATUS ( status );
1298 DBGC ( ehci, "EHCI %s %s completion %d failed (status "
1299 "%02x): %s\n", usb->name,
1300 usb_endpoint_name ( ep ), index, status,
1303 iobuf = ehci_dequeue ( ring );
1304 usb_complete_err ( endpoint->ep, iobuf, rc );
1308 /* Accumulate residual data count */
1309 ring->residual += ( le16_to_cpu ( desc->len ) & EHCI_LEN_MASK );
1311 /* If this is not the end of a transaction (i.e. has
1312 * no I/O buffer), then continue to next descriptor.
1317 /* Update I/O buffer length */
1318 iob_unput ( iobuf, ring->residual );
1321 /* Report completion to USB core */
1322 usb_complete ( endpoint->ep, iobuf );
1326 /******************************************************************************
1330 ******************************************************************************
1337 * @ret rc Return status code
1339 static int ehci_device_open ( struct usb_device *usb ) {
1340 struct ehci_device *ehci = usb_bus_get_hostdata ( usb->port->hub->bus );
1342 usb_set_hostdata ( usb, ehci );
1351 static void ehci_device_close ( struct usb_device *usb ) {
1352 struct ehci_device *ehci = usb_get_hostdata ( usb );
1353 struct usb_bus *bus = ehci->bus;
1355 /* Free device address, if assigned */
1357 usb_free_address ( bus, usb->address );
1361 * Assign device address
1364 * @ret rc Return status code
1366 static int ehci_device_address ( struct usb_device *usb ) {
1367 struct ehci_device *ehci = usb_get_hostdata ( usb );
1368 struct usb_bus *bus = ehci->bus;
1369 struct usb_endpoint *ep0 = usb_endpoint ( usb, USB_EP0_ADDRESS );
1374 assert ( usb->address == 0 );
1375 assert ( ep0 != NULL );
1377 /* Allocate device address */
1378 address = usb_alloc_address ( bus );
1379 if ( address < 0 ) {
1381 DBGC ( ehci, "EHCI %s could not allocate address: %s\n",
1382 usb->name, strerror ( rc ) );
1383 goto err_alloc_address;
1387 if ( ( rc = usb_set_address ( usb, address ) ) != 0 )
1388 goto err_set_address;
1390 /* Update device address */
1391 usb->address = address;
1393 /* Update control endpoint characteristics and capabilities */
1394 ehci_endpoint_update ( ep0 );
1399 usb_free_address ( bus, address );
1404 /******************************************************************************
1408 ******************************************************************************
1415 * @ret rc Return status code
1417 static int ehci_hub_open ( struct usb_hub *hub __unused ) {
1428 static void ehci_hub_close ( struct usb_hub *hub __unused ) {
1433 /******************************************************************************
1435 * Root hub operations
1437 ******************************************************************************
1444 * @ret rc Return status code
1446 static int ehci_root_open ( struct usb_hub *hub ) {
1447 struct usb_bus *bus = hub->bus;
1448 struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
1452 /* Route all ports to EHCI controller */
1453 writel ( EHCI_CONFIGFLAG_CF, ehci->op + EHCI_OP_CONFIGFLAG );
1455 /* Enable power to all ports */
1456 for ( i = 1 ; i <= ehci->ports ; i++ ) {
1457 portsc = readl ( ehci->op + EHCI_OP_PORTSC ( i ) );
1458 portsc &= ~EHCI_PORTSC_CHANGE;
1459 portsc |= EHCI_PORTSC_PP;
1460 writel ( portsc, ehci->op + EHCI_OP_PORTSC ( i ) );
1463 /* Wait 20ms after potentially enabling power to a port */
1464 mdelay ( EHCI_PORT_POWER_DELAY_MS );
1466 /* Record hub driver private data */
1467 usb_hub_set_drvdata ( hub, ehci );
1477 static void ehci_root_close ( struct usb_hub *hub ) {
1478 struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1480 /* Route all ports back to companion controllers */
1481 writel ( 0, ehci->op + EHCI_OP_CONFIGFLAG );
1483 /* Clear hub driver private data */
1484 usb_hub_set_drvdata ( hub, NULL );
1492 * @ret rc Return status code
1494 static int ehci_root_enable ( struct usb_hub *hub, struct usb_port *port ) {
1495 struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1500 /* Check for a low-speed device */
1501 portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1502 line = EHCI_PORTSC_LINE_STATUS ( portsc );
1503 if ( line == EHCI_PORTSC_LINE_STATUS_LOW ) {
1504 DBGC ( ehci, "EHCI %s-%d detected low-speed device: "
1505 "disowning\n", ehci->name, port->address );
1510 portsc &= ~( EHCI_PORTSC_PED | EHCI_PORTSC_CHANGE );
1511 portsc |= EHCI_PORTSC_PR;
1512 writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1513 mdelay ( USB_RESET_DELAY_MS );
1514 portsc &= ~EHCI_PORTSC_PR;
1515 writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1517 /* Wait for reset to complete */
1518 for ( i = 0 ; i < EHCI_PORT_RESET_MAX_WAIT_MS ; i++ ) {
1520 /* Check port status */
1521 portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1522 if ( ! ( portsc & EHCI_PORTSC_PR ) ) {
1523 if ( portsc & EHCI_PORTSC_PED )
1525 DBGC ( ehci, "EHCI %s-%d not enabled after reset: "
1526 "disowning\n", ehci->name, port->address );
1534 DBGC ( ehci, "EHCI %s-%d timed out waiting for port to reset\n",
1535 ehci->name, port->address );
1540 portsc &= ~EHCI_PORTSC_CHANGE;
1541 portsc |= EHCI_PORTSC_OWNER;
1542 writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1544 /* Delay to allow child companion controllers to settle */
1545 mdelay ( EHCI_DISOWN_DELAY_MS );
1547 /* Poll child companion controllers */
1548 ehci_poll_companions ( ehci );
1558 * @ret rc Return status code
1560 static int ehci_root_disable ( struct usb_hub *hub, struct usb_port *port ) {
1561 struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1565 portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1566 portsc &= ~( EHCI_PORTSC_PED | EHCI_PORTSC_CHANGE );
1567 writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1573 * Update root hub port speed
1577 * @ret rc Return status code
1579 static int ehci_root_speed ( struct usb_hub *hub, struct usb_port *port ) {
1580 struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1588 /* Read port status */
1589 portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1590 DBGC2 ( ehci, "EHCI %s-%d status is %08x\n",
1591 ehci->name, port->address, portsc );
1592 ccs = ( portsc & EHCI_PORTSC_CCS );
1593 csc = ( portsc & EHCI_PORTSC_CSC );
1594 ped = ( portsc & EHCI_PORTSC_PED );
1595 line = EHCI_PORTSC_LINE_STATUS ( portsc );
1597 /* Record disconnections and clear changes */
1598 port->disconnected |= csc;
1599 writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1601 /* Determine port speed */
1603 /* Port not connected */
1604 speed = USB_SPEED_NONE;
1605 } else if ( line == EHCI_PORTSC_LINE_STATUS_LOW ) {
1606 /* Detected as low-speed */
1607 speed = USB_SPEED_LOW;
1609 /* Port already enabled: must be high-speed */
1610 speed = USB_SPEED_HIGH;
1612 /* Not low-speed and not yet enabled. Could be either
1613 * full-speed or high-speed; we can't yet tell.
1615 speed = USB_SPEED_FULL;
1617 port->speed = speed;
1622 * Clear transaction translator buffer
1626 * @v ep USB endpoint
1627 * @ret rc Return status code
1629 static int ehci_root_clear_tt ( struct usb_hub *hub, struct usb_port *port,
1630 struct usb_endpoint *ep ) {
1631 struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1633 /* Should never be called; this is a root hub */
1634 DBGC ( ehci, "EHCI %s-%d nonsensical CLEAR_TT for %s %s\n", ehci->name,
1635 port->address, ep->usb->name, usb_endpoint_name ( ep ) );
1641 * Poll for port status changes
1646 static void ehci_root_poll ( struct usb_hub *hub, struct usb_port *port ) {
1647 struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1651 /* Do nothing unless something has changed */
1652 portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1653 change = ( portsc & EHCI_PORTSC_CHANGE );
1657 /* Record disconnections and clear changes */
1658 port->disconnected |= ( portsc & EHCI_PORTSC_CSC );
1659 writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1661 /* Report port status change */
1662 usb_port_changed ( port );
1665 /******************************************************************************
1669 ******************************************************************************
1676 * @ret rc Return status code
1678 static int ehci_bus_open ( struct usb_bus *bus ) {
1679 struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
1680 unsigned int frames;
1685 assert ( list_empty ( &ehci->async ) );
1686 assert ( list_empty ( &ehci->periodic ) );
1688 /* Allocate and initialise asynchronous queue head */
1689 ehci->head = malloc_dma ( sizeof ( *ehci->head ),
1690 ehci_align ( sizeof ( *ehci->head ) ) );
1691 if ( ! ehci->head ) {
1693 goto err_alloc_head;
1695 memset ( ehci->head, 0, sizeof ( *ehci->head ) );
1696 ehci->head->chr = cpu_to_le32 ( EHCI_CHR_HEAD );
1697 ehci->head->cache.next = cpu_to_le32 ( EHCI_LINK_TERMINATE );
1698 ehci->head->cache.status = EHCI_STATUS_HALTED;
1699 ehci_async_schedule ( ehci );
1700 writel ( virt_to_phys ( ehci->head ),
1701 ehci->op + EHCI_OP_ASYNCLISTADDR );
1703 /* Use async queue head to determine control data structure segment */
1704 ehci->ctrldssegment =
1705 ( ( ( uint64_t ) virt_to_phys ( ehci->head ) ) >> 32 );
1706 if ( ehci->addr64 ) {
1707 writel ( ehci->ctrldssegment, ehci->op + EHCI_OP_CTRLDSSEGMENT);
1708 } else if ( ehci->ctrldssegment ) {
1709 DBGC ( ehci, "EHCI %s CTRLDSSEGMENT not supported\n",
1712 goto err_ctrldssegment;
1715 /* Allocate periodic frame list */
1716 frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
1717 len = ( frames * sizeof ( ehci->frame[0] ) );
1718 ehci->frame = malloc_dma ( len, EHCI_PAGE_ALIGN );
1719 if ( ! ehci->frame ) {
1721 goto err_alloc_frame;
1723 if ( ( rc = ehci_ctrl_reachable ( ehci, ehci->frame ) ) != 0 ) {
1724 DBGC ( ehci, "EHCI %s frame list unreachable\n", ehci->name );
1725 goto err_unreachable_frame;
1727 ehci_periodic_schedule ( ehci );
1728 writel ( virt_to_phys ( ehci->frame ),
1729 ehci->op + EHCI_OP_PERIODICLISTBASE );
1731 /* Start controller */
1737 err_unreachable_frame:
1738 free_dma ( ehci->frame, len );
1741 free_dma ( ehci->head, sizeof ( *ehci->head ) );
1751 static void ehci_bus_close ( struct usb_bus *bus ) {
1752 struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
1753 unsigned int frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
1756 assert ( list_empty ( &ehci->async ) );
1757 assert ( list_empty ( &ehci->periodic ) );
1759 /* Stop controller */
1762 /* Free periodic frame list */
1763 free_dma ( ehci->frame, ( frames * sizeof ( ehci->frame[0] ) ) );
1765 /* Free asynchronous schedule */
1766 free_dma ( ehci->head, sizeof ( *ehci->head ) );
1774 static void ehci_bus_poll ( struct usb_bus *bus ) {
1775 struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
1776 struct usb_hub *hub = bus->hub;
1777 struct ehci_endpoint *endpoint;
1782 /* Do nothing unless something has changed */
1783 usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
1784 assert ( usbsts & EHCI_USBSTS_ASYNC );
1785 assert ( usbsts & EHCI_USBSTS_PERIODIC );
1786 assert ( ! ( usbsts & EHCI_USBSTS_HCH ) );
1787 change = ( usbsts & EHCI_USBSTS_CHANGE );
1791 /* Acknowledge changes */
1792 writel ( usbsts, ehci->op + EHCI_OP_USBSTS );
1794 /* Process completions, if applicable */
1795 if ( change & ( EHCI_USBSTS_USBINT | EHCI_USBSTS_USBERRINT ) ) {
1797 /* Iterate over all endpoints looking for completed
1798 * descriptors. We trust that completion handlers are
1799 * minimal and will not do anything that could
1800 * plausibly affect the endpoint list itself.
1802 list_for_each_entry ( endpoint, &ehci->endpoints, list )
1803 ehci_endpoint_poll ( endpoint );
1806 /* Process port status changes, if applicable */
1807 if ( change & EHCI_USBSTS_PORT ) {
1809 /* Iterate over all ports looking for status changes */
1810 for ( i = 1 ; i <= ehci->ports ; i++ )
1811 ehci_root_poll ( hub, usb_port ( hub, i ) );
1814 /* Report fatal errors */
1815 if ( change & EHCI_USBSTS_SYSERR )
1816 DBGC ( ehci, "EHCI %s host system error\n", ehci->name );
1819 /******************************************************************************
1823 ******************************************************************************
1826 /** USB host controller operations */
1827 static struct usb_host_operations ehci_operations = {
1829 .open = ehci_endpoint_open,
1830 .close = ehci_endpoint_close,
1831 .reset = ehci_endpoint_reset,
1832 .mtu = ehci_endpoint_mtu,
1833 .message = ehci_endpoint_message,
1834 .stream = ehci_endpoint_stream,
1837 .open = ehci_device_open,
1838 .close = ehci_device_close,
1839 .address = ehci_device_address,
1842 .open = ehci_bus_open,
1843 .close = ehci_bus_close,
1844 .poll = ehci_bus_poll,
1847 .open = ehci_hub_open,
1848 .close = ehci_hub_close,
1851 .open = ehci_root_open,
1852 .close = ehci_root_close,
1853 .enable = ehci_root_enable,
1854 .disable = ehci_root_disable,
1855 .speed = ehci_root_speed,
1856 .clear_tt = ehci_root_clear_tt,
1864 * @ret rc Return status code
1866 static int ehci_probe ( struct pci_device *pci ) {
1867 struct ehci_device *ehci;
1868 struct usb_port *port;
1869 unsigned long bar_start;
1874 /* Allocate and initialise structure */
1875 ehci = zalloc ( sizeof ( *ehci ) );
1880 ehci->name = pci->dev.name;
1881 INIT_LIST_HEAD ( &ehci->endpoints );
1882 INIT_LIST_HEAD ( &ehci->async );
1883 INIT_LIST_HEAD ( &ehci->periodic );
1885 /* Fix up PCI device */
1886 adjust_pci_device ( pci );
1889 bar_start = pci_bar_start ( pci, EHCI_BAR );
1890 bar_size = pci_bar_size ( pci, EHCI_BAR );
1891 ehci->regs = ioremap ( bar_start, bar_size );
1892 if ( ! ehci->regs ) {
1897 /* Initialise EHCI device */
1898 ehci_init ( ehci, ehci->regs );
1900 /* Initialise USB legacy support and claim ownership */
1901 ehci_legacy_init ( ehci, pci );
1902 ehci_legacy_claim ( ehci, pci );
1905 if ( ( rc = ehci_reset ( ehci ) ) != 0 )
1908 /* Allocate USB bus */
1909 ehci->bus = alloc_usb_bus ( &pci->dev, ehci->ports, EHCI_MTU,
1911 if ( ! ehci->bus ) {
1915 usb_bus_set_hostdata ( ehci->bus, ehci );
1916 usb_hub_set_drvdata ( ehci->bus->hub, ehci );
1918 /* Set port protocols */
1919 for ( i = 1 ; i <= ehci->ports ; i++ ) {
1920 port = usb_port ( ehci->bus->hub, i );
1921 port->protocol = USB_PROTO_2_0;
1924 /* Register USB bus */
1925 if ( ( rc = register_usb_bus ( ehci->bus ) ) != 0 )
1928 pci_set_drvdata ( pci, ehci );
1931 unregister_usb_bus ( ehci->bus );
1933 free_usb_bus ( ehci->bus );
1935 ehci_reset ( ehci );
1937 ehci_legacy_release ( ehci, pci );
1938 iounmap ( ehci->regs );
1950 static void ehci_remove ( struct pci_device *pci ) {
1951 struct ehci_device *ehci = pci_get_drvdata ( pci );
1952 struct usb_bus *bus = ehci->bus;
1954 unregister_usb_bus ( bus );
1955 assert ( list_empty ( &ehci->async ) );
1956 assert ( list_empty ( &ehci->periodic ) );
1957 free_usb_bus ( bus );
1958 ehci_reset ( ehci );
1959 ehci_legacy_release ( ehci, pci );
1960 iounmap ( ehci->regs );
1964 /** EHCI PCI device IDs */
1965 static struct pci_device_id ehci_ids[] = {
1966 PCI_ROM ( 0xffff, 0xffff, "ehci", "EHCI", 0 ),
1969 /** EHCI PCI driver */
1970 struct pci_driver ehci_driver __pci_driver = {
1972 .id_count = ( sizeof ( ehci_ids ) / sizeof ( ehci_ids[0] ) ),
1973 .class = PCI_CLASS_ID ( PCI_CLASS_SERIAL, PCI_CLASS_SERIAL_USB,
1974 PCI_CLASS_SERIAL_USB_EHCI ),
1975 .probe = ehci_probe,
1976 .remove = ehci_remove,
1982 * @v booting System is shutting down for OS boot
1984 static void ehci_shutdown ( int booting ) {
1985 /* If we are shutting down to boot an OS, then prevent the
1986 * release of ownership back to BIOS.
1988 ehci_legacy_prevent_release = booting;
1991 /** Startup/shutdown function */
1992 struct startup_fn ehci_startup __startup_fn ( STARTUP_LATE ) = {
1993 .shutdown = ehci_shutdown,