blob: a5d3c3702780352542e674d65730168d4235e102 [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>
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000025#include <arch/byteorder.h>
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +030026#include <cpu/x86/car.h>
Kyösti Mälkkie23c22d2013-07-28 23:16:47 +030027#include <string.h>
Yinghai Lud57241f2007-02-28 11:17:02 +000028
29#include <usb_ch9.h>
30#include <ehci.h>
Stefan Reinauerda323732010-05-25 16:17:45 +000031#include <usbdebug.h>
Yinghai Lud57241f2007-02-28 11:17:02 +000032
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030033
34#define DBGP_EP_VALID (1<<0)
35#define DBGP_EP_ENABLED (1<<1)
Kyösti Mälkkid7991402013-08-12 16:11:34 +030036#define DBGP_EP_BUSY (1<<2)
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030037#define DBGP_EP_STATMASK (DBGP_EP_VALID | DBGP_EP_ENABLED)
38
39struct dbgp_pipe
40{
41 u8 endpoint;
42 u8 status;
43 u8 bufidx;
44 char buf[8];
45};
46
47#define DBGP_MAX_ENDPOINTS 4
Kyösti Mälkkid7991402013-08-12 16:11:34 +030048#define DBGP_SETUP_EP0 0 /* Compulsory endpoint 0. */
49#define DBGP_CONSOLE_EPOUT 1
50#define DBGP_CONSOLE_EPIN 2
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +030051
52struct ehci_debug_info {
53 u8 devnum;
54
55 void *ehci_caps;
56 void *ehci_regs;
57 void *ehci_debug;
58
59 struct dbgp_pipe ep_pipe[DBGP_MAX_ENDPOINTS];
60};
61
Kyösti Mälkki3be80cc2013-06-06 10:46:37 +030062#if CONFIG_DEBUG_USBDEBUG
Kyösti Mälkkid7991402013-08-12 16:11:34 +030063static int dbgp_enabled(void);
64# define dprintk(LEVEL, args...) \
65 do { if (!dbgp_enabled()) printk(LEVEL, ##args); } while (0)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000066#else
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +030067# define dprintk(LEVEL, args...) do {} while(0)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +000068#endif
69
Yinghai Lud57241f2007-02-28 11:17:02 +000070#define USB_DEBUG_DEVNUM 127
71
72#define DBGP_DATA_TOGGLE 0x8800
73#define DBGP_PID_UPDATE(x, tok) \
74 ((((x) ^ DBGP_DATA_TOGGLE) & 0xffff00) | ((tok) & 0xff))
75
76#define DBGP_LEN_UPDATE(x, len) (((x) & ~0x0f) | ((len) & 0x0f))
77/*
78 * USB Packet IDs (PIDs)
79 */
80
81/* token */
82#define USB_PID_OUT 0xe1
83#define USB_PID_IN 0x69
84#define USB_PID_SOF 0xa5
85#define USB_PID_SETUP 0x2d
86/* handshake */
87#define USB_PID_ACK 0xd2
88#define USB_PID_NAK 0x5a
89#define USB_PID_STALL 0x1e
90#define USB_PID_NYET 0x96
91/* data */
92#define USB_PID_DATA0 0xc3
93#define USB_PID_DATA1 0x4b
94#define USB_PID_DATA2 0x87
95#define USB_PID_MDATA 0x0f
96/* Special */
97#define USB_PID_PREAMBLE 0x3c
98#define USB_PID_ERR 0x3c
99#define USB_PID_SPLIT 0x78
100#define USB_PID_PING 0xb4
101#define USB_PID_UNDEF_0 0xf0
102
103#define USB_PID_DATA_TOGGLE 0x88
104#define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
105
106#define PCI_CAP_ID_EHCI_DEBUG 0xa
107
108#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
109#define HUB_SHORT_RESET_TIME 10
110#define HUB_LONG_RESET_TIME 200
111#define HUB_RESET_TIMEOUT 500
112
113#define DBGP_MAX_PACKET 8
Sven Schnelle10680872012-07-25 14:19:45 +0200114#define DBGP_LOOPS 1000
Yinghai Lud57241f2007-02-28 11:17:02 +0000115
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +0300116static struct ehci_debug_info glob_dbg_info CAR_GLOBAL;
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300117#if !defined(__PRE_RAM__) && !defined(__SMM__)
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300118static struct device_operations *ehci_drv_ops;
119static struct device_operations ehci_dbg_ops;
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300120#endif
121
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300122static inline struct ehci_debug_info *dbgp_ehci_info(void)
123{
124 return car_get_var_ptr(&glob_dbg_info);
125}
126
Yinghai Lud57241f2007-02-28 11:17:02 +0000127static int dbgp_wait_until_complete(struct ehci_dbg_port *ehci_debug)
128{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000129 u32 ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000130 int loop = 0x100000;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000131
Yinghai Lud57241f2007-02-28 11:17:02 +0000132 do {
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000133 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000134 /* Stop when the transaction is finished */
135 if (ctrl & DBGP_DONE)
136 break;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000137 } while (--loop > 0);
Yinghai Lud57241f2007-02-28 11:17:02 +0000138
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000139 if (!loop)
140 return -1;
Yinghai Lud57241f2007-02-28 11:17:02 +0000141
142 /* Now that we have observed the completed transaction,
143 * clear the done bit.
144 */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000145 write32((unsigned long)&ehci_debug->control, ctrl | DBGP_DONE);
Yinghai Lud57241f2007-02-28 11:17:02 +0000146 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
147}
148
Yinghai Lud57241f2007-02-28 11:17:02 +0000149static void dbgp_breath(void)
150{
151 /* Sleep to give the debug port a chance to breathe */
152}
153
Sven Schnelle10680872012-07-25 14:19:45 +0200154static int dbgp_wait_until_done(struct ehci_dbg_port *ehci_debug, unsigned ctrl, int loop)
Yinghai Lud57241f2007-02-28 11:17:02 +0000155{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000156 u32 pids, lpid;
Yinghai Lud57241f2007-02-28 11:17:02 +0000157 int ret;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000158
Yinghai Lud57241f2007-02-28 11:17:02 +0000159retry:
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000160 write32((unsigned long)&ehci_debug->control, ctrl | DBGP_GO);
Yinghai Lud57241f2007-02-28 11:17:02 +0000161 ret = dbgp_wait_until_complete(ehci_debug);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000162 pids = read32((unsigned long)&ehci_debug->pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000163 lpid = DBGP_PID_GET(pids);
164
Sven Schnelle10680872012-07-25 14:19:45 +0200165 if (ret < 0) {
166 if (ret == -DBGP_ERR_BAD && --loop > 0)
167 goto retry;
Yinghai Lud57241f2007-02-28 11:17:02 +0000168 return ret;
Sven Schnelle10680872012-07-25 14:19:45 +0200169 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000170
171 /* If the port is getting full or it has dropped data
172 * start pacing ourselves, not necessary but it's friendly.
173 */
174 if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
175 dbgp_breath();
Stefan Reinauer14e22772010-04-27 06:56:47 +0000176
Yinghai Lud57241f2007-02-28 11:17:02 +0000177 /* If I get a NACK reissue the transmission */
178 if (lpid == USB_PID_NAK) {
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000179 if (--loop > 0)
180 goto retry;
Yinghai Lud57241f2007-02-28 11:17:02 +0000181 }
182
183 return ret;
184}
185
186static void dbgp_set_data(struct ehci_dbg_port *ehci_debug, const void *buf, int size)
187{
188 const unsigned char *bytes = buf;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000189 u32 lo, hi;
Yinghai Lud57241f2007-02-28 11:17:02 +0000190 int i;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000191
Yinghai Lud57241f2007-02-28 11:17:02 +0000192 lo = hi = 0;
193 for (i = 0; i < 4 && i < size; i++)
194 lo |= bytes[i] << (8*i);
195 for (; i < 8 && i < size; i++)
196 hi |= bytes[i] << (8*(i - 4));
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000197 write32((unsigned long)&ehci_debug->data03, lo);
198 write32((unsigned long)&ehci_debug->data47, hi);
Yinghai Lud57241f2007-02-28 11:17:02 +0000199}
200
201static void dbgp_get_data(struct ehci_dbg_port *ehci_debug, void *buf, int size)
202{
203 unsigned char *bytes = buf;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000204 u32 lo, hi;
Yinghai Lud57241f2007-02-28 11:17:02 +0000205 int i;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000206
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000207 lo = read32((unsigned long)&ehci_debug->data03);
208 hi = read32((unsigned long)&ehci_debug->data47);
Yinghai Lud57241f2007-02-28 11:17:02 +0000209 for (i = 0; i < 4 && i < size; i++)
210 bytes[i] = (lo >> (8*i)) & 0xff;
211 for (; i < 8 && i < size; i++)
212 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
213}
214
Stefan Reinauer5ff7c132011-10-31 12:56:45 -0700215static int dbgp_bulk_write(struct ehci_dbg_port *ehci_debug,
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000216 unsigned devnum, unsigned endpoint, const char *bytes, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000217{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000218 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000219 int ret;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000220
Yinghai Lud57241f2007-02-28 11:17:02 +0000221 if (size > DBGP_MAX_PACKET)
222 return -1;
223
224 addr = DBGP_EPADDR(devnum, endpoint);
225
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000226 pids = read32((unsigned long)&ehci_debug->pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000227 pids = DBGP_PID_UPDATE(pids, USB_PID_OUT);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000228
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000229 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000230 ctrl = DBGP_LEN_UPDATE(ctrl, size);
231 ctrl |= DBGP_OUT;
232 ctrl |= DBGP_GO;
233
234 dbgp_set_data(ehci_debug, bytes, size);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000235 write32((unsigned long)&ehci_debug->address, addr);
236 write32((unsigned long)&ehci_debug->pids, pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000237
Sven Schnelle10680872012-07-25 14:19:45 +0200238 ret = dbgp_wait_until_done(ehci_debug, ctrl, DBGP_LOOPS);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000239
Yinghai Lud57241f2007-02-28 11:17:02 +0000240 return ret;
241}
242
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300243int dbgp_bulk_write_x(struct dbgp_pipe *pipe, const char *bytes, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000244{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300245 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000246 return dbgp_bulk_write(dbg_info->ehci_debug, dbg_info->devnum,
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300247 pipe->endpoint, bytes, size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000248}
249
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000250static int dbgp_bulk_read(struct ehci_dbg_port *ehci_debug, unsigned devnum,
Sven Schnelle10680872012-07-25 14:19:45 +0200251 unsigned endpoint, void *data, int size, int loops)
Yinghai Lud57241f2007-02-28 11:17:02 +0000252{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000253 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000254 int ret;
255
256 if (size > DBGP_MAX_PACKET)
257 return -1;
258
259 addr = DBGP_EPADDR(devnum, endpoint);
260
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000261 pids = read32((unsigned long)&ehci_debug->pids);
Yinghai Lud57241f2007-02-28 11:17:02 +0000262 pids = DBGP_PID_UPDATE(pids, USB_PID_IN);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000263
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000264 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000265 ctrl = DBGP_LEN_UPDATE(ctrl, size);
266 ctrl &= ~DBGP_OUT;
267 ctrl |= DBGP_GO;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000268
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000269 write32((unsigned long)&ehci_debug->address, addr);
270 write32((unsigned long)&ehci_debug->pids, pids);
Sven Schnelle10680872012-07-25 14:19:45 +0200271 ret = dbgp_wait_until_done(ehci_debug, ctrl, loops);
Yinghai Lud57241f2007-02-28 11:17:02 +0000272 if (ret < 0)
273 return ret;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000274
Yinghai Lud57241f2007-02-28 11:17:02 +0000275 if (size > ret)
276 size = ret;
277 dbgp_get_data(ehci_debug, data, size);
278 return ret;
279}
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000280
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300281int dbgp_bulk_read_x(struct dbgp_pipe *pipe, void *data, int size)
Yinghai Lud57241f2007-02-28 11:17:02 +0000282{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300283 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Stefan Reinauer5ff7c132011-10-31 12:56:45 -0700284 return dbgp_bulk_read(dbg_info->ehci_debug, dbg_info->devnum,
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300285 pipe->endpoint, data, size, DBGP_LOOPS);
Yinghai Lud57241f2007-02-28 11:17:02 +0000286}
287
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000288static void dbgp_mdelay(int ms)
Yinghai Lud57241f2007-02-28 11:17:02 +0000289{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000290 int i;
291
292 while (ms--) {
293 for (i = 0; i < 1000; i++)
294 inb(0x80);
295 }
296}
297
298static int dbgp_control_msg(struct ehci_dbg_port *ehci_debug, unsigned devnum, int requesttype,
299 int request, int value, int index, void *data, int size)
300{
301 u32 pids, addr, ctrl;
Yinghai Lud57241f2007-02-28 11:17:02 +0000302 struct usb_ctrlrequest req;
303 int read;
304 int ret;
305
306 read = (requesttype & USB_DIR_IN) != 0;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000307 if (size > (read ? DBGP_MAX_PACKET:0))
Yinghai Lud57241f2007-02-28 11:17:02 +0000308 return -1;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000309
Yinghai Lud57241f2007-02-28 11:17:02 +0000310 /* Compute the control message */
311 req.bRequestType = requesttype;
312 req.bRequest = request;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000313 req.wValue = cpu_to_le16(value);
314 req.wIndex = cpu_to_le16(index);
315 req.wLength = cpu_to_le16(size);
Yinghai Lud57241f2007-02-28 11:17:02 +0000316
317 pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
318 addr = DBGP_EPADDR(devnum, 0);
319
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000320 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000321 ctrl = DBGP_LEN_UPDATE(ctrl, sizeof(req));
322 ctrl |= DBGP_OUT;
323 ctrl |= DBGP_GO;
324
325 /* Send the setup message */
326 dbgp_set_data(ehci_debug, &req, sizeof(req));
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000327 write32((unsigned long)&ehci_debug->address, addr);
328 write32((unsigned long)&ehci_debug->pids, pids);
Sven Schnelle10680872012-07-25 14:19:45 +0200329 ret = dbgp_wait_until_done(ehci_debug, ctrl, DBGP_LOOPS);
Yinghai Lud57241f2007-02-28 11:17:02 +0000330 if (ret < 0)
331 return ret;
332
333
334 /* Read the result */
Sven Schnelle10680872012-07-25 14:19:45 +0200335 ret = dbgp_bulk_read(ehci_debug, devnum, 0, data, size, DBGP_LOOPS);
Yinghai Lud57241f2007-02-28 11:17:02 +0000336 return ret;
337}
338
339static int ehci_reset_port(struct ehci_regs *ehci_regs, int port)
340{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000341 u32 portsc;
342 u32 delay_time, delay_ms;
Yinghai Lud57241f2007-02-28 11:17:02 +0000343 int loop;
344
345 /* Reset the usb debug port */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000346 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000347 portsc &= ~PORT_PE;
348 portsc |= PORT_RESET;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000349 write32((unsigned long)&ehci_regs->port_status[port - 1], portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000350
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000351 delay_ms = HUB_ROOT_RESET_TIME;
Yinghai Lud57241f2007-02-28 11:17:02 +0000352 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000353 delay_time += delay_ms) {
354 dbgp_mdelay(delay_ms);
Yinghai Lud57241f2007-02-28 11:17:02 +0000355
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000356 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000357 if (portsc & PORT_RESET) {
358 /* force reset to complete */
359 loop = 2;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000360 write32((unsigned long)&ehci_regs->port_status[port - 1],
Stefan Reinauer9fe4d792010-01-16 17:53:38 +0000361 portsc & ~(PORT_RWC_BITS | PORT_RESET));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000362 do {
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000363 dbgp_mdelay(delay_ms);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000364 portsc = read32((unsigned long)&ehci_regs->port_status[port - 1]);
Uwe Hermanna34a0b12010-09-22 23:42:32 +0000365 delay_time += delay_ms;
Yinghai Lud57241f2007-02-28 11:17:02 +0000366 } while ((portsc & PORT_RESET) && (--loop > 0));
367 if (!loop) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000368 printk(BIOS_DEBUG, "ehci_reset_port forced done");
Yinghai Lud57241f2007-02-28 11:17:02 +0000369 }
370 }
371
372 /* Device went away? */
373 if (!(portsc & PORT_CONNECT))
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000374 return -1; //-ENOTCONN;
Yinghai Lud57241f2007-02-28 11:17:02 +0000375
Martin Rothcbf2bd72013-07-09 21:51:14 -0600376 /* bomb out completely if something weird happened */
Yinghai Lud57241f2007-02-28 11:17:02 +0000377 if ((portsc & PORT_CSC))
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000378 return -2; //-EINVAL;
Yinghai Lud57241f2007-02-28 11:17:02 +0000379
380 /* If we've finished resetting, then break out of the loop */
381 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
382 return 0;
383 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000384 return -3; //-EBUSY;
Yinghai Lud57241f2007-02-28 11:17:02 +0000385}
386
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000387static int ehci_wait_for_port(struct ehci_regs *ehci_regs, int port)
Yinghai Lud57241f2007-02-28 11:17:02 +0000388{
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000389 u32 status;
Yinghai Lud57241f2007-02-28 11:17:02 +0000390 int ret, reps;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000391
Yinghai Lud57241f2007-02-28 11:17:02 +0000392 for (reps = 0; reps < 3; reps++) {
393 dbgp_mdelay(100);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000394 status = read32((unsigned long)&ehci_regs->status);
Yinghai Lud57241f2007-02-28 11:17:02 +0000395 if (status & STS_PCD) {
396 ret = ehci_reset_port(ehci_regs, port);
397 if (ret == 0)
398 return 0;
399 }
400 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000401 return -1; //-ENOTCONN;
Yinghai Lud57241f2007-02-28 11:17:02 +0000402}
403
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300404static int usbdebug_init_(unsigned ehci_bar, unsigned offset, struct ehci_debug_info *info)
Yinghai Lud57241f2007-02-28 11:17:02 +0000405{
406 struct ehci_caps *ehci_caps;
407 struct ehci_regs *ehci_regs;
408 struct ehci_dbg_port *ehci_debug;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000409
Yinghai Lud57241f2007-02-28 11:17:02 +0000410 struct usb_debug_descriptor dbgp_desc;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000411 u32 cmd, ctrl, status, portsc, hcs_params;
412 u32 debug_port, new_debug_port = 0, n_ports;
413 u32 devnum;
414 int ret, i;
Yinghai Lud57241f2007-02-28 11:17:02 +0000415 int loop;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000416 int port_map_tried;
417 int playtimes = 3;
Yinghai Lud57241f2007-02-28 11:17:02 +0000418
419 ehci_caps = (struct ehci_caps *)ehci_bar;
Stefan Reinauer5ff7c132011-10-31 12:56:45 -0700420 ehci_regs = (struct ehci_regs *)(ehci_bar +
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000421 HC_LENGTH(read32((unsigned long)&ehci_caps->hc_capbase)));
Yinghai Lud57241f2007-02-28 11:17:02 +0000422 ehci_debug = (struct ehci_dbg_port *)(ehci_bar + offset);
Yinghai Lud57241f2007-02-28 11:17:02 +0000423 info->ehci_debug = (void *)0;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300424
425 memset(&info->ep_pipe, 0, sizeof (info->ep_pipe));
Yinghai Lud57241f2007-02-28 11:17:02 +0000426try_next_time:
427 port_map_tried = 0;
428
429try_next_port:
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000430 hcs_params = read32((unsigned long)&ehci_caps->hcs_params);
Yinghai Lud57241f2007-02-28 11:17:02 +0000431 debug_port = HCS_DEBUG_PORT(hcs_params);
432 n_ports = HCS_N_PORTS(hcs_params);
433
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300434 dprintk(BIOS_INFO, "ehci_bar: 0x%x\n", ehci_bar);
435 dprintk(BIOS_INFO, "debug_port: %d\n", debug_port);
436 dprintk(BIOS_INFO, "n_ports: %d\n", n_ports);
Yinghai Lud57241f2007-02-28 11:17:02 +0000437
Yinghai Lud57241f2007-02-28 11:17:02 +0000438 for (i = 1; i <= n_ports; i++) {
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000439 portsc = read32((unsigned long)&ehci_regs->port_status[i-1]);
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300440 dprintk(BIOS_INFO, "PORTSC #%d: %08x\n", i, portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000441 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000442
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000443 if(port_map_tried && (new_debug_port != debug_port)) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000444 if(--playtimes) {
445 set_debug_port(debug_port);
446 goto try_next_time;
447 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000448 return -1;
Yinghai Lud57241f2007-02-28 11:17:02 +0000449 }
450
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000451 loop = 100;
Yinghai Lud57241f2007-02-28 11:17:02 +0000452 /* Reset the EHCI controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000453 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000454 cmd |= CMD_RESET;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000455 write32((unsigned long)&ehci_regs->command, cmd);
Yinghai Lud57241f2007-02-28 11:17:02 +0000456 do {
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000457 dbgp_mdelay(10);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000458 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000459 } while ((cmd & CMD_RESET) && (--loop > 0));
460
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000461 if(!loop) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300462 dprintk(BIOS_INFO, "Could not reset EHCI controller.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000463 // on some systems it works without succeeding here.
464 // return -2;
465 } else {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300466 dprintk(BIOS_INFO, "EHCI controller reset successfully.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000467 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000468
469 /* Claim ownership, but do not enable yet */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000470 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000471 ctrl |= DBGP_OWNER;
472 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000473 write32((unsigned long)&ehci_debug->control, ctrl);
Yinghai Lud57241f2007-02-28 11:17:02 +0000474
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000475 /* Start EHCI controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000476 cmd = read32((unsigned long)&ehci_regs->command);
Yinghai Lud57241f2007-02-28 11:17:02 +0000477 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
478 cmd |= CMD_RUN;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000479 write32((unsigned long)&ehci_regs->command, cmd);
Yinghai Lud57241f2007-02-28 11:17:02 +0000480
481 /* Ensure everything is routed to the EHCI */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000482 write32((unsigned long)&ehci_regs->configured_flag, FLAG_CF);
Yinghai Lud57241f2007-02-28 11:17:02 +0000483
484 /* Wait until the controller is no longer halted */
485 loop = 10;
486 do {
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000487 dbgp_mdelay(10);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000488 status = read32((unsigned long)&ehci_regs->status);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000489 } while ((status & STS_HALT) && (--loop > 0));
Yinghai Lud57241f2007-02-28 11:17:02 +0000490
491 if(!loop) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300492 dprintk(BIOS_INFO, "EHCI could not be started.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000493 return -3;
Yinghai Lud57241f2007-02-28 11:17:02 +0000494 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300495 dprintk(BIOS_INFO, "EHCI started.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000496
497 /* Wait for a device to show up in the debug port */
498 ret = ehci_wait_for_port(ehci_regs, debug_port);
499 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300500 dprintk(BIOS_INFO, "No device found in debug port %d\n", debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000501 goto next_debug_port;
502 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300503 dprintk(BIOS_INFO, "EHCI done waiting for port.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000504
505 /* Enable the debug port */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000506 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000507 ctrl |= DBGP_CLAIM;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000508 write32((unsigned long)&ehci_debug->control, ctrl);
509 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000510 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300511 dprintk(BIOS_INFO, "No device in EHCI debug port.\n");
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000512 write32((unsigned long)&ehci_debug->control, ctrl & ~DBGP_CLAIM);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000513 ret = -4;
Yinghai Lud57241f2007-02-28 11:17:02 +0000514 goto err;
515 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300516 dprintk(BIOS_INFO, "EHCI debug port enabled.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000517
518 /* Completely transfer the debug device to the debug controller */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000519 portsc = read32((unsigned long)&ehci_regs->port_status[debug_port - 1]);
Yinghai Lud57241f2007-02-28 11:17:02 +0000520 portsc &= ~PORT_PE;
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000521 write32((unsigned long)&ehci_regs->port_status[debug_port - 1], portsc);
Yinghai Lud57241f2007-02-28 11:17:02 +0000522
523 dbgp_mdelay(100);
524
525 /* Find the debug device and make it device number 127 */
526 for (devnum = 0; devnum <= 127; devnum++) {
527 ret = dbgp_control_msg(ehci_debug, devnum,
528 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
529 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
530 &dbgp_desc, sizeof(dbgp_desc));
531 if (ret > 0)
532 break;
533 }
534 if (devnum > 127) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300535 dprintk(BIOS_INFO, "Could not find attached debug device.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000536 ret = -5;
Yinghai Lud57241f2007-02-28 11:17:02 +0000537 goto err;
538 }
539 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300540 dprintk(BIOS_INFO, "Attached device is not a debug device.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000541 ret = -6;
Yinghai Lud57241f2007-02-28 11:17:02 +0000542 goto err;
543 }
Yinghai Lud57241f2007-02-28 11:17:02 +0000544
545 /* Move the device to 127 if it isn't already there */
546 if (devnum != USB_DEBUG_DEVNUM) {
547 ret = dbgp_control_msg(ehci_debug, devnum,
548 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000549 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
Yinghai Lud57241f2007-02-28 11:17:02 +0000550 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300551 dprintk(BIOS_INFO, "Could not move attached device to %d.\n",
Yinghai Lud57241f2007-02-28 11:17:02 +0000552 USB_DEBUG_DEVNUM);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000553 ret = -7;
Yinghai Lud57241f2007-02-28 11:17:02 +0000554 goto err;
555 }
556 devnum = USB_DEBUG_DEVNUM;
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300557 dprintk(BIOS_INFO, "EHCI debug device renamed to 127.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000558 }
559
560 /* Enable the debug interface */
561 ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
562 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000563 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
Yinghai Lud57241f2007-02-28 11:17:02 +0000564 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300565 dprintk(BIOS_INFO, "Could not enable EHCI debug device.\n");
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000566 ret = -8;
Yinghai Lud57241f2007-02-28 11:17:02 +0000567 goto err;
568 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300569 dprintk(BIOS_INFO, "EHCI debug interface enabled.\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000570
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000571 /* Perform a small write to get the even/odd data state in sync */
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300572 ret = dbgp_bulk_write(ehci_debug, USB_DEBUG_DEVNUM, dbgp_desc.bDebugOutEndpoint, "USB\r\n",5);
Yinghai Lud57241f2007-02-28 11:17:02 +0000573 if (ret < 0) {
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300574 dprintk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000575 ret = -9;
Yinghai Lud57241f2007-02-28 11:17:02 +0000576 goto err;
577 }
Kyösti Mälkki8ff3d682013-08-12 16:11:34 +0300578 dprintk(BIOS_INFO, "Test write done\n");
Yinghai Lud57241f2007-02-28 11:17:02 +0000579
580 info->ehci_caps = ehci_caps;
581 info->ehci_regs = ehci_regs;
582 info->ehci_debug = ehci_debug;
583 info->devnum = devnum;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000584
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300585 info->ep_pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint;
586 info->ep_pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint;
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300587 info->ep_pipe[DBGP_SETUP_EP0].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300588 info->ep_pipe[DBGP_CONSOLE_EPOUT].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
589 info->ep_pipe[DBGP_CONSOLE_EPIN].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000590 return 0;
Yinghai Lud57241f2007-02-28 11:17:02 +0000591err:
592 /* Things didn't work so remove my claim */
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000593 ctrl = read32((unsigned long)&ehci_debug->control);
Yinghai Lud57241f2007-02-28 11:17:02 +0000594 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
Stefan Reinauer75a05dc2010-05-25 16:35:51 +0000595 write32((unsigned long)(unsigned long)&ehci_debug->control, ctrl);
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000596 //return ret;
Yinghai Lud57241f2007-02-28 11:17:02 +0000597
598next_debug_port:
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000599 port_map_tried |= (1 << (debug_port - 1));
600 new_debug_port = ((debug_port-1 + 1) % n_ports) + 1;
601 if (port_map_tried != ((1 << n_ports) - 1)) {
Yinghai Lud57241f2007-02-28 11:17:02 +0000602 set_debug_port(new_debug_port);
603 goto try_next_port;
604 }
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000605 if (--playtimes) {
606 //set_debug_port(new_debug_port);
Yinghai Lud57241f2007-02-28 11:17:02 +0000607 set_debug_port(debug_port);
608 goto try_next_time;
609 }
610
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000611 return -10;
612}
613
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300614#if CONFIG_DEBUG_USBDEBUG
615static int dbgp_enabled(void)
616{
617 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
618 return (globals->status & DBGP_EP_ENABLED);
619}
620#endif
621
622static int dbgp_try_get(struct dbgp_pipe *pipe)
623{
624 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
625 if (!dbgp_ep_is_active(pipe) || (globals->status & DBGP_EP_BUSY))
626 return 0;
627 globals->status |= DBGP_EP_BUSY;
628 pipe->status |= DBGP_EP_BUSY;
629 return 1;
630}
631
632static void dbgp_put(struct dbgp_pipe *pipe)
633{
634 struct dbgp_pipe *globals = &dbgp_ehci_info()->ep_pipe[DBGP_SETUP_EP0];
635 globals->status &= ~DBGP_EP_BUSY;
636 pipe->status &= ~DBGP_EP_BUSY;
637}
638
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300639void usbdebug_tx_byte(struct dbgp_pipe *pipe, unsigned char data)
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000640{
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300641 if (!dbgp_try_get(pipe))
642 return;
643 pipe->buf[pipe->bufidx++] = data;
644 if (pipe->bufidx >= 8) {
645 dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx);
646 pipe->bufidx = 0;
Sven Schnelle82704c62012-07-26 14:31:40 +0200647 }
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300648 dbgp_put(pipe);
Sven Schnelle82704c62012-07-26 14:31:40 +0200649}
650
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300651void usbdebug_tx_flush(struct dbgp_pipe *pipe)
Sven Schnelle82704c62012-07-26 14:31:40 +0200652{
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300653 if (!dbgp_try_get(pipe))
654 return;
655 if (pipe->bufidx > 0) {
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300656 dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx);
657 pipe->bufidx = 0;
Stefan Reinauer16ce01b2011-01-28 08:05:54 +0000658 }
Kyösti Mälkkid7991402013-08-12 16:11:34 +0300659 dbgp_put(pipe);
Kyösti Mälkki4d409b52013-07-05 21:38:54 +0300660}
661
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300662#if !defined(__PRE_RAM__) && !defined(__SMM__)
663static void usbdebug_re_enable(unsigned ehci_base)
664{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300665 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300666 unsigned diff;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300667 int i;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300668
669 if (!dbg_info->ehci_debug)
670 return;
671
672 diff = (unsigned)dbg_info->ehci_caps - ehci_base;
673 dbg_info->ehci_regs -= diff;
674 dbg_info->ehci_debug -= diff;
675 dbg_info->ehci_caps = (void*)ehci_base;
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300676
677 for (i=0; i<DBGP_MAX_ENDPOINTS; i++)
678 dbg_info->ep_pipe[i].status |= DBGP_EP_ENABLED;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300679}
680
681static void usbdebug_disable(void)
682{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300683 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300684 int i;
685 for (i=0; i<DBGP_MAX_ENDPOINTS; i++)
686 dbg_info->ep_pipe[i].status &= ~DBGP_EP_ENABLED;
Kyösti Mälkki41c10cd2013-07-09 04:19:22 +0300687}
688
689static void pci_ehci_set_resources(struct device *dev)
690{
691 struct resource *res;
692
693 printk(BIOS_DEBUG, "%s EHCI Debug Port hook triggered\n", dev_path(dev));
694 usbdebug_disable();
695
696 if (ehci_drv_ops->set_resources)
697 ehci_drv_ops->set_resources(dev);
698 res = find_resource(dev, EHCI_BAR_INDEX);
699 if (!res)
700 return;
701
702 usbdebug_re_enable((u32)res->base);
703 report_resource_stored(dev, res, "");
704 printk(BIOS_DEBUG, "%s EHCI Debug Port relocated\n", dev_path(dev));
705}
706
707void pci_ehci_read_resources(struct device *dev)
708{
709 if (!ehci_drv_ops) {
710 memcpy(&ehci_dbg_ops, dev->ops, sizeof(ehci_dbg_ops));
711 ehci_drv_ops = dev->ops;
712 ehci_dbg_ops.set_resources = pci_ehci_set_resources;
713 dev->ops = &ehci_dbg_ops;
714 printk(BIOS_DEBUG, "%s EHCI BAR hook registered\n", dev_path(dev));
715 } else {
716 printk(BIOS_DEBUG, "More than one caller of %s from %s\n", __func__, dev_path(dev));
717 }
718
719 pci_dev_read_resources(dev);
720}
721#endif
722
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300723int dbgp_ep_is_active(struct dbgp_pipe *pipe)
Kyösti Mälkki4d409b52013-07-05 21:38:54 +0300724{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300725 return (pipe->status & DBGP_EP_STATMASK) == (DBGP_EP_VALID | DBGP_EP_ENABLED);
Yinghai Lud57241f2007-02-28 11:17:02 +0000726}
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300727
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300728struct dbgp_pipe *dbgp_console_output(void)
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300729{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300730 return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPOUT];
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300731}
732
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300733struct dbgp_pipe *dbgp_console_input(void)
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300734{
Kyösti Mälkki026ff3e2013-07-08 18:11:44 +0300735 return &dbgp_ehci_info()->ep_pipe[DBGP_CONSOLE_EPIN];
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300736}
737
738int usbdebug_init(void)
739{
Kyösti Mälkkicbe2ede2013-07-11 07:49:46 +0300740 struct ehci_debug_info *dbg_info = dbgp_ehci_info();
Kyösti Mälkki8ee04d72013-07-06 11:41:09 +0300741
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300742#if defined(__PRE_RAM__) || !CONFIG_EARLY_CONSOLE
743 enable_usbdebug(CONFIG_USBDEBUG_DEFAULT_PORT);
744#endif
745 return usbdebug_init_(CONFIG_EHCI_BAR, CONFIG_EHCI_DEBUG_OFFSET, dbg_info);
746}