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