Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 4 | * Copyright (C) 2013 secunet Security Networks AG |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Julius Werner | 1f86434 | 2013-09-03 17:15:31 -0700 | [diff] [blame] | 30 | //#define USB_DEBUG |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 31 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 32 | #include <usb/usb.h> |
| 33 | #include "generic_hub.h" |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 34 | #include "xhci_private.h" |
| 35 | #include "xhci.h" |
| 36 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 37 | static int |
| 38 | xhci_rh_hub_status_changed(usbdev_t *const dev) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 39 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 40 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 41 | const int changed = !!(xhci->opreg->usbsts & USBSTS_PCD); |
| 42 | if (changed) |
| 43 | xhci->opreg->usbsts = |
| 44 | (xhci->opreg->usbsts & USBSTS_PRSRV_MASK) | USBSTS_PCD; |
| 45 | return changed; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static int |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 49 | xhci_rh_port_status_changed(usbdev_t *const dev, const int port) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 50 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 51 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 52 | volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc; |
| 53 | |
| 54 | const int changed = !!(*portsc & PORTSC_CSC); |
| 55 | /* always clear all the status change bits */ |
| 56 | *portsc = (*portsc & PORTSC_RW_MASK) | 0x00ef0000; |
| 57 | return changed; |
| 58 | } |
| 59 | |
| 60 | static int |
| 61 | xhci_rh_port_connected(usbdev_t *const dev, const int port) |
| 62 | { |
| 63 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 64 | volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc; |
| 65 | |
| 66 | return *portsc & PORTSC_CCS; |
| 67 | } |
| 68 | |
| 69 | static int |
| 70 | xhci_rh_port_in_reset(usbdev_t *const dev, const int port) |
| 71 | { |
| 72 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 73 | volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc; |
| 74 | |
| 75 | return !!(*portsc & PORTSC_PR); |
| 76 | } |
| 77 | |
| 78 | static int |
| 79 | xhci_rh_port_enabled(usbdev_t *const dev, const int port) |
| 80 | { |
| 81 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 82 | volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc; |
| 83 | |
| 84 | return !!(*portsc & PORTSC_PED); |
| 85 | } |
| 86 | |
Julius Werner | e00ba21 | 2013-09-24 20:03:54 -0700 | [diff] [blame^] | 87 | static usb_speed |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 88 | xhci_rh_port_speed(usbdev_t *const dev, const int port) |
| 89 | { |
| 90 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 91 | volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc; |
| 92 | |
| 93 | if (*portsc & PORTSC_PED) { |
| 94 | return ((*portsc & PORTSC_PORT_SPEED_MASK) |
| 95 | >> PORTSC_PORT_SPEED_START) |
| 96 | - 1; |
| 97 | } else { |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 98 | return -1; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 99 | } |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 102 | static int |
| 103 | xhci_rh_start_port_reset(usbdev_t *const dev, const int port) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 104 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 105 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 106 | volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc; |
| 107 | |
| 108 | *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PR; |
| 109 | return 0; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 112 | static const generic_hub_ops_t xhci_rh_ops = { |
| 113 | .hub_status_changed = xhci_rh_hub_status_changed, |
| 114 | .port_status_changed = xhci_rh_port_status_changed, |
| 115 | .port_connected = xhci_rh_port_connected, |
| 116 | .port_in_reset = xhci_rh_port_in_reset, |
| 117 | .port_enabled = xhci_rh_port_enabled, |
| 118 | .port_speed = xhci_rh_port_speed, |
| 119 | .enable_port = NULL, |
| 120 | .disable_port = NULL, |
| 121 | .start_port_reset = xhci_rh_start_port_reset, |
| 122 | .reset_port = generic_hub_rh_resetport, |
| 123 | }; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 124 | |
| 125 | void |
| 126 | xhci_rh_init (usbdev_t *dev) |
| 127 | { |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 128 | /* we can set them here because a root hub _really_ shouldn't |
| 129 | appear elsewhere */ |
| 130 | dev->address = 0; |
| 131 | dev->hub = -1; |
| 132 | dev->port = -1; |
| 133 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 134 | const int num_ports = /* TODO: maybe we need to read extended caps */ |
| 135 | (XHCI_INST(dev->controller)->capreg->hcsparams1 >> 24) & 0xff; |
| 136 | generic_hub_init(dev, num_ports, &xhci_rh_ops); |
| 137 | |
| 138 | usb_debug("xHCI: root hub init done\n"); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 139 | } |