Kyösti Mälkki | 83fe6d7 | 2014-02-25 20:11:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <console/console.h> |
Kyösti Mälkki | 46249be2 | 2014-10-27 14:07:28 +0200 | [diff] [blame^] | 20 | #include <string.h> |
Kyösti Mälkki | 83fe6d7 | 2014-02-25 20:11:52 +0200 | [diff] [blame] | 21 | |
| 22 | #include "ehci_debug.h" |
| 23 | #include "usb_ch9.h" |
| 24 | #include "ehci.h" |
| 25 | |
Kyösti Mälkki | 46249be2 | 2014-10-27 14:07:28 +0200 | [diff] [blame^] | 26 | #define dprintk printk |
Kyösti Mälkki | 83fe6d7 | 2014-02-25 20:11:52 +0200 | [diff] [blame] | 27 | |
| 28 | #define USB_HUB_PORT_CONNECTION 0 |
| 29 | #define USB_HUB_PORT_ENABLED 1 |
| 30 | #define USB_HUB_PORT_RESET 4 |
| 31 | #define USB_HUB_PORT_POWER 8 |
| 32 | #define USB_HUB_C_PORT_CONNECTION 16 |
| 33 | #define USB_HUB_C_PORT_RESET 20 |
| 34 | |
| 35 | |
| 36 | static int hub_port_status(const char * buf, int feature) |
| 37 | { |
| 38 | return !!(buf[feature>>3] & (1<<(feature&0x7))); |
| 39 | } |
| 40 | |
| 41 | /* After USB port reset, treat device number 0 as an USB hub. Assign it with |
| 42 | * a device number hub_addr. Then apply enable and reset on downstream port. |
| 43 | */ |
| 44 | static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned char hub_addr, |
| 45 | unsigned char port) |
| 46 | { |
| 47 | char status[8]; |
| 48 | int ret, loop; |
| 49 | |
| 50 | /* Assign a devicenumber for the hub. */ |
| 51 | ret = dbgp_control_msg(ehci_debug, 0, |
| 52 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 53 | USB_REQ_SET_ADDRESS, hub_addr, 0, NULL, 0); |
| 54 | if (ret < 0) |
| 55 | goto err; |
| 56 | |
| 57 | /* Enter configured state on hub. */ |
| 58 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 59 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 60 | USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0); |
| 61 | if (ret < 0) |
| 62 | goto err; |
| 63 | |
| 64 | /* Set PORT_POWER, poll for PORT_CONNECTION. */ |
| 65 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 66 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 67 | USB_REQ_SET_FEATURE, USB_HUB_PORT_POWER, port, NULL, 0); |
| 68 | if (ret < 0) |
| 69 | goto err; |
| 70 | |
| 71 | loop = 100; |
| 72 | do { |
| 73 | dbgp_mdelay(10); |
| 74 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 75 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 76 | USB_REQ_GET_STATUS, 0, port, status, 4); |
| 77 | if (ret < 0) |
| 78 | goto err; |
| 79 | if (hub_port_status(status, USB_HUB_PORT_CONNECTION)) |
| 80 | break; |
| 81 | } while (--loop); |
| 82 | if (! loop) |
| 83 | goto err; |
| 84 | |
| 85 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 86 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 87 | USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_CONNECTION, port, NULL, 0); |
| 88 | if (ret < 0) |
| 89 | goto err; |
| 90 | |
| 91 | |
| 92 | /* Set PORT_RESET, poll for C_PORT_RESET. */ |
| 93 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 94 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 95 | USB_REQ_SET_FEATURE, USB_HUB_PORT_RESET, port, NULL, 0); |
| 96 | if (ret < 0) |
| 97 | goto err; |
| 98 | |
| 99 | loop = 100; |
| 100 | do { |
| 101 | dbgp_mdelay(10); |
| 102 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 103 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 104 | USB_REQ_GET_STATUS, 0, port, status, 4); |
| 105 | if (ret < 0) |
| 106 | goto err; |
| 107 | if (hub_port_status(status, USB_HUB_C_PORT_RESET)) |
| 108 | break; |
| 109 | } while (--loop); |
| 110 | if (! loop) |
| 111 | goto err; |
| 112 | |
| 113 | ret = dbgp_control_msg(ehci_debug, hub_addr, |
| 114 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, |
| 115 | USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_RESET, port, NULL, 0); |
| 116 | if (ret < 0) |
| 117 | goto err; |
| 118 | |
| 119 | if (hub_port_status(status, USB_HUB_PORT_ENABLED)) |
| 120 | return 0; |
| 121 | err: |
| 122 | return -1; |
| 123 | } |
| 124 | |
Kyösti Mälkki | 46249be2 | 2014-10-27 14:07:28 +0200 | [diff] [blame^] | 125 | static void ack_set_configuration(struct dbgp_pipe *pipe, u8 devnum, int timeout) |
| 126 | { |
| 127 | int i = DBGP_SETUP_EP0; |
| 128 | while (++i < DBGP_MAX_ENDPOINTS) { |
| 129 | if (pipe[i].endpoint != 0) { |
| 130 | pipe[i].devnum = devnum; |
| 131 | pipe[i].pid = USB_PID_DATA0; |
| 132 | pipe[i].timeout = timeout; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void activate_endpoints(struct dbgp_pipe *pipe) |
| 138 | { |
| 139 | int i = DBGP_SETUP_EP0; |
| 140 | pipe[i].status |= DBGP_EP_ENABLED | DBGP_EP_VALID; |
| 141 | while (++i < DBGP_MAX_ENDPOINTS) { |
| 142 | if (pipe[i].endpoint != 0) |
| 143 | pipe[i].status |= DBGP_EP_ENABLED | DBGP_EP_VALID; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static int probe_for_debug_descriptor(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe) |
| 148 | { |
| 149 | struct usb_debug_descriptor dbgp_desc; |
| 150 | int configured = 0, ret; |
| 151 | u8 devnum = 0; |
| 152 | |
| 153 | /* Find the debug device and make it device number 127 */ |
| 154 | debug_dev_retry: |
| 155 | memset(&dbgp_desc, 0, sizeof(dbgp_desc)); |
| 156 | ret = dbgp_control_msg(ehci_debug, devnum, |
| 157 | USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 158 | USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0, |
| 159 | &dbgp_desc, sizeof(dbgp_desc)); |
| 160 | if (ret == sizeof(dbgp_desc)) { |
| 161 | if (dbgp_desc.bLength == sizeof(dbgp_desc) && dbgp_desc.bDescriptorType==USB_DT_DEBUG) |
| 162 | goto debug_dev_found; |
| 163 | else |
| 164 | dprintk(BIOS_INFO, "Invalid debug device descriptor.\n"); |
| 165 | } |
| 166 | if (devnum == 0) { |
| 167 | devnum = USB_DEBUG_DEVNUM; |
| 168 | goto debug_dev_retry; |
| 169 | } else { |
| 170 | dprintk(BIOS_INFO, "Could not find attached debug device.\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | debug_dev_found: |
| 174 | |
| 175 | /* Move the device to 127 if it isn't already there */ |
| 176 | if (devnum != USB_DEBUG_DEVNUM) { |
| 177 | ret = dbgp_control_msg(ehci_debug, devnum, |
| 178 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 179 | USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0); |
| 180 | if (ret < 0) { |
| 181 | dprintk(BIOS_INFO, "Could not move attached device to %d.\n", |
| 182 | USB_DEBUG_DEVNUM); |
| 183 | return -2; |
| 184 | } |
| 185 | devnum = USB_DEBUG_DEVNUM; |
| 186 | dprintk(BIOS_INFO, "EHCI debug device renamed to 127.\n"); |
| 187 | } |
| 188 | |
| 189 | /* Enable the debug interface */ |
| 190 | ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM, |
| 191 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 192 | USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0); |
| 193 | if (ret < 0) { |
| 194 | dprintk(BIOS_INFO, "Could not enable EHCI debug device.\n"); |
| 195 | return -3; |
| 196 | } |
| 197 | dprintk(BIOS_INFO, "EHCI debug interface enabled.\n"); |
| 198 | |
| 199 | pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint; |
| 200 | pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint; |
| 201 | |
| 202 | ack_set_configuration(pipe, devnum, 1000); |
| 203 | |
| 204 | /* Perform a small write. */ |
| 205 | configured = 0; |
| 206 | small_write: |
| 207 | ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n",5); |
| 208 | if (ret < 0) { |
| 209 | dprintk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret); |
| 210 | if (!configured) { |
| 211 | /* Send Set Configure request to device. This is required for FX2 |
| 212 | (CY7C68013) to transfer from USB state Addressed to Configured, |
| 213 | only then endpoints other than 0 are enabled. */ |
| 214 | if (dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM, |
| 215 | USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE, |
| 216 | USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0) >= 0) { |
| 217 | configured = 1; |
| 218 | goto small_write; |
| 219 | } |
| 220 | } |
| 221 | return -4; |
| 222 | } |
| 223 | dprintk(BIOS_INFO, "Test write done\n"); |
| 224 | return 0; |
| 225 | } |
| 226 | |
Kyösti Mälkki | 83fe6d7 | 2014-02-25 20:11:52 +0200 | [diff] [blame] | 227 | |
| 228 | int dbgp_probe_gadget(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe) |
| 229 | { |
| 230 | int ret; |
| 231 | |
| 232 | if (CONFIG_USBDEBUG_OPTIONAL_HUB_PORT != 0) { |
| 233 | ret = dbgp_hub_enable(ehci_debug, USB_DEBUG_DEVNUM-1, |
| 234 | CONFIG_USBDEBUG_OPTIONAL_HUB_PORT); |
| 235 | if (ret < 0) { |
| 236 | printk(BIOS_INFO, "Could not enable USB hub on debug port.\n"); |
| 237 | return ret; |
| 238 | } |
| 239 | } |
| 240 | |
Kyösti Mälkki | 46249be2 | 2014-10-27 14:07:28 +0200 | [diff] [blame^] | 241 | ret = probe_for_debug_descriptor(ehci_debug, pipe); |
| 242 | if (ret < 0) { |
| 243 | dprintk(BIOS_INFO, "Could not enable gadget using Debug Descriptor.\n"); |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | activate_endpoints(pipe); |
Kyösti Mälkki | 83fe6d7 | 2014-02-25 20:11:52 +0200 | [diff] [blame] | 248 | return 0; |
| 249 | } |