Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | bb01f60 | 2009-07-21 21:20:45 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
| 3 | * |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006 Eric Biederman (ebiederm@xmission.com) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 AMD |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 6 | * |
Stefan Reinauer | bb01f60 | 2009-07-21 21:20:45 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 10 | * |
Stefan Reinauer | bb01f60 | 2009-07-21 21:20:45 +0000 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 19 | */ |
Uwe Hermann | 06694a8 | 2010-09-23 18:16:46 +0000 | [diff] [blame] | 20 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 21 | #include <stddef.h> |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 22 | #include <console/console.h> |
Kyösti Mälkki | e2227a2 | 2014-02-05 13:02:55 +0200 | [diff] [blame] | 23 | #include <console/usb.h> |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 24 | #include <arch/io.h> |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 25 | #include <arch/byteorder.h> |
Stefan Reinauer | fd4f413 | 2013-06-19 12:25:44 -0700 | [diff] [blame] | 26 | #include <arch/early_variables.h> |
Kyösti Mälkki | e23c22d | 2013-07-28 23:16:47 +0300 | [diff] [blame] | 27 | #include <string.h> |
Kyösti Mälkki | 690bf2f | 2013-07-06 11:41:21 +0300 | [diff] [blame] | 28 | #include <cbmem.h> |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 29 | |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 30 | #include "ehci_debug.h" |
Kyösti Mälkki | e2227a2 | 2014-02-05 13:02:55 +0200 | [diff] [blame] | 31 | #include "usb_ch9.h" |
| 32 | #include "ehci.h" |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 33 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 34 | #define DBGP_MAX_ENDPOINTS 4 |
Kyösti Mälkki | d799140 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 35 | #define DBGP_SETUP_EP0 0 /* Compulsory endpoint 0. */ |
| 36 | #define DBGP_CONSOLE_EPOUT 1 |
| 37 | #define DBGP_CONSOLE_EPIN 2 |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 38 | |
| 39 | struct ehci_debug_info { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 40 | void *ehci_caps; |
| 41 | void *ehci_regs; |
| 42 | void *ehci_debug; |
| 43 | |
| 44 | struct dbgp_pipe ep_pipe[DBGP_MAX_ENDPOINTS]; |
| 45 | }; |
| 46 | |
Kyösti Mälkki | 3be80cc | 2013-06-06 10:46:37 +0300 | [diff] [blame] | 47 | #if CONFIG_DEBUG_USBDEBUG |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 48 | static void dbgp_print_data(struct ehci_dbg_port *ehci_debug); |
Kyösti Mälkki | d799140 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 49 | static int dbgp_enabled(void); |
| 50 | # define dprintk(LEVEL, args...) \ |
| 51 | do { if (!dbgp_enabled()) printk(LEVEL, ##args); } while (0) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 52 | #else |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 53 | # define dbgp_print_data(x) do {} while(0) |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 54 | # define dprintk(LEVEL, args...) do {} while(0) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 57 | #define USB_DEBUG_DEVNUM 127 |
| 58 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 59 | #define DBGP_LEN_UPDATE(x, len) (((x) & ~0x0f) | ((len) & 0x0f)) |
| 60 | /* |
| 61 | * USB Packet IDs (PIDs) |
| 62 | */ |
| 63 | |
| 64 | /* token */ |
| 65 | #define USB_PID_OUT 0xe1 |
| 66 | #define USB_PID_IN 0x69 |
| 67 | #define USB_PID_SOF 0xa5 |
| 68 | #define USB_PID_SETUP 0x2d |
| 69 | /* handshake */ |
| 70 | #define USB_PID_ACK 0xd2 |
| 71 | #define USB_PID_NAK 0x5a |
| 72 | #define USB_PID_STALL 0x1e |
| 73 | #define USB_PID_NYET 0x96 |
| 74 | /* data */ |
| 75 | #define USB_PID_DATA0 0xc3 |
| 76 | #define USB_PID_DATA1 0x4b |
| 77 | #define USB_PID_DATA2 0x87 |
| 78 | #define USB_PID_MDATA 0x0f |
| 79 | /* Special */ |
| 80 | #define USB_PID_PREAMBLE 0x3c |
| 81 | #define USB_PID_ERR 0x3c |
| 82 | #define USB_PID_SPLIT 0x78 |
| 83 | #define USB_PID_PING 0xb4 |
| 84 | #define USB_PID_UNDEF_0 0xf0 |
| 85 | |
| 86 | #define USB_PID_DATA_TOGGLE 0x88 |
| 87 | #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE) |
| 88 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 89 | #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ |
| 90 | #define HUB_SHORT_RESET_TIME 10 |
| 91 | #define HUB_LONG_RESET_TIME 200 |
| 92 | #define HUB_RESET_TIMEOUT 500 |
| 93 | |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 94 | #define DBGP_MICROFRAME_TIMEOUT_LOOPS 1000 |
| 95 | #define DBGP_MICROFRAME_RETRIES 10 |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 96 | #define DBGP_MAX_PACKET 8 |
| 97 | |
Kyösti Mälkki | 8ee04d7 | 2013-07-06 11:41:09 +0300 | [diff] [blame] | 98 | static struct ehci_debug_info glob_dbg_info CAR_GLOBAL; |
Kyösti Mälkki | 618d179 | 2014-10-27 08:01:55 +0200 | [diff] [blame^] | 99 | static struct ehci_debug_info * glob_dbg_info_p CAR_GLOBAL; |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 100 | |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 101 | static inline struct ehci_debug_info *dbgp_ehci_info(void) |
| 102 | { |
Kyösti Mälkki | 618d179 | 2014-10-27 08:01:55 +0200 | [diff] [blame^] | 103 | if (car_get_var(glob_dbg_info_p) == NULL) |
| 104 | car_set_var(glob_dbg_info_p, &glob_dbg_info); |
| 105 | |
| 106 | return car_get_var(glob_dbg_info_p); |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 107 | } |
| 108 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 109 | static int dbgp_wait_until_complete(struct ehci_dbg_port *ehci_debug) |
| 110 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 111 | u32 ctrl; |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 112 | int loop = 0; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 113 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 114 | do { |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 115 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 116 | /* Stop when the transaction is finished */ |
| 117 | if (ctrl & DBGP_DONE) |
| 118 | break; |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 119 | } while (++loop < DBGP_MICROFRAME_TIMEOUT_LOOPS); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 120 | |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 121 | if (! (ctrl & DBGP_DONE)) { |
| 122 | dprintk(BIOS_ERR, "dbgp_wait_until_complete: retry timeout.\n"); |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 123 | return -DBGP_ERR_SIGNAL; |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 124 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 125 | |
| 126 | /* Now that we have observed the completed transaction, |
| 127 | * clear the done bit. |
| 128 | */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 129 | write32((unsigned long)&ehci_debug->control, ctrl | DBGP_DONE); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 130 | return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl); |
| 131 | } |
| 132 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 133 | static void dbgp_breath(void) |
| 134 | { |
| 135 | /* Sleep to give the debug port a chance to breathe */ |
| 136 | } |
| 137 | |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 138 | static int dbgp_wait_until_done(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe, |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 139 | unsigned ctrl, const int timeout) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 140 | { |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 141 | u32 rd_ctrl, rd_pids; |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 142 | u32 ctrl_prev = 0, pids_prev = 0; |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 143 | u8 lpid; |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 144 | int ret, host_retries; |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 145 | int loop; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 146 | |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 147 | loop = 0; |
| 148 | device_retry: |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 149 | host_retries = 0; |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 150 | if (loop++ >= timeout) |
| 151 | return -DBGP_ERR_BAD; |
| 152 | |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 153 | host_retry: |
| 154 | if (host_retries++ >= DBGP_MICROFRAME_RETRIES) |
| 155 | return -DBGP_ERR_BAD; |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 156 | if (loop == 1 || host_retries > 1) |
| 157 | dprintk(BIOS_SPEW, "dbgp: start (@ %3d,%d) ctrl=%08x\n", |
| 158 | loop, host_retries, ctrl | DBGP_GO); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 159 | write32((unsigned long)&ehci_debug->control, ctrl | DBGP_GO); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 160 | ret = dbgp_wait_until_complete(ehci_debug); |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 161 | rd_ctrl = read32((unsigned long)&ehci_debug->control); |
| 162 | rd_pids = read32((unsigned long)&ehci_debug->pids); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 163 | |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 164 | if (rd_ctrl != ctrl_prev || rd_pids != pids_prev || (ret<0)) { |
| 165 | ctrl_prev = rd_ctrl; |
| 166 | pids_prev = rd_pids; |
| 167 | dprintk(BIOS_SPEW, "dbgp: status (@ %3d,%d) ctrl=%08x pids=%08x ret=%d\n", |
| 168 | loop, host_retries, rd_ctrl, rd_pids, ret); |
| 169 | } |
| 170 | |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 171 | /* Controller hardware failure. */ |
| 172 | if (ret == -DBGP_ERR_SIGNAL) { |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 173 | return ret; |
Kyösti Mälkki | 2de841b3 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 174 | |
| 175 | /* Bus failure (corrupted microframe). */ |
| 176 | } else if (ret == -DBGP_ERR_BAD) { |
| 177 | goto host_retry; |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 178 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 179 | |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 180 | lpid = DBGP_PID_GET(rd_pids); |
| 181 | |
| 182 | /* If I get an ACK or in-sync DATA PID, we are done. */ |
| 183 | if ((lpid == USB_PID_ACK) || (lpid == pipe->pid)) { |
Kyösti Mälkki | 48e899d | 2014-01-21 10:44:08 +0200 | [diff] [blame] | 184 | pipe->pid ^= USB_PID_DATA_TOGGLE; |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 185 | } |
| 186 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 187 | /* If the port is getting full or it has dropped data |
| 188 | * start pacing ourselves, not necessary but it's friendly. |
| 189 | */ |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 190 | else if (lpid == USB_PID_NYET) { |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 191 | dbgp_breath(); |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 192 | goto device_retry; |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* If I get a NACK or out-of-sync DATA PID, reissue the transmission. */ |
| 196 | else if ((lpid == USB_PID_NAK) || (lpid == (pipe->pid ^ USB_PID_DATA_TOGGLE))) { |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 197 | goto device_retry; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Kyösti Mälkki | dcccbd1 | 2013-08-10 10:08:38 +0300 | [diff] [blame] | 200 | /* Abort on STALL handshake for endpoint 0.*/ |
| 201 | else if ((lpid == USB_PID_STALL) && (pipe->endpoint == 0x0)) { |
| 202 | ret = -DBGP_ERR_BAD; |
| 203 | } |
| 204 | |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 205 | dbgp_print_data(ehci_debug); |
| 206 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static void dbgp_set_data(struct ehci_dbg_port *ehci_debug, const void *buf, int size) |
| 211 | { |
| 212 | const unsigned char *bytes = buf; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 213 | u32 lo, hi; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 214 | int i; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 215 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 216 | lo = hi = 0; |
| 217 | for (i = 0; i < 4 && i < size; i++) |
| 218 | lo |= bytes[i] << (8*i); |
| 219 | for (; i < 8 && i < size; i++) |
| 220 | hi |= bytes[i] << (8*(i - 4)); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 221 | write32((unsigned long)&ehci_debug->data03, lo); |
| 222 | write32((unsigned long)&ehci_debug->data47, hi); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static void dbgp_get_data(struct ehci_dbg_port *ehci_debug, void *buf, int size) |
| 226 | { |
| 227 | unsigned char *bytes = buf; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 228 | u32 lo, hi; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 229 | int i; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 230 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 231 | lo = read32((unsigned long)&ehci_debug->data03); |
| 232 | hi = read32((unsigned long)&ehci_debug->data47); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 233 | for (i = 0; i < 4 && i < size; i++) |
| 234 | bytes[i] = (lo >> (8*i)) & 0xff; |
| 235 | for (; i < 8 && i < size; i++) |
| 236 | bytes[i] = (hi >> (8*(i - 4))) & 0xff; |
| 237 | } |
| 238 | |
Kyösti Mälkki | e53cece | 2013-08-10 10:50:43 +0300 | [diff] [blame] | 239 | #if CONFIG_DEBUG_USBDEBUG |
| 240 | static void dbgp_print_data(struct ehci_dbg_port *ehci_debug) |
| 241 | { |
| 242 | u32 ctrl = read32((unsigned long)&ehci_debug->control); |
| 243 | u32 lo = read32((unsigned long)&ehci_debug->data03); |
| 244 | u32 hi = read32((unsigned long)&ehci_debug->data47); |
| 245 | int len = DBGP_LEN(ctrl); |
| 246 | if (len) { |
| 247 | int i; |
| 248 | dprintk(BIOS_SPEW, "dbgp: buf:"); |
| 249 | for (i = 0; i < 4 && i < len; i++) |
| 250 | dprintk(BIOS_SPEW, " %02x", (lo >> (8*i)) & 0xff); |
| 251 | for (; i < 8 && i < len; i++) |
| 252 | dprintk(BIOS_SPEW, " %02x", (hi >> (8*(i - 4))) & 0xff); |
| 253 | dprintk(BIOS_SPEW, "\n"); |
| 254 | } |
| 255 | } |
| 256 | #endif |
| 257 | |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 258 | static int dbgp_bulk_write(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe, |
| 259 | const char *bytes, int size) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 260 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 261 | u32 pids, addr, ctrl; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 262 | int ret; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 263 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 264 | if (size > DBGP_MAX_PACKET) |
| 265 | return -1; |
| 266 | |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 267 | addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint); |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 268 | pids = DBGP_PID_SET(pipe->pid, USB_PID_OUT); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 269 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 270 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 271 | ctrl = DBGP_LEN_UPDATE(ctrl, size); |
| 272 | ctrl |= DBGP_OUT; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 273 | |
| 274 | dbgp_set_data(ehci_debug, bytes, size); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 275 | write32((unsigned long)&ehci_debug->address, addr); |
| 276 | write32((unsigned long)&ehci_debug->pids, pids); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 277 | |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 278 | ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 279 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 280 | return ret; |
| 281 | } |
| 282 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 283 | int dbgp_bulk_write_x(struct dbgp_pipe *pipe, const char *bytes, int size) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 284 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 285 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 286 | return dbgp_bulk_write(dbg_info->ehci_debug, pipe, bytes, size); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 289 | static int dbgp_bulk_read(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe, |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 290 | void *data, int size) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 291 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 292 | u32 pids, addr, ctrl; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 293 | int ret; |
| 294 | |
| 295 | if (size > DBGP_MAX_PACKET) |
| 296 | return -1; |
| 297 | |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 298 | addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint); |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 299 | pids = DBGP_PID_SET(pipe->pid, USB_PID_IN); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 300 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 301 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 302 | ctrl = DBGP_LEN_UPDATE(ctrl, size); |
| 303 | ctrl &= ~DBGP_OUT; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 304 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 305 | write32((unsigned long)&ehci_debug->address, addr); |
| 306 | write32((unsigned long)&ehci_debug->pids, pids); |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 307 | ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 308 | if (ret < 0) |
| 309 | return ret; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 310 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 311 | if (size > ret) |
| 312 | size = ret; |
| 313 | dbgp_get_data(ehci_debug, data, size); |
| 314 | return ret; |
| 315 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 316 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 317 | int dbgp_bulk_read_x(struct dbgp_pipe *pipe, void *data, int size) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 318 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 319 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 320 | return dbgp_bulk_read(dbg_info->ehci_debug, pipe, data, size); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 323 | static void dbgp_mdelay(int ms) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 324 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 325 | int i; |
| 326 | |
| 327 | while (ms--) { |
| 328 | for (i = 0; i < 1000; i++) |
| 329 | inb(0x80); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static int dbgp_control_msg(struct ehci_dbg_port *ehci_debug, unsigned devnum, int requesttype, |
| 334 | int request, int value, int index, void *data, int size) |
| 335 | { |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 336 | struct ehci_debug_info *info = dbgp_ehci_info(); |
| 337 | struct dbgp_pipe *pipe = &info->ep_pipe[DBGP_SETUP_EP0]; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 338 | u32 pids, addr, ctrl; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 339 | struct usb_ctrlrequest req; |
| 340 | int read; |
Kyösti Mälkki | dcccbd1 | 2013-08-10 10:08:38 +0300 | [diff] [blame] | 341 | int ret, ret2; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 342 | |
| 343 | read = (requesttype & USB_DIR_IN) != 0; |
Kyösti Mälkki | dcccbd1 | 2013-08-10 10:08:38 +0300 | [diff] [blame] | 344 | if (size > DBGP_MAX_PACKET) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 345 | return -1; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 346 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 347 | /* Compute the control message */ |
| 348 | req.bRequestType = requesttype; |
| 349 | req.bRequest = request; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 350 | req.wValue = cpu_to_le16(value); |
| 351 | req.wIndex = cpu_to_le16(index); |
| 352 | req.wLength = cpu_to_le16(size); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 353 | |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 354 | pipe->devnum = devnum; |
| 355 | pipe->endpoint = 0; |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 356 | pipe->pid = USB_PID_DATA0; |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 357 | pipe->timeout = 1000; |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 358 | addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint); |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 359 | pids = DBGP_PID_SET(pipe->pid, USB_PID_SETUP); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 360 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 361 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 362 | ctrl = DBGP_LEN_UPDATE(ctrl, sizeof(req)); |
| 363 | ctrl |= DBGP_OUT; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 364 | |
Kyösti Mälkki | dcccbd1 | 2013-08-10 10:08:38 +0300 | [diff] [blame] | 365 | /* Setup stage */ |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 366 | dbgp_set_data(ehci_debug, &req, sizeof(req)); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 367 | write32((unsigned long)&ehci_debug->address, addr); |
| 368 | write32((unsigned long)&ehci_debug->pids, pids); |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 369 | ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, 1); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | |
Kyösti Mälkki | dcccbd1 | 2013-08-10 10:08:38 +0300 | [diff] [blame] | 373 | /* Data stage (optional) */ |
| 374 | if (read && size) |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 375 | ret = dbgp_bulk_read(ehci_debug, pipe, data, size); |
Kyösti Mälkki | dcccbd1 | 2013-08-10 10:08:38 +0300 | [diff] [blame] | 376 | else if (!read && size) |
| 377 | ret = dbgp_bulk_write(ehci_debug, pipe, data, size); |
| 378 | |
| 379 | /* Status stage in opposite direction */ |
| 380 | pipe->pid = USB_PID_DATA1; |
| 381 | ctrl = read32((unsigned long)&ehci_debug->control); |
| 382 | ctrl = DBGP_LEN_UPDATE(ctrl, 0); |
| 383 | if (read) { |
| 384 | pids = DBGP_PID_SET(pipe->pid, USB_PID_OUT); |
| 385 | ctrl |= DBGP_OUT; |
| 386 | } else { |
| 387 | pids = DBGP_PID_SET(pipe->pid, USB_PID_IN); |
| 388 | ctrl &= ~DBGP_OUT; |
| 389 | } |
| 390 | |
| 391 | write32((unsigned long)&ehci_debug->pids, pids); |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 392 | ret2 = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout); |
Kyösti Mälkki | dcccbd1 | 2013-08-10 10:08:38 +0300 | [diff] [blame] | 393 | if (ret2 < 0) |
| 394 | return ret2; |
| 395 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static int ehci_reset_port(struct ehci_regs *ehci_regs, int port) |
| 400 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 401 | u32 portsc; |
| 402 | u32 delay_time, delay_ms; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 403 | int loop; |
| 404 | |
| 405 | /* Reset the usb debug port */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 406 | portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 407 | portsc &= ~PORT_PE; |
| 408 | portsc |= PORT_RESET; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 409 | write32((unsigned long)&ehci_regs->port_status[port - 1], portsc); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 410 | |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 411 | delay_ms = HUB_ROOT_RESET_TIME; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 412 | for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT; |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 413 | delay_time += delay_ms) { |
| 414 | dbgp_mdelay(delay_ms); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 415 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 416 | portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 417 | if (portsc & PORT_RESET) { |
| 418 | /* force reset to complete */ |
| 419 | loop = 2; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 420 | write32((unsigned long)&ehci_regs->port_status[port - 1], |
Stefan Reinauer | 9fe4d79 | 2010-01-16 17:53:38 +0000 | [diff] [blame] | 421 | portsc & ~(PORT_RWC_BITS | PORT_RESET)); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 422 | do { |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 423 | dbgp_mdelay(delay_ms); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 424 | portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]); |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 425 | delay_time += delay_ms; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 426 | } while ((portsc & PORT_RESET) && (--loop > 0)); |
| 427 | if (!loop) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 428 | printk(BIOS_DEBUG, "ehci_reset_port forced done"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | /* Device went away? */ |
| 433 | if (!(portsc & PORT_CONNECT)) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 434 | return -1; //-ENOTCONN; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 435 | |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 436 | /* bomb out completely if something weird happened */ |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 437 | if ((portsc & PORT_CSC)) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 438 | return -2; //-EINVAL; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 439 | |
| 440 | /* If we've finished resetting, then break out of the loop */ |
| 441 | if (!(portsc & PORT_RESET) && (portsc & PORT_PE)) |
| 442 | return 0; |
| 443 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 444 | return -3; //-EBUSY; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 447 | static int ehci_wait_for_port(struct ehci_regs *ehci_regs, int port) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 448 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 449 | u32 status; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 450 | int ret, reps; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 451 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 452 | for (reps = 0; reps < 3; reps++) { |
| 453 | dbgp_mdelay(100); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 454 | status = read32((unsigned long)&ehci_regs->status); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 455 | if (status & STS_PCD) { |
| 456 | ret = ehci_reset_port(ehci_regs, port); |
| 457 | if (ret == 0) |
| 458 | return 0; |
| 459 | } |
| 460 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 461 | return -1; //-ENOTCONN; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Kyösti Mälkki | d2dac0a | 2013-08-23 23:33:16 +0300 | [diff] [blame] | 464 | #define USB_HUB_PORT_CONNECTION 0 |
| 465 | #define USB_HUB_PORT_ENABLED 1 |
| 466 | #define USB_HUB_PORT_RESET 4 |
| 467 | #define USB_HUB_PORT_POWER 8 |
| 468 | #define USB_HUB_C_PORT_CONNECTION 16 |
| 469 | #define USB_HUB_C_PORT_RESET 20 |
| 470 | |
| 471 | #if CONFIG_USBDEBUG_OPTIONAL_HUB_PORT |
| 472 | |
| 473 | static int hub_port_status(const char * buf, int feature) |
| 474 | { |
| 475 | return !!(buf[feature>>3] & (1<<(feature&0x7))); |
| 476 | } |
| 477 | |
| 478 | static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned int port) |
| 479 | { |
| 480 | const u8 hub_addr = USB_DEBUG_DEVNUM-1; |
| 481 | char status[8]; |
| 482 | int ret, loop; |
| 483 | |
| 484 | /* Move hub to address 126. */ |
| 485 | ret = dbgp_control_msg(ehci_debug, 0, |
| 486 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 487 | USB_REQ_SET_ADDRESS, hub_addr, 0, NULL, 0); |
| 488 | if (ret < 0) |
| 489 | goto err; |
| 490 | |
| 491 | /* Enter configured state on hub. */ |
| 492 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 493 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 494 | USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0); |
| 495 | if (ret < 0) |
| 496 | goto err; |
| 497 | |
| 498 | /* Set PORT_POWER, poll for PORT_CONNECTION. */ |
| 499 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 500 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 501 | USB_REQ_SET_FEATURE, USB_HUB_PORT_POWER, port, NULL, 0); |
| 502 | if (ret < 0) |
| 503 | goto err; |
| 504 | |
| 505 | loop = 100; |
| 506 | do { |
| 507 | dbgp_mdelay(10); |
| 508 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 509 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 510 | USB_REQ_GET_STATUS, 0, port, status, 4); |
| 511 | if (ret < 0) |
| 512 | goto err; |
| 513 | if (hub_port_status(status, USB_HUB_PORT_CONNECTION)) |
| 514 | break; |
| 515 | } while (--loop); |
| 516 | if (! loop) |
| 517 | goto err; |
| 518 | |
| 519 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 520 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 521 | USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_CONNECTION, port, NULL, 0); |
| 522 | if (ret < 0) |
| 523 | goto err; |
| 524 | |
| 525 | |
| 526 | /* Set PORT_RESET, poll for C_PORT_RESET. */ |
| 527 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 528 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 529 | USB_REQ_SET_FEATURE, USB_HUB_PORT_RESET, port, NULL, 0); |
| 530 | if (ret < 0) |
| 531 | goto err; |
| 532 | |
| 533 | loop = 100; |
| 534 | do { |
| 535 | dbgp_mdelay(10); |
| 536 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 537 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 538 | USB_REQ_GET_STATUS, 0, port, status, 4); |
| 539 | if (ret < 0) |
| 540 | goto err; |
| 541 | if (hub_port_status(status, USB_HUB_C_PORT_RESET)) |
| 542 | break; |
| 543 | } while (--loop); |
| 544 | if (! loop) |
| 545 | goto err; |
| 546 | |
| 547 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 548 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 549 | USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_RESET, port, NULL, 0); |
| 550 | if (ret < 0) |
| 551 | goto err; |
| 552 | |
| 553 | if (hub_port_status(status, USB_HUB_PORT_ENABLED)) |
| 554 | return 0; |
| 555 | err: |
| 556 | return -1; |
| 557 | } |
| 558 | #endif /* CONFIG_USBDEBUG_OPTIONAL_HUB_PORT */ |
| 559 | |
Kyösti Mälkki | 8101aa6 | 2013-08-15 16:27:06 +0300 | [diff] [blame] | 560 | |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 561 | static int usbdebug_init_(unsigned ehci_bar, unsigned offset, struct ehci_debug_info *info) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 562 | { |
| 563 | struct ehci_caps *ehci_caps; |
| 564 | struct ehci_regs *ehci_regs; |
| 565 | struct ehci_dbg_port *ehci_debug; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 566 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 567 | struct usb_debug_descriptor dbgp_desc; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 568 | u32 cmd, ctrl, status, portsc, hcs_params; |
| 569 | u32 debug_port, new_debug_port = 0, n_ports; |
| 570 | u32 devnum; |
Kyösti Mälkki | 6bfe61d | 2013-06-06 10:33:39 +0300 | [diff] [blame] | 571 | int ret, i, configured; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 572 | int loop; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 573 | int port_map_tried; |
| 574 | int playtimes = 3; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 575 | |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 576 | dprintk(BIOS_INFO, "ehci_bar: 0x%x debug_offset 0x%x\n", ehci_bar, offset); |
| 577 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 578 | ehci_caps = (struct ehci_caps *)ehci_bar; |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 579 | ehci_regs = (struct ehci_regs *)(ehci_bar + |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 580 | HC_LENGTH(read32((unsigned long)&ehci_caps->hc_capbase))); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 581 | ehci_debug = (struct ehci_dbg_port *)(ehci_bar + offset); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 582 | info->ehci_debug = (void *)0; |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 583 | memset(&info->ep_pipe, 0, sizeof (info->ep_pipe)); |
Kyösti Mälkki | 2410010 | 2013-08-12 20:40:37 +0300 | [diff] [blame] | 584 | |
| 585 | if (CONFIG_USBDEBUG_DEFAULT_PORT > 0) |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 586 | ehci_debug_select_port(CONFIG_USBDEBUG_DEFAULT_PORT); |
Kyösti Mälkki | 2410010 | 2013-08-12 20:40:37 +0300 | [diff] [blame] | 587 | else |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 588 | ehci_debug_select_port(1); |
Kyösti Mälkki | 2410010 | 2013-08-12 20:40:37 +0300 | [diff] [blame] | 589 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 590 | try_next_time: |
| 591 | port_map_tried = 0; |
| 592 | |
| 593 | try_next_port: |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 594 | hcs_params = read32((unsigned long)&ehci_caps->hcs_params); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 595 | debug_port = HCS_DEBUG_PORT(hcs_params); |
| 596 | n_ports = HCS_N_PORTS(hcs_params); |
| 597 | |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 598 | dprintk(BIOS_INFO, "debug_port: %d\n", debug_port); |
| 599 | dprintk(BIOS_INFO, "n_ports: %d\n", n_ports); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 600 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 601 | for (i = 1; i <= n_ports; i++) { |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 602 | portsc = read32((unsigned long)&ehci_regs->port_status[i-1]); |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 603 | dprintk(BIOS_INFO, "PORTSC #%d: %08x\n", i, portsc); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 604 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 605 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 606 | if(port_map_tried && (new_debug_port != debug_port)) { |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 607 | if(--playtimes) { |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 608 | ehci_debug_select_port(debug_port); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 609 | goto try_next_time; |
| 610 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 611 | return -1; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Kyösti Mälkki | 16c0145 | 2013-08-12 15:32:25 +0300 | [diff] [blame] | 614 | /* Wait until the controller is halted */ |
| 615 | status = read32((unsigned long)&ehci_regs->status); |
| 616 | if (!(status & STS_HALT)) { |
| 617 | cmd = read32((unsigned long)&ehci_regs->command); |
| 618 | cmd &= ~CMD_RUN; |
| 619 | write32((unsigned long)&ehci_regs->command, cmd); |
| 620 | loop = 100; |
| 621 | do { |
| 622 | dbgp_mdelay(10); |
| 623 | status = read32((unsigned long)&ehci_regs->status); |
| 624 | } while (!(status & STS_HALT) && (--loop > 0)); |
| 625 | if (status & STS_HALT) |
| 626 | dprintk(BIOS_INFO, "EHCI controller halted successfully.\n"); |
| 627 | else |
| 628 | dprintk(BIOS_INFO, "EHCI controller is not halted. Reset may fail.\n"); |
| 629 | } |
| 630 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 631 | loop = 100; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 632 | /* Reset the EHCI controller */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 633 | cmd = read32((unsigned long)&ehci_regs->command); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 634 | cmd |= CMD_RESET; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 635 | write32((unsigned long)&ehci_regs->command, cmd); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 636 | do { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 637 | dbgp_mdelay(10); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 638 | cmd = read32((unsigned long)&ehci_regs->command); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 639 | } while ((cmd & CMD_RESET) && (--loop > 0)); |
| 640 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 641 | if(!loop) { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 642 | dprintk(BIOS_INFO, "Could not reset EHCI controller.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 643 | // on some systems it works without succeeding here. |
| 644 | // return -2; |
| 645 | } else { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 646 | dprintk(BIOS_INFO, "EHCI controller reset successfully.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 647 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 648 | |
| 649 | /* Claim ownership, but do not enable yet */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 650 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 651 | ctrl |= DBGP_OWNER; |
| 652 | ctrl &= ~(DBGP_ENABLED | DBGP_INUSE); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 653 | write32((unsigned long)&ehci_debug->control, ctrl); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 654 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 655 | /* Start EHCI controller */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 656 | cmd = read32((unsigned long)&ehci_regs->command); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 657 | cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET); |
| 658 | cmd |= CMD_RUN; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 659 | write32((unsigned long)&ehci_regs->command, cmd); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 660 | |
| 661 | /* Ensure everything is routed to the EHCI */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 662 | write32((unsigned long)&ehci_regs->configured_flag, FLAG_CF); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 663 | |
| 664 | /* Wait until the controller is no longer halted */ |
| 665 | loop = 10; |
| 666 | do { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 667 | dbgp_mdelay(10); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 668 | status = read32((unsigned long)&ehci_regs->status); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 669 | } while ((status & STS_HALT) && (--loop > 0)); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 670 | |
| 671 | if(!loop) { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 672 | dprintk(BIOS_INFO, "EHCI could not be started.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 673 | return -3; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 674 | } |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 675 | dprintk(BIOS_INFO, "EHCI started.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 676 | |
| 677 | /* Wait for a device to show up in the debug port */ |
| 678 | ret = ehci_wait_for_port(ehci_regs, debug_port); |
| 679 | if (ret < 0) { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 680 | dprintk(BIOS_INFO, "No device found in debug port %d\n", debug_port); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 681 | goto next_debug_port; |
| 682 | } |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 683 | dprintk(BIOS_INFO, "EHCI done waiting for port.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 684 | |
| 685 | /* Enable the debug port */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 686 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 687 | ctrl |= DBGP_CLAIM; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 688 | write32((unsigned long)&ehci_debug->control, ctrl); |
| 689 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 690 | if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 691 | dprintk(BIOS_INFO, "No device in EHCI debug port.\n"); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 692 | write32((unsigned long)&ehci_debug->control, ctrl & ~DBGP_CLAIM); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 693 | ret = -4; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 694 | goto err; |
| 695 | } |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 696 | dprintk(BIOS_INFO, "EHCI debug port enabled.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 697 | |
| 698 | /* Completely transfer the debug device to the debug controller */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 699 | portsc = read32((unsigned long)&ehci_regs->port_status[debug_port - 1]); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 700 | portsc &= ~PORT_PE; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 701 | write32((unsigned long)&ehci_regs->port_status[debug_port - 1], portsc); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 702 | |
| 703 | dbgp_mdelay(100); |
| 704 | |
Kyösti Mälkki | d2dac0a | 2013-08-23 23:33:16 +0300 | [diff] [blame] | 705 | #if CONFIG_USBDEBUG_OPTIONAL_HUB_PORT |
| 706 | ret = dbgp_hub_enable(ehci_debug, CONFIG_USBDEBUG_OPTIONAL_HUB_PORT); |
| 707 | if (ret < 0) { |
| 708 | dprintk(BIOS_INFO, "Could not enable USB hub on debug port.\n"); |
| 709 | ret = -6; |
| 710 | goto err; |
| 711 | } |
| 712 | #endif |
| 713 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 714 | /* Find the debug device and make it device number 127 */ |
Kyösti Mälkki | a2adaeb | 2013-08-12 00:09:21 +0300 | [diff] [blame] | 715 | devnum = 0; |
| 716 | debug_dev_retry: |
| 717 | memset(&dbgp_desc, 0, sizeof(dbgp_desc)); |
| 718 | ret = dbgp_control_msg(ehci_debug, devnum, |
| 719 | USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 720 | USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0, |
| 721 | &dbgp_desc, sizeof(dbgp_desc)); |
| 722 | if (ret == sizeof(dbgp_desc)) { |
| 723 | if (dbgp_desc.bLength == sizeof(dbgp_desc) && dbgp_desc.bDescriptorType==USB_DT_DEBUG) |
| 724 | goto debug_dev_found; |
| 725 | else |
| 726 | dprintk(BIOS_INFO, "Invalid debug device descriptor.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 727 | } |
Kyösti Mälkki | a2adaeb | 2013-08-12 00:09:21 +0300 | [diff] [blame] | 728 | if (devnum == 0) { |
| 729 | devnum = USB_DEBUG_DEVNUM; |
| 730 | goto debug_dev_retry; |
| 731 | } else { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 732 | dprintk(BIOS_INFO, "Could not find attached debug device.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 733 | ret = -5; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 734 | goto err; |
| 735 | } |
Kyösti Mälkki | a2adaeb | 2013-08-12 00:09:21 +0300 | [diff] [blame] | 736 | debug_dev_found: |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 737 | |
| 738 | /* Move the device to 127 if it isn't already there */ |
| 739 | if (devnum != USB_DEBUG_DEVNUM) { |
| 740 | ret = dbgp_control_msg(ehci_debug, devnum, |
| 741 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 742 | USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 743 | if (ret < 0) { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 744 | dprintk(BIOS_INFO, "Could not move attached device to %d.\n", |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 745 | USB_DEBUG_DEVNUM); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 746 | ret = -7; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 747 | goto err; |
| 748 | } |
| 749 | devnum = USB_DEBUG_DEVNUM; |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 750 | dprintk(BIOS_INFO, "EHCI debug device renamed to 127.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* Enable the debug interface */ |
| 754 | ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM, |
| 755 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 756 | USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 757 | if (ret < 0) { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 758 | dprintk(BIOS_INFO, "Could not enable EHCI debug device.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 759 | ret = -8; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 760 | goto err; |
| 761 | } |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 762 | dprintk(BIOS_INFO, "EHCI debug interface enabled.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 763 | |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 764 | /* Prepare endpoint pipes. */ |
| 765 | for (i=1; i<DBGP_MAX_ENDPOINTS; i++) { |
| 766 | info->ep_pipe[i].devnum = USB_DEBUG_DEVNUM; |
Kyösti Mälkki | e29584c | 2013-08-10 10:50:38 +0300 | [diff] [blame] | 767 | info->ep_pipe[i].pid = USB_PID_DATA0; |
Kyösti Mälkki | 5c87d2f1 | 2013-08-19 12:45:16 +0300 | [diff] [blame] | 768 | info->ep_pipe[i].timeout = 1000; |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 769 | } |
| 770 | info->ep_pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint; |
| 771 | info->ep_pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint; |
| 772 | |
| 773 | /* Perform a small write. */ |
Kyösti Mälkki | 6bfe61d | 2013-06-06 10:33:39 +0300 | [diff] [blame] | 774 | configured = 0; |
| 775 | small_write: |
Kyösti Mälkki | 75d0062 | 2013-08-10 10:34:01 +0300 | [diff] [blame] | 776 | ret = dbgp_bulk_write(ehci_debug, &info->ep_pipe[DBGP_CONSOLE_EPOUT], "USB\r\n",5); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 777 | if (ret < 0) { |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 778 | dprintk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret); |
Kyösti Mälkki | 6bfe61d | 2013-06-06 10:33:39 +0300 | [diff] [blame] | 779 | if (!configured) { |
| 780 | /* Send Set Configure request to device. This is required for FX2 |
| 781 | (CY7C68013) to transfer from USB state Addressed to Configured, |
| 782 | only then endpoints other than 0 are enabled. */ |
| 783 | if (dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM, |
| 784 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 785 | USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0) >= 0) { |
| 786 | configured = 1; |
| 787 | goto small_write; |
| 788 | } |
| 789 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 790 | ret = -9; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 791 | goto err; |
| 792 | } |
Kyösti Mälkki | 8ff3d68 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 793 | dprintk(BIOS_INFO, "Test write done\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 794 | |
| 795 | info->ehci_caps = ehci_caps; |
| 796 | info->ehci_regs = ehci_regs; |
| 797 | info->ehci_debug = ehci_debug; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 798 | |
Kyösti Mälkki | d799140 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 799 | info->ep_pipe[DBGP_SETUP_EP0].status |= DBGP_EP_ENABLED | DBGP_EP_VALID; |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 800 | info->ep_pipe[DBGP_CONSOLE_EPOUT].status |= DBGP_EP_ENABLED | DBGP_EP_VALID; |
| 801 | info->ep_pipe[DBGP_CONSOLE_EPIN].status |= DBGP_EP_ENABLED | DBGP_EP_VALID; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 802 | return 0; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 803 | err: |
| 804 | /* Things didn't work so remove my claim */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 805 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 806 | ctrl &= ~(DBGP_CLAIM | DBGP_OUT); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 807 | write32((unsigned long)(unsigned long)&ehci_debug->control, ctrl); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 808 | //return ret; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 809 | |
| 810 | next_debug_port: |
Kyösti Mälkki | 2410010 | 2013-08-12 20:40:37 +0300 | [diff] [blame] | 811 | #if CONFIG_USBDEBUG_DEFAULT_PORT==0 |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 812 | port_map_tried |= (1 << (debug_port - 1)); |
| 813 | new_debug_port = ((debug_port-1 + 1) % n_ports) + 1; |
| 814 | if (port_map_tried != ((1 << n_ports) - 1)) { |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 815 | ehci_debug_select_port(new_debug_port); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 816 | goto try_next_port; |
| 817 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 818 | if (--playtimes) { |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 819 | ehci_debug_select_port(new_debug_port); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 820 | goto try_next_time; |
| 821 | } |
Kyösti Mälkki | 2410010 | 2013-08-12 20:40:37 +0300 | [diff] [blame] | 822 | #else |
| 823 | if (0) |
| 824 | goto try_next_port; |
| 825 | if (--playtimes) |
| 826 | goto try_next_time; |
| 827 | #endif |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 828 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 829 | return -10; |
| 830 | } |
| 831 | |
Kyösti Mälkki | d799140 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 832 | #if CONFIG_DEBUG_USBDEBUG |
| 833 | static int dbgp_enabled(void) |
| 834 | { |
| 835 | struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0]; |
| 836 | return (globals->status & DBGP_EP_ENABLED); |
| 837 | } |
| 838 | #endif |
| 839 | |
Kyösti Mälkki | 8cde852 | 2014-02-09 23:35:39 +0200 | [diff] [blame] | 840 | int dbgp_try_get(struct dbgp_pipe *pipe) |
Kyösti Mälkki | d799140 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 841 | { |
| 842 | struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0]; |
| 843 | if (!dbgp_ep_is_active(pipe) || (globals->status & DBGP_EP_BUSY)) |
| 844 | return 0; |
| 845 | globals->status |= DBGP_EP_BUSY; |
| 846 | pipe->status |= DBGP_EP_BUSY; |
| 847 | return 1; |
| 848 | } |
| 849 | |
Kyösti Mälkki | 8cde852 | 2014-02-09 23:35:39 +0200 | [diff] [blame] | 850 | void dbgp_put(struct dbgp_pipe *pipe) |
Kyösti Mälkki | d799140 | 2013-08-12 16:11:34 +0300 | [diff] [blame] | 851 | { |
| 852 | struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0]; |
| 853 | globals->status &= ~DBGP_EP_BUSY; |
| 854 | pipe->status &= ~DBGP_EP_BUSY; |
| 855 | } |
| 856 | |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 857 | #if !defined(__PRE_RAM__) && !defined(__SMM__) |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 858 | void usbdebug_re_enable(unsigned ehci_base) |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 859 | { |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 860 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 861 | unsigned diff; |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 862 | int i; |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 863 | |
| 864 | if (!dbg_info->ehci_debug) |
| 865 | return; |
| 866 | |
| 867 | diff = (unsigned)dbg_info->ehci_caps - ehci_base; |
| 868 | dbg_info->ehci_regs -= diff; |
| 869 | dbg_info->ehci_debug -= diff; |
| 870 | dbg_info->ehci_caps = (void*)ehci_base; |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 871 | |
| 872 | for (i=0; i<DBGP_MAX_ENDPOINTS; i++) |
| 873 | dbg_info->ep_pipe[i].status |= DBGP_EP_ENABLED; |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 874 | } |
| 875 | |
Kyösti Mälkki | cb141bc | 2014-02-07 19:24:23 +0200 | [diff] [blame] | 876 | void usbdebug_disable(void) |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 877 | { |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 878 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 879 | int i; |
| 880 | for (i=0; i<DBGP_MAX_ENDPOINTS; i++) |
| 881 | dbg_info->ep_pipe[i].status &= ~DBGP_EP_ENABLED; |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 882 | } |
| 883 | |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 884 | #endif |
| 885 | |
Kyösti Mälkki | 690bf2f | 2013-07-06 11:41:21 +0300 | [diff] [blame] | 886 | #if !defined(__PRE_RAM__) && !defined(__SMM__) |
| 887 | static int get_usbdebug_from_cbmem(struct ehci_debug_info *info) |
| 888 | { |
| 889 | struct ehci_debug_info *dbg_info_cbmem; |
| 890 | |
| 891 | dbg_info_cbmem = cbmem_find(CBMEM_ID_EHCI_DEBUG); |
| 892 | if (dbg_info_cbmem == NULL) |
| 893 | return -1; |
| 894 | |
| 895 | memcpy(info, dbg_info_cbmem, sizeof (*info)); |
| 896 | printk(BIOS_DEBUG, "EHCI debug port found in CBMEM.\n"); |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | #elif defined(__PRE_RAM__) |
| 902 | static void migrate_ehci_debug(void) |
| 903 | { |
| 904 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
| 905 | struct ehci_debug_info *dbg_info_cbmem; |
| 906 | |
| 907 | dbg_info_cbmem = cbmem_add(CBMEM_ID_EHCI_DEBUG, sizeof(*dbg_info)); |
| 908 | if (dbg_info_cbmem == NULL) |
| 909 | return; |
| 910 | |
| 911 | memcpy(dbg_info_cbmem, dbg_info, sizeof(*dbg_info)); |
Kyösti Mälkki | 618d179 | 2014-10-27 08:01:55 +0200 | [diff] [blame^] | 912 | car_set_var(glob_dbg_info_p, dbg_info_cbmem); |
Kyösti Mälkki | 690bf2f | 2013-07-06 11:41:21 +0300 | [diff] [blame] | 913 | } |
| 914 | CAR_MIGRATE(migrate_ehci_debug); |
| 915 | #endif |
Kyösti Mälkki | 690bf2f | 2013-07-06 11:41:21 +0300 | [diff] [blame] | 916 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 917 | int dbgp_ep_is_active(struct dbgp_pipe *pipe) |
Kyösti Mälkki | 4d409b5 | 2013-07-05 21:38:54 +0300 | [diff] [blame] | 918 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 919 | return (pipe->status & DBGP_EP_STATMASK) == (DBGP_EP_VALID | DBGP_EP_ENABLED); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 920 | } |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 921 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 922 | struct dbgp_pipe *dbgp_console_output(void) |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 923 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 924 | return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPOUT]; |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 925 | } |
| 926 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 927 | struct dbgp_pipe *dbgp_console_input(void) |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 928 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 929 | return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPIN]; |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | int usbdebug_init(void) |
| 933 | { |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 934 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 935 | unsigned int ehci_base, dbg_offset; |
Kyösti Mälkki | 8ee04d7 | 2013-07-06 11:41:09 +0300 | [diff] [blame] | 936 | |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 937 | #if !defined(__PRE_RAM__) && !defined(__SMM__) |
Kyösti Mälkki | 690bf2f | 2013-07-06 11:41:21 +0300 | [diff] [blame] | 938 | if (!get_usbdebug_from_cbmem(dbg_info)) |
| 939 | return 0; |
| 940 | #endif |
Kyösti Mälkki | 6f6a249 | 2014-02-09 19:21:30 +0200 | [diff] [blame] | 941 | if (ehci_debug_hw_enable(&ehci_base, &dbg_offset)) |
| 942 | return -1; |
| 943 | return usbdebug_init_(ehci_base, dbg_offset, dbg_info); |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 944 | } |