blob: 5a62999dc3ae8798e15889244a0d46eb644d2e90 [file] [log] [blame]
Yinghai Lud57241f2007-02-28 11:17:02 +00001/*
Stefan Reinauerbb01f602009-07-21 21:20:45 +00002 * This file is part of the coreboot project.
3 *
Yinghai Lud57241f2007-02-28 11:17:02 +00004 * Copyright (C) 2006 Eric Biederman (ebiederm@xmission.com)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +00005 * Copyright (C) 2007 AMD
Yinghai Lud57241f2007-02-28 11:17:02 +00006 *
Stefan Reinauerbb01f602009-07-21 21:20:45 +00007 * 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 Lud57241f2007-02-28 11:17:02 +000010 *
Stefan Reinauerbb01f602009-07-21 21:20:45 +000011 * 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 Hermann06694a82010-09-23 18:16:46 +000020
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000021#include <stddef.h>
Yinghai Lud57241f2007-02-28 11:17:02 +000022#include <console/console.h>
Kyösti Mälkkie2227a22014-02-05 13:02:55 +020023#include <console/usb.h>
Yinghai Lud57241f2007-02-28 11:17:02 +000024#include <arch/io.h>
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000025#include <arch/byteorder.h>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070026#include <arch/early_variables.h>
Kyösti Mälkkie23c22d2013-07-28 23:16:47 +030027#include <string.h>
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +030028#include <cbmem.h>
Yinghai Lud57241f2007-02-28 11:17:02 +000029
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +020030#include "ehci_debug.h"
Kyösti Mälkkie2227a22014-02-05 13:02:55 +020031#include "usb_ch9.h"
32#include "ehci.h"
Yinghai Lud57241f2007-02-28 11:17:02 +000033
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030034#define DBGP_MAX_ENDPOINTS 4
Kyösti Mälkkid7991402013-08-12 16:11:34 +030035#define DBGP_SETUP_EP0 0 /* Compulsory endpoint 0. */
36#define DBGP_CONSOLE_EPOUT 1
37#define DBGP_CONSOLE_EPIN 2
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030038
39struct ehci_debug_info {
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030040 void *ehci_caps;
41 void *ehci_regs;
42 void *ehci_debug;
43
44 struct dbgp_pipe ep_pipe[DBGP_MAX_ENDPOINTS];
45};
46
Kyösti Mälkki3be80cc2013-06-06 10:46:37 +030047#if CONFIG_DEBUG_USBDEBUG
Kyösti Mälkkie53cece2013-08-10 10:50:43 +030048static void dbgp_print_data(struct ehci_dbg_port *ehci_debug);
Kyösti Mälkkid7991402013-08-12 16:11:34 +030049static int dbgp_enabled(void);
50# define dprintk(LEVEL, args...) \
51 do { if (!dbgp_enabled()) printk(LEVEL, ##args); } while (0)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000052#else
Kyösti Mälkkie53cece2013-08-10 10:50:43 +030053# define dbgp_print_data(x) do {} while(0)
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +030054# define dprintk(LEVEL, args...) do {} while(0)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000055#endif
56
Yinghai Lud57241f2007-02-28 11:17:02 +000057#define USB_DEBUG_DEVNUM 127
58
Yinghai Lud57241f2007-02-28 11:17:02 +000059#define DBGP_LEN_UPDATE(x, len) (((x) & ~0x0f) | ((len) & 0x0f))
60/*
61 * USB Packet IDs (PIDs)
62 */
63
64/* token */
65#define USB_PID_OUT 0xe1
66#define USB_PID_IN 0x69
67#define USB_PID_SOF 0xa5
68#define USB_PID_SETUP 0x2d
69/* handshake */
70#define USB_PID_ACK 0xd2
71#define USB_PID_NAK 0x5a
72#define USB_PID_STALL 0x1e
73#define USB_PID_NYET 0x96
74/* data */
75#define USB_PID_DATA0 0xc3
76#define USB_PID_DATA1 0x4b
77#define USB_PID_DATA2 0x87
78#define USB_PID_MDATA 0x0f
79/* Special */
80#define USB_PID_PREAMBLE 0x3c
81#define USB_PID_ERR 0x3c
82#define USB_PID_SPLIT 0x78
83#define USB_PID_PING 0xb4
84#define USB_PID_UNDEF_0 0xf0
85
86#define USB_PID_DATA_TOGGLE 0x88
87#define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
88
Yinghai Lud57241f2007-02-28 11:17:02 +000089#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
90#define HUB_SHORT_RESET_TIME 10
91#define HUB_LONG_RESET_TIME 200
92#define HUB_RESET_TIMEOUT 500
93
Kyösti Mälkki2de841b32013-08-19 12:45:16 +030094#define DBGP_MICROFRAME_TIMEOUT_LOOPS 1000
95#define DBGP_MICROFRAME_RETRIES 10
Yinghai Lud57241f2007-02-28 11:17:02 +000096#define DBGP_MAX_PACKET 8
97
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +030098static struct ehci_debug_info glob_dbg_info CAR_GLOBAL;
Kyösti Mälkki618d1792014-10-27 08:01:55 +020099static struct ehci_debug_info * glob_dbg_info_p CAR_GLOBAL;
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300100
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300101static inline struct ehci_debug_info *dbgp_ehci_info(void)
102{
Kyösti Mälkki618d1792014-10-27 08:01:55 +0200103 if (car_get_var(glob_dbg_info_p) == NULL)
104 car_set_var(glob_dbg_info_p, &glob_dbg_info);
105
106 return car_get_var(glob_dbg_info_p);
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300107}
108
Yinghai Lud57241f2007-02-28 11:17:02 +0000109static int dbgp_wait_until_complete(struct ehci_dbg_port *ehci_debug)
110{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000111 u32 ctrl;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300112 int loop = 0;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000113
Yinghai Lud57241f2007-02-28 11:17:02 +0000114 do {
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000115 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000116 /* Stop when the transaction is finished */
117 if (ctrl & DBGP_DONE)
118 break;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300119 } while (++loop < DBGP_MICROFRAME_TIMEOUT_LOOPS);
Yinghai Lud57241f2007-02-28 11:17:02 +0000120
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300121 if (! (ctrl & DBGP_DONE)) {
122 dprintk(BIOS_ERR, "dbgp_wait_until_complete: retry timeout.\n");
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300123 return -DBGP_ERR_SIGNAL;
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300124 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000125
126 /* Now that we have observed the completed transaction,
127 * clear the done bit.
128 */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000129 write32((unsigned long)&ehci_debug->control, ctrl | DBGP_DONE);
Yinghai Lud57241f2007-02-28 11:17:02 +0000130 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
131}
132
Yinghai Lud57241f2007-02-28 11:17:02 +0000133static void dbgp_breath(void)
134{
135 /* Sleep to give the debug port a chance to breathe */
136}
137
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300138static int dbgp_wait_until_done(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe,
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300139 unsigned ctrl, const int timeout)
Yinghai Lud57241f2007-02-28 11:17:02 +0000140{
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300141 u32 rd_ctrl, rd_pids;
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300142 u32 ctrl_prev = 0, pids_prev = 0;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300143 u8 lpid;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300144 int ret, host_retries;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300145 int loop;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000146
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300147 loop = 0;
148device_retry:
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300149 host_retries = 0;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300150 if (loop++ >= timeout)
151 return -DBGP_ERR_BAD;
152
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300153host_retry:
154 if (host_retries++ >= DBGP_MICROFRAME_RETRIES)
155 return -DBGP_ERR_BAD;
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300156 if (loop == 1 || host_retries > 1)
157 dprintk(BIOS_SPEW, "dbgp: start (@ %3d,%d) ctrl=%08x\n",
158 loop, host_retries, ctrl | DBGP_GO);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000159 write32((unsigned long)&ehci_debug->control, ctrl | DBGP_GO);
Yinghai Lud57241f2007-02-28 11:17:02 +0000160 ret = dbgp_wait_until_complete(ehci_debug);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300161 rd_ctrl = read32((unsigned long)&ehci_debug->control);
162 rd_pids = read32((unsigned long)&ehci_debug->pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000163
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300164 if (rd_ctrl != ctrl_prev || rd_pids != pids_prev || (ret<0)) {
165 ctrl_prev = rd_ctrl;
166 pids_prev = rd_pids;
167 dprintk(BIOS_SPEW, "dbgp: status (@ %3d,%d) ctrl=%08x pids=%08x ret=%d\n",
168 loop, host_retries, rd_ctrl, rd_pids, ret);
169 }
170
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300171 /* Controller hardware failure. */
172 if (ret == -DBGP_ERR_SIGNAL) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000173 return ret;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300174
175 /* Bus failure (corrupted microframe). */
176 } else if (ret == -DBGP_ERR_BAD) {
177 goto host_retry;
Sven Schnelle10680872012-07-25 14:19:45 +0200178 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000179
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300180 lpid = DBGP_PID_GET(rd_pids);
181
182 /* If I get an ACK or in-sync DATA PID, we are done. */
183 if ((lpid == USB_PID_ACK) || (lpid == pipe->pid)) {
Kyösti Mälkki48e899d2014-01-21 10:44:08 +0200184 pipe->pid ^= USB_PID_DATA_TOGGLE;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300185 }
186
Yinghai Lud57241f2007-02-28 11:17:02 +0000187 /* If the port is getting full or it has dropped data
188 * start pacing ourselves, not necessary but it's friendly.
189 */
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300190 else if (lpid == USB_PID_NYET) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000191 dbgp_breath();
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300192 goto device_retry;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300193 }
194
195 /* If I get a NACK or out-of-sync DATA PID, reissue the transmission. */
196 else if ((lpid == USB_PID_NAK) || (lpid == (pipe->pid ^ USB_PID_DATA_TOGGLE))) {
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300197 goto device_retry;
Yinghai Lud57241f2007-02-28 11:17:02 +0000198 }
199
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300200 /* Abort on STALL handshake for endpoint 0.*/
201 else if ((lpid == USB_PID_STALL) && (pipe->endpoint == 0x0)) {
202 ret = -DBGP_ERR_BAD;
203 }
204
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300205 dbgp_print_data(ehci_debug);
206
Yinghai Lud57241f2007-02-28 11:17:02 +0000207 return ret;
208}
209
210static void dbgp_set_data(struct ehci_dbg_port *ehci_debug, const void *buf, int size)
211{
212 const unsigned char *bytes = buf;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000213 u32 lo, hi;
Yinghai Lud57241f2007-02-28 11:17:02 +0000214 int i;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000215
Yinghai Lud57241f2007-02-28 11:17:02 +0000216 lo = hi = 0;
217 for (i = 0; i < 4 && i < size; i++)
218 lo |= bytes[i] << (8*i);
219 for (; i < 8 && i < size; i++)
220 hi |= bytes[i] << (8*(i - 4));
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000221 write32((unsigned long)&ehci_debug->data03, lo);
222 write32((unsigned long)&ehci_debug->data47, hi);
Yinghai Lud57241f2007-02-28 11:17:02 +0000223}
224
225static void dbgp_get_data(struct ehci_dbg_port *ehci_debug, void *buf, int size)
226{
227 unsigned char *bytes = buf;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000228 u32 lo, hi;
Yinghai Lud57241f2007-02-28 11:17:02 +0000229 int i;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000230
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000231 lo = read32((unsigned long)&ehci_debug->data03);
232 hi = read32((unsigned long)&ehci_debug->data47);
Yinghai Lud57241f2007-02-28 11:17:02 +0000233 for (i = 0; i < 4 && i < size; i++)
234 bytes[i] = (lo >> (8*i)) & 0xff;
235 for (; i < 8 && i < size; i++)
236 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
237}
238
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300239#if CONFIG_DEBUG_USBDEBUG
240static void dbgp_print_data(struct ehci_dbg_port *ehci_debug)
241{
242 u32 ctrl = read32((unsigned long)&ehci_debug->control);
243 u32 lo = read32((unsigned long)&ehci_debug->data03);
244 u32 hi = read32((unsigned long)&ehci_debug->data47);
245 int len = DBGP_LEN(ctrl);
246 if (len) {
247 int i;
248 dprintk(BIOS_SPEW, "dbgp: buf:");
249 for (i = 0; i < 4 && i < len; i++)
250 dprintk(BIOS_SPEW, " %02x", (lo >> (8*i)) & 0xff);
251 for (; i < 8 && i < len; i++)
252 dprintk(BIOS_SPEW, " %02x", (hi >> (8*(i - 4))) & 0xff);
253 dprintk(BIOS_SPEW, "\n");
254 }
255}
256#endif
257
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300258static int dbgp_bulk_write(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe,
259 const char *bytes, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000260{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000261 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000262 int ret;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000263
Yinghai Lud57241f2007-02-28 11:17:02 +0000264 if (size > DBGP_MAX_PACKET)
265 return -1;
266
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300267 addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300268 pids = DBGP_PID_SET(pipe->pid, USB_PID_OUT);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000269
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000270 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000271 ctrl = DBGP_LEN_UPDATE(ctrl, size);
272 ctrl |= DBGP_OUT;
Yinghai Lud57241f2007-02-28 11:17:02 +0000273
274 dbgp_set_data(ehci_debug, bytes, size);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000275 write32((unsigned long)&ehci_debug->address, addr);
276 write32((unsigned long)&ehci_debug->pids, pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000277
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300278 ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000279
Yinghai Lud57241f2007-02-28 11:17:02 +0000280 return ret;
281}
282
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300283int dbgp_bulk_write_x(struct dbgp_pipe *pipe, const char *bytes, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000284{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300285 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300286 return dbgp_bulk_write(dbg_info->ehci_debug, pipe, bytes, size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000287}
288
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300289static int dbgp_bulk_read(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe,
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300290 void *data, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000291{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000292 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000293 int ret;
294
295 if (size > DBGP_MAX_PACKET)
296 return -1;
297
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300298 addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300299 pids = DBGP_PID_SET(pipe->pid, USB_PID_IN);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000300
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000301 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000302 ctrl = DBGP_LEN_UPDATE(ctrl, size);
303 ctrl &= ~DBGP_OUT;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000304
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000305 write32((unsigned long)&ehci_debug->address, addr);
306 write32((unsigned long)&ehci_debug->pids, pids);
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300307 ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout);
Yinghai Lud57241f2007-02-28 11:17:02 +0000308 if (ret < 0)
309 return ret;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000310
Yinghai Lud57241f2007-02-28 11:17:02 +0000311 if (size > ret)
312 size = ret;
313 dbgp_get_data(ehci_debug, data, size);
314 return ret;
315}
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000316
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300317int dbgp_bulk_read_x(struct dbgp_pipe *pipe, void *data, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000318{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300319 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300320 return dbgp_bulk_read(dbg_info->ehci_debug, pipe, data, size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000321}
322
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000323static void dbgp_mdelay(int ms)
Yinghai Lud57241f2007-02-28 11:17:02 +0000324{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000325 int i;
326
327 while (ms--) {
328 for (i = 0; i < 1000; i++)
329 inb(0x80);
330 }
331}
332
333static int dbgp_control_msg(struct ehci_dbg_port *ehci_debug, unsigned devnum, int requesttype,
334 int request, int value, int index, void *data, int size)
335{
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300336 struct ehci_debug_info *info = dbgp_ehci_info();
337 struct dbgp_pipe *pipe = &info->ep_pipe[DBGP_SETUP_EP0];
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000338 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000339 struct usb_ctrlrequest req;
340 int read;
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300341 int ret, ret2;
Yinghai Lud57241f2007-02-28 11:17:02 +0000342
343 read = (requesttype & USB_DIR_IN) != 0;
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300344 if (size > DBGP_MAX_PACKET)
Yinghai Lud57241f2007-02-28 11:17:02 +0000345 return -1;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000346
Yinghai Lud57241f2007-02-28 11:17:02 +0000347 /* Compute the control message */
348 req.bRequestType = requesttype;
349 req.bRequest = request;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000350 req.wValue = cpu_to_le16(value);
351 req.wIndex = cpu_to_le16(index);
352 req.wLength = cpu_to_le16(size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000353
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300354 pipe->devnum = devnum;
355 pipe->endpoint = 0;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300356 pipe->pid = USB_PID_DATA0;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300357 pipe->timeout = 1000;
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300358 addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300359 pids = DBGP_PID_SET(pipe->pid, USB_PID_SETUP);
Yinghai Lud57241f2007-02-28 11:17:02 +0000360
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000361 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000362 ctrl = DBGP_LEN_UPDATE(ctrl, sizeof(req));
363 ctrl |= DBGP_OUT;
Yinghai Lud57241f2007-02-28 11:17:02 +0000364
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300365 /* Setup stage */
Yinghai Lud57241f2007-02-28 11:17:02 +0000366 dbgp_set_data(ehci_debug, &req, sizeof(req));
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000367 write32((unsigned long)&ehci_debug->address, addr);
368 write32((unsigned long)&ehci_debug->pids, pids);
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300369 ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, 1);
Yinghai Lud57241f2007-02-28 11:17:02 +0000370 if (ret < 0)
371 return ret;
372
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300373 /* Data stage (optional) */
374 if (read && size)
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300375 ret = dbgp_bulk_read(ehci_debug, pipe, data, size);
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300376 else if (!read && size)
377 ret = dbgp_bulk_write(ehci_debug, pipe, data, size);
378
379 /* Status stage in opposite direction */
380 pipe->pid = USB_PID_DATA1;
381 ctrl = read32((unsigned long)&ehci_debug->control);
382 ctrl = DBGP_LEN_UPDATE(ctrl, 0);
383 if (read) {
384 pids = DBGP_PID_SET(pipe->pid, USB_PID_OUT);
385 ctrl |= DBGP_OUT;
386 } else {
387 pids = DBGP_PID_SET(pipe->pid, USB_PID_IN);
388 ctrl &= ~DBGP_OUT;
389 }
390
391 write32((unsigned long)&ehci_debug->pids, pids);
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300392 ret2 = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout);
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300393 if (ret2 < 0)
394 return ret2;
395
Yinghai Lud57241f2007-02-28 11:17:02 +0000396 return ret;
397}
398
399static int ehci_reset_port(struct ehci_regs *ehci_regs, int port)
400{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000401 u32 portsc;
402 u32 delay_time, delay_ms;
Yinghai Lud57241f2007-02-28 11:17:02 +0000403 int loop;
404
405 /* Reset the usb debug port */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000406 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000407 portsc &= ~PORT_PE;
408 portsc |= PORT_RESET;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000409 write32((unsigned long)&ehci_regs->port_status[port - 1], portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000410
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000411 delay_ms = HUB_ROOT_RESET_TIME;
Yinghai Lud57241f2007-02-28 11:17:02 +0000412 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000413 delay_time += delay_ms) {
414 dbgp_mdelay(delay_ms);
Yinghai Lud57241f2007-02-28 11:17:02 +0000415
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000416 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000417 if (portsc & PORT_RESET) {
418 /* force reset to complete */
419 loop = 2;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000420 write32((unsigned long)&ehci_regs->port_status[port - 1],
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000421 portsc & ~(PORT_RWC_BITS | PORT_RESET));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000422 do {
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000423 dbgp_mdelay(delay_ms);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000424 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000425 delay_time += delay_ms;
Yinghai Lud57241f2007-02-28 11:17:02 +0000426 } while ((portsc & PORT_RESET) && (--loop > 0));
427 if (!loop) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000428 printk(BIOS_DEBUG, "ehci_reset_port forced done");
Yinghai Lud57241f2007-02-28 11:17:02 +0000429 }
430 }
431
432 /* Device went away? */
433 if (!(portsc & PORT_CONNECT))
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000434 return -1; //-ENOTCONN;
Yinghai Lud57241f2007-02-28 11:17:02 +0000435
Martin Rothcbf2bd72013-07-09 21:51:14 -0600436 /* bomb out completely if something weird happened */
Yinghai Lud57241f2007-02-28 11:17:02 +0000437 if ((portsc & PORT_CSC))
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000438 return -2; //-EINVAL;
Yinghai Lud57241f2007-02-28 11:17:02 +0000439
440 /* If we've finished resetting, then break out of the loop */
441 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
442 return 0;
443 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000444 return -3; //-EBUSY;
Yinghai Lud57241f2007-02-28 11:17:02 +0000445}
446
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000447static int ehci_wait_for_port(struct ehci_regs *ehci_regs, int port)
Yinghai Lud57241f2007-02-28 11:17:02 +0000448{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000449 u32 status;
Yinghai Lud57241f2007-02-28 11:17:02 +0000450 int ret, reps;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000451
Yinghai Lud57241f2007-02-28 11:17:02 +0000452 for (reps = 0; reps < 3; reps++) {
453 dbgp_mdelay(100);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000454 status = read32((unsigned long)&ehci_regs->status);
Yinghai Lud57241f2007-02-28 11:17:02 +0000455 if (status & STS_PCD) {
456 ret = ehci_reset_port(ehci_regs, port);
457 if (ret == 0)
458 return 0;
459 }
460 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000461 return -1; //-ENOTCONN;
Yinghai Lud57241f2007-02-28 11:17:02 +0000462}
463
Kyösti Mälkkid2dac0a2013-08-23 23:33:16 +0300464#define USB_HUB_PORT_CONNECTION 0
465#define USB_HUB_PORT_ENABLED 1
466#define USB_HUB_PORT_RESET 4
467#define USB_HUB_PORT_POWER 8
468#define USB_HUB_C_PORT_CONNECTION 16
469#define USB_HUB_C_PORT_RESET 20
470
471#if CONFIG_USBDEBUG_OPTIONAL_HUB_PORT
472
473static int hub_port_status(const char * buf, int feature)
474{
475 return !!(buf[feature>>3] & (1<<(feature&0x7)));
476}
477
478static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned int port)
479{
480 const u8 hub_addr = USB_DEBUG_DEVNUM-1;
481 char status[8];
482 int ret, loop;
483
484 /* Move hub to address 126. */
485 ret = dbgp_control_msg(ehci_debug, 0,
486 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
487 USB_REQ_SET_ADDRESS, hub_addr, 0, NULL, 0);
488 if (ret < 0)
489 goto err;
490
491 /* Enter configured state on hub. */
492 ret = dbgp_control_msg(ehci_debug, hub_addr,
493 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
494 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
495 if (ret < 0)
496 goto err;
497
498 /* Set PORT_POWER, poll for PORT_CONNECTION. */
499 ret = dbgp_control_msg(ehci_debug, hub_addr,
500 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
501 USB_REQ_SET_FEATURE, USB_HUB_PORT_POWER, port, NULL, 0);
502 if (ret < 0)
503 goto err;
504
505 loop = 100;
506 do {
507 dbgp_mdelay(10);
508 ret = dbgp_control_msg(ehci_debug, hub_addr,
509 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
510 USB_REQ_GET_STATUS, 0, port, status, 4);
511 if (ret < 0)
512 goto err;
513 if (hub_port_status(status, USB_HUB_PORT_CONNECTION))
514 break;
515 } while (--loop);
516 if (! loop)
517 goto err;
518
519 ret = dbgp_control_msg(ehci_debug, hub_addr,
520 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
521 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_CONNECTION, port, NULL, 0);
522 if (ret < 0)
523 goto err;
524
525
526 /* Set PORT_RESET, poll for C_PORT_RESET. */
527 ret = dbgp_control_msg(ehci_debug, hub_addr,
528 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
529 USB_REQ_SET_FEATURE, USB_HUB_PORT_RESET, port, NULL, 0);
530 if (ret < 0)
531 goto err;
532
533 loop = 100;
534 do {
535 dbgp_mdelay(10);
536 ret = dbgp_control_msg(ehci_debug, hub_addr,
537 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
538 USB_REQ_GET_STATUS, 0, port, status, 4);
539 if (ret < 0)
540 goto err;
541 if (hub_port_status(status, USB_HUB_C_PORT_RESET))
542 break;
543 } while (--loop);
544 if (! loop)
545 goto err;
546
547 ret = dbgp_control_msg(ehci_debug, hub_addr,
548 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
549 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_RESET, port, NULL, 0);
550 if (ret < 0)
551 goto err;
552
553 if (hub_port_status(status, USB_HUB_PORT_ENABLED))
554 return 0;
555err:
556 return -1;
557}
558#endif /* CONFIG_USBDEBUG_OPTIONAL_HUB_PORT */
559
Kyösti Mälkki8101aa62013-08-15 16:27:06 +0300560
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300561static int usbdebug_init_(unsigned ehci_bar, unsigned offset, struct ehci_debug_info *info)
Yinghai Lud57241f2007-02-28 11:17:02 +0000562{
563 struct ehci_caps *ehci_caps;
564 struct ehci_regs *ehci_regs;
565 struct ehci_dbg_port *ehci_debug;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000566
Yinghai Lud57241f2007-02-28 11:17:02 +0000567 struct usb_debug_descriptor dbgp_desc;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000568 u32 cmd, ctrl, status, portsc, hcs_params;
569 u32 debug_port, new_debug_port = 0, n_ports;
570 u32 devnum;
Kyösti Mälkki6bfe61d2013-06-06 10:33:39 +0300571 int ret, i, configured;
Yinghai Lud57241f2007-02-28 11:17:02 +0000572 int loop;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000573 int port_map_tried;
574 int playtimes = 3;
Yinghai Lud57241f2007-02-28 11:17:02 +0000575
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +0200576 dprintk(BIOS_INFO, "ehci_bar: 0x%x debug_offset 0x%x\n", ehci_bar, offset);
577
Yinghai Lud57241f2007-02-28 11:17:02 +0000578 ehci_caps = (struct ehci_caps *)ehci_bar;
Stefan Reinauer5ff7c132011-10-31 12:56:45 -0700579 ehci_regs = (struct ehci_regs *)(ehci_bar +
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000580 HC_LENGTH(read32((unsigned long)&ehci_caps->hc_capbase)));
Yinghai Lud57241f2007-02-28 11:17:02 +0000581 ehci_debug = (struct ehci_dbg_port *)(ehci_bar + offset);
Yinghai Lud57241f2007-02-28 11:17:02 +0000582 info->ehci_debug = (void *)0;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300583 memset(&info->ep_pipe, 0, sizeof (info->ep_pipe));
Kyösti Mälkki24100102013-08-12 20:40:37 +0300584
585 if (CONFIG_USBDEBUG_DEFAULT_PORT > 0)
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200586 ehci_debug_select_port(CONFIG_USBDEBUG_DEFAULT_PORT);
Kyösti Mälkki24100102013-08-12 20:40:37 +0300587 else
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200588 ehci_debug_select_port(1);
Kyösti Mälkki24100102013-08-12 20:40:37 +0300589
Yinghai Lud57241f2007-02-28 11:17:02 +0000590try_next_time:
591 port_map_tried = 0;
592
593try_next_port:
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000594 hcs_params = read32((unsigned long)&ehci_caps->hcs_params);
Yinghai Lud57241f2007-02-28 11:17:02 +0000595 debug_port = HCS_DEBUG_PORT(hcs_params);
596 n_ports = HCS_N_PORTS(hcs_params);
597
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300598 dprintk(BIOS_INFO, "debug_port: %d\n", debug_port);
599 dprintk(BIOS_INFO, "n_ports: %d\n", n_ports);
Yinghai Lud57241f2007-02-28 11:17:02 +0000600
Yinghai Lud57241f2007-02-28 11:17:02 +0000601 for (i = 1; i <= n_ports; i++) {
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000602 portsc = read32((unsigned long)&ehci_regs->port_status[i-1]);
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300603 dprintk(BIOS_INFO, "PORTSC #%d: %08x\n", i, portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000604 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000605
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000606 if(port_map_tried && (new_debug_port != debug_port)) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000607 if(--playtimes) {
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200608 ehci_debug_select_port(debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000609 goto try_next_time;
610 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000611 return -1;
Yinghai Lud57241f2007-02-28 11:17:02 +0000612 }
613
Kyösti Mälkki16c01452013-08-12 15:32:25 +0300614 /* Wait until the controller is halted */
615 status = read32((unsigned long)&ehci_regs->status);
616 if (!(status & STS_HALT)) {
617 cmd = read32((unsigned long)&ehci_regs->command);
618 cmd &= ~CMD_RUN;
619 write32((unsigned long)&ehci_regs->command, cmd);
620 loop = 100;
621 do {
622 dbgp_mdelay(10);
623 status = read32((unsigned long)&ehci_regs->status);
624 } while (!(status & STS_HALT) && (--loop > 0));
625 if (status & STS_HALT)
626 dprintk(BIOS_INFO, "EHCI controller halted successfully.\n");
627 else
628 dprintk(BIOS_INFO, "EHCI controller is not halted. Reset may fail.\n");
629 }
630
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000631 loop = 100;
Yinghai Lud57241f2007-02-28 11:17:02 +0000632 /* Reset the EHCI controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000633 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000634 cmd |= CMD_RESET;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000635 write32((unsigned long)&ehci_regs->command, cmd);
Yinghai Lud57241f2007-02-28 11:17:02 +0000636 do {
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000637 dbgp_mdelay(10);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000638 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000639 } while ((cmd & CMD_RESET) && (--loop > 0));
640
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000641 if(!loop) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300642 dprintk(BIOS_INFO, "Could not reset EHCI controller.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000643 // on some systems it works without succeeding here.
644 // return -2;
645 } else {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300646 dprintk(BIOS_INFO, "EHCI controller reset successfully.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000647 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000648
649 /* Claim ownership, but do not enable yet */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000650 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000651 ctrl |= DBGP_OWNER;
652 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000653 write32((unsigned long)&ehci_debug->control, ctrl);
Yinghai Lud57241f2007-02-28 11:17:02 +0000654
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000655 /* Start EHCI controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000656 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000657 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
658 cmd |= CMD_RUN;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000659 write32((unsigned long)&ehci_regs->command, cmd);
Yinghai Lud57241f2007-02-28 11:17:02 +0000660
661 /* Ensure everything is routed to the EHCI */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000662 write32((unsigned long)&ehci_regs->configured_flag, FLAG_CF);
Yinghai Lud57241f2007-02-28 11:17:02 +0000663
664 /* Wait until the controller is no longer halted */
665 loop = 10;
666 do {
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000667 dbgp_mdelay(10);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000668 status = read32((unsigned long)&ehci_regs->status);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000669 } while ((status & STS_HALT) && (--loop > 0));
Yinghai Lud57241f2007-02-28 11:17:02 +0000670
671 if(!loop) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300672 dprintk(BIOS_INFO, "EHCI could not be started.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000673 return -3;
Yinghai Lud57241f2007-02-28 11:17:02 +0000674 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300675 dprintk(BIOS_INFO, "EHCI started.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000676
677 /* Wait for a device to show up in the debug port */
678 ret = ehci_wait_for_port(ehci_regs, debug_port);
679 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300680 dprintk(BIOS_INFO, "No device found in debug port %d\n", debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000681 goto next_debug_port;
682 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300683 dprintk(BIOS_INFO, "EHCI done waiting for port.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000684
685 /* Enable the debug port */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000686 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000687 ctrl |= DBGP_CLAIM;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000688 write32((unsigned long)&ehci_debug->control, ctrl);
689 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000690 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300691 dprintk(BIOS_INFO, "No device in EHCI debug port.\n");
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000692 write32((unsigned long)&ehci_debug->control, ctrl & ~DBGP_CLAIM);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000693 ret = -4;
Yinghai Lud57241f2007-02-28 11:17:02 +0000694 goto err;
695 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300696 dprintk(BIOS_INFO, "EHCI debug port enabled.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000697
698 /* Completely transfer the debug device to the debug controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000699 portsc = read32((unsigned long)&ehci_regs->port_status[debug_port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000700 portsc &= ~PORT_PE;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000701 write32((unsigned long)&ehci_regs->port_status[debug_port - 1], portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000702
703 dbgp_mdelay(100);
704
Kyösti Mälkkid2dac0a2013-08-23 23:33:16 +0300705#if CONFIG_USBDEBUG_OPTIONAL_HUB_PORT
706 ret = dbgp_hub_enable(ehci_debug, CONFIG_USBDEBUG_OPTIONAL_HUB_PORT);
707 if (ret < 0) {
708 dprintk(BIOS_INFO, "Could not enable USB hub on debug port.\n");
709 ret = -6;
710 goto err;
711 }
712#endif
713
Yinghai Lud57241f2007-02-28 11:17:02 +0000714 /* Find the debug device and make it device number 127 */
Kyösti Mälkkia2adaeb2013-08-12 00:09:21 +0300715 devnum = 0;
716debug_dev_retry:
717 memset(&dbgp_desc, 0, sizeof(dbgp_desc));
718 ret = dbgp_control_msg(ehci_debug, devnum,
719 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
720 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
721 &dbgp_desc, sizeof(dbgp_desc));
722 if (ret == sizeof(dbgp_desc)) {
723 if (dbgp_desc.bLength == sizeof(dbgp_desc) && dbgp_desc.bDescriptorType==USB_DT_DEBUG)
724 goto debug_dev_found;
725 else
726 dprintk(BIOS_INFO, "Invalid debug device descriptor.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000727 }
Kyösti Mälkkia2adaeb2013-08-12 00:09:21 +0300728 if (devnum == 0) {
729 devnum = USB_DEBUG_DEVNUM;
730 goto debug_dev_retry;
731 } else {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300732 dprintk(BIOS_INFO, "Could not find attached debug device.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000733 ret = -5;
Yinghai Lud57241f2007-02-28 11:17:02 +0000734 goto err;
735 }
Kyösti Mälkkia2adaeb2013-08-12 00:09:21 +0300736debug_dev_found:
Yinghai Lud57241f2007-02-28 11:17:02 +0000737
738 /* Move the device to 127 if it isn't already there */
739 if (devnum != USB_DEBUG_DEVNUM) {
740 ret = dbgp_control_msg(ehci_debug, devnum,
741 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000742 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
Yinghai Lud57241f2007-02-28 11:17:02 +0000743 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300744 dprintk(BIOS_INFO, "Could not move attached device to %d.\n",
Yinghai Lud57241f2007-02-28 11:17:02 +0000745 USB_DEBUG_DEVNUM);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000746 ret = -7;
Yinghai Lud57241f2007-02-28 11:17:02 +0000747 goto err;
748 }
749 devnum = USB_DEBUG_DEVNUM;
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300750 dprintk(BIOS_INFO, "EHCI debug device renamed to 127.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000751 }
752
753 /* Enable the debug interface */
754 ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
755 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000756 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
Yinghai Lud57241f2007-02-28 11:17:02 +0000757 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300758 dprintk(BIOS_INFO, "Could not enable EHCI debug device.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000759 ret = -8;
Yinghai Lud57241f2007-02-28 11:17:02 +0000760 goto err;
761 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300762 dprintk(BIOS_INFO, "EHCI debug interface enabled.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000763
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300764 /* Prepare endpoint pipes. */
765 for (i=1; i<DBGP_MAX_ENDPOINTS; i++) {
766 info->ep_pipe[i].devnum = USB_DEBUG_DEVNUM;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300767 info->ep_pipe[i].pid = USB_PID_DATA0;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300768 info->ep_pipe[i].timeout = 1000;
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300769 }
770 info->ep_pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint;
771 info->ep_pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint;
772
773 /* Perform a small write. */
Kyösti Mälkki6bfe61d2013-06-06 10:33:39 +0300774 configured = 0;
775small_write:
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300776 ret = dbgp_bulk_write(ehci_debug, &info->ep_pipe[DBGP_CONSOLE_EPOUT], "USB\r\n",5);
Yinghai Lud57241f2007-02-28 11:17:02 +0000777 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300778 dprintk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
Kyösti Mälkki6bfe61d2013-06-06 10:33:39 +0300779 if (!configured) {
780 /* Send Set Configure request to device. This is required for FX2
781 (CY7C68013) to transfer from USB state Addressed to Configured,
782 only then endpoints other than 0 are enabled. */
783 if (dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
784 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
785 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0) >= 0) {
786 configured = 1;
787 goto small_write;
788 }
789 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000790 ret = -9;
Yinghai Lud57241f2007-02-28 11:17:02 +0000791 goto err;
792 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300793 dprintk(BIOS_INFO, "Test write done\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000794
795 info->ehci_caps = ehci_caps;
796 info->ehci_regs = ehci_regs;
797 info->ehci_debug = ehci_debug;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000798
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300799 info->ep_pipe[DBGP_SETUP_EP0].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300800 info->ep_pipe[DBGP_CONSOLE_EPOUT].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
801 info->ep_pipe[DBGP_CONSOLE_EPIN].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000802 return 0;
Yinghai Lud57241f2007-02-28 11:17:02 +0000803err:
804 /* Things didn't work so remove my claim */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000805 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000806 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000807 write32((unsigned long)(unsigned long)&ehci_debug->control, ctrl);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000808 //return ret;
Yinghai Lud57241f2007-02-28 11:17:02 +0000809
810next_debug_port:
Kyösti Mälkki24100102013-08-12 20:40:37 +0300811#if CONFIG_USBDEBUG_DEFAULT_PORT==0
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000812 port_map_tried |= (1 << (debug_port - 1));
813 new_debug_port = ((debug_port-1 + 1) % n_ports) + 1;
814 if (port_map_tried != ((1 << n_ports) - 1)) {
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200815 ehci_debug_select_port(new_debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000816 goto try_next_port;
817 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000818 if (--playtimes) {
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200819 ehci_debug_select_port(new_debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000820 goto try_next_time;
821 }
Kyösti Mälkki24100102013-08-12 20:40:37 +0300822#else
823 if (0)
824 goto try_next_port;
825 if (--playtimes)
826 goto try_next_time;
827#endif
Yinghai Lud57241f2007-02-28 11:17:02 +0000828
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000829 return -10;
830}
831
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300832#if CONFIG_DEBUG_USBDEBUG
833static int dbgp_enabled(void)
834{
835 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
836 return (globals->status & DBGP_EP_ENABLED);
837}
838#endif
839
Kyösti Mälkki8cde8522014-02-09 23:35:39 +0200840int dbgp_try_get(struct dbgp_pipe *pipe)
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300841{
842 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
843 if (!dbgp_ep_is_active(pipe) || (globals->status & DBGP_EP_BUSY))
844 return 0;
845 globals->status |= DBGP_EP_BUSY;
846 pipe->status |= DBGP_EP_BUSY;
847 return 1;
848}
849
Kyösti Mälkki8cde8522014-02-09 23:35:39 +0200850void dbgp_put(struct dbgp_pipe *pipe)
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300851{
852 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
853 globals->status &= ~DBGP_EP_BUSY;
854 pipe->status &= ~DBGP_EP_BUSY;
855}
856
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300857#if !defined(__PRE_RAM__) && !defined(__SMM__)
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200858void usbdebug_re_enable(unsigned ehci_base)
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300859{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300860 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300861 unsigned diff;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300862 int i;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300863
864 if (!dbg_info->ehci_debug)
865 return;
866
867 diff = (unsigned)dbg_info->ehci_caps - ehci_base;
868 dbg_info->ehci_regs -= diff;
869 dbg_info->ehci_debug -= diff;
870 dbg_info->ehci_caps = (void*)ehci_base;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300871
872 for (i=0; i<DBGP_MAX_ENDPOINTS; i++)
873 dbg_info->ep_pipe[i].status |= DBGP_EP_ENABLED;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300874}
875
Kyösti Mälkkicb141bc2014-02-07 19:24:23 +0200876void usbdebug_disable(void)
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300877{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300878 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300879 int i;
880 for (i=0; i<DBGP_MAX_ENDPOINTS; i++)
881 dbg_info->ep_pipe[i].status &= ~DBGP_EP_ENABLED;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300882}
883
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300884#endif
885
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +0300886#if !defined(__PRE_RAM__) && !defined(__SMM__)
887static int get_usbdebug_from_cbmem(struct ehci_debug_info *info)
888{
889 struct ehci_debug_info *dbg_info_cbmem;
890
891 dbg_info_cbmem = cbmem_find(CBMEM_ID_EHCI_DEBUG);
892 if (dbg_info_cbmem == NULL)
893 return -1;
894
895 memcpy(info, dbg_info_cbmem, sizeof (*info));
896 printk(BIOS_DEBUG, "EHCI debug port found in CBMEM.\n");
897
898 return 0;
899}
900
901#elif defined(__PRE_RAM__)
902static void migrate_ehci_debug(void)
903{
904 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
905 struct ehci_debug_info *dbg_info_cbmem;
906
907 dbg_info_cbmem = cbmem_add(CBMEM_ID_EHCI_DEBUG, sizeof(*dbg_info));
908 if (dbg_info_cbmem == NULL)
909 return;
910
911 memcpy(dbg_info_cbmem, dbg_info, sizeof(*dbg_info));
Kyösti Mälkki618d1792014-10-27 08:01:55 +0200912 car_set_var(glob_dbg_info_p, dbg_info_cbmem);
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +0300913}
914CAR_MIGRATE(migrate_ehci_debug);
915#endif
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +0300916
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300917int dbgp_ep_is_active(struct dbgp_pipe *pipe)
Kyösti Mälkki4d409b52013-07-05 21:38:54 +0300918{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300919 return (pipe->status & DBGP_EP_STATMASK) == (DBGP_EP_VALID | DBGP_EP_ENABLED);
Yinghai Lud57241f2007-02-28 11:17:02 +0000920}
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300921
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300922struct dbgp_pipe *dbgp_console_output(void)
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300923{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300924 return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPOUT];
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300925}
926
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300927struct dbgp_pipe *dbgp_console_input(void)
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300928{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300929 return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPIN];
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300930}
931
932int usbdebug_init(void)
933{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300934 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +0200935 unsigned int ehci_base, dbg_offset;
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +0300936
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300937#if !defined(__PRE_RAM__) && !defined(__SMM__)
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +0300938 if (!get_usbdebug_from_cbmem(dbg_info))
939 return 0;
940#endif
Kyösti Mälkki6f6a2492014-02-09 19:21:30 +0200941 if (ehci_debug_hw_enable(&ehci_base, &dbg_offset))
942 return -1;
943 return usbdebug_init_(ehci_base, dbg_offset, dbg_info);
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300944}