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