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> |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 25 | #include <arch/byteorder.h> |
Kyösti Mälkki | 8ee04d7 | 2013-07-06 11:41:09 +0300 | [diff] [blame] | 26 | #include <cpu/x86/car.h> |
Kyösti Mälkki | e23c22d | 2013-07-28 23:16:47 +0300 | [diff] [blame] | 27 | #include <string.h> |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 28 | |
| 29 | #include <usb_ch9.h> |
| 30 | #include <ehci.h> |
Stefan Reinauer | da32373 | 2010-05-25 16:17:45 +0000 | [diff] [blame] | 31 | #include <usbdebug.h> |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 32 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 33 | |
| 34 | #define DBGP_EP_VALID (1<<0) |
| 35 | #define DBGP_EP_ENABLED (1<<1) |
| 36 | #define DBGP_EP_STATMASK (DBGP_EP_VALID | DBGP_EP_ENABLED) |
| 37 | |
| 38 | struct dbgp_pipe |
| 39 | { |
| 40 | u8 endpoint; |
| 41 | u8 status; |
| 42 | u8 bufidx; |
| 43 | char buf[8]; |
| 44 | }; |
| 45 | |
| 46 | #define DBGP_MAX_ENDPOINTS 4 |
| 47 | #define DBGP_CONSOLE_EPOUT 0 |
| 48 | #define DBGP_CONSOLE_EPIN 1 |
| 49 | |
| 50 | struct ehci_debug_info { |
| 51 | u8 devnum; |
| 52 | |
| 53 | void *ehci_caps; |
| 54 | void *ehci_regs; |
| 55 | void *ehci_debug; |
| 56 | |
| 57 | struct dbgp_pipe ep_pipe[DBGP_MAX_ENDPOINTS]; |
| 58 | }; |
| 59 | |
Kyösti Mälkki | 3be80cc | 2013-06-06 10:46:37 +0300 | [diff] [blame^] | 60 | #if CONFIG_DEBUG_USBDEBUG |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 61 | # define dbgp_printk(fmt_arg...) printk(BIOS_DEBUG, fmt_arg) |
| 62 | #else |
Kyösti Mälkki | 3be80cc | 2013-06-06 10:46:37 +0300 | [diff] [blame^] | 63 | # define dbgp_printk(fmt_arg...) do {} while(0) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 66 | #define USB_DEBUG_DEVNUM 127 |
| 67 | |
| 68 | #define DBGP_DATA_TOGGLE 0x8800 |
| 69 | #define DBGP_PID_UPDATE(x, tok) \ |
| 70 | ((((x) ^ DBGP_DATA_TOGGLE) & 0xffff00) | ((tok) & 0xff)) |
| 71 | |
| 72 | #define DBGP_LEN_UPDATE(x, len) (((x) & ~0x0f) | ((len) & 0x0f)) |
| 73 | /* |
| 74 | * USB Packet IDs (PIDs) |
| 75 | */ |
| 76 | |
| 77 | /* token */ |
| 78 | #define USB_PID_OUT 0xe1 |
| 79 | #define USB_PID_IN 0x69 |
| 80 | #define USB_PID_SOF 0xa5 |
| 81 | #define USB_PID_SETUP 0x2d |
| 82 | /* handshake */ |
| 83 | #define USB_PID_ACK 0xd2 |
| 84 | #define USB_PID_NAK 0x5a |
| 85 | #define USB_PID_STALL 0x1e |
| 86 | #define USB_PID_NYET 0x96 |
| 87 | /* data */ |
| 88 | #define USB_PID_DATA0 0xc3 |
| 89 | #define USB_PID_DATA1 0x4b |
| 90 | #define USB_PID_DATA2 0x87 |
| 91 | #define USB_PID_MDATA 0x0f |
| 92 | /* Special */ |
| 93 | #define USB_PID_PREAMBLE 0x3c |
| 94 | #define USB_PID_ERR 0x3c |
| 95 | #define USB_PID_SPLIT 0x78 |
| 96 | #define USB_PID_PING 0xb4 |
| 97 | #define USB_PID_UNDEF_0 0xf0 |
| 98 | |
| 99 | #define USB_PID_DATA_TOGGLE 0x88 |
| 100 | #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE) |
| 101 | |
| 102 | #define PCI_CAP_ID_EHCI_DEBUG 0xa |
| 103 | |
| 104 | #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ |
| 105 | #define HUB_SHORT_RESET_TIME 10 |
| 106 | #define HUB_LONG_RESET_TIME 200 |
| 107 | #define HUB_RESET_TIMEOUT 500 |
| 108 | |
| 109 | #define DBGP_MAX_PACKET 8 |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 110 | #define DBGP_LOOPS 1000 |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 111 | |
Kyösti Mälkki | 8ee04d7 | 2013-07-06 11:41:09 +0300 | [diff] [blame] | 112 | static struct ehci_debug_info glob_dbg_info CAR_GLOBAL; |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 113 | #if !defined(__PRE_RAM__) && !defined(__SMM__) |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 114 | static struct device_operations *ehci_drv_ops; |
| 115 | static struct device_operations ehci_dbg_ops; |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 116 | #endif |
| 117 | |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 118 | static inline struct ehci_debug_info *dbgp_ehci_info(void) |
| 119 | { |
| 120 | return car_get_var_ptr(&glob_dbg_info); |
| 121 | } |
| 122 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 123 | static int dbgp_wait_until_complete(struct ehci_dbg_port *ehci_debug) |
| 124 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 125 | u32 ctrl; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 126 | int loop = 0x100000; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 127 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 128 | do { |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 129 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 130 | /* Stop when the transaction is finished */ |
| 131 | if (ctrl & DBGP_DONE) |
| 132 | break; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 133 | } while (--loop > 0); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 134 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 135 | if (!loop) |
| 136 | return -1; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 137 | |
| 138 | /* Now that we have observed the completed transaction, |
| 139 | * clear the done bit. |
| 140 | */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 141 | write32((unsigned long)&ehci_debug->control, ctrl | DBGP_DONE); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 142 | return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl); |
| 143 | } |
| 144 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 145 | static void dbgp_breath(void) |
| 146 | { |
| 147 | /* Sleep to give the debug port a chance to breathe */ |
| 148 | } |
| 149 | |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 150 | static int dbgp_wait_until_done(struct ehci_dbg_port *ehci_debug, unsigned ctrl, int loop) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 151 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 152 | u32 pids, lpid; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 153 | int ret; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 154 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 155 | retry: |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 156 | write32((unsigned long)&ehci_debug->control, ctrl | DBGP_GO); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 157 | ret = dbgp_wait_until_complete(ehci_debug); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 158 | pids = read32((unsigned long)&ehci_debug->pids); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 159 | lpid = DBGP_PID_GET(pids); |
| 160 | |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 161 | if (ret < 0) { |
| 162 | if (ret == -DBGP_ERR_BAD && --loop > 0) |
| 163 | goto retry; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 164 | return ret; |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 165 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 166 | |
| 167 | /* If the port is getting full or it has dropped data |
| 168 | * start pacing ourselves, not necessary but it's friendly. |
| 169 | */ |
| 170 | if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET)) |
| 171 | dbgp_breath(); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 172 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 173 | /* If I get a NACK reissue the transmission */ |
| 174 | if (lpid == USB_PID_NAK) { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 175 | if (--loop > 0) |
| 176 | goto retry; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static void dbgp_set_data(struct ehci_dbg_port *ehci_debug, const void *buf, int size) |
| 183 | { |
| 184 | const unsigned char *bytes = buf; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 185 | u32 lo, hi; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 186 | int i; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 187 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 188 | lo = hi = 0; |
| 189 | for (i = 0; i < 4 && i < size; i++) |
| 190 | lo |= bytes[i] << (8*i); |
| 191 | for (; i < 8 && i < size; i++) |
| 192 | hi |= bytes[i] << (8*(i - 4)); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 193 | write32((unsigned long)&ehci_debug->data03, lo); |
| 194 | write32((unsigned long)&ehci_debug->data47, hi); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void dbgp_get_data(struct ehci_dbg_port *ehci_debug, void *buf, int size) |
| 198 | { |
| 199 | unsigned char *bytes = buf; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 200 | u32 lo, hi; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 201 | int i; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 202 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 203 | lo = read32((unsigned long)&ehci_debug->data03); |
| 204 | hi = read32((unsigned long)&ehci_debug->data47); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 205 | for (i = 0; i < 4 && i < size; i++) |
| 206 | bytes[i] = (lo >> (8*i)) & 0xff; |
| 207 | for (; i < 8 && i < size; i++) |
| 208 | bytes[i] = (hi >> (8*(i - 4))) & 0xff; |
| 209 | } |
| 210 | |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 211 | static int dbgp_bulk_write(struct ehci_dbg_port *ehci_debug, |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 212 | unsigned devnum, unsigned endpoint, const char *bytes, int size) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 213 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 214 | u32 pids, addr, ctrl; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 215 | int ret; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 216 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 217 | if (size > DBGP_MAX_PACKET) |
| 218 | return -1; |
| 219 | |
| 220 | addr = DBGP_EPADDR(devnum, endpoint); |
| 221 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 222 | pids = read32((unsigned long)&ehci_debug->pids); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 223 | pids = DBGP_PID_UPDATE(pids, USB_PID_OUT); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 224 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 225 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 226 | ctrl = DBGP_LEN_UPDATE(ctrl, size); |
| 227 | ctrl |= DBGP_OUT; |
| 228 | ctrl |= DBGP_GO; |
| 229 | |
| 230 | dbgp_set_data(ehci_debug, bytes, size); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 231 | write32((unsigned long)&ehci_debug->address, addr); |
| 232 | write32((unsigned long)&ehci_debug->pids, pids); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 233 | |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 234 | ret = dbgp_wait_until_done(ehci_debug, ctrl, DBGP_LOOPS); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 235 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 236 | return ret; |
| 237 | } |
| 238 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 239 | 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] | 240 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 241 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 242 | return dbgp_bulk_write(dbg_info->ehci_debug, dbg_info->devnum, |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 243 | pipe->endpoint, bytes, size); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 246 | static int dbgp_bulk_read(struct ehci_dbg_port *ehci_debug, unsigned devnum, |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 247 | unsigned endpoint, void *data, int size, int loops) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 248 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 249 | u32 pids, addr, ctrl; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 250 | int ret; |
| 251 | |
| 252 | if (size > DBGP_MAX_PACKET) |
| 253 | return -1; |
| 254 | |
| 255 | addr = DBGP_EPADDR(devnum, endpoint); |
| 256 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 257 | pids = read32((unsigned long)&ehci_debug->pids); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 258 | pids = DBGP_PID_UPDATE(pids, USB_PID_IN); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 259 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 260 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 261 | ctrl = DBGP_LEN_UPDATE(ctrl, size); |
| 262 | ctrl &= ~DBGP_OUT; |
| 263 | ctrl |= DBGP_GO; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 264 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 265 | write32((unsigned long)&ehci_debug->address, addr); |
| 266 | write32((unsigned long)&ehci_debug->pids, pids); |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 267 | ret = dbgp_wait_until_done(ehci_debug, ctrl, loops); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 268 | if (ret < 0) |
| 269 | return ret; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 270 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 271 | if (size > ret) |
| 272 | size = ret; |
| 273 | dbgp_get_data(ehci_debug, data, size); |
| 274 | return ret; |
| 275 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 276 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 277 | 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] | 278 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 279 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 280 | return dbgp_bulk_read(dbg_info->ehci_debug, dbg_info->devnum, |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 281 | pipe->endpoint, data, size, DBGP_LOOPS); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 284 | static void dbgp_mdelay(int ms) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 285 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 286 | int i; |
| 287 | |
| 288 | while (ms--) { |
| 289 | for (i = 0; i < 1000; i++) |
| 290 | inb(0x80); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static int dbgp_control_msg(struct ehci_dbg_port *ehci_debug, unsigned devnum, int requesttype, |
| 295 | int request, int value, int index, void *data, int size) |
| 296 | { |
| 297 | u32 pids, addr, ctrl; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 298 | struct usb_ctrlrequest req; |
| 299 | int read; |
| 300 | int ret; |
| 301 | |
| 302 | read = (requesttype & USB_DIR_IN) != 0; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 303 | if (size > (read ? DBGP_MAX_PACKET:0)) |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 304 | return -1; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 305 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 306 | /* Compute the control message */ |
| 307 | req.bRequestType = requesttype; |
| 308 | req.bRequest = request; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 309 | req.wValue = cpu_to_le16(value); |
| 310 | req.wIndex = cpu_to_le16(index); |
| 311 | req.wLength = cpu_to_le16(size); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 312 | |
| 313 | pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP); |
| 314 | addr = DBGP_EPADDR(devnum, 0); |
| 315 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 316 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 317 | ctrl = DBGP_LEN_UPDATE(ctrl, sizeof(req)); |
| 318 | ctrl |= DBGP_OUT; |
| 319 | ctrl |= DBGP_GO; |
| 320 | |
| 321 | /* Send the setup message */ |
| 322 | dbgp_set_data(ehci_debug, &req, sizeof(req)); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 323 | write32((unsigned long)&ehci_debug->address, addr); |
| 324 | write32((unsigned long)&ehci_debug->pids, pids); |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 325 | ret = dbgp_wait_until_done(ehci_debug, ctrl, DBGP_LOOPS); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 326 | if (ret < 0) |
| 327 | return ret; |
| 328 | |
| 329 | |
| 330 | /* Read the result */ |
Sven Schnelle | 1068087 | 2012-07-25 14:19:45 +0200 | [diff] [blame] | 331 | ret = dbgp_bulk_read(ehci_debug, devnum, 0, data, size, DBGP_LOOPS); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | static int ehci_reset_port(struct ehci_regs *ehci_regs, int port) |
| 336 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 337 | u32 portsc; |
| 338 | u32 delay_time, delay_ms; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 339 | int loop; |
| 340 | |
| 341 | /* Reset the usb debug port */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 342 | portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 343 | portsc &= ~PORT_PE; |
| 344 | portsc |= PORT_RESET; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 345 | write32((unsigned long)&ehci_regs->port_status[port - 1], portsc); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 346 | |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 347 | delay_ms = HUB_ROOT_RESET_TIME; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 348 | for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT; |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 349 | delay_time += delay_ms) { |
| 350 | dbgp_mdelay(delay_ms); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 351 | |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 352 | portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 353 | if (portsc & PORT_RESET) { |
| 354 | /* force reset to complete */ |
| 355 | loop = 2; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 356 | write32((unsigned long)&ehci_regs->port_status[port - 1], |
Stefan Reinauer | 9fe4d79 | 2010-01-16 17:53:38 +0000 | [diff] [blame] | 357 | portsc & ~(PORT_RWC_BITS | PORT_RESET)); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 358 | do { |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 359 | dbgp_mdelay(delay_ms); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 360 | portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]); |
Uwe Hermann | a34a0b1 | 2010-09-22 23:42:32 +0000 | [diff] [blame] | 361 | delay_time += delay_ms; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 362 | } while ((portsc & PORT_RESET) && (--loop > 0)); |
| 363 | if (!loop) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 364 | printk(BIOS_DEBUG, "ehci_reset_port forced done"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | /* Device went away? */ |
| 369 | if (!(portsc & PORT_CONNECT)) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 370 | return -1; //-ENOTCONN; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 371 | |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 372 | /* bomb out completely if something weird happened */ |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 373 | if ((portsc & PORT_CSC)) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 374 | return -2; //-EINVAL; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 375 | |
| 376 | /* If we've finished resetting, then break out of the loop */ |
| 377 | if (!(portsc & PORT_RESET) && (portsc & PORT_PE)) |
| 378 | return 0; |
| 379 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 380 | return -3; //-EBUSY; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 383 | 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] | 384 | { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 385 | u32 status; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 386 | int ret, reps; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 387 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 388 | for (reps = 0; reps < 3; reps++) { |
| 389 | dbgp_mdelay(100); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 390 | status = read32((unsigned long)&ehci_regs->status); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 391 | if (status & STS_PCD) { |
| 392 | ret = ehci_reset_port(ehci_regs, port); |
| 393 | if (ret == 0) |
| 394 | return 0; |
| 395 | } |
| 396 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 397 | return -1; //-ENOTCONN; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 400 | 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] | 401 | { |
| 402 | struct ehci_caps *ehci_caps; |
| 403 | struct ehci_regs *ehci_regs; |
| 404 | struct ehci_dbg_port *ehci_debug; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 405 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 406 | struct usb_debug_descriptor dbgp_desc; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 407 | u32 cmd, ctrl, status, portsc, hcs_params; |
| 408 | u32 debug_port, new_debug_port = 0, n_ports; |
| 409 | u32 devnum; |
| 410 | int ret, i; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 411 | int loop; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 412 | int port_map_tried; |
| 413 | int playtimes = 3; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 414 | |
| 415 | ehci_caps = (struct ehci_caps *)ehci_bar; |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 416 | ehci_regs = (struct ehci_regs *)(ehci_bar + |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 417 | HC_LENGTH(read32((unsigned long)&ehci_caps->hc_capbase))); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 418 | ehci_debug = (struct ehci_dbg_port *)(ehci_bar + offset); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 419 | info->ehci_debug = (void *)0; |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 420 | |
| 421 | memset(&info->ep_pipe, 0, sizeof (info->ep_pipe)); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 422 | try_next_time: |
| 423 | port_map_tried = 0; |
| 424 | |
| 425 | try_next_port: |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 426 | hcs_params = read32((unsigned long)&ehci_caps->hcs_params); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 427 | debug_port = HCS_DEBUG_PORT(hcs_params); |
| 428 | n_ports = HCS_N_PORTS(hcs_params); |
| 429 | |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 430 | dbgp_printk("ehci_bar: 0x%x\n", ehci_bar); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 431 | dbgp_printk("debug_port: %d\n", debug_port); |
| 432 | dbgp_printk("n_ports: %d\n", n_ports); |
| 433 | |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 434 | for (i = 1; i <= n_ports; i++) { |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 435 | portsc = read32((unsigned long)&ehci_regs->port_status[i-1]); |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 436 | dbgp_printk("PORTSC #%d: %08x\n", i, portsc); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 437 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 438 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 439 | if(port_map_tried && (new_debug_port != debug_port)) { |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 440 | if(--playtimes) { |
| 441 | set_debug_port(debug_port); |
| 442 | goto try_next_time; |
| 443 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 444 | return -1; |
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 | loop = 100; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 448 | /* Reset the EHCI controller */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 449 | cmd = read32((unsigned long)&ehci_regs->command); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 450 | cmd |= CMD_RESET; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 451 | write32((unsigned long)&ehci_regs->command, cmd); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 452 | do { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 453 | dbgp_mdelay(10); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 454 | cmd = read32((unsigned long)&ehci_regs->command); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 455 | } while ((cmd & CMD_RESET) && (--loop > 0)); |
| 456 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 457 | if(!loop) { |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 458 | dbgp_printk("Could not reset EHCI controller.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 459 | // on some systems it works without succeeding here. |
| 460 | // return -2; |
| 461 | } else { |
Stefan Reinauer | b53bed1 | 2009-10-24 13:02:14 +0000 | [diff] [blame] | 462 | dbgp_printk("EHCI controller reset successfully.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 463 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 464 | |
| 465 | /* Claim ownership, but do not enable yet */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 466 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 467 | ctrl |= DBGP_OWNER; |
| 468 | ctrl &= ~(DBGP_ENABLED | DBGP_INUSE); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 469 | write32((unsigned long)&ehci_debug->control, ctrl); |
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 | /* Start EHCI controller */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 472 | cmd = read32((unsigned long)&ehci_regs->command); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 473 | cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET); |
| 474 | cmd |= CMD_RUN; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 475 | write32((unsigned long)&ehci_regs->command, cmd); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 476 | |
| 477 | /* Ensure everything is routed to the EHCI */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 478 | write32((unsigned long)&ehci_regs->configured_flag, FLAG_CF); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 479 | |
| 480 | /* Wait until the controller is no longer halted */ |
| 481 | loop = 10; |
| 482 | do { |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 483 | dbgp_mdelay(10); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 484 | status = read32((unsigned long)&ehci_regs->status); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 485 | } while ((status & STS_HALT) && (--loop > 0)); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 486 | |
| 487 | if(!loop) { |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 488 | dbgp_printk("EHCI could not be started.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 489 | return -3; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 490 | } |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 491 | dbgp_printk("EHCI started.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 492 | |
| 493 | /* Wait for a device to show up in the debug port */ |
| 494 | ret = ehci_wait_for_port(ehci_regs, debug_port); |
| 495 | if (ret < 0) { |
| 496 | dbgp_printk("No device found in debug port %d\n", debug_port); |
| 497 | goto next_debug_port; |
| 498 | } |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 499 | dbgp_printk("EHCI done waiting for port.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 500 | |
| 501 | /* Enable the debug port */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 502 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 503 | ctrl |= DBGP_CLAIM; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 504 | write32((unsigned long)&ehci_debug->control, ctrl); |
| 505 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 506 | if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) { |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 507 | dbgp_printk("No device in EHCI debug port.\n"); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 508 | write32((unsigned long)&ehci_debug->control, ctrl & ~DBGP_CLAIM); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 509 | ret = -4; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 510 | goto err; |
| 511 | } |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 512 | dbgp_printk("EHCI debug port enabled.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 513 | |
| 514 | /* Completely transfer the debug device to the debug controller */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 515 | portsc = read32((unsigned long)&ehci_regs->port_status[debug_port - 1]); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 516 | portsc &= ~PORT_PE; |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 517 | write32((unsigned long)&ehci_regs->port_status[debug_port - 1], portsc); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 518 | |
| 519 | dbgp_mdelay(100); |
| 520 | |
| 521 | /* Find the debug device and make it device number 127 */ |
| 522 | for (devnum = 0; devnum <= 127; devnum++) { |
| 523 | ret = dbgp_control_msg(ehci_debug, devnum, |
| 524 | USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 525 | USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0, |
| 526 | &dbgp_desc, sizeof(dbgp_desc)); |
| 527 | if (ret > 0) |
| 528 | break; |
| 529 | } |
| 530 | if (devnum > 127) { |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 531 | dbgp_printk("Could not find attached debug device.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 532 | ret = -5; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 533 | goto err; |
| 534 | } |
| 535 | if (ret < 0) { |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 536 | dbgp_printk("Attached device is not a debug device.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 537 | ret = -6; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 538 | goto err; |
| 539 | } |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 540 | |
| 541 | /* Move the device to 127 if it isn't already there */ |
| 542 | if (devnum != USB_DEBUG_DEVNUM) { |
| 543 | ret = dbgp_control_msg(ehci_debug, devnum, |
| 544 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 545 | USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 546 | if (ret < 0) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 547 | dbgp_printk("Could not move attached device to %d.\n", |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 548 | USB_DEBUG_DEVNUM); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 549 | ret = -7; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 550 | goto err; |
| 551 | } |
| 552 | devnum = USB_DEBUG_DEVNUM; |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 553 | dbgp_printk("EHCI debug device renamed to 127.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* Enable the debug interface */ |
| 557 | ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM, |
| 558 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 559 | USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 560 | if (ret < 0) { |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 561 | dbgp_printk("Could not enable EHCI debug device.\n"); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 562 | ret = -8; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 563 | goto err; |
| 564 | } |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 565 | dbgp_printk("EHCI debug interface enabled.\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 566 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 567 | /* Perform a small write to get the even/odd data state in sync */ |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 568 | ret = dbgp_bulk_write(ehci_debug, USB_DEBUG_DEVNUM, dbgp_desc.bDebugOutEndpoint, "USB\r\n",5); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 569 | if (ret < 0) { |
| 570 | dbgp_printk("dbgp_bulk_write failed: %d\n", ret); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 571 | ret = -9; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 572 | goto err; |
| 573 | } |
Stefan Reinauer | 7e16bf3 | 2009-04-21 23:05:13 +0000 | [diff] [blame] | 574 | dbgp_printk("Test write done\n"); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 575 | |
| 576 | info->ehci_caps = ehci_caps; |
| 577 | info->ehci_regs = ehci_regs; |
| 578 | info->ehci_debug = ehci_debug; |
| 579 | info->devnum = devnum; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 580 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 581 | info->ep_pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint; |
| 582 | info->ep_pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint; |
| 583 | info->ep_pipe[DBGP_CONSOLE_EPOUT].status |= DBGP_EP_ENABLED | DBGP_EP_VALID; |
| 584 | 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] | 585 | return 0; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 586 | err: |
| 587 | /* Things didn't work so remove my claim */ |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 588 | ctrl = read32((unsigned long)&ehci_debug->control); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 589 | ctrl &= ~(DBGP_CLAIM | DBGP_OUT); |
Stefan Reinauer | 75a05dc | 2010-05-25 16:35:51 +0000 | [diff] [blame] | 590 | write32((unsigned long)(unsigned long)&ehci_debug->control, ctrl); |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 591 | //return ret; |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 592 | |
| 593 | next_debug_port: |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 594 | port_map_tried |= (1 << (debug_port - 1)); |
| 595 | new_debug_port = ((debug_port-1 + 1) % n_ports) + 1; |
| 596 | if (port_map_tried != ((1 << n_ports) - 1)) { |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 597 | set_debug_port(new_debug_port); |
| 598 | goto try_next_port; |
| 599 | } |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 600 | if (--playtimes) { |
| 601 | //set_debug_port(new_debug_port); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 602 | set_debug_port(debug_port); |
| 603 | goto try_next_time; |
| 604 | } |
| 605 | |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 606 | return -10; |
| 607 | } |
| 608 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 609 | void usbdebug_tx_byte(struct dbgp_pipe *pipe, unsigned char data) |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 610 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 611 | if (dbgp_ep_is_active(pipe)) { |
| 612 | pipe->buf[pipe->bufidx++] = data; |
| 613 | if (pipe->bufidx >= 8) { |
| 614 | dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx); |
| 615 | pipe->bufidx = 0; |
Sven Schnelle | 82704c6 | 2012-07-26 14:31:40 +0200 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 620 | void usbdebug_tx_flush(struct dbgp_pipe *pipe) |
Sven Schnelle | 82704c6 | 2012-07-26 14:31:40 +0200 | [diff] [blame] | 621 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 622 | if (dbgp_ep_is_active(pipe) && pipe->bufidx > 0) { |
| 623 | dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx); |
| 624 | pipe->bufidx = 0; |
Stefan Reinauer | 16ce01b | 2011-01-28 08:05:54 +0000 | [diff] [blame] | 625 | } |
Kyösti Mälkki | 4d409b5 | 2013-07-05 21:38:54 +0300 | [diff] [blame] | 626 | } |
| 627 | |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 628 | #if !defined(__PRE_RAM__) && !defined(__SMM__) |
| 629 | static void usbdebug_re_enable(unsigned ehci_base) |
| 630 | { |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 631 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 632 | unsigned diff; |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 633 | int i; |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 634 | |
| 635 | if (!dbg_info->ehci_debug) |
| 636 | return; |
| 637 | |
| 638 | diff = (unsigned)dbg_info->ehci_caps - ehci_base; |
| 639 | dbg_info->ehci_regs -= diff; |
| 640 | dbg_info->ehci_debug -= diff; |
| 641 | dbg_info->ehci_caps = (void*)ehci_base; |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 642 | |
| 643 | for (i=0; i<DBGP_MAX_ENDPOINTS; i++) |
| 644 | dbg_info->ep_pipe[i].status |= DBGP_EP_ENABLED; |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static void usbdebug_disable(void) |
| 648 | { |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 649 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 650 | int i; |
| 651 | for (i=0; i<DBGP_MAX_ENDPOINTS; i++) |
| 652 | dbg_info->ep_pipe[i].status &= ~DBGP_EP_ENABLED; |
Kyösti Mälkki | 41c10cd | 2013-07-09 04:19:22 +0300 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static void pci_ehci_set_resources(struct device *dev) |
| 656 | { |
| 657 | struct resource *res; |
| 658 | |
| 659 | printk(BIOS_DEBUG, "%s EHCI Debug Port hook triggered\n", dev_path(dev)); |
| 660 | usbdebug_disable(); |
| 661 | |
| 662 | if (ehci_drv_ops->set_resources) |
| 663 | ehci_drv_ops->set_resources(dev); |
| 664 | res = find_resource(dev, EHCI_BAR_INDEX); |
| 665 | if (!res) |
| 666 | return; |
| 667 | |
| 668 | usbdebug_re_enable((u32)res->base); |
| 669 | report_resource_stored(dev, res, ""); |
| 670 | printk(BIOS_DEBUG, "%s EHCI Debug Port relocated\n", dev_path(dev)); |
| 671 | } |
| 672 | |
| 673 | void pci_ehci_read_resources(struct device *dev) |
| 674 | { |
| 675 | if (!ehci_drv_ops) { |
| 676 | memcpy(&ehci_dbg_ops, dev->ops, sizeof(ehci_dbg_ops)); |
| 677 | ehci_drv_ops = dev->ops; |
| 678 | ehci_dbg_ops.set_resources = pci_ehci_set_resources; |
| 679 | dev->ops = &ehci_dbg_ops; |
| 680 | printk(BIOS_DEBUG, "%s EHCI BAR hook registered\n", dev_path(dev)); |
| 681 | } else { |
| 682 | printk(BIOS_DEBUG, "More than one caller of %s from %s\n", __func__, dev_path(dev)); |
| 683 | } |
| 684 | |
| 685 | pci_dev_read_resources(dev); |
| 686 | } |
| 687 | #endif |
| 688 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 689 | int dbgp_ep_is_active(struct dbgp_pipe *pipe) |
Kyösti Mälkki | 4d409b5 | 2013-07-05 21:38:54 +0300 | [diff] [blame] | 690 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 691 | return (pipe->status & DBGP_EP_STATMASK) == (DBGP_EP_VALID | DBGP_EP_ENABLED); |
Yinghai Lu | d57241f | 2007-02-28 11:17:02 +0000 | [diff] [blame] | 692 | } |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 693 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 694 | struct dbgp_pipe *dbgp_console_output(void) |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 695 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 696 | return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPOUT]; |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 697 | } |
| 698 | |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 699 | struct dbgp_pipe *dbgp_console_input(void) |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 700 | { |
Kyösti Mälkki | 026ff3e | 2013-07-08 18:11:44 +0300 | [diff] [blame] | 701 | return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPIN]; |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | int usbdebug_init(void) |
| 705 | { |
Kyösti Mälkki | cbe2ede | 2013-07-11 07:49:46 +0300 | [diff] [blame] | 706 | struct ehci_debug_info *dbg_info = dbgp_ehci_info(); |
Kyösti Mälkki | 8ee04d7 | 2013-07-06 11:41:09 +0300 | [diff] [blame] | 707 | |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 708 | #if defined(__PRE_RAM__) || !CONFIG_EARLY_CONSOLE |
| 709 | enable_usbdebug(CONFIG_USBDEBUG_DEFAULT_PORT); |
| 710 | #endif |
| 711 | return usbdebug_init_(CONFIG_EHCI_BAR, CONFIG_EHCI_DEBUG_OFFSET, dbg_info); |
| 712 | } |