blob: 39cd138ba06f9a3b8293c4f92f232970129b8a54 [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>
Yinghai Lud57241f2007-02-28 11:17:02 +000023#include <arch/io.h>
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +030024#include <device/pci.h>
Kyösti Mälkki8101aa62013-08-15 16:27:06 +030025#include <device/pci_def.h>
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000026#include <arch/byteorder.h>
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +030027#include <cpu/x86/car.h>
Kyösti Mälkkie23c22d2013-07-28 23:16:47 +030028#include <string.h>
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +030029#include <cbmem.h>
Yinghai Lud57241f2007-02-28 11:17:02 +000030
31#include <usb_ch9.h>
32#include <ehci.h>
Stefan Reinauerda323732010-05-25 16:17:45 +000033#include <usbdebug.h>
Yinghai Lud57241f2007-02-28 11:17:02 +000034
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030035
36#define DBGP_EP_VALID (1<<0)
37#define DBGP_EP_ENABLED (1<<1)
Kyösti Mälkkid7991402013-08-12 16:11:34 +030038#define DBGP_EP_BUSY (1<<2)
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030039#define DBGP_EP_STATMASK (DBGP_EP_VALID | DBGP_EP_ENABLED)
40
41struct dbgp_pipe
42{
Kyösti Mälkki75d00622013-08-10 10:34:01 +030043 u8 devnum;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030044 u8 endpoint;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +030045 u8 pid;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030046 u8 status;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +030047 int timeout;
48
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030049 u8 bufidx;
50 char buf[8];
51};
52
53#define DBGP_MAX_ENDPOINTS 4
Kyösti Mälkkid7991402013-08-12 16:11:34 +030054#define DBGP_SETUP_EP0 0 /* Compulsory endpoint 0. */
55#define DBGP_CONSOLE_EPOUT 1
56#define DBGP_CONSOLE_EPIN 2
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030057
58struct ehci_debug_info {
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030059 void *ehci_caps;
60 void *ehci_regs;
61 void *ehci_debug;
62
63 struct dbgp_pipe ep_pipe[DBGP_MAX_ENDPOINTS];
64};
65
Kyösti Mälkki3be80cc2013-06-06 10:46:37 +030066#if CONFIG_DEBUG_USBDEBUG
Kyösti Mälkkie53cece2013-08-10 10:50:43 +030067static void dbgp_print_data(struct ehci_dbg_port *ehci_debug);
Kyösti Mälkkid7991402013-08-12 16:11:34 +030068static int dbgp_enabled(void);
69# define dprintk(LEVEL, args...) \
70 do { if (!dbgp_enabled()) printk(LEVEL, ##args); } while (0)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000071#else
Kyösti Mälkkie53cece2013-08-10 10:50:43 +030072# define dbgp_print_data(x) do {} while(0)
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +030073# define dprintk(LEVEL, args...) do {} while(0)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000074#endif
75
Yinghai Lud57241f2007-02-28 11:17:02 +000076#define USB_DEBUG_DEVNUM 127
77
Yinghai Lud57241f2007-02-28 11:17:02 +000078#define DBGP_LEN_UPDATE(x, len) (((x) & ~0x0f) | ((len) & 0x0f))
79/*
80 * USB Packet IDs (PIDs)
81 */
82
83/* token */
84#define USB_PID_OUT 0xe1
85#define USB_PID_IN 0x69
86#define USB_PID_SOF 0xa5
87#define USB_PID_SETUP 0x2d
88/* handshake */
89#define USB_PID_ACK 0xd2
90#define USB_PID_NAK 0x5a
91#define USB_PID_STALL 0x1e
92#define USB_PID_NYET 0x96
93/* data */
94#define USB_PID_DATA0 0xc3
95#define USB_PID_DATA1 0x4b
96#define USB_PID_DATA2 0x87
97#define USB_PID_MDATA 0x0f
98/* Special */
99#define USB_PID_PREAMBLE 0x3c
100#define USB_PID_ERR 0x3c
101#define USB_PID_SPLIT 0x78
102#define USB_PID_PING 0xb4
103#define USB_PID_UNDEF_0 0xf0
104
105#define USB_PID_DATA_TOGGLE 0x88
106#define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
107
108#define PCI_CAP_ID_EHCI_DEBUG 0xa
109
110#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
111#define HUB_SHORT_RESET_TIME 10
112#define HUB_LONG_RESET_TIME 200
113#define HUB_RESET_TIMEOUT 500
114
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300115#define DBGP_MICROFRAME_TIMEOUT_LOOPS 1000
116#define DBGP_MICROFRAME_RETRIES 10
Yinghai Lud57241f2007-02-28 11:17:02 +0000117#define DBGP_MAX_PACKET 8
118
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +0300119static struct ehci_debug_info glob_dbg_info CAR_GLOBAL;
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300120#if !defined(__PRE_RAM__) && !defined(__SMM__)
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300121static struct device_operations *ehci_drv_ops;
122static struct device_operations ehci_dbg_ops;
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300123#endif
124
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300125static inline struct ehci_debug_info *dbgp_ehci_info(void)
126{
127 return car_get_var_ptr(&glob_dbg_info);
128}
129
Yinghai Lud57241f2007-02-28 11:17:02 +0000130static int dbgp_wait_until_complete(struct ehci_dbg_port *ehci_debug)
131{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000132 u32 ctrl;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300133 int loop = 0;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000134
Yinghai Lud57241f2007-02-28 11:17:02 +0000135 do {
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000136 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000137 /* Stop when the transaction is finished */
138 if (ctrl & DBGP_DONE)
139 break;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300140 } while (++loop < DBGP_MICROFRAME_TIMEOUT_LOOPS);
Yinghai Lud57241f2007-02-28 11:17:02 +0000141
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300142 if (! (ctrl & DBGP_DONE)) {
143 dprintk(BIOS_ERR, "dbgp_wait_until_complete: retry timeout.\n");
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300144 return -DBGP_ERR_SIGNAL;
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300145 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000146
147 /* Now that we have observed the completed transaction,
148 * clear the done bit.
149 */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000150 write32((unsigned long)&ehci_debug->control, ctrl | DBGP_DONE);
Yinghai Lud57241f2007-02-28 11:17:02 +0000151 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
152}
153
Yinghai Lud57241f2007-02-28 11:17:02 +0000154static void dbgp_breath(void)
155{
156 /* Sleep to give the debug port a chance to breathe */
157}
158
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300159static int dbgp_wait_until_done(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe,
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300160 unsigned ctrl, const int timeout)
Yinghai Lud57241f2007-02-28 11:17:02 +0000161{
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300162 u32 rd_ctrl, rd_pids;
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300163 u32 ctrl_prev = 0, pids_prev = 0;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300164 u8 lpid;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300165 int ret, host_retries;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300166 int loop;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000167
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300168 loop = 0;
169device_retry:
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300170 host_retries = 0;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300171 if (loop++ >= timeout)
172 return -DBGP_ERR_BAD;
173
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300174host_retry:
175 if (host_retries++ >= DBGP_MICROFRAME_RETRIES)
176 return -DBGP_ERR_BAD;
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300177 if (loop == 1 || host_retries > 1)
178 dprintk(BIOS_SPEW, "dbgp: start (@ %3d,%d) ctrl=%08x\n",
179 loop, host_retries, ctrl | DBGP_GO);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000180 write32((unsigned long)&ehci_debug->control, ctrl | DBGP_GO);
Yinghai Lud57241f2007-02-28 11:17:02 +0000181 ret = dbgp_wait_until_complete(ehci_debug);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300182 rd_ctrl = read32((unsigned long)&ehci_debug->control);
183 rd_pids = read32((unsigned long)&ehci_debug->pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000184
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300185 if (rd_ctrl != ctrl_prev || rd_pids != pids_prev || (ret<0)) {
186 ctrl_prev = rd_ctrl;
187 pids_prev = rd_pids;
188 dprintk(BIOS_SPEW, "dbgp: status (@ %3d,%d) ctrl=%08x pids=%08x ret=%d\n",
189 loop, host_retries, rd_ctrl, rd_pids, ret);
190 }
191
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300192 /* Controller hardware failure. */
193 if (ret == -DBGP_ERR_SIGNAL) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000194 return ret;
Kyösti Mälkki2de841b32013-08-19 12:45:16 +0300195
196 /* Bus failure (corrupted microframe). */
197 } else if (ret == -DBGP_ERR_BAD) {
198 goto host_retry;
Sven Schnelle10680872012-07-25 14:19:45 +0200199 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000200
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300201 lpid = DBGP_PID_GET(rd_pids);
202
203 /* If I get an ACK or in-sync DATA PID, we are done. */
204 if ((lpid == USB_PID_ACK) || (lpid == pipe->pid)) {
205 if (DBGP_LEN(rd_ctrl))
206 pipe->pid ^= USB_PID_DATA_TOGGLE;
207 }
208
Yinghai Lud57241f2007-02-28 11:17:02 +0000209 /* If the port is getting full or it has dropped data
210 * start pacing ourselves, not necessary but it's friendly.
211 */
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300212 else if (lpid == USB_PID_NYET) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000213 dbgp_breath();
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300214 goto device_retry;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300215 }
216
217 /* If I get a NACK or out-of-sync DATA PID, reissue the transmission. */
218 else if ((lpid == USB_PID_NAK) || (lpid == (pipe->pid ^ USB_PID_DATA_TOGGLE))) {
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300219 goto device_retry;
Yinghai Lud57241f2007-02-28 11:17:02 +0000220 }
221
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300222 /* Abort on STALL handshake for endpoint 0.*/
223 else if ((lpid == USB_PID_STALL) && (pipe->endpoint == 0x0)) {
224 ret = -DBGP_ERR_BAD;
225 }
226
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300227 dbgp_print_data(ehci_debug);
228
Yinghai Lud57241f2007-02-28 11:17:02 +0000229 return ret;
230}
231
232static void dbgp_set_data(struct ehci_dbg_port *ehci_debug, const void *buf, int size)
233{
234 const unsigned char *bytes = buf;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000235 u32 lo, hi;
Yinghai Lud57241f2007-02-28 11:17:02 +0000236 int i;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000237
Yinghai Lud57241f2007-02-28 11:17:02 +0000238 lo = hi = 0;
239 for (i = 0; i < 4 && i < size; i++)
240 lo |= bytes[i] << (8*i);
241 for (; i < 8 && i < size; i++)
242 hi |= bytes[i] << (8*(i - 4));
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000243 write32((unsigned long)&ehci_debug->data03, lo);
244 write32((unsigned long)&ehci_debug->data47, hi);
Yinghai Lud57241f2007-02-28 11:17:02 +0000245}
246
247static void dbgp_get_data(struct ehci_dbg_port *ehci_debug, void *buf, int size)
248{
249 unsigned char *bytes = buf;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000250 u32 lo, hi;
Yinghai Lud57241f2007-02-28 11:17:02 +0000251 int i;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000252
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000253 lo = read32((unsigned long)&ehci_debug->data03);
254 hi = read32((unsigned long)&ehci_debug->data47);
Yinghai Lud57241f2007-02-28 11:17:02 +0000255 for (i = 0; i < 4 && i < size; i++)
256 bytes[i] = (lo >> (8*i)) & 0xff;
257 for (; i < 8 && i < size; i++)
258 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
259}
260
Kyösti Mälkkie53cece2013-08-10 10:50:43 +0300261#if CONFIG_DEBUG_USBDEBUG
262static void dbgp_print_data(struct ehci_dbg_port *ehci_debug)
263{
264 u32 ctrl = read32((unsigned long)&ehci_debug->control);
265 u32 lo = read32((unsigned long)&ehci_debug->data03);
266 u32 hi = read32((unsigned long)&ehci_debug->data47);
267 int len = DBGP_LEN(ctrl);
268 if (len) {
269 int i;
270 dprintk(BIOS_SPEW, "dbgp: buf:");
271 for (i = 0; i < 4 && i < len; i++)
272 dprintk(BIOS_SPEW, " %02x", (lo >> (8*i)) & 0xff);
273 for (; i < 8 && i < len; i++)
274 dprintk(BIOS_SPEW, " %02x", (hi >> (8*(i - 4))) & 0xff);
275 dprintk(BIOS_SPEW, "\n");
276 }
277}
278#endif
279
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300280static int dbgp_bulk_write(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe,
281 const char *bytes, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000282{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000283 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000284 int ret;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000285
Yinghai Lud57241f2007-02-28 11:17:02 +0000286 if (size > DBGP_MAX_PACKET)
287 return -1;
288
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300289 addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300290 pids = DBGP_PID_SET(pipe->pid, USB_PID_OUT);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000291
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000292 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000293 ctrl = DBGP_LEN_UPDATE(ctrl, size);
294 ctrl |= DBGP_OUT;
Yinghai Lud57241f2007-02-28 11:17:02 +0000295
296 dbgp_set_data(ehci_debug, bytes, size);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000297 write32((unsigned long)&ehci_debug->address, addr);
298 write32((unsigned long)&ehci_debug->pids, pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000299
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300300 ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000301
Yinghai Lud57241f2007-02-28 11:17:02 +0000302 return ret;
303}
304
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300305int dbgp_bulk_write_x(struct dbgp_pipe *pipe, const char *bytes, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000306{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300307 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300308 return dbgp_bulk_write(dbg_info->ehci_debug, pipe, bytes, size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000309}
310
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300311static int dbgp_bulk_read(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe,
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300312 void *data, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000313{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000314 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000315 int ret;
316
317 if (size > DBGP_MAX_PACKET)
318 return -1;
319
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300320 addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300321 pids = DBGP_PID_SET(pipe->pid, USB_PID_IN);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000322
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000323 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000324 ctrl = DBGP_LEN_UPDATE(ctrl, size);
325 ctrl &= ~DBGP_OUT;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000326
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000327 write32((unsigned long)&ehci_debug->address, addr);
328 write32((unsigned long)&ehci_debug->pids, pids);
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300329 ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout);
Yinghai Lud57241f2007-02-28 11:17:02 +0000330 if (ret < 0)
331 return ret;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000332
Yinghai Lud57241f2007-02-28 11:17:02 +0000333 if (size > ret)
334 size = ret;
335 dbgp_get_data(ehci_debug, data, size);
336 return ret;
337}
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000338
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300339int dbgp_bulk_read_x(struct dbgp_pipe *pipe, void *data, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000340{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300341 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300342 return dbgp_bulk_read(dbg_info->ehci_debug, pipe, data, size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000343}
344
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000345static void dbgp_mdelay(int ms)
Yinghai Lud57241f2007-02-28 11:17:02 +0000346{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000347 int i;
348
349 while (ms--) {
350 for (i = 0; i < 1000; i++)
351 inb(0x80);
352 }
353}
354
355static int dbgp_control_msg(struct ehci_dbg_port *ehci_debug, unsigned devnum, int requesttype,
356 int request, int value, int index, void *data, int size)
357{
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300358 struct ehci_debug_info *info = dbgp_ehci_info();
359 struct dbgp_pipe *pipe = &info->ep_pipe[DBGP_SETUP_EP0];
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000360 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000361 struct usb_ctrlrequest req;
362 int read;
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300363 int ret, ret2;
Yinghai Lud57241f2007-02-28 11:17:02 +0000364
365 read = (requesttype & USB_DIR_IN) != 0;
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300366 if (size > DBGP_MAX_PACKET)
Yinghai Lud57241f2007-02-28 11:17:02 +0000367 return -1;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000368
Yinghai Lud57241f2007-02-28 11:17:02 +0000369 /* Compute the control message */
370 req.bRequestType = requesttype;
371 req.bRequest = request;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000372 req.wValue = cpu_to_le16(value);
373 req.wIndex = cpu_to_le16(index);
374 req.wLength = cpu_to_le16(size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000375
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300376 pipe->devnum = devnum;
377 pipe->endpoint = 0;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300378 pipe->pid = USB_PID_DATA0;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300379 pipe->timeout = 1000;
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300380 addr = DBGP_EPADDR(pipe->devnum, pipe->endpoint);
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300381 pids = DBGP_PID_SET(pipe->pid, USB_PID_SETUP);
Yinghai Lud57241f2007-02-28 11:17:02 +0000382
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000383 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000384 ctrl = DBGP_LEN_UPDATE(ctrl, sizeof(req));
385 ctrl |= DBGP_OUT;
Yinghai Lud57241f2007-02-28 11:17:02 +0000386
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300387 /* Setup stage */
Yinghai Lud57241f2007-02-28 11:17:02 +0000388 dbgp_set_data(ehci_debug, &req, sizeof(req));
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000389 write32((unsigned long)&ehci_debug->address, addr);
390 write32((unsigned long)&ehci_debug->pids, pids);
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300391 ret = dbgp_wait_until_done(ehci_debug, pipe, ctrl, 1);
Yinghai Lud57241f2007-02-28 11:17:02 +0000392 if (ret < 0)
393 return ret;
394
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300395 /* Data stage (optional) */
396 if (read && size)
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300397 ret = dbgp_bulk_read(ehci_debug, pipe, data, size);
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300398 else if (!read && size)
399 ret = dbgp_bulk_write(ehci_debug, pipe, data, size);
400
401 /* Status stage in opposite direction */
402 pipe->pid = USB_PID_DATA1;
403 ctrl = read32((unsigned long)&ehci_debug->control);
404 ctrl = DBGP_LEN_UPDATE(ctrl, 0);
405 if (read) {
406 pids = DBGP_PID_SET(pipe->pid, USB_PID_OUT);
407 ctrl |= DBGP_OUT;
408 } else {
409 pids = DBGP_PID_SET(pipe->pid, USB_PID_IN);
410 ctrl &= ~DBGP_OUT;
411 }
412
413 write32((unsigned long)&ehci_debug->pids, pids);
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300414 ret2 = dbgp_wait_until_done(ehci_debug, pipe, ctrl, pipe->timeout);
Kyösti Mälkkidcccbd12013-08-10 10:08:38 +0300415 if (ret2 < 0)
416 return ret2;
417
Yinghai Lud57241f2007-02-28 11:17:02 +0000418 return ret;
419}
420
421static int ehci_reset_port(struct ehci_regs *ehci_regs, int port)
422{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000423 u32 portsc;
424 u32 delay_time, delay_ms;
Yinghai Lud57241f2007-02-28 11:17:02 +0000425 int loop;
426
427 /* Reset the usb debug port */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000428 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000429 portsc &= ~PORT_PE;
430 portsc |= PORT_RESET;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000431 write32((unsigned long)&ehci_regs->port_status[port - 1], portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000432
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000433 delay_ms = HUB_ROOT_RESET_TIME;
Yinghai Lud57241f2007-02-28 11:17:02 +0000434 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000435 delay_time += delay_ms) {
436 dbgp_mdelay(delay_ms);
Yinghai Lud57241f2007-02-28 11:17:02 +0000437
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000438 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000439 if (portsc & PORT_RESET) {
440 /* force reset to complete */
441 loop = 2;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000442 write32((unsigned long)&ehci_regs->port_status[port - 1],
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000443 portsc & ~(PORT_RWC_BITS | PORT_RESET));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000444 do {
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000445 dbgp_mdelay(delay_ms);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000446 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000447 delay_time += delay_ms;
Yinghai Lud57241f2007-02-28 11:17:02 +0000448 } while ((portsc & PORT_RESET) && (--loop > 0));
449 if (!loop) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000450 printk(BIOS_DEBUG, "ehci_reset_port forced done");
Yinghai Lud57241f2007-02-28 11:17:02 +0000451 }
452 }
453
454 /* Device went away? */
455 if (!(portsc & PORT_CONNECT))
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000456 return -1; //-ENOTCONN;
Yinghai Lud57241f2007-02-28 11:17:02 +0000457
Martin Rothcbf2bd72013-07-09 21:51:14 -0600458 /* bomb out completely if something weird happened */
Yinghai Lud57241f2007-02-28 11:17:02 +0000459 if ((portsc & PORT_CSC))
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000460 return -2; //-EINVAL;
Yinghai Lud57241f2007-02-28 11:17:02 +0000461
462 /* If we've finished resetting, then break out of the loop */
463 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
464 return 0;
465 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000466 return -3; //-EBUSY;
Yinghai Lud57241f2007-02-28 11:17:02 +0000467}
468
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000469static int ehci_wait_for_port(struct ehci_regs *ehci_regs, int port)
Yinghai Lud57241f2007-02-28 11:17:02 +0000470{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000471 u32 status;
Yinghai Lud57241f2007-02-28 11:17:02 +0000472 int ret, reps;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000473
Yinghai Lud57241f2007-02-28 11:17:02 +0000474 for (reps = 0; reps < 3; reps++) {
475 dbgp_mdelay(100);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000476 status = read32((unsigned long)&ehci_regs->status);
Yinghai Lud57241f2007-02-28 11:17:02 +0000477 if (status & STS_PCD) {
478 ret = ehci_reset_port(ehci_regs, port);
479 if (ret == 0)
480 return 0;
481 }
482 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000483 return -1; //-ENOTCONN;
Yinghai Lud57241f2007-02-28 11:17:02 +0000484}
485
Kyösti Mälkkid2dac0a2013-08-23 23:33:16 +0300486#define USB_HUB_PORT_CONNECTION 0
487#define USB_HUB_PORT_ENABLED 1
488#define USB_HUB_PORT_RESET 4
489#define USB_HUB_PORT_POWER 8
490#define USB_HUB_C_PORT_CONNECTION 16
491#define USB_HUB_C_PORT_RESET 20
492
493#if CONFIG_USBDEBUG_OPTIONAL_HUB_PORT
494
495static int hub_port_status(const char * buf, int feature)
496{
497 return !!(buf[feature>>3] & (1<<(feature&0x7)));
498}
499
500static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned int port)
501{
502 const u8 hub_addr = USB_DEBUG_DEVNUM-1;
503 char status[8];
504 int ret, loop;
505
506 /* Move hub to address 126. */
507 ret = dbgp_control_msg(ehci_debug, 0,
508 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
509 USB_REQ_SET_ADDRESS, hub_addr, 0, NULL, 0);
510 if (ret < 0)
511 goto err;
512
513 /* Enter configured state on hub. */
514 ret = dbgp_control_msg(ehci_debug, hub_addr,
515 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
516 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
517 if (ret < 0)
518 goto err;
519
520 /* Set PORT_POWER, poll for PORT_CONNECTION. */
521 ret = dbgp_control_msg(ehci_debug, hub_addr,
522 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
523 USB_REQ_SET_FEATURE, USB_HUB_PORT_POWER, port, NULL, 0);
524 if (ret < 0)
525 goto err;
526
527 loop = 100;
528 do {
529 dbgp_mdelay(10);
530 ret = dbgp_control_msg(ehci_debug, hub_addr,
531 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
532 USB_REQ_GET_STATUS, 0, port, status, 4);
533 if (ret < 0)
534 goto err;
535 if (hub_port_status(status, USB_HUB_PORT_CONNECTION))
536 break;
537 } while (--loop);
538 if (! loop)
539 goto err;
540
541 ret = dbgp_control_msg(ehci_debug, hub_addr,
542 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
543 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_CONNECTION, port, NULL, 0);
544 if (ret < 0)
545 goto err;
546
547
548 /* Set PORT_RESET, poll for C_PORT_RESET. */
549 ret = dbgp_control_msg(ehci_debug, hub_addr,
550 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
551 USB_REQ_SET_FEATURE, USB_HUB_PORT_RESET, port, NULL, 0);
552 if (ret < 0)
553 goto err;
554
555 loop = 100;
556 do {
557 dbgp_mdelay(10);
558 ret = dbgp_control_msg(ehci_debug, hub_addr,
559 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
560 USB_REQ_GET_STATUS, 0, port, status, 4);
561 if (ret < 0)
562 goto err;
563 if (hub_port_status(status, USB_HUB_C_PORT_RESET))
564 break;
565 } while (--loop);
566 if (! loop)
567 goto err;
568
569 ret = dbgp_control_msg(ehci_debug, hub_addr,
570 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
571 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_RESET, port, NULL, 0);
572 if (ret < 0)
573 goto err;
574
575 if (hub_port_status(status, USB_HUB_PORT_ENABLED))
576 return 0;
577err:
578 return -1;
579}
580#endif /* CONFIG_USBDEBUG_OPTIONAL_HUB_PORT */
581
Kyösti Mälkki8101aa62013-08-15 16:27:06 +0300582#if defined(__PRE_RAM__) || !CONFIG_EARLY_CONSOLE
583static void enable_usbdebug(void)
584{
585 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
586 pci_ehci_dbg_enable(dbg_dev, CONFIG_EHCI_BAR);
587}
588#endif
589
590static void set_debug_port(unsigned int port)
591{
592 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
593 pci_ehci_dbg_set_port(dbg_dev, port);
594}
595
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300596static int usbdebug_init_(unsigned ehci_bar, unsigned offset, struct ehci_debug_info *info)
Yinghai Lud57241f2007-02-28 11:17:02 +0000597{
598 struct ehci_caps *ehci_caps;
599 struct ehci_regs *ehci_regs;
600 struct ehci_dbg_port *ehci_debug;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000601
Yinghai Lud57241f2007-02-28 11:17:02 +0000602 struct usb_debug_descriptor dbgp_desc;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000603 u32 cmd, ctrl, status, portsc, hcs_params;
604 u32 debug_port, new_debug_port = 0, n_ports;
605 u32 devnum;
Kyösti Mälkki6bfe61d2013-06-06 10:33:39 +0300606 int ret, i, configured;
Yinghai Lud57241f2007-02-28 11:17:02 +0000607 int loop;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000608 int port_map_tried;
609 int playtimes = 3;
Yinghai Lud57241f2007-02-28 11:17:02 +0000610
611 ehci_caps = (struct ehci_caps *)ehci_bar;
Stefan Reinauer5ff7c132011-10-31 12:56:45 -0700612 ehci_regs = (struct ehci_regs *)(ehci_bar +
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000613 HC_LENGTH(read32((unsigned long)&ehci_caps->hc_capbase)));
Yinghai Lud57241f2007-02-28 11:17:02 +0000614 ehci_debug = (struct ehci_dbg_port *)(ehci_bar + offset);
Yinghai Lud57241f2007-02-28 11:17:02 +0000615 info->ehci_debug = (void *)0;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300616 memset(&info->ep_pipe, 0, sizeof (info->ep_pipe));
Kyösti Mälkki24100102013-08-12 20:40:37 +0300617
618 if (CONFIG_USBDEBUG_DEFAULT_PORT > 0)
619 set_debug_port(CONFIG_USBDEBUG_DEFAULT_PORT);
620 else
621 set_debug_port(1);
622
Yinghai Lud57241f2007-02-28 11:17:02 +0000623try_next_time:
624 port_map_tried = 0;
625
626try_next_port:
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000627 hcs_params = read32((unsigned long)&ehci_caps->hcs_params);
Yinghai Lud57241f2007-02-28 11:17:02 +0000628 debug_port = HCS_DEBUG_PORT(hcs_params);
629 n_ports = HCS_N_PORTS(hcs_params);
630
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300631 dprintk(BIOS_INFO, "ehci_bar: 0x%x\n", ehci_bar);
632 dprintk(BIOS_INFO, "debug_port: %d\n", debug_port);
633 dprintk(BIOS_INFO, "n_ports: %d\n", n_ports);
Yinghai Lud57241f2007-02-28 11:17:02 +0000634
Yinghai Lud57241f2007-02-28 11:17:02 +0000635 for (i = 1; i <= n_ports; i++) {
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000636 portsc = read32((unsigned long)&ehci_regs->port_status[i-1]);
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300637 dprintk(BIOS_INFO, "PORTSC #%d: %08x\n", i, portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000638 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000639
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000640 if(port_map_tried && (new_debug_port != debug_port)) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000641 if(--playtimes) {
642 set_debug_port(debug_port);
643 goto try_next_time;
644 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000645 return -1;
Yinghai Lud57241f2007-02-28 11:17:02 +0000646 }
647
Kyösti Mälkki16c01452013-08-12 15:32:25 +0300648 /* Wait until the controller is halted */
649 status = read32((unsigned long)&ehci_regs->status);
650 if (!(status & STS_HALT)) {
651 cmd = read32((unsigned long)&ehci_regs->command);
652 cmd &= ~CMD_RUN;
653 write32((unsigned long)&ehci_regs->command, cmd);
654 loop = 100;
655 do {
656 dbgp_mdelay(10);
657 status = read32((unsigned long)&ehci_regs->status);
658 } while (!(status & STS_HALT) && (--loop > 0));
659 if (status & STS_HALT)
660 dprintk(BIOS_INFO, "EHCI controller halted successfully.\n");
661 else
662 dprintk(BIOS_INFO, "EHCI controller is not halted. Reset may fail.\n");
663 }
664
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000665 loop = 100;
Yinghai Lud57241f2007-02-28 11:17:02 +0000666 /* Reset the EHCI controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000667 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000668 cmd |= CMD_RESET;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000669 write32((unsigned long)&ehci_regs->command, cmd);
Yinghai Lud57241f2007-02-28 11:17:02 +0000670 do {
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000671 dbgp_mdelay(10);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000672 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000673 } while ((cmd & CMD_RESET) && (--loop > 0));
674
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000675 if(!loop) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300676 dprintk(BIOS_INFO, "Could not reset EHCI controller.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000677 // on some systems it works without succeeding here.
678 // return -2;
679 } else {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300680 dprintk(BIOS_INFO, "EHCI controller reset successfully.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000681 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000682
683 /* Claim ownership, but do not enable yet */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000684 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000685 ctrl |= DBGP_OWNER;
686 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000687 write32((unsigned long)&ehci_debug->control, ctrl);
Yinghai Lud57241f2007-02-28 11:17:02 +0000688
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000689 /* Start EHCI controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000690 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000691 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
692 cmd |= CMD_RUN;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000693 write32((unsigned long)&ehci_regs->command, cmd);
Yinghai Lud57241f2007-02-28 11:17:02 +0000694
695 /* Ensure everything is routed to the EHCI */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000696 write32((unsigned long)&ehci_regs->configured_flag, FLAG_CF);
Yinghai Lud57241f2007-02-28 11:17:02 +0000697
698 /* Wait until the controller is no longer halted */
699 loop = 10;
700 do {
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000701 dbgp_mdelay(10);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000702 status = read32((unsigned long)&ehci_regs->status);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000703 } while ((status & STS_HALT) && (--loop > 0));
Yinghai Lud57241f2007-02-28 11:17:02 +0000704
705 if(!loop) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300706 dprintk(BIOS_INFO, "EHCI could not be started.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000707 return -3;
Yinghai Lud57241f2007-02-28 11:17:02 +0000708 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300709 dprintk(BIOS_INFO, "EHCI started.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000710
711 /* Wait for a device to show up in the debug port */
712 ret = ehci_wait_for_port(ehci_regs, debug_port);
713 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300714 dprintk(BIOS_INFO, "No device found in debug port %d\n", debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000715 goto next_debug_port;
716 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300717 dprintk(BIOS_INFO, "EHCI done waiting for port.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000718
719 /* Enable the debug port */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000720 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000721 ctrl |= DBGP_CLAIM;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000722 write32((unsigned long)&ehci_debug->control, ctrl);
723 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000724 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300725 dprintk(BIOS_INFO, "No device in EHCI debug port.\n");
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000726 write32((unsigned long)&ehci_debug->control, ctrl & ~DBGP_CLAIM);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000727 ret = -4;
Yinghai Lud57241f2007-02-28 11:17:02 +0000728 goto err;
729 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300730 dprintk(BIOS_INFO, "EHCI debug port enabled.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000731
732 /* Completely transfer the debug device to the debug controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000733 portsc = read32((unsigned long)&ehci_regs->port_status[debug_port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000734 portsc &= ~PORT_PE;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000735 write32((unsigned long)&ehci_regs->port_status[debug_port - 1], portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000736
737 dbgp_mdelay(100);
738
Kyösti Mälkkid2dac0a2013-08-23 23:33:16 +0300739#if CONFIG_USBDEBUG_OPTIONAL_HUB_PORT
740 ret = dbgp_hub_enable(ehci_debug, CONFIG_USBDEBUG_OPTIONAL_HUB_PORT);
741 if (ret < 0) {
742 dprintk(BIOS_INFO, "Could not enable USB hub on debug port.\n");
743 ret = -6;
744 goto err;
745 }
746#endif
747
Yinghai Lud57241f2007-02-28 11:17:02 +0000748 /* Find the debug device and make it device number 127 */
Kyösti Mälkkia2adaeb2013-08-12 00:09:21 +0300749 devnum = 0;
750debug_dev_retry:
751 memset(&dbgp_desc, 0, sizeof(dbgp_desc));
752 ret = dbgp_control_msg(ehci_debug, devnum,
753 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
754 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
755 &dbgp_desc, sizeof(dbgp_desc));
756 if (ret == sizeof(dbgp_desc)) {
757 if (dbgp_desc.bLength == sizeof(dbgp_desc) && dbgp_desc.bDescriptorType==USB_DT_DEBUG)
758 goto debug_dev_found;
759 else
760 dprintk(BIOS_INFO, "Invalid debug device descriptor.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000761 }
Kyösti Mälkkia2adaeb2013-08-12 00:09:21 +0300762 if (devnum == 0) {
763 devnum = USB_DEBUG_DEVNUM;
764 goto debug_dev_retry;
765 } else {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300766 dprintk(BIOS_INFO, "Could not find attached debug device.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000767 ret = -5;
Yinghai Lud57241f2007-02-28 11:17:02 +0000768 goto err;
769 }
Kyösti Mälkkia2adaeb2013-08-12 00:09:21 +0300770debug_dev_found:
Yinghai Lud57241f2007-02-28 11:17:02 +0000771
772 /* Move the device to 127 if it isn't already there */
773 if (devnum != USB_DEBUG_DEVNUM) {
774 ret = dbgp_control_msg(ehci_debug, devnum,
775 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000776 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
Yinghai Lud57241f2007-02-28 11:17:02 +0000777 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300778 dprintk(BIOS_INFO, "Could not move attached device to %d.\n",
Yinghai Lud57241f2007-02-28 11:17:02 +0000779 USB_DEBUG_DEVNUM);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000780 ret = -7;
Yinghai Lud57241f2007-02-28 11:17:02 +0000781 goto err;
782 }
783 devnum = USB_DEBUG_DEVNUM;
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300784 dprintk(BIOS_INFO, "EHCI debug device renamed to 127.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000785 }
786
787 /* Enable the debug interface */
788 ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
789 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000790 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
Yinghai Lud57241f2007-02-28 11:17:02 +0000791 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300792 dprintk(BIOS_INFO, "Could not enable EHCI debug device.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000793 ret = -8;
Yinghai Lud57241f2007-02-28 11:17:02 +0000794 goto err;
795 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300796 dprintk(BIOS_INFO, "EHCI debug interface enabled.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000797
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300798 /* Prepare endpoint pipes. */
799 for (i=1; i<DBGP_MAX_ENDPOINTS; i++) {
800 info->ep_pipe[i].devnum = USB_DEBUG_DEVNUM;
Kyösti Mälkkie29584c2013-08-10 10:50:38 +0300801 info->ep_pipe[i].pid = USB_PID_DATA0;
Kyösti Mälkki5c87d2f12013-08-19 12:45:16 +0300802 info->ep_pipe[i].timeout = 1000;
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300803 }
804 info->ep_pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint;
805 info->ep_pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint;
806
807 /* Perform a small write. */
Kyösti Mälkki6bfe61d2013-06-06 10:33:39 +0300808 configured = 0;
809small_write:
Kyösti Mälkki75d00622013-08-10 10:34:01 +0300810 ret = dbgp_bulk_write(ehci_debug, &info->ep_pipe[DBGP_CONSOLE_EPOUT], "USB\r\n",5);
Yinghai Lud57241f2007-02-28 11:17:02 +0000811 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300812 dprintk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
Kyösti Mälkki6bfe61d2013-06-06 10:33:39 +0300813 if (!configured) {
814 /* Send Set Configure request to device. This is required for FX2
815 (CY7C68013) to transfer from USB state Addressed to Configured,
816 only then endpoints other than 0 are enabled. */
817 if (dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
818 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
819 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0) >= 0) {
820 configured = 1;
821 goto small_write;
822 }
823 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000824 ret = -9;
Yinghai Lud57241f2007-02-28 11:17:02 +0000825 goto err;
826 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300827 dprintk(BIOS_INFO, "Test write done\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000828
829 info->ehci_caps = ehci_caps;
830 info->ehci_regs = ehci_regs;
831 info->ehci_debug = ehci_debug;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000832
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300833 info->ep_pipe[DBGP_SETUP_EP0].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300834 info->ep_pipe[DBGP_CONSOLE_EPOUT].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
835 info->ep_pipe[DBGP_CONSOLE_EPIN].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000836 return 0;
Yinghai Lud57241f2007-02-28 11:17:02 +0000837err:
838 /* Things didn't work so remove my claim */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000839 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000840 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000841 write32((unsigned long)(unsigned long)&ehci_debug->control, ctrl);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000842 //return ret;
Yinghai Lud57241f2007-02-28 11:17:02 +0000843
844next_debug_port:
Kyösti Mälkki24100102013-08-12 20:40:37 +0300845#if CONFIG_USBDEBUG_DEFAULT_PORT==0
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000846 port_map_tried |= (1 << (debug_port - 1));
847 new_debug_port = ((debug_port-1 + 1) % n_ports) + 1;
848 if (port_map_tried != ((1 << n_ports) - 1)) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000849 set_debug_port(new_debug_port);
850 goto try_next_port;
851 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000852 if (--playtimes) {
Kyösti Mälkki24100102013-08-12 20:40:37 +0300853 set_debug_port(new_debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000854 goto try_next_time;
855 }
Kyösti Mälkki24100102013-08-12 20:40:37 +0300856#else
857 if (0)
858 goto try_next_port;
859 if (--playtimes)
860 goto try_next_time;
861#endif
Yinghai Lud57241f2007-02-28 11:17:02 +0000862
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000863 return -10;
864}
865
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300866#if CONFIG_DEBUG_USBDEBUG
867static int dbgp_enabled(void)
868{
869 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
870 return (globals->status & DBGP_EP_ENABLED);
871}
872#endif
873
874static int dbgp_try_get(struct dbgp_pipe *pipe)
875{
876 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
877 if (!dbgp_ep_is_active(pipe) || (globals->status & DBGP_EP_BUSY))
878 return 0;
879 globals->status |= DBGP_EP_BUSY;
880 pipe->status |= DBGP_EP_BUSY;
881 return 1;
882}
883
884static void dbgp_put(struct dbgp_pipe *pipe)
885{
886 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
887 globals->status &= ~DBGP_EP_BUSY;
888 pipe->status &= ~DBGP_EP_BUSY;
889}
890
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300891void usbdebug_tx_byte(struct dbgp_pipe *pipe, unsigned char data)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000892{
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300893 if (!dbgp_try_get(pipe))
894 return;
895 pipe->buf[pipe->bufidx++] = data;
896 if (pipe->bufidx >= 8) {
897 dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx);
898 pipe->bufidx = 0;
Sven Schnelle82704c62012-07-26 14:31:40 +0200899 }
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300900 dbgp_put(pipe);
Sven Schnelle82704c62012-07-26 14:31:40 +0200901}
902
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300903void usbdebug_tx_flush(struct dbgp_pipe *pipe)
Sven Schnelle82704c62012-07-26 14:31:40 +0200904{
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300905 if (!dbgp_try_get(pipe))
906 return;
907 if (pipe->bufidx > 0) {
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300908 dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx);
909 pipe->bufidx = 0;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000910 }
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300911 dbgp_put(pipe);
Kyösti Mälkki4d409b52013-07-05 21:38:54 +0300912}
913
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300914#if !defined(__PRE_RAM__) && !defined(__SMM__)
915static void usbdebug_re_enable(unsigned ehci_base)
916{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300917 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300918 unsigned diff;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300919 int i;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300920
921 if (!dbg_info->ehci_debug)
922 return;
923
924 diff = (unsigned)dbg_info->ehci_caps - ehci_base;
925 dbg_info->ehci_regs -= diff;
926 dbg_info->ehci_debug -= diff;
927 dbg_info->ehci_caps = (void*)ehci_base;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300928
929 for (i=0; i<DBGP_MAX_ENDPOINTS; i++)
930 dbg_info->ep_pipe[i].status |= DBGP_EP_ENABLED;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300931}
932
933static void usbdebug_disable(void)
934{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300935 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300936 int i;
937 for (i=0; i<DBGP_MAX_ENDPOINTS; i++)
938 dbg_info->ep_pipe[i].status &= ~DBGP_EP_ENABLED;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300939}
940
941static void pci_ehci_set_resources(struct device *dev)
942{
943 struct resource *res;
944
945 printk(BIOS_DEBUG, "%s EHCI Debug Port hook triggered\n", dev_path(dev));
946 usbdebug_disable();
947
948 if (ehci_drv_ops->set_resources)
949 ehci_drv_ops->set_resources(dev);
950 res = find_resource(dev, EHCI_BAR_INDEX);
951 if (!res)
952 return;
953
954 usbdebug_re_enable((u32)res->base);
955 report_resource_stored(dev, res, "");
956 printk(BIOS_DEBUG, "%s EHCI Debug Port relocated\n", dev_path(dev));
957}
958
959void pci_ehci_read_resources(struct device *dev)
960{
Kyösti Mälkki8101aa62013-08-15 16:27:06 +0300961 pci_devfn_t dbg_dev = pci_ehci_dbg_dev(CONFIG_USBDEBUG_HCD_INDEX);
962
963 if (!ehci_drv_ops && pci_match_simple_dev(dev, dbg_dev)) {
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300964 memcpy(&ehci_dbg_ops, dev->ops, sizeof(ehci_dbg_ops));
965 ehci_drv_ops = dev->ops;
966 ehci_dbg_ops.set_resources = pci_ehci_set_resources;
967 dev->ops = &ehci_dbg_ops;
968 printk(BIOS_DEBUG, "%s EHCI BAR hook registered\n", dev_path(dev));
969 } else {
970 printk(BIOS_DEBUG, "More than one caller of %s from %s\n", __func__, dev_path(dev));
971 }
972
973 pci_dev_read_resources(dev);
974}
975#endif
976
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +0300977#if !defined(__PRE_RAM__) && !defined(__SMM__)
978static int get_usbdebug_from_cbmem(struct ehci_debug_info *info)
979{
980 struct ehci_debug_info *dbg_info_cbmem;
981
982 dbg_info_cbmem = cbmem_find(CBMEM_ID_EHCI_DEBUG);
983 if (dbg_info_cbmem == NULL)
984 return -1;
985
986 memcpy(info, dbg_info_cbmem, sizeof (*info));
987 printk(BIOS_DEBUG, "EHCI debug port found in CBMEM.\n");
988
989 return 0;
990}
991
992#elif defined(__PRE_RAM__)
993static void migrate_ehci_debug(void)
994{
995 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
996 struct ehci_debug_info *dbg_info_cbmem;
997
998 dbg_info_cbmem = cbmem_add(CBMEM_ID_EHCI_DEBUG, sizeof(*dbg_info));
999 if (dbg_info_cbmem == NULL)
1000 return;
1001
1002 memcpy(dbg_info_cbmem, dbg_info, sizeof(*dbg_info));
1003}
1004CAR_MIGRATE(migrate_ehci_debug);
1005#endif
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +03001006
Kyösti Mälkki8101aa62013-08-15 16:27:06 +03001007unsigned long pci_ehci_base_regs(pci_devfn_t sdev)
1008{
1009#ifdef __SIMPLE_DEVICE__
1010 unsigned long base = pci_read_config32(sdev, EHCI_BAR_INDEX) & ~0x0f;
1011#else
1012 device_t dev = dev_find_slot(PCI_DEV2SEGBUS(sdev), PCI_DEV2DEVFN(sdev));
1013 unsigned long base = pci_read_config32(dev, EHCI_BAR_INDEX) & ~0x0f;
1014#endif
1015 return base + HC_LENGTH(read32(base));
1016}
1017
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +03001018int dbgp_ep_is_active(struct dbgp_pipe *pipe)
Kyösti Mälkki4d409b52013-07-05 21:38:54 +03001019{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +03001020 return (pipe->status & DBGP_EP_STATMASK) == (DBGP_EP_VALID | DBGP_EP_ENABLED);
Yinghai Lud57241f2007-02-28 11:17:02 +00001021}
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +03001022
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +03001023struct dbgp_pipe *dbgp_console_output(void)
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +03001024{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +03001025 return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPOUT];
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +03001026}
1027
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +03001028struct dbgp_pipe *dbgp_console_input(void)
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +03001029{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +03001030 return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPIN];
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +03001031}
1032
1033int usbdebug_init(void)
1034{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +03001035 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +03001036
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +03001037#if !defined(__PRE_RAM__) && !defined(__SMM__)
Kyösti Mälkki690bf2f2013-07-06 11:41:21 +03001038 if (!get_usbdebug_from_cbmem(dbg_info))
1039 return 0;
1040#endif
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +03001041#if defined(__PRE_RAM__) || !CONFIG_EARLY_CONSOLE
Kyösti Mälkki8101aa62013-08-15 16:27:06 +03001042 enable_usbdebug();
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +03001043#endif
1044 return usbdebug_init_(CONFIG_EHCI_BAR, CONFIG_EHCI_DEBUG_OFFSET, dbg_info);
1045}