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 | |
Shawn Nematbakhsh | e1dad0c | 2014-03-10 14:46:06 -0700 | [diff] [blame] | 54 | const int changed = !!(*portsc & (PORTSC_CSC | PORTSC_PRC)); |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 55 | /* always clear all the status change bits */ |
Julius Werner | 6f4c7a6 | 2015-08-07 15:29:41 -0700 | [diff] [blame] | 56 | *portsc = (*portsc & PORTSC_RW_MASK) | 0x00fe0000; |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 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 |
Shawn Nematbakhsh | a16029a | 2014-03-10 14:12:29 -0700 | [diff] [blame] | 103 | xhci_rh_reset_port(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 | |
Shawn Nematbakhsh | a16029a | 2014-03-10 14:12:29 -0700 | [diff] [blame] | 108 | /* Trigger port reset. */ |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 109 | *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PR; |
Shawn Nematbakhsh | a16029a | 2014-03-10 14:12:29 -0700 | [diff] [blame] | 110 | |
| 111 | /* Wait for port_in_reset == 0, up to 150 * 1000us = 150ms */ |
| 112 | if (generic_hub_wait_for_port(dev, port, 0, xhci_rh_port_in_reset, |
| 113 | 150, 1000) == 0) |
| 114 | usb_debug("xhci_rh: Reset timed out at port %d\n", port); |
| 115 | else |
| 116 | /* Clear reset status bits, since port is out of reset. */ |
| 117 | *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PRC | PORTSC_WRC; |
| 118 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 119 | return 0; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Yidi Lin | d42ee15 | 2015-05-07 15:36:04 +0800 | [diff] [blame] | 122 | static int |
| 123 | xhci_rh_enable_port(usbdev_t *const dev, int port) |
| 124 | { |
| 125 | if (IS_ENABLED(CONFIG_LP_USB_XHCI_MTK_QUIRK)) { |
| 126 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 127 | volatile u32 *const portsc = |
| 128 | &xhci->opreg->prs[port - 1].portsc; |
| 129 | |
| 130 | /* |
| 131 | * Before sending commands to a port, the Port Power in |
| 132 | * PORTSC register should be enabled on MTK's xHCI. |
| 133 | */ |
| 134 | *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PP; |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 140 | static const generic_hub_ops_t xhci_rh_ops = { |
| 141 | .hub_status_changed = xhci_rh_hub_status_changed, |
| 142 | .port_status_changed = xhci_rh_port_status_changed, |
| 143 | .port_connected = xhci_rh_port_connected, |
| 144 | .port_in_reset = xhci_rh_port_in_reset, |
| 145 | .port_enabled = xhci_rh_port_enabled, |
| 146 | .port_speed = xhci_rh_port_speed, |
Yidi Lin | d42ee15 | 2015-05-07 15:36:04 +0800 | [diff] [blame] | 147 | .enable_port = xhci_rh_enable_port, |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 148 | .disable_port = NULL, |
Shawn Nematbakhsh | a16029a | 2014-03-10 14:12:29 -0700 | [diff] [blame] | 149 | .start_port_reset = NULL, |
| 150 | .reset_port = xhci_rh_reset_port, |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 151 | }; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 152 | |
| 153 | void |
| 154 | xhci_rh_init (usbdev_t *dev) |
| 155 | { |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 156 | /* we can set them here because a root hub _really_ shouldn't |
| 157 | appear elsewhere */ |
| 158 | dev->address = 0; |
| 159 | dev->hub = -1; |
| 160 | dev->port = -1; |
| 161 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 162 | const int num_ports = /* TODO: maybe we need to read extended caps */ |
| 163 | (XHCI_INST(dev->controller)->capreg->hcsparams1 >> 24) & 0xff; |
| 164 | generic_hub_init(dev, num_ports, &xhci_rh_ops); |
| 165 | |
| 166 | usb_debug("xHCI: root hub init done\n"); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 167 | } |