From: "Jan Beulich" Subject: [PATCH RFC 3/8] console: add EHCI debug port based serial console Date: Tue, 11 Sep 2012 11:16:15 +0100 Message-ID: <504F2B8F020000780009A800@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part0736157F.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part0736157F.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Low level hardware interface pieces adapted from Linux. For setup information, see Linux'es Documentation/x86/earlyprintk.txt and/or http://www.coreboot.org/EHCI_Debug_Port. Signed-off-by: Jan Beulich Reviewed-by: Konrad Rzeszutek Wilk --- a/docs/misc/xen-command-line.markdown +++ b/docs/misc/xen-command-line.markdown @@ -244,7 +244,7 @@ A typical setup for most situations migh Specify the size of the console ring buffer. =20 ### console -> `=3D List of [ vga | com1[H,L] | com2[H,L] | none ]` +> `=3D List of [ vga | com1[H,L] | com2[H,L] | dbgp | none ]` =20 > Default: `console=3Dcom1,vga` =20 @@ -260,6 +260,8 @@ the converse; transmitted and received c cleared. This allows a single port to be shared by two subsystems (e.g. console and debugger). =20 +`dbgp` indicates that Xen should use a USB debug port. + `none` indicates that Xen should not use a console. This option only makes sense on its own. =20 @@ -352,6 +354,12 @@ combination with the `low_crashinfo` com ### credit2\_load\_window\_shift > `=3D ` =20 +### dbgp +> `=3D ehci[ | @pci:. ]` + +Specify the USB controller to use, either by instance number (when going +over the PCI busses sequentially) or by PCI device (must be on segment = 0). + ### debug\_stack\_lines > `=3D ` =20 --- a/xen/arch/x86/Rules.mk +++ b/xen/arch/x86/Rules.mk @@ -7,6 +7,7 @@ HAS_CPUFREQ :=3D y HAS_PCI :=3D y HAS_PASSTHROUGH :=3D y HAS_NS16550 :=3D y +HAS_EHCI :=3D y HAS_KEXEC :=3D y HAS_GDBSX :=3D y xenoprof :=3D y --- a/xen/arch/x86/physdev.c +++ b/xen/arch/x86/physdev.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -722,6 +723,19 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H =20 break; } + + case PHYSDEVOP_dbgp_op: { + struct physdev_dbgp_op op; + + if ( !IS_PRIV(v->domain) ) + ret =3D -EPERM; + else if ( copy_from_guest(&op, arg, 1) ) + ret =3D -EFAULT; + else + ret =3D dbgp_op(&op); + break; + } + default: ret =3D -ENOSYS; break; --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -606,6 +606,7 @@ void __init __start_xen(unsigned long mb ns16550.io_base =3D 0x2f8; ns16550.irq =3D 3; ns16550_init(1, &ns16550); + ehci_dbgp_init(); console_init_preirq(); =20 printk("Bootloader: %s\n", loader); --- a/xen/drivers/char/Makefile +++ b/xen/drivers/char/Makefile @@ -1,4 +1,5 @@ obj-y +=3D console.o obj-$(HAS_NS16550) +=3D ns16550.o obj-$(HAS_PL011) +=3D pl011.o +obj-$(HAS_EHCI) +=3D ehci-dbgp.o obj-y +=3D serial.o --- /dev/null +++ b/xen/drivers/char/ehci-dbgp.c @@ -0,0 +1,1577 @@ +/* + * Standalone EHCI USB debug driver + * + * Hardware interface code based on the respective early console driver = in + * Linux; see the Linux source for authorship and copyrights. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* #define DBGP_DEBUG */ + +/* EHCI register interface, corresponds to EHCI Revision 0.95 specificatio= n */ + +/* Section 2.2 Host Controller Capability Registers */ +struct ehci_caps { + /* + * These fields are specified as 8 and 16 bit registers, + * but some hosts can't perform 8 or 16 bit PCI accesses. + * some hosts treat caplength and hciversion as parts of a 32-bit + * register, others treat them as two separate registers, this + * affects the memory map for big endian controllers. + */ + u32 hc_capbase; +#define HC_LENGTH(p) (0x00ff & (p)) /* bits 7:0 / offset 0x00 */ +#define HC_VERSION(p) (0xffff & ((p) >> 16)) /* bits 31:16 / offset = 0x02 */ + + u32 hcs_params; /* HCSPARAMS - offset 0x04 */ +#define HCS_DEBUG_PORT(p) (((p) >> 20) & 0xf) /* bits 23:20, debug port? = */ +#define HCS_INDICATOR(p) ((p) & (1 << 16)) /* true: has port indicators= */ +#define HCS_N_CC(p) (((p) >> 12) & 0xf) /* bits 15:12, #companion = HCs */ +#define HCS_N_PCC(p) (((p) >> 8) & 0xf) /* bits 11:8, ports per CC = */ +#define HCS_PORTROUTED(p) ((p) & (1 << 7)) /* true: port routing */ +#define HCS_PPC(p) ((p) & (1 << 4)) /* true: port power control = */ +#define HCS_N_PORTS(p) (((p) >> 0) & 0xf) /* bits 3:0, ports on HC */ + + u32 hcc_params; /* HCCPARAMS - offset 0x08 */ +/* EHCI 1.1 addendum */ +#define HCC_32FRAME_PERIODIC_LIST(p) ((p) & (1 << 19)) +#define HCC_PER_PORT_CHANGE_EVENT(p) ((p) & (1 << 18)) +#define HCC_LPM(p) ((p) & (1 << 17)) +#define HCC_HW_PREFETCH(p) ((p) & (1 << 16)) +#define HCC_EXT_CAPS(p) (((p) >> 8) & 0xff) /* for pci extended caps */ +#define HCC_ISOC_CACHE(p) ((p) & (1 << 7)) /* true: can cache isoc = frame */ +#define HCC_ISOC_THRES(p) (((p) >> 4) & 0x7) /* bits 6:4, uframes cached = */ +#define HCC_CANPARK(p) ((p) & (1 << 2)) /* true: can park on async = qh */ +#define HCC_PGM_FRAMELISTLEN(p) ((p) & (1 << 1)) /* true: periodic_size = changes */ +#define HCC_64BIT_ADDR(p) ((p) & 1) /* true: can use 64-bit = addr */ + + u8 portroute[8]; /* nibbles for routing - offset 0x0C */ +}; + +/* Section 2.3 Host Controller Operational Registers */ +struct ehci_regs { + /* USBCMD: offset 0x00 */ + u32 command; + +/* EHCI 1.1 addendum */ +#define CMD_HIRD (0xf << 24) /* host initiated resume duration */ +#define CMD_PPCEE (1 << 15) /* per port change event enable */ +#define CMD_FSP (1 << 14) /* fully synchronized prefetch */ +#define CMD_ASPE (1 << 13) /* async schedule prefetch enable */ +#define CMD_PSPE (1 << 12) /* periodic schedule prefetch enable = */ +/* 23:16 is r/w intr rate, in microframes; default "8" =3D=3D 1/msec */ +#define CMD_PARK (1 << 11) /* enable "park" on async qh */ +#define CMD_PARK_CNT(c) (((c) >> 8) & 3) /* how many transfers to park = for */ +#define CMD_LRESET (1 << 7) /* partial reset (no ports, etc) */ +#define CMD_IAAD (1 << 6) /* "doorbell" interrupt async advance = */ +#define CMD_ASE (1 << 5) /* async schedule enable */ +#define CMD_PSE (1 << 4) /* periodic schedule enable */ +/* 3:2 is periodic frame list size */ +#define CMD_RESET (1 << 1) /* reset HC not bus */ +#define CMD_RUN (1 << 0) /* start/stop HC */ + + /* USBSTS: offset 0x04 */ + u32 status; +#define STS_PPCE_MASK (0xff << 16) /* Per-Port change event 1-16 */ +#define STS_ASS (1 << 15) /* Async Schedule Status */ +#define STS_PSS (1 << 14) /* Periodic Schedule Status */ +#define STS_RECL (1 << 13) /* Reclamation */ +#define STS_HALT (1 << 12) /* Not running (any reason) */ +/* some bits reserved */ + /* these STS_* flags are also intr_enable bits (USBINTR) */ +#define STS_IAA (1 << 5) /* Interrupted on async advance */ +#define STS_FATAL (1 << 4) /* such as some PCI access errors */ +#define STS_FLR (1 << 3) /* frame list rolled over */ +#define STS_PCD (1 << 2) /* port change detect */ +#define STS_ERR (1 << 1) /* "error" completion (overflow, ...) = */ +#define STS_INT (1 << 0) /* "normal" completion (short, ...) = */ + + /* USBINTR: offset 0x08 */ + u32 intr_enable; + + /* FRINDEX: offset 0x0C */ + u32 frame_index; /* current microframe number */ + /* CTRLDSSEGMENT: offset 0x10 */ + u32 segment; /* address bits 63:32 if needed */ + /* PERIODICLISTBASE: offset 0x14 */ + u32 frame_list; /* points to periodic list */ + /* ASYNCLISTADDR: offset 0x18 */ + u32 async_next; /* address of next async queue head */ + + u32 reserved[9]; + + /* CONFIGFLAG: offset 0x40 */ + u32 configured_flag; +#define FLAG_CF (1 << 0) /* true: we'll support "high speed" = */ + + /* PORTSC: offset 0x44 */ + u32 port_status[0]; /* up to N_PORTS */ +/* EHCI 1.1 addendum */ +#define PORTSC_SUSPEND_STS_ACK 0 +#define PORTSC_SUSPEND_STS_NYET 1 +#define PORTSC_SUSPEND_STS_STALL 2 +#define PORTSC_SUSPEND_STS_ERR 3 + +#define PORT_DEV_ADDR (0x7f << 25) /* device address */ +#define PORT_SSTS (0x3 << 23) /* suspend status */ +/* 31:23 reserved */ +#define PORT_WKOC_E (1 << 22) /* wake on overcurrent (enable) */ +#define PORT_WKDISC_E (1 << 21) /* wake on disconnect (enable) */ +#define PORT_WKCONN_E (1 << 20) /* wake on connect (enable) */ +/* 19:16 for port testing */ +#define PORT_TEST(x) (((x) & 0xf) << 16) /* Port Test Control */ +#define PORT_TEST_PKT PORT_TEST(0x4) /* Port Test Control - packet test = */ +#define PORT_TEST_FORCE PORT_TEST(0x5) /* Port Test Control - force = enable */ +#define PORT_LED_OFF (0 << 14) +#define PORT_LED_AMBER (1 << 14) +#define PORT_LED_GREEN (2 << 14) +#define PORT_LED_MASK (3 << 14) +#define PORT_OWNER (1 << 13) /* true: companion hc owns this port = */ +#define PORT_POWER (1 << 12) /* true: has power (see PPC) */ +#define PORT_USB11(x) (((x) & (3 << 10)) =3D=3D (1 << 10)) /* USB 1.1 = device */ +/* 11:10 for detecting lowspeed devices (reset vs release ownership) */ +/* 9 reserved */ +#define PORT_LPM (1 << 9) /* LPM transaction */ +#define PORT_RESET (1 << 8) /* reset port */ +#define PORT_SUSPEND (1 << 7) /* suspend port */ +#define PORT_RESUME (1 << 6) /* resume it */ +#define PORT_OCC (1 << 5) /* over current change */ +#define PORT_OC (1 << 4) /* over current active */ +#define PORT_PEC (1 << 3) /* port enable change */ +#define PORT_PE (1 << 2) /* port enable */ +#define PORT_CSC (1 << 1) /* connect status change */ +#define PORT_CONNECT (1 << 0) /* device connected */ +#define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC) +}; + +/* + * Appendix C, Debug port ... intended for use with special "debug = devices" + * that can help if there's no serial console. (nonstandard enumeration.)= + */ +struct ehci_dbg_port { + u32 control; +#define DBGP_OWNER (1 << 30) +#define DBGP_ENABLED (1 << 28) +#define DBGP_DONE (1 << 16) +#define DBGP_INUSE (1 << 10) +#define DBGP_ERRCODE(x) (((x) >> 7) & 0x07) +# define DBGP_ERR_BAD 1 +# define DBGP_ERR_SIGNAL 2 +#define DBGP_ERROR (1 << 6) +#define DBGP_GO (1 << 5) +#define DBGP_OUT (1 << 4) +#define DBGP_LEN (0xf << 0) +#define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE) + u32 pids; +#define DBGP_PID_GET(x) (((x) >> 16) & 0xff) +#define DBGP_PID_SET(data, tok) (((data) << 8) | (tok)) + u32 data03; + u32 data47; + u32 address; +#define DBGP_EPADDR(dev, ep) (((dev) << 8) | (ep)) +}; + +/* CONTROL REQUEST SUPPORT */ + +/* + * USB directions + * + * This bit flag is used in endpoint descriptors' bEndpointAddress field. + * It's also one of three fields in control requests bRequestType. + */ +#define USB_DIR_OUT 0 /* to device */ +#define USB_DIR_IN 0x80 /* to host */ + +/* + * USB types, the second of three bRequestType fields + */ +#define USB_TYPE_MASK (0x03 << 5) +#define USB_TYPE_STANDARD (0x00 << 5) +#define USB_TYPE_CLASS (0x01 << 5) +#define USB_TYPE_VENDOR (0x02 << 5) +#define USB_TYPE_RESERVED (0x03 << 5) + +/* + * USB recipients, the third of three bRequestType fields + */ +#define USB_RECIP_MASK 0x1f +#define USB_RECIP_DEVICE 0x00 +#define USB_RECIP_INTERFACE 0x01 +#define USB_RECIP_ENDPOINT 0x02 +#define USB_RECIP_OTHER 0x03 +/* From Wireless USB 1.0 */ +#define USB_RECIP_PORT 0x04 +#define USB_RECIP_RPIPE 0x05 + +/* + * Standard requests, for the bRequest field of a SETUP packet. + * + * These are qualified by the bRequestType field, so that for example + * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved + * by a GET_STATUS request. + */ +#define USB_REQ_GET_STATUS 0x00 +#define USB_REQ_CLEAR_FEATURE 0x01 +#define USB_REQ_SET_FEATURE 0x03 +#define USB_REQ_SET_ADDRESS 0x05 +#define USB_REQ_GET_DESCRIPTOR 0x06 +#define USB_REQ_SET_DESCRIPTOR 0x07 +#define USB_REQ_GET_CONFIGURATION 0x08 +#define USB_REQ_SET_CONFIGURATION 0x09 +#define USB_REQ_GET_INTERFACE 0x0A +#define USB_REQ_SET_INTERFACE 0x0B +#define USB_REQ_SYNCH_FRAME 0x0C + +#define USB_DEVICE_DEBUG_MODE 6 /* (special devices only) */ + +/** + * struct usb_ctrlrequest - SETUP data for a USB device control request + * @bRequestType: matches the USB bmRequestType field + * @bRequest: matches the USB bRequest field + * @wValue: matches the USB wValue field (le16 byte order) + * @wIndex: matches the USB wIndex field (le16 byte order) + * @wLength: matches the USB wLength field (le16 byte order) + * + * This structure is used to send control requests to a USB device. It = matches + * the different fields of the USB 2.0 Spec section 9.3, table 9-2. See = the + * USB spec for a fuller description of the different fields, and what = they are + * used for. + * + * Note that the driver for any interface can issue control requests. + * For most devices, interfaces don't coordinate with each other, so + * such requests may be made at any time. + */ +struct usb_ctrlrequest { + u8 bRequestType; + u8 bRequest; + __le16 wValue; + __le16 wIndex; + __le16 wLength; +} __attribute__ ((packed)); + +/* USB_DT_DEBUG: for special highspeed devices, replacing serial console = */ + +#define USB_DT_DEBUG 0x0a + +struct usb_debug_descriptor { + u8 bLength; + u8 bDescriptorType; + /* bulk endpoints with 8 byte maxpacket */ + u8 bDebugInEndpoint; + u8 bDebugOutEndpoint; +} __attribute__((packed)); + +#define USB_DEBUG_DEVNUM 127 + +/* + * USB Packet IDs (PIDs) + */ + +/* token */ +#define USB_PID_OUT 0xe1 +#define USB_PID_IN 0x69 +#define USB_PID_SOF 0xa5 +#define USB_PID_SETUP 0x2d +/* handshake */ +#define USB_PID_ACK 0xd2 +#define USB_PID_NAK 0x5a +#define USB_PID_STALL 0x1e +#define USB_PID_NYET 0x96 +/* data */ +#define USB_PID_DATA0 0xc3 +#define USB_PID_DATA1 0x4b +#define USB_PID_DATA2 0x87 +#define USB_PID_MDATA 0x0f +/* Special */ +#define USB_PID_PREAMBLE 0x3c +#define USB_PID_ERR 0x3c +#define USB_PID_SPLIT 0x78 +#define USB_PID_PING 0xb4 +#define USB_PID_UNDEF_0 0xf0 + +#define PCI_CLASS_SERIAL_USB_EHCI 0x0c0320 +#define PCI_CAP_ID_EHCI_DEBUG 0x0a + +#define HUB_ROOT_RESET_TIME 50 /* times are in msec */ +#define HUB_SHORT_RESET_TIME 10 +#define HUB_LONG_RESET_TIME 200 +#define HUB_RESET_TIMEOUT 500 + +#define DBGP_MAX_PACKET 8 +#define DBGP_LOOPS 1000 +#define DBGP_TIMEOUT (250 * 1000) /* us */ +#define DBGP_CHECK_INTERVAL 100 /* us */ +/* This one can be set arbitrarily - only affects input responsiveness: = */ +#define DBGP_IDLE_INTERVAL 100 /* ms */ + +struct ehci_dbgp { + struct ehci_dbg_port __iomem *ehci_debug; + enum dbgp_state { + dbgp_idle, + dbgp_out, + dbgp_in, + dbgp_ctrl, + dbgp_unsafe /* cannot use debug device during EHCI reset */ + } state; + unsigned int phys_port; + struct { + unsigned int endpoint; + unsigned int chunk; + char buf[DBGP_MAX_PACKET]; + } out, in; + unsigned long timeout; + struct timer timer; + spinlock_t *lock; + bool_t reset_run; + u8 bus, slot, func, bar; + u16 pci_cr; + u32 bar_val; + unsigned int cap; + struct ehci_regs __iomem *ehci_regs; + struct ehci_caps __iomem *ehci_caps; +}; + +static int ehci_dbgp_external_startup(struct ehci_dbgp *); + +static void ehci_dbgp_status(struct ehci_dbgp *dbgp, const char *str) +{ +#ifdef DBGP_DEBUG +#define dbgp_printk printk + if ( !dbgp->ehci_debug ) + return; + dbgp_printk("dbgp: %s\n", str); + dbgp_printk(" debug control: %08x\n", readl(&dbgp->ehci_debug->contro= l)); + dbgp_printk(" EHCI cmd : %08x\n", readl(&dbgp->ehci_regs->command= )); + dbgp_printk(" EHCI conf flg: %08x\n", + readl(&dbgp->ehci_regs->configured_flag)); + dbgp_printk(" EHCI status : %08x\n", readl(&dbgp->ehci_regs->status)= ); + dbgp_printk(" EHCI portsc : %08x\n", + readl(&dbgp->ehci_regs->port_status[dbgp->phys_port - = 1])); +#endif +} + +#ifndef DBGP_DEBUG +static inline __attribute__ ((format (printf, 1, 2))) void +dbgp_printk(const char *fmt, ...) { } +#endif + +static inline u32 dbgp_len_update(u32 x, u32 len) +{ + return (x & ~DBGP_LEN) | (len & DBGP_LEN) | DBGP_OUT; +} + +static inline u32 dbgp_pid_write_update(u32 x, u32 tok) +{ + static u8 data0 =3D USB_PID_DATA1; + + data0 ^=3D USB_PID_DATA0 ^ USB_PID_DATA1; + return (x & 0xffff0000) | (data0 << 8) | (tok & 0xff); +} + +static inline u32 dbgp_pid_read_update(u32 x, u32 tok) +{ + return (x & 0xffffff00) | (tok & 0xff); +} + +static inline void dbgp_set_data(struct ehci_dbg_port __iomem *ehci_debug,= + const void *buf, unsigned int size) +{ + const unsigned char *bytes =3D buf; + u32 lo =3D 0, hi =3D 0; + unsigned int i; + + for ( i =3D 0; i < 4 && i < size; i++ ) + lo |=3D bytes[i] << (8 * i); + for ( ; i < 8 && i < size; i++ ) + hi |=3D bytes[i] << (8 * (i - 4)); + writel(lo, &ehci_debug->data03); + writel(hi, &ehci_debug->data47); +} + +static inline void dbgp_get_data(struct ehci_dbg_port __iomem *ehci_debug,= + void *buf, int size) +{ + unsigned char *bytes =3D buf; + u32 lo =3D readl(&ehci_debug->data03); + u32 hi =3D readl(&ehci_debug->data47); + unsigned int i; + + for ( i =3D 0; i < 4 && i < size; i++ ) + bytes[i] =3D (lo >> (8 * i)) & 0xff; + for ( ; i < 8 && i < size; i++ ) + bytes[i] =3D (hi >> (8 * (i - 4))) & 0xff; +} + +static void dbgp_issue_command(struct ehci_dbgp *dbgp, u32 ctrl, + enum dbgp_state state) +{ + u32 cmd =3D readl(&dbgp->ehci_regs->command); + + if ( unlikely(!(cmd & CMD_RUN)) ) + { + /* + * If the EHCI controller is not in the run state do extended + * checks to see if ACPI or some other initialization also + * reset the EHCI debug port. + */ + u32 ctrl =3D readl(&dbgp->ehci_debug->control); + + if ( ctrl & DBGP_ENABLED ) + { + cmd |=3D CMD_RUN; + writel(cmd, &dbgp->ehci_regs->command); + dbgp->reset_run =3D 1; + } + else if ( dbgp->state !=3D dbgp_unsafe ) + { + dbgp->state =3D dbgp_unsafe; + ehci_dbgp_external_startup(dbgp); + } + } + + writel(ctrl | DBGP_GO, &dbgp->ehci_debug->control); + dbgp->timeout =3D DBGP_TIMEOUT; + if ( dbgp->state !=3D dbgp_unsafe ) + dbgp->state =3D state; +} + +static int dbgp_check_for_completion(struct ehci_dbgp *dbgp, + unsigned int interval, u8 *ppid) +{ + u32 ctrl; + int ret; + + if ( dbgp->state =3D=3D dbgp_idle ) + return 0; + + ctrl =3D readl(&dbgp->ehci_debug->control) & ~DBGP_GO; + if ( !(ctrl & DBGP_DONE) ) + { + if ( dbgp->timeout > interval ) + dbgp->timeout -=3D interval; + else if ( interval ) + { + /* See the timeout related comment in dbgp_wait_until_done(). = */ + dbgp->state =3D dbgp_unsafe; + dbgp->timeout =3D 0; + } + return -DBGP_TIMEOUT; + } + + if ( ctrl & DBGP_ERROR ) + { + ret =3D -DBGP_ERRCODE(ctrl); + if ( ret =3D=3D -DBGP_ERR_BAD && dbgp->timeout > interval ) + ctrl |=3D DBGP_GO; + } + else + { + u8 pid =3D DBGP_PID_GET(readl(&dbgp->ehci_debug->pids)); + + ret =3D ctrl & DBGP_LEN; + if ( ppid ) + *ppid =3D pid; + else if ( dbgp->state =3D=3D dbgp_in ) + { + dbgp_get_data(dbgp->ehci_debug, dbgp->in.buf, ret); + dbgp->in.chunk =3D ret; + } + else if ( pid =3D=3D USB_PID_NAK && dbgp->timeout > interval ) + ctrl |=3D DBGP_GO; + } + + writel(ctrl, &dbgp->ehci_debug->control); + if ( ctrl & DBGP_GO ) + { + dbgp->timeout -=3D interval; + return -DBGP_TIMEOUT; + } + + if ( unlikely(dbgp->reset_run) ) + { + writel(readl(&dbgp->ehci_regs->command) & ~CMD_RUN, + &dbgp->ehci_regs->command); + dbgp->reset_run =3D 0; + } + + if ( dbgp->state !=3D dbgp_unsafe ) + dbgp->state =3D dbgp_idle; + + return ret; +} + +static int dbgp_wait_until_complete(struct ehci_dbgp *dbgp, u8 *ppid) +{ + unsigned int loop =3D DBGP_TIMEOUT; + int ret; + + do { + ret =3D dbgp_check_for_completion(dbgp, 0, ppid); + if ( ret !=3D -DBGP_TIMEOUT ) + break; + udelay(1); + } while ( --loop ); + + if ( !ppid && !loop ) + dbgp->state =3D dbgp_unsafe; + + return ret; +} + +static inline void dbgp_mdelay(unsigned int ms) +{ + while ( ms-- ) + { + unsigned int i; + + for ( i =3D 0; i < 1000; i++ ) + outb(0x1, 0x80); + } +} + +static void dbgp_breathe(void) +{ + /* Sleep to give the debug port a chance to breathe. */ + dbgp_mdelay(1); +} + +static int dbgp_wait_until_done(struct ehci_dbgp *dbgp, u32 ctrl, + unsigned int loop) +{ + int ret; + + dbgp->timeout =3D 0; + + for ( ; ; writel(ctrl | DBGP_GO, &dbgp->ehci_debug->control) ) + { + u8 pid; + + ret =3D dbgp_wait_until_complete(dbgp, &pid); + if ( ret < 0 ) + { + /* + * A -DBGP_TIMEOUT failure here means the device has failed, + * perhaps because it was unplugged, in which case we do not + * want to hang the system so the dbgp will be marked as = unsafe + * to use. EHCI reset is the only way to recover if you = unplug + * the dbgp device. + */ + if ( ret =3D=3D -DBGP_TIMEOUT ) + dbgp->state =3D dbgp_unsafe; + if ( ret !=3D -DBGP_ERR_BAD || !--loop ) + break; + } + else + { + /* + * If the port is getting full or it has dropped data + * start pacing ourselves, not necessary but it's friendly. + */ + if ( pid =3D=3D USB_PID_NAK || pid =3D=3D USB_PID_NYET ) + dbgp_breathe(); + + /* If we got a NACK, reissue the transmission. */ + if ( pid !=3D USB_PID_NAK || !--loop ) + break; + } + } + + return ret; +} + +static int dbgp_bulk_write(struct ehci_dbgp *dbgp, + unsigned int devnum, unsigned int endpoint, + const void *bytes, unsigned int size, u32 = *pctrl) +{ + u32 addr, pids, ctrl; + + if ( size > DBGP_MAX_PACKET ) + return -EINVAL; + + addr =3D DBGP_EPADDR(devnum, endpoint); + pids =3D dbgp_pid_write_update(readl(&dbgp->ehci_debug->pids), = USB_PID_OUT); + ctrl =3D dbgp_len_update(readl(&dbgp->ehci_debug->control), size); + if ( pctrl ) + *pctrl =3D ctrl; + + dbgp_set_data(dbgp->ehci_debug, bytes, size); + writel(addr, &dbgp->ehci_debug->address); + writel(pids, &dbgp->ehci_debug->pids); + dbgp_issue_command(dbgp, ctrl, dbgp_out); + + return 0; +} + +static int dbgp_bulk_read(struct ehci_dbgp *dbgp, + unsigned int devnum, unsigned int endpoint, + unsigned int size, u32 *pctrl) +{ + u32 addr, pids, ctrl; + + if ( size > DBGP_MAX_PACKET ) + return -EINVAL; + + addr =3D DBGP_EPADDR(devnum, endpoint); + pids =3D dbgp_pid_read_update(readl(&dbgp->ehci_debug->pids), = USB_PID_IN); + ctrl =3D readl(&dbgp->ehci_debug->control) & ~DBGP_OUT; + + writel(addr, &dbgp->ehci_debug->address); + writel(pids, &dbgp->ehci_debug->pids); + if ( likely(!pctrl) ) + dbgp_issue_command(dbgp, ctrl, dbgp_in); + else + dbgp_issue_command(dbgp, *pctrl =3D ctrl, dbgp_ctrl); + + return 0; +} + +static int dbgp_control_msg(struct ehci_dbgp *dbgp, unsigned int devnum, + int requesttype, int request, int value, + int index, void *data, unsigned int size) +{ + u32 addr, pids, ctrl; + struct usb_ctrlrequest req; + bool_t read =3D (requesttype & USB_DIR_IN) !=3D 0; + int ret; + + if ( size > (read ? DBGP_MAX_PACKET : 0) ) + return -EINVAL; + + /* Compute the control message */ + req.bRequestType =3D requesttype; + req.bRequest =3D request; + req.wValue =3D cpu_to_le16(value); + req.wIndex =3D cpu_to_le16(index); + req.wLength =3D cpu_to_le16(size); + + pids =3D DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP); + addr =3D DBGP_EPADDR(devnum, 0); + ctrl =3D dbgp_len_update(readl(&dbgp->ehci_debug->control), sizeof(req= )); + + /* Send the setup message */ + dbgp_set_data(dbgp->ehci_debug, &req, sizeof(req)); + writel(addr, &dbgp->ehci_debug->address); + writel(pids, &dbgp->ehci_debug->pids); + dbgp_issue_command(dbgp, ctrl, dbgp_ctrl); + ret =3D dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS); + if ( ret < 0 ) + return ret; + + /* Read the result */ + ret =3D dbgp_bulk_read(dbgp, devnum, 0, size, &ctrl); + if ( !ret ) + ret =3D dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS); + if ( ret > 0 ) + { + if ( size > ret ) + size =3D ret; + dbgp_get_data(dbgp->ehci_debug, data, size); + } + + return ret; +} + +static unsigned int __init __find_dbgp(u8 bus, u8 slot, u8 func) +{ + u32 class =3D pci_conf_read32(0, bus, slot, func, PCI_CLASS_REVISION);= + + if ( (class >> 8) !=3D PCI_CLASS_SERIAL_USB_EHCI ) + return 0; + + return pci_find_cap_offset(0, bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);= +} + +static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp, + unsigned int ehci_num) +{ + unsigned int bus, slot, func; + + for ( bus =3D 0; bus < 256; bus++ ) + { + for ( slot =3D 0; slot < 32; slot++ ) + { + for ( func =3D 0; func < 8; func++ ) + { + unsigned int cap; + + if ( !pci_device_detect(0, bus, slot, func) ) + { + if ( !func ) + break; + continue; + } + + cap =3D __find_dbgp(bus, slot, func); + if ( !cap || ehci_num-- ) + { + if ( !func && !(pci_conf_read8(0, bus, slot, func, + PCI_HEADER_TYPE) & = 0x80) ) + break; + continue; + } + + dbgp->bus =3D bus; + dbgp->slot =3D slot; + dbgp->func =3D func; + return cap; + } + } + } + + return 0; +} + +static int ehci_dbgp_startup(struct ehci_dbgp *dbgp) +{ + u32 ctrl, cmd, status; + unsigned int loop; + + /* Claim ownership, but do not enable yet */ + ctrl =3D readl(&dbgp->ehci_debug->control); + ctrl |=3D DBGP_OWNER; + ctrl &=3D ~(DBGP_ENABLED | DBGP_INUSE); + writel(ctrl, &dbgp->ehci_debug->control); + udelay(1); + + ehci_dbgp_status(dbgp, "EHCI startup"); + /* Start the EHCI. */ + cmd =3D readl(&dbgp->ehci_regs->command); + cmd &=3D ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET); + cmd |=3D CMD_RUN; + writel(cmd, &dbgp->ehci_regs->command); + + /* Ensure everything is routed to the EHCI */ + writel(FLAG_CF, &dbgp->ehci_regs->configured_flag); + + /* Wait until the controller is no longer halted. */ + loop =3D 1000; + do { + status =3D readl(&dbgp->ehci_regs->status); + if ( !(status & STS_HALT) ) + break; + udelay(1); + } while ( --loop ); + + if ( !loop ) + { + dbgp_printk("EHCI cannot be started\n"); + return -ENODEV; + } + dbgp_printk("EHCI started\n"); + + return 0; +} + +static int ehci_dbgp_controller_reset(struct ehci_dbgp *dbgp) +{ + unsigned int loop =3D 250 * 1000; + u32 cmd; + + /* Reset the EHCI controller */ + cmd =3D readl(&dbgp->ehci_regs->command); + cmd |=3D CMD_RESET; + writel(cmd, &dbgp->ehci_regs->command); + do { + cmd =3D readl(&dbgp->ehci_regs->command); + } while ( (cmd & CMD_RESET) && --loop ); + + if ( !loop ) + { + dbgp_printk("cannot reset EHCI\n"); + return -1; + } + ehci_dbgp_status(dbgp, "ehci reset done"); + + return 0; +} + +static int ehci_reset_port(struct ehci_dbgp *dbgp, unsigned int port) +{ + u32 portsc, delay_time, delay; + + ehci_dbgp_status(dbgp, "reset port"); + /* Reset the USB debug port. */ + portsc =3D readl(&dbgp->ehci_regs->port_status[port - 1]); + portsc &=3D ~PORT_PE; + portsc |=3D PORT_RESET; + writel(portsc, &dbgp->ehci_regs->port_status[port - 1]); + + delay =3D HUB_ROOT_RESET_TIME; + for ( delay_time =3D 0; delay_time < HUB_RESET_TIMEOUT; + delay_time +=3D delay ) + { + dbgp_mdelay(delay); + portsc =3D readl(&dbgp->ehci_regs->port_status[port - 1]); + if (!(portsc & PORT_RESET)) + break; + } + + if ( portsc & PORT_RESET ) + { + /* force reset to complete */ + unsigned int loop =3D 100 * 1000; + + writel(portsc & ~(PORT_RWC_BITS | PORT_RESET), + &dbgp->ehci_regs->port_status[port - 1]); + do { + udelay(1); + portsc =3D readl(&dbgp->ehci_regs->port_status[port-1]); + } while ( (portsc & PORT_RESET) && --loop ); + } + + /* Device went away? */ + if ( !(portsc & PORT_CONNECT) ) + return -ENOTCONN; + + /* bomb out completely if something weird happened */ + if ( portsc & PORT_CSC ) + return -EINVAL; + + /* If we've finished resetting, then break out of the loop */ + if ( !(portsc & PORT_RESET) && (portsc & PORT_PE) ) + return 0; + + return -EBUSY; +} + +static int ehci_wait_for_port(struct ehci_dbgp *dbgp, unsigned int port) +{ + u32 status; + unsigned int reps; + + for ( reps =3D 0; reps < 300; reps++ ) + { + status =3D readl(&dbgp->ehci_regs->status); + if ( status & STS_PCD ) + break; + dbgp_mdelay(1); + } + + return ehci_reset_port(dbgp, port) =3D=3D 0 ? 0 : -ENOTCONN; +} + +/* Return 0 on success + * Return -ENODEV for any general failure + * Return -EIO if wait for port fails + */ +static int ehci_dbgp_external_startup(struct ehci_dbgp *dbgp) +{ + unsigned int devnum; + struct usb_debug_descriptor dbgp_desc; + int ret; + u32 ctrl, portsc, cmd; + unsigned int dbg_port =3D dbgp->phys_port; + unsigned int tries =3D 3; + unsigned int reset_port_tries =3D 1; + bool_t try_hard_once =3D 1; + +try_port_reset_again: + ret =3D ehci_dbgp_startup(dbgp); + if ( ret ) + return ret; + + /* Wait for a device to show up in the debug port */ + ret =3D ehci_wait_for_port(dbgp, dbg_port); + if ( ret < 0 ) + { + portsc =3D readl(&dbgp->ehci_regs->port_status[dbg_port - 1]); + if ( !(portsc & PORT_CONNECT) && try_hard_once ) + { + /* + * Last ditch effort to try to force enable the debug device = by + * using the packet test EHCI command to try and wake it up. + */ + try_hard_once =3D 0; + cmd =3D readl(&dbgp->ehci_regs->command); + cmd &=3D ~CMD_RUN; + writel(cmd, &dbgp->ehci_regs->command); + portsc =3D readl(&dbgp->ehci_regs->port_status[dbg_port - = 1]); + portsc |=3D PORT_TEST_PKT; + writel(portsc, &dbgp->ehci_regs->port_status[dbg_port - 1]); + ehci_dbgp_status(dbgp, "Trying to force debug port online"); + mdelay(50); + ehci_dbgp_controller_reset(dbgp); + goto try_port_reset_again; + } + else if ( reset_port_tries-- ) + goto try_port_reset_again; + dbgp_printk("no device found in debug port\n"); + return -EIO; + } + ehci_dbgp_status(dbgp, "wait for port done"); + + /* Enable the debug port */ + ctrl =3D readl(&dbgp->ehci_debug->control); + ctrl |=3D DBGP_CLAIM; + writel(ctrl, &dbgp->ehci_debug->control); + ctrl =3D readl(&dbgp->ehci_debug->control); + if ( (ctrl & DBGP_CLAIM) !=3D DBGP_CLAIM ) + { + dbgp_printk("no device in debug port\n"); + writel(ctrl & ~DBGP_CLAIM, &dbgp->ehci_debug->control); + return -ENODEV; + } + ehci_dbgp_status(dbgp, "debug port enabled"); + + /* Completely transfer the debug device to the debug controller */ + portsc =3D readl(&dbgp->ehci_regs->port_status[dbg_port - 1]); + portsc &=3D ~PORT_PE; + writel(portsc, &dbgp->ehci_regs->port_status[dbg_port - 1]); + + dbgp_mdelay(100); + +try_again: + /* Find the debug device and make it device number 127 */ + for ( devnum =3D 0; devnum <=3D 127; devnum++ ) + { + ret =3D dbgp_control_msg(dbgp, devnum, + USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_= DEVICE, + USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << = 8), 0, + &dbgp_desc, sizeof(dbgp_desc)); + if ( ret > 0 ) + break; + } + if ( devnum > 127 ) + { + dbgp_printk("could not find attached debug device\n"); + goto err; + } + if ( ret < 0 ) + { + dbgp_printk("attached device is not a debug device\n"); + goto err; + } + dbgp->out.endpoint =3D dbgp_desc.bDebugOutEndpoint; + dbgp->in.endpoint =3D dbgp_desc.bDebugInEndpoint; + + /* Move the device to 127 if it isn't already there. */ + if ( devnum !=3D USB_DEBUG_DEVNUM ) + { + ret =3D dbgp_control_msg(dbgp, devnum, + USB_DIR_OUT | USB_TYPE_STANDARD | = USB_RECIP_DEVICE, + USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, = NULL, 0); + if ( ret < 0 ) + { + dbgp_printk("could not move attached device to %d\n", + USB_DEBUG_DEVNUM); + goto err; + } + devnum =3D USB_DEBUG_DEVNUM; + dbgp_printk("debug device renamed to 127\n"); + } + + /* Enable the debug interface */ + ret =3D dbgp_control_msg(dbgp, USB_DEBUG_DEVNUM, + USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEV= ICE, + USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, + 0, NULL, 0); + if ( ret < 0 ) + { + dbgp_printk("could not enable the debug device\n"); + goto err; + } + dbgp_printk("debug interface enabled\n"); + + /* Perform a small write to get the even/odd data state in sync. */ + ret =3D dbgp_bulk_write(dbgp, USB_DEBUG_DEVNUM, dbgp->out.endpoint, + "\n", 1, &ctrl); + if ( !ret ) + ret =3D dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS); + if ( ret < 0 ) + { + dbgp_printk("dbgp_bulk_write failed: %d\n", ret); + goto err; + } + dbgp_printk("small write done\n"); + dbgp->state =3D dbgp_idle; + + return 0; +err: + if ( tries-- ) + goto try_again; + return -ENODEV; +} + +typedef void (*set_debug_port_t)(struct ehci_dbgp *, unsigned int); + +static void default_set_debug_port(struct ehci_dbgp *dbgp, unsigned int = port) +{ +} + +static set_debug_port_t __read_mostly set_debug_port =3D default_set_debug= _port; + +static void nvidia_set_debug_port(struct ehci_dbgp *dbgp, unsigned int = port) +{ + u32 dword =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, = 0x74); + + dword &=3D ~(0x0f << 12); + dword |=3D (port & 0x0f) << 12; + pci_conf_write32(0, dbgp->bus, dbgp->slot, dbgp->func, 0x74, dword); + dbgp_printk("set debug port to %u\n", port); +} + +static void __init detect_set_debug_port(struct ehci_dbgp *dbgp) +{ + if ( pci_conf_read16(0, dbgp->bus, dbgp->slot, dbgp->func, + PCI_VENDOR_ID) =3D=3D 0x10de ) + { + dbgp_printk("using nvidia set_debug_port\n"); + set_debug_port =3D nvidia_set_debug_port; + } +} + +/* + * The code in ehci_dbgp_bios_handoff() is derived from the USB PCI + * quirk initialization in Linux. + */ +#define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */ +#define EHCI_USBLEGCTLSTS 4 /* legacy control/status */ +static void ehci_dbgp_bios_handoff(struct ehci_dbgp *dbgp, u32 hcc_params)= +{ + u32 cap; + unsigned int offset =3D HCC_EXT_CAPS(hcc_params); + int msec; + + if ( !offset ) + return; + + cap =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, = offset); + dbgp_printk("dbgp: EHCI BIOS state %08x\n", cap); + + if ( (cap & 0xff) =3D=3D 1 && (cap & EHCI_USBLEGSUP_BIOS) ) + { + dbgp_printk("dbgp: BIOS handoff\n"); + pci_conf_write8(0, dbgp->bus, dbgp->slot, dbgp->func, offset + 3, = 1); + } + + /* if boot firmware now owns EHCI, spin till it hands it over. */ + msec =3D 1000; + while ( (cap & EHCI_USBLEGSUP_BIOS) && (msec > 0) ) + { + mdelay(10); + msec -=3D 10; + cap =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, = offset); + } + + if ( cap & EHCI_USBLEGSUP_BIOS ) + { + /* well, possibly buggy BIOS... try to shut it down, + * and hope nothing goes too wrong */ + dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap); + pci_conf_write8(0, dbgp->bus, dbgp->slot, dbgp->func, offset + 2, = 0); + } + + /* just in case, always disable EHCI SMIs */ + pci_conf_write8(0, dbgp->bus, dbgp->slot, dbgp->func, + offset + EHCI_USBLEGCTLSTS, 0); +} + +static int ehci_dbgp_setup(struct ehci_dbgp *dbgp) +{ + u32 ctrl, portsc, hcs_params; + unsigned int i, debug_port, new_debug_port =3D 0, n_ports; + unsigned int port_map_tried, playtimes =3D 3; + int ret; + + ehci_dbgp_bios_handoff(dbgp, readl(&dbgp->ehci_caps->hcc_params)); + +try_next_time: + port_map_tried =3D 0; + +try_next_port: + + hcs_params =3D readl(&dbgp->ehci_caps->hcs_params); + debug_port =3D HCS_DEBUG_PORT(hcs_params); + dbgp->phys_port =3D debug_port; + n_ports =3D HCS_N_PORTS(hcs_params); + + dbgp_printk("debug_port: %u\n", debug_port); + dbgp_printk("n_ports: %u\n", n_ports); + ehci_dbgp_status(dbgp, ""); + + for ( i =3D 1; i <=3D n_ports; i++ ) + { + portsc =3D readl(&dbgp->ehci_regs->port_status[i-1]); + dbgp_printk("portstatus%d: %08x\n", i, portsc); + } + + if ( port_map_tried && (new_debug_port !=3D debug_port) ) + { + if ( --playtimes ) + { + set_debug_port(dbgp, new_debug_port); + goto try_next_time; + } + return -1; + } + + /* Only reset the controller if it is not already in the + * configured state */ + if ( readl(&dbgp->ehci_regs->configured_flag) & FLAG_CF ) + ehci_dbgp_status(dbgp, "ehci skip - already configured"); + else if ( ehci_dbgp_controller_reset(dbgp) !=3D 0 ) + return -1; + + ret =3D ehci_dbgp_external_startup(dbgp); + if (ret =3D=3D -EIO) + goto next_debug_port; + + if ( ret < 0 ) + { + /* Things didn't work so remove my claim */ + ctrl =3D readl(&dbgp->ehci_debug->control); + ctrl &=3D ~(DBGP_CLAIM | DBGP_OUT); + writel(ctrl, &dbgp->ehci_debug->control); + return -1; + } + + return 0; + +next_debug_port: + port_map_tried |=3D 1 << (debug_port - 1); + new_debug_port =3D (debug_port % n_ports) + 1; + if ( port_map_tried !=3D ((1 << n_ports) - 1) ) + { + set_debug_port(dbgp, new_debug_port); + goto try_next_port; + } + if ( --playtimes ) + { + set_debug_port(dbgp, new_debug_port); + goto try_next_time; + } + + return -1; +} + +static inline void _ehci_dbgp_flush(struct ehci_dbgp *dbgp) +{ + if ( dbgp_bulk_write(dbgp, USB_DEBUG_DEVNUM, dbgp->out.endpoint, + dbgp->out.buf, dbgp->out.chunk, NULL) ) + BUG(); + dbgp->out.chunk =3D 0; +} + +static void ehci_dbgp_flush(struct serial_port *port) +{ + struct ehci_dbgp *dbgp =3D port->uart; + s_time_t goal; + + if ( !dbgp->out.chunk || !dbgp->ehci_debug || dbgp->state =3D=3D = dbgp_unsafe ) + return; + + if ( dbgp->state =3D=3D dbgp_idle || !port->sync ) + dbgp_check_for_completion(dbgp, 1, NULL); + else + dbgp_wait_until_complete(dbgp, NULL); + + if ( dbgp->state =3D=3D dbgp_idle ) + { + _ehci_dbgp_flush(dbgp); + + if ( port->sync ) + { + dbgp_wait_until_complete(dbgp, NULL); + return; + } + } + + goal =3D NOW() + MICROSECS(DBGP_CHECK_INTERVAL); + if ( dbgp->timer.expires > goal ) + set_timer(&dbgp->timer, goal); +} + +static void ehci_dbgp_putc(struct serial_port *port, char c) +{ + struct ehci_dbgp *dbgp =3D port->uart; + + if ( unlikely(dbgp->out.chunk >=3D DBGP_MAX_PACKET) ) + return; + + dbgp->out.buf[dbgp->out.chunk++] =3D c; + + if ( dbgp->out.chunk =3D=3D DBGP_MAX_PACKET ) + ehci_dbgp_flush(port); +} + +static int ehci_dbgp_tx_empty(struct serial_port *port) +{ + struct ehci_dbgp *dbgp =3D port->uart; + + if ( unlikely(!dbgp->ehci_debug) || unlikely(dbgp->state =3D=3D = dbgp_unsafe) ) + return port->sync || port->tx_log_everything || !port->txbuf; + + if ( dbgp->out.chunk =3D=3D DBGP_MAX_PACKET ) + ehci_dbgp_flush(port); + else + dbgp_check_for_completion(dbgp, 1, NULL); + + if ( dbgp->state !=3D dbgp_idle && dbgp->out.chunk >=3D DBGP_MAX_PACKE= T ) + return 0; + + port->tx_fifo_size =3D DBGP_MAX_PACKET - dbgp->out.chunk; + if ( dbgp->state =3D=3D dbgp_idle ) + port->tx_fifo_size +=3D DBGP_MAX_PACKET; + + return 1; +} + +static int ehci_dbgp_getc(struct serial_port *port, char *pc) +{ + struct ehci_dbgp *dbgp =3D port->uart; + + if ( !dbgp->in.chunk ) + return 0; + + *pc =3D *dbgp->in.buf; + if ( --dbgp->in.chunk ) + memmove(dbgp->in.buf, dbgp->in.buf + 1, dbgp->in.chunk); + + return 1; +} + +/* Safe: ehci_dbgp_poll() runs as timer handler, so not reentrant. */ +static struct serial_port *poll_port; + +static void _ehci_dbgp_poll(struct cpu_user_regs *regs) +{ + struct serial_port *port =3D poll_port; + struct ehci_dbgp *dbgp =3D port->uart; + unsigned long flags; + unsigned int timeout =3D MICROSECS(DBGP_CHECK_INTERVAL); + bool_t empty =3D 0; + + if ( !dbgp->ehci_debug ) + return; + + if ( spin_trylock_irqsave(&port->tx_lock, flags) ) + { + if ( dbgp->state !=3D dbgp_unsafe ) + dbgp_check_for_completion(dbgp, DBGP_CHECK_INTERVAL, NULL); + if ( dbgp->state =3D=3D dbgp_idle && dbgp->out.chunk ) + _ehci_dbgp_flush(dbgp); + if ( dbgp->state =3D=3D dbgp_idle || dbgp->out.chunk < DBGP_MAX_PA= CKET ) + empty =3D 1; + spin_unlock_irqrestore(&port->tx_lock, flags); + } + + if ( dbgp->in.chunk ) + serial_rx_interrupt(port, regs); + + if ( empty ) + serial_tx_interrupt(port, regs); + + if ( spin_trylock_irqsave(&port->tx_lock, flags) ) + { + if ( dbgp->state =3D=3D dbgp_idle && !dbgp->in.chunk && + !dbgp->out.chunk && port->txbufp =3D=3D port->txbufc ) + { + if ( dbgp_bulk_read(dbgp, USB_DEBUG_DEVNUM, dbgp->in.endpoint,= + DBGP_MAX_PACKET, NULL) ) + BUG(); + timeout =3D MILLISECS(DBGP_IDLE_INTERVAL); + } + spin_unlock_irqrestore(&port->tx_lock, flags); + } + + set_timer(&dbgp->timer, NOW() + timeout); +} + +static void ehci_dbgp_poll(void *data) +{ + poll_port =3D data; +#ifdef run_in_exception_handler + run_in_exception_handler(_ehci_dbgp_poll); +#else + _ehci_dbgp_poll(guest_cpu_user_regs()); +#endif +} + +static bool_t ehci_dbgp_setup_preirq(struct ehci_dbgp *dbgp) +{ + if ( !ehci_dbgp_setup(dbgp) ) + return 1; + + dbgp_printk("ehci_dbgp_setup failed\n"); + dbgp->ehci_debug =3D NULL; + return 0; +} + +static void __init ehci_dbgp_init_preirq(struct serial_port *port) +{ + struct ehci_dbgp *dbgp =3D port->uart; + u32 debug_port, offset; + void __iomem *ehci_bar; + + debug_port =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, + dbgp->cap); + offset =3D (debug_port >> 16) & 0xfff; + + /* double check if the mem space is enabled */ + dbgp->pci_cr =3D pci_conf_read8(0, dbgp->bus, dbgp->slot, dbgp->func, + PCI_COMMAND); + if ( !(dbgp->pci_cr & PCI_COMMAND_MEMORY) ) + { + dbgp->pci_cr |=3D PCI_COMMAND_MEMORY; + pci_conf_write16(0, dbgp->bus, dbgp->slot, dbgp->func, PCI_COMMAND= , + dbgp->pci_cr); + dbgp_printk("MMIO for EHCI enabled\n"); + } + + /* + * FIXME I don't have the bar size so just guess PAGE_SIZE is more + * than enough. 1k is the biggest that was seen. + */ + set_fixmap_nocache(FIX_EHCI_DBGP, dbgp->bar_val); + ehci_bar =3D (void __iomem *)fix_to_virt(FIX_EHCI_DBGP); + ehci_bar +=3D dbgp->bar_val & ~PAGE_MASK; + dbgp_printk("ehci_bar: %p\n", ehci_bar); + + dbgp->ehci_caps =3D ehci_bar; + dbgp->ehci_regs =3D ehci_bar + + HC_LENGTH(readl(&dbgp->ehci_caps->hc_capbase)); + dbgp->ehci_debug =3D ehci_bar + offset; + + detect_set_debug_port(dbgp); + + if ( ehci_dbgp_setup_preirq(dbgp) ) + ehci_dbgp_status(dbgp, "ehci_dbgp_init_preirq complete"); + + port->tx_fifo_size =3D DBGP_MAX_PACKET; + dbgp->lock =3D &port->tx_lock; +} + +static void ehci_dbgp_setup_postirq(struct ehci_dbgp *dbgp) +{ + set_timer(&dbgp->timer, NOW() + MILLISECS(1)); +} + +static void __init ehci_dbgp_init_postirq(struct serial_port *port) +{ + struct ehci_dbgp *dbgp =3D port->uart; + + if ( !dbgp->ehci_debug ) + return; + + serial_async_transmit(port); + + init_timer(&dbgp->timer, ehci_dbgp_poll, port, 0); + + ehci_dbgp_setup_postirq(dbgp); +} + +static int ehci_dbgp_check_release(struct ehci_dbgp *dbgp) +{ + struct ehci_dbg_port __iomem *ehci_debug =3D dbgp->ehci_debug; + u32 ctrl; + unsigned int i; + + if ( !ehci_debug ) + return 0; + + for ( i =3D 0; i < DBGP_MAX_PACKET; ++i ) + if ( dbgp->out.buf[i] ) + return 1; + + /* + * This means the console is not initialized, or should get shutdown + * so as to allow for reuse of the USB device, which means it is time + * to shutdown the USB debug port. + */ + printk(XENLOG_INFO "Releasing EHCI debug port at %02x:%02x.%u\n", + dbgp->bus, dbgp->slot, dbgp->func); + + kill_timer(&dbgp->timer); + dbgp->ehci_debug =3D NULL; + + ctrl =3D readl(&ehci_debug->control); + if ( ctrl & DBGP_ENABLED ) + { + ctrl &=3D ~DBGP_CLAIM; + writel(ctrl, &ehci_debug->control); + } + + return 0; +} + +static void __init ehci_dbgp_endboot(struct serial_port *port) +{ + ehci_dbgp_check_release(port->uart); +} + +static void ehci_dbgp_suspend(struct serial_port *port) +{ + struct ehci_dbgp *dbgp =3D port->uart; + + if ( !dbgp->ehci_debug ) + return; + + stop_timer(&dbgp->timer); + dbgp->timer.expires =3D 0; + + dbgp->pci_cr =3D pci_conf_read16(0, dbgp->bus, dbgp->slot, dbgp->func,= + PCI_COMMAND); + + dbgp->state =3D dbgp_unsafe; +} + +static void ehci_dbgp_resume(struct serial_port *port) +{ + struct ehci_dbgp *dbgp =3D port->uart; + + if ( !dbgp->ehci_debug ) + return; + + pci_conf_write32(0, dbgp->bus, dbgp->slot, dbgp->func, dbgp->bar, + dbgp->bar_val); + pci_conf_write16(0, dbgp->bus, dbgp->slot, dbgp->func, + PCI_COMMAND, dbgp->pci_cr); + + ehci_dbgp_setup_preirq(dbgp); + ehci_dbgp_setup_postirq(dbgp); +} + +static struct uart_driver __read_mostly ehci_dbgp_driver =3D { + .init_preirq =3D ehci_dbgp_init_preirq, + .init_postirq =3D ehci_dbgp_init_postirq, + .endboot =3D ehci_dbgp_endboot, + .suspend =3D ehci_dbgp_suspend, + .resume =3D ehci_dbgp_resume, + .tx_empty =3D ehci_dbgp_tx_empty, + .putc =3D ehci_dbgp_putc, + .flush =3D ehci_dbgp_flush, + .getc =3D ehci_dbgp_getc +}; + +static struct ehci_dbgp ehci_dbgp =3D { .state =3D dbgp_unsafe, .phys_port= =3D 1 }; + +static char __initdata opt_dbgp[30]; +string_param("dbgp", opt_dbgp); + +void __init ehci_dbgp_init(void) +{ + struct ehci_dbgp *dbgp =3D &ehci_dbgp; + u32 debug_port, offset, bar_val; + const char *e; + + if ( strncmp(opt_dbgp, "ehci", 4) ) + return; + + if ( isdigit(opt_dbgp[4]) || !opt_dbgp[4] ) + { + unsigned int num =3D 0; + + if ( opt_dbgp[4] ) + simple_strtoul(opt_dbgp + 4, &e, 10); + + dbgp->cap =3D find_dbgp(dbgp, num); + if ( !dbgp->cap ) + return; + + dbgp_printk("Found EHCI debug port on %02x:%02x.%u\n", + dbgp->bus, dbgp->slot, dbgp->func); + } + else if ( strncmp(opt_dbgp + 4, "@pci", 4) =3D=3D 0 ) + { + unsigned long val =3D simple_strtoul(opt_dbgp + 8, &e, 16); + + dbgp->bus =3D val; + if ( dbgp->bus !=3D val || *e !=3D ':' ) + return; + + val =3D simple_strtoul(e + 1, &e, 16); + if ( PCI_SLOT(PCI_DEVFN(val, 0)) !=3D val || *e !=3D '.' ) + return; + dbgp->slot =3D val; + + val =3D simple_strtoul(e + 1, &e, 16); + if ( PCI_FUNC(PCI_DEVFN(0, val)) !=3D val || *e ) + return; + dbgp->func =3D val; + + if ( !pci_device_detect(0, dbgp->bus, dbgp->slot, dbgp->func) ) + return; + + dbgp->cap =3D __find_dbgp(dbgp->bus, dbgp->slot, dbgp->func); + if ( !dbgp->cap ) + return; + + dbgp_printk("Using EHCI debug port on %02x:%02x.%u\n", + dbgp->bus, dbgp->slot, dbgp->func); + } + else + return; + + debug_port =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, + dbgp->cap); + dbgp->bar =3D (debug_port >> 29) & 0x7; + dbgp->bar =3D ((dbgp->bar - 1) * 4) + PCI_BASE_ADDRESS_0; + offset =3D (debug_port >> 16) & 0xfff; + dbgp_printk("bar: %02x offset: %03x\n", dbgp->bar, offset); + if ( dbgp->bar < PCI_BASE_ADDRESS_0 || dbgp->bar > PCI_BASE_ADDRESS_5 = ) + { + dbgp_printk("unsupported/invalid bar\n"); + return; + } + + dbgp->bar_val =3D bar_val =3D pci_conf_read32(0, dbgp->bus, dbgp->slot= , + dbgp->func, dbgp->bar); + dbgp_printk("bar_val: %08x\n", bar_val); + if ( bar_val & ~PCI_BASE_ADDRESS_MEM_MASK ) + { + dbgp_printk("only simple 32-bit MMIO BARs supported\n"); + return; + } + bar_val &=3D PCI_BASE_ADDRESS_MEM_MASK; + if ( !bar_val || !(bar_val + (bar_val & -bar_val)) ) + { + dbgp_printk("firmware initialization of MMIO BAR required\n"); + return; + } + + serial_register_uart(SERHND_DBGP, &ehci_dbgp_driver, dbgp); +} + +int dbgp_op(const struct physdev_dbgp_op *op) +{ + if ( !ehci_dbgp.ehci_debug ) + return 0; + + switch ( op->bus ) + { + case PHYSDEVOP_DBGP_BUS_UNKNOWN: + break; + case PHYSDEVOP_DBGP_BUS_PCI: + if ( op->u.pci.seg || ehci_dbgp.bus !=3D op->u.pci.bus || + PCI_DEVFN(ehci_dbgp.slot, ehci_dbgp.func) !=3D op->u.pci.devfn= ) + default: + return 0; + break; + } + + switch ( op->op ) + { + case PHYSDEVOP_DBGP_RESET_PREPARE: + spin_lock_irq(ehci_dbgp.lock); + ehci_dbgp.state =3D dbgp_unsafe; + dbgp_wait_until_complete(&ehci_dbgp, NULL); + spin_unlock_irq(ehci_dbgp.lock); + + return ehci_dbgp_check_release(&ehci_dbgp); + + case PHYSDEVOP_DBGP_RESET_DONE: + return ehci_dbgp_external_startup(&ehci_dbgp) ?: 1; + } + + return -ENOSYS; +} --- a/xen/drivers/char/serial.c +++ b/xen/drivers/char/serial.c @@ -265,6 +265,14 @@ int __init serial_parse_handle(char *con { int handle; =20 + if ( !strncmp(conf, "dbgp", 4) && (!conf[4] || conf[4] =3D=3D ',') ) + { + if ( !com[SERHND_DBGP].driver ) + goto fail; + + return SERHND_DBGP | SERHND_COOKED; + } + if ( strncmp(conf, "com", 3) ) goto fail; =20 --- a/xen/include/asm-x86/fixmap.h +++ b/xen/include/asm-x86/fixmap.h @@ -36,7 +36,15 @@ * from the end of virtual memory backwards. */ enum fixed_addresses { - FIX_RESERVED, /* Index 0 is reserved since fix_to_virt(0) > FIXADDR_TO= P. */ + /* Index 0 is reserved since fix_to_virt(0) =3D=3D FIXADDR_TOP. */ + FIX_RESERVED, + /* + * Indexes using the page tables set up before entering __start_xen() + * must be among the first (L1_PAGETABLE_ENTRIES - 1) entries. + * These are generally those needed by the various console drivers. + */ + FIX_EHCI_DBGP, + /* Everything else should go further down. */ #ifdef __i386__ FIX_PAE_HIGHMEM_0, FIX_PAE_HIGHMEM_END =3D FIX_PAE_HIGHMEM_0 + NR_CPUS-1, --- a/xen/include/public/physdev.h +++ b/xen/include/public/physdev.h @@ -312,6 +312,24 @@ struct physdev_pci_device { typedef struct physdev_pci_device physdev_pci_device_t; DEFINE_XEN_GUEST_HANDLE(physdev_pci_device_t); =20 +#define PHYSDEVOP_DBGP_RESET_PREPARE 1 +#define PHYSDEVOP_DBGP_RESET_DONE 2 + +#define PHYSDEVOP_DBGP_BUS_UNKNOWN 0 +#define PHYSDEVOP_DBGP_BUS_PCI 1 + +#define PHYSDEVOP_dbgp_op 29 +struct physdev_dbgp_op { + /* IN */ + uint8_t op; + uint8_t bus; + union { + struct physdev_pci_device pci; + } u; +}; +typedef struct physdev_dbgp_op physdev_dbgp_op_t; +DEFINE_XEN_GUEST_HANDLE(physdev_dbgp_op_t); + /* * Notify that some PIRQ-bound event channels have been unmasked. * ** This command is obsolete since interface version 0x00030202 and is = ** --- a/xen/include/xen/serial.h +++ b/xen/include/xen/serial.h @@ -69,9 +69,10 @@ struct uart_driver { }; =20 /* 'Serial handles' are composed from the following fields. */ -#define SERHND_IDX (3<<0) /* COM1 or COM2? = */ +#define SERHND_IDX (3<<0) /* COM1, COM2, or DBGP? = */ # define SERHND_COM1 (0<<0) # define SERHND_COM2 (1<<0) +# define SERHND_DBGP (2<<0) #define SERHND_HI (1<<2) /* Mux/demux each transferred char by MSB. = */ #define SERHND_LO (1<<3) /* Ditto, except that the MSB is cleared. = */ #define SERHND_COOKED (1<<4) /* Newline/carriage-return translation? = */ @@ -142,9 +143,13 @@ struct ns16550_defaults { unsigned long io_base; /* default io_base address */ }; void ns16550_init(int index, struct ns16550_defaults *defaults); +void ehci_dbgp_init(void); =20 void pl011_init(int index, unsigned long register_base_address); =20 +struct physdev_dbgp_op; +int dbgp_op(const struct physdev_dbgp_op *); + /* Baud rate was pre-configured before invoking the UART driver. */ #define BAUD_AUTO (-1) =20 --=__Part0736157F.0__= Content-Type: text/plain; name="sercon-ehci-dbgp.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="sercon-ehci-dbgp.patch" console: add EHCI debug port based serial console=0A=0ALow level hardware = interface pieces adapted from Linux.=0A=0AFor setup information, see = Linux'es Documentation/x86/earlyprintk.txt=0Aand/or http://www.coreboot.org= /EHCI_Debug_Port.=0A=0ASigned-off-by: Jan Beulich =0ARev= iewed-by: Konrad Rzeszutek Wilk =0A=0A--- = a/docs/misc/xen-command-line.markdown=0A+++ b/docs/misc/xen-command-line.ma= rkdown=0A@@ -244,7 +244,7 @@ A typical setup for most situations migh=0A = Specify the size of the console ring buffer.=0A =0A ### console=0A-> `=3D = List of [ vga | com1[H,L] | com2[H,L] | none ]`=0A+> `=3D List of [ vga | = com1[H,L] | com2[H,L] | dbgp | none ]`=0A =0A > Default: `console=3Dcom1,vg= a`=0A =0A@@ -260,6 +260,8 @@ the converse; transmitted and received c=0A = cleared. This allows a single port to be shared by two subsystems=0A = (e.g. console and debugger).=0A =0A+`dbgp` indicates that Xen should use a = USB debug port.=0A+=0A `none` indicates that Xen should not use a console. = This option only=0A makes sense on its own.=0A =0A@@ -352,6 +354,12 @@ = combination with the `low_crashinfo` com=0A ### credit2\_load\_window\_shif= t=0A > `=3D `=0A =0A+### dbgp=0A+> `=3D ehci[ | = @pci:. ]`=0A+=0A+Specify the USB controller to use, = either by instance number (when going=0A+over the PCI busses sequentially) = or by PCI device (must be on segment 0).=0A+=0A ### debug\_stack\_lines=0A = > `=3D `=0A =0A--- a/xen/arch/x86/Rules.mk=0A+++ b/xen/arch/x86/Ru= les.mk=0A@@ -7,6 +7,7 @@ HAS_CPUFREQ :=3D y=0A HAS_PCI :=3D y=0A HAS_PASSTH= ROUGH :=3D y=0A HAS_NS16550 :=3D y=0A+HAS_EHCI :=3D y=0A HAS_KEXEC :=3D = y=0A HAS_GDBSX :=3D y=0A xenoprof :=3D y=0A--- a/xen/arch/x86/physdev.c=0A+= ++ b/xen/arch/x86/physdev.c=0A@@ -8,6 +8,7 @@=0A #include =0A = #include =0A #include =0A+#include = =0A #include =0A #include =0A = #include =0A@@ -722,6 +723,19 @@ ret_t do_physdev_op(int cmd, = XEN_GUEST_H=0A =0A break;=0A }=0A+=0A+ case PHYSDEVOP_dbgp_o= p: {=0A+ struct physdev_dbgp_op op;=0A+=0A+ if ( !IS_PRIV(v->= domain) )=0A+ ret =3D -EPERM;=0A+ else if ( copy_from_gue= st(&op, arg, 1) )=0A+ ret =3D -EFAULT;=0A+ else=0A+ = ret =3D dbgp_op(&op);=0A+ break;=0A+ }=0A+=0A = default:=0A ret =3D -ENOSYS;=0A break;=0A--- a/xen/arch/x86= /setup.c=0A+++ b/xen/arch/x86/setup.c=0A@@ -606,6 +606,7 @@ void __init = __start_xen(unsigned long mb=0A ns16550.io_base =3D 0x2f8;=0A = ns16550.irq =3D 3;=0A ns16550_init(1, &ns16550);=0A+ ehci_dbgp_i= nit();=0A console_init_preirq();=0A =0A printk("Bootloader: %s\n", = loader);=0A--- a/xen/drivers/char/Makefile=0A+++ b/xen/drivers/char/Makefil= e=0A@@ -1,4 +1,5 @@=0A obj-y +=3D console.o=0A obj-$(HAS_NS16550) +=3D = ns16550.o=0A obj-$(HAS_PL011) +=3D pl011.o=0A+obj-$(HAS_EHCI) +=3D = ehci-dbgp.o=0A obj-y +=3D serial.o=0A--- /dev/null=0A+++ b/xen/drivers/char= /ehci-dbgp.c=0A@@ -0,0 +1,1577 @@=0A+/*=0A+ * Standalone EHCI USB debug = driver=0A+ *=0A+ * Hardware interface code based on the respective early = console driver in=0A+ * Linux; see the Linux source for authorship and = copyrights.=0A+ */=0A+=0A+#include =0A+#include =0A+#include =0A+#include =0A+#include = =0A+#include =0A+#include =0A+#in= clude =0A+#include =0A+#include = =0A+=0A+/* #define DBGP_DEBUG */=0A+=0A+/* EHCI register interface, = corresponds to EHCI Revision 0.95 specification */=0A+=0A+/* Section 2.2 = Host Controller Capability Registers */=0A+struct ehci_caps {=0A+ = /*=0A+ * These fields are specified as 8 and 16 bit registers,=0A+ = * but some hosts can't perform 8 or 16 bit PCI accesses.=0A+ * some = hosts treat caplength and hciversion as parts of a 32-bit=0A+ * = register, others treat them as two separate registers, this=0A+ * = affects the memory map for big endian controllers.=0A+ */=0A+ u32 = hc_capbase;=0A+#define HC_LENGTH(p) (0x00ff & (p)) /* bits 7:0 / = offset 0x00 */=0A+#define HC_VERSION(p) (0xffff & ((p) >> 16)) /* bits = 31:16 / offset 0x02 */=0A+=0A+ u32 hcs_params; /* HCSPARAMS - = offset 0x04 */=0A+#define HCS_DEBUG_PORT(p) (((p) >> 20) & 0xf) /* bits = 23:20, debug port? */=0A+#define HCS_INDICATOR(p) ((p) & (1 << 16)) /* = true: has port indicators */=0A+#define HCS_N_CC(p) (((p) >> 12) & = 0xf) /* bits 15:12, #companion HCs */=0A+#define HCS_N_PCC(p) (((p) = >> 8) & 0xf) /* bits 11:8, ports per CC */=0A+#define HCS_PORTROUTED(p) = ((p) & (1 << 7)) /* true: port routing */=0A+#define HCS_PPC(p) = ((p) & (1 << 4)) /* true: port power control */=0A+#define HCS_N_PORTS(p= ) (((p) >> 0) & 0xf) /* bits 3:0, ports on HC */=0A+=0A+ u32 = hcc_params; /* HCCPARAMS - offset 0x08 */=0A+/* EHCI 1.1 addendum = */=0A+#define HCC_32FRAME_PERIODIC_LIST(p) ((p) & (1 << 19))=0A+#define = HCC_PER_PORT_CHANGE_EVENT(p) ((p) & (1 << 18))=0A+#define HCC_LPM(p) = ((p) & (1 << 17))=0A+#define HCC_HW_PREFETCH(p) ((p) & (1 << 16))=0A+#defi= ne HCC_EXT_CAPS(p) (((p) >> 8) & 0xff) /* for pci extended caps = */=0A+#define HCC_ISOC_CACHE(p) ((p) & (1 << 7)) /* true: can cache = isoc frame */=0A+#define HCC_ISOC_THRES(p) (((p) >> 4) & 0x7) /* bits = 6:4, uframes cached */=0A+#define HCC_CANPARK(p) ((p) & (1 << 2)) /* = true: can park on async qh */=0A+#define HCC_PGM_FRAMELISTLEN(p) ((p) & (1 = << 1)) /* true: periodic_size changes */=0A+#define HCC_64BIT_ADDR(p) ((p) = & 1) /* true: can use 64-bit addr */=0A+=0A+ u8 portroute[8];= /* nibbles for routing - offset 0x0C */=0A+};=0A+=0A+/* Section 2.3 = Host Controller Operational Registers */=0A+struct ehci_regs {=0A+ /* = USBCMD: offset 0x00 */=0A+ u32 command;=0A+=0A+/* EHCI 1.1 addendum = */=0A+#define CMD_HIRD (0xf << 24) /* host initiated resume = duration */=0A+#define CMD_PPCEE (1 << 15) /* per port change = event enable */=0A+#define CMD_FSP (1 << 14) /* fully synchronize= d prefetch */=0A+#define CMD_ASPE (1 << 13) /* async schedule = prefetch enable */=0A+#define CMD_PSPE (1 << 12) /* periodic = schedule prefetch enable */=0A+/* 23:16 is r/w intr rate, in microframes; = default "8" =3D=3D 1/msec */=0A+#define CMD_PARK (1 << 11) /* = enable "park" on async qh */=0A+#define CMD_PARK_CNT(c) (((c) >> 8) & 3) = /* how many transfers to park for */=0A+#define CMD_LRESET (1 << 7) = /* partial reset (no ports, etc) */=0A+#define CMD_IAAD (1 << 6) = /* "doorbell" interrupt async advance */=0A+#define CMD_ASE (1 << = 5) /* async schedule enable */=0A+#define CMD_PSE (1 << 4) = /* periodic schedule enable */=0A+/* 3:2 is periodic frame list size = */=0A+#define CMD_RESET (1 << 1) /* reset HC not bus */=0A+#define= CMD_RUN (1 << 0) /* start/stop HC */=0A+=0A+ /* USBSTS: = offset 0x04 */=0A+ u32 status;=0A+#define STS_PPCE_MASK (0xff << 16) = /* Per-Port change event 1-16 */=0A+#define STS_ASS (1 << 15) /* = Async Schedule Status */=0A+#define STS_PSS (1 << 14) /* = Periodic Schedule Status */=0A+#define STS_RECL (1 << 13) /* = Reclamation */=0A+#define STS_HALT (1 << 12) /* Not running (any = reason) */=0A+/* some bits reserved */=0A+ /* these STS_* flags are = also intr_enable bits (USBINTR) */=0A+#define STS_IAA (1 << 5) = /* Interrupted on async advance */=0A+#define STS_FATAL (1 << 4) = /* such as some PCI access errors */=0A+#define STS_FLR (1 << 3) = /* frame list rolled over */=0A+#define STS_PCD (1 << 2) /* = port change detect */=0A+#define STS_ERR (1 << 1) /* "error" = completion (overflow, ...) */=0A+#define STS_INT (1 << 0) /* = "normal" completion (short, ...) */=0A+=0A+ /* USBINTR: offset 0x08 = */=0A+ u32 intr_enable;=0A+=0A+ /* FRINDEX: offset 0x0C */=0A+ = u32 frame_index; /* current microframe number */=0A+ /* CTRLDSSEGMENT= : offset 0x10 */=0A+ u32 segment; /* address bits 63:32 if needed = */=0A+ /* PERIODICLISTBASE: offset 0x14 */=0A+ u32 frame_list; /* = points to periodic list */=0A+ /* ASYNCLISTADDR: offset 0x18 */=0A+ = u32 async_next; /* address of next async queue head */=0A+=0A+ u32 = reserved[9];=0A+=0A+ /* CONFIGFLAG: offset 0x40 */=0A+ u32 configured= _flag;=0A+#define FLAG_CF (1 << 0) /* true: we'll support "high = speed" */=0A+=0A+ /* PORTSC: offset 0x44 */=0A+ u32 port_status[0]; = /* up to N_PORTS */=0A+/* EHCI 1.1 addendum */=0A+#define PORTSC_SUSPEND_= STS_ACK 0=0A+#define PORTSC_SUSPEND_STS_NYET 1=0A+#define PORTSC_SUSPEND= _STS_STALL 2=0A+#define PORTSC_SUSPEND_STS_ERR 3=0A+=0A+#define = PORT_DEV_ADDR (0x7f << 25) /* device address */=0A+#define PORT_SSTS = (0x3 << 23) /* suspend status */=0A+/* 31:23 reserved */=0A+#define = PORT_WKOC_E (1 << 22) /* wake on overcurrent (enable) */=0A+#define = PORT_WKDISC_E (1 << 21) /* wake on disconnect (enable) */=0A+#define = PORT_WKCONN_E (1 << 20) /* wake on connect (enable) */=0A+/* 19:16 = for port testing */=0A+#define PORT_TEST(x) (((x) & 0xf) << 16) /* Port = Test Control */=0A+#define PORT_TEST_PKT PORT_TEST(0x4) /* Port Test = Control - packet test */=0A+#define PORT_TEST_FORCE PORT_TEST(0x5) /* Port = Test Control - force enable */=0A+#define PORT_LED_OFF (0 << 14)=0A+#def= ine PORT_LED_AMBER (1 << 14)=0A+#define PORT_LED_GREEN (2 << 14)=0A+#defi= ne PORT_LED_MASK (3 << 14)=0A+#define PORT_OWNER (1 << 13) /* = true: companion hc owns this port */=0A+#define PORT_POWER (1 << 12) = /* true: has power (see PPC) */=0A+#define PORT_USB11(x) (((x) & (3 << = 10)) =3D=3D (1 << 10)) /* USB 1.1 device */=0A+/* 11:10 for detecting = lowspeed devices (reset vs release ownership) */=0A+/* 9 reserved = */=0A+#define PORT_LPM (1 << 9) /* LPM transaction */=0A+#define= PORT_RESET (1 << 8) /* reset port */=0A+#define PORT_SUSPEND = (1 << 7) /* suspend port */=0A+#define PORT_RESUME (1 << 6) /* = resume it */=0A+#define PORT_OCC (1 << 5) /* over current = change */=0A+#define PORT_OC (1 << 4) /* over current active = */=0A+#define PORT_PEC (1 << 3) /* port enable change */=0A+#def= ine PORT_PE (1 << 2) /* port enable */=0A+#define PORT_CSC = (1 << 1) /* connect status change */=0A+#define PORT_CONNECT (1 = << 0) /* device connected */=0A+#define PORT_RWC_BITS (PORT_CSC | = PORT_PEC | PORT_OCC)=0A+};=0A+=0A+/*=0A+ * Appendix C, Debug port ... = intended for use with special "debug devices"=0A+ * that can help if = there's no serial console. (nonstandard enumeration.)=0A+ */=0A+struct = ehci_dbg_port {=0A+ u32 control;=0A+#define DBGP_OWNER (1 << = 30)=0A+#define DBGP_ENABLED (1 << 28)=0A+#define DBGP_DONE (1 << = 16)=0A+#define DBGP_INUSE (1 << 10)=0A+#define DBGP_ERRCODE(x) (((x) = >> 7) & 0x07)=0A+# define DBGP_ERR_BAD 1=0A+# define DBGP_ERR_SIGNAL = 2=0A+#define DBGP_ERROR (1 << 6)=0A+#define DBGP_GO (1 << = 5)=0A+#define DBGP_OUT (1 << 4)=0A+#define DBGP_LEN (0xf << = 0)=0A+#define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)=0A+ = u32 pids;=0A+#define DBGP_PID_GET(x) (((x) >> 16) & 0xff)=0A+#de= fine DBGP_PID_SET(data, tok) (((data) << 8) | (tok))=0A+ u32 data03;=0A+= u32 data47;=0A+ u32 address;=0A+#define DBGP_EPADDR(dev, ep) = (((dev) << 8) | (ep))=0A+};=0A+=0A+/* CONTROL REQUEST SUPPORT */=0A+=0A+/*= =0A+ * USB directions=0A+ *=0A+ * This bit flag is used in endpoint = descriptors' bEndpointAddress field.=0A+ * It's also one of three fields = in control requests bRequestType.=0A+ */=0A+#define USB_DIR_OUT 0 = /* to device */=0A+#define USB_DIR_IN 0x80 /* to host */=0A+=0A+/*= =0A+ * USB types, the second of three bRequestType fields=0A+ */=0A+#define= USB_TYPE_MASK (0x03 << 5)=0A+#define USB_TYPE_STANDARD (0x00 << = 5)=0A+#define USB_TYPE_CLASS (0x01 << 5)=0A+#define USB_TYPE_VENDOR = (0x02 << 5)=0A+#define USB_TYPE_RESERVED (0x03 << 5)=0A+=0A+/*=0A+ * USB = recipients, the third of three bRequestType fields=0A+ */=0A+#define = USB_RECIP_MASK 0x1f=0A+#define USB_RECIP_DEVICE 0x00=0A+#define = USB_RECIP_INTERFACE 0x01=0A+#define USB_RECIP_ENDPOINT 0x02=0A+#define = USB_RECIP_OTHER 0x03=0A+/* From Wireless USB 1.0 */=0A+#define = USB_RECIP_PORT 0x04=0A+#define USB_RECIP_RPIPE 0x05=0A+=0A+/*=0A+ = * Standard requests, for the bRequest field of a SETUP packet.=0A+ *=0A+ * = These are qualified by the bRequestType field, so that for example=0A+ * = TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved=0A+ * = by a GET_STATUS request.=0A+ */=0A+#define USB_REQ_GET_STATUS = 0x00=0A+#define USB_REQ_CLEAR_FEATURE 0x01=0A+#define USB_REQ_SET_FEATU= RE 0x03=0A+#define USB_REQ_SET_ADDRESS 0x05=0A+#define = USB_REQ_GET_DESCRIPTOR 0x06=0A+#define USB_REQ_SET_DESCRIPTOR = 0x07=0A+#define USB_REQ_GET_CONFIGURATION 0x08=0A+#define USB_REQ_SET_CONFI= GURATION 0x09=0A+#define USB_REQ_GET_INTERFACE 0x0A=0A+#define = USB_REQ_SET_INTERFACE 0x0B=0A+#define USB_REQ_SYNCH_FRAME = 0x0C=0A+=0A+#define USB_DEVICE_DEBUG_MODE 6 /* (special devices = only) */=0A+=0A+/**=0A+ * struct usb_ctrlrequest - SETUP data for a USB = device control request=0A+ * @bRequestType: matches the USB bmRequestType = field=0A+ * @bRequest: matches the USB bRequest field=0A+ * @wValue: = matches the USB wValue field (le16 byte order)=0A+ * @wIndex: matches the = USB wIndex field (le16 byte order)=0A+ * @wLength: matches the USB wLength = field (le16 byte order)=0A+ *=0A+ * This structure is used to send control = requests to a USB device. It matches=0A+ * the different fields of the = USB 2.0 Spec section 9.3, table 9-2. See the=0A+ * USB spec for a fuller = description of the different fields, and what they are=0A+ * used for.=0A+ = *=0A+ * Note that the driver for any interface can issue control = requests.=0A+ * For most devices, interfaces don't coordinate with each = other, so=0A+ * such requests may be made at any time.=0A+ */=0A+struct = usb_ctrlrequest {=0A+ u8 bRequestType;=0A+ u8 bRequest;=0A+ = __le16 wValue;=0A+ __le16 wIndex;=0A+ __le16 wLength;=0A+} __attribut= e__ ((packed));=0A+=0A+/* USB_DT_DEBUG: for special highspeed devices, = replacing serial console */=0A+=0A+#define USB_DT_DEBUG 0x0a=0A+=0A+stru= ct usb_debug_descriptor {=0A+ u8 bLength;=0A+ u8 bDescriptorType;=0A+= /* bulk endpoints with 8 byte maxpacket */=0A+ u8 bDebugInEndpoint;= =0A+ u8 bDebugOutEndpoint;=0A+} __attribute__((packed));=0A+=0A+#define = USB_DEBUG_DEVNUM 127=0A+=0A+/*=0A+ * USB Packet IDs (PIDs)=0A+ */=0A+=0A+/*= token */=0A+#define USB_PID_OUT 0xe1=0A+#define USB_PID_IN = 0x69=0A+#define USB_PID_SOF 0xa5=0A+#define USB_PID_SETUP = 0x2d=0A+/* handshake */=0A+#define USB_PID_ACK 0xd2=0A+#de= fine USB_PID_NAK 0x5a=0A+#define USB_PID_STALL 0x1e=0A+#d= efine USB_PID_NYET 0x96=0A+/* data */=0A+#define USB_PID_DATA0 = 0xc3=0A+#define USB_PID_DATA1 0x4b=0A+#define USB_PID_DATA2 = 0x87=0A+#define USB_PID_MDATA 0x0f=0A+/* Special */=0A+#defin= e USB_PID_PREAMBLE 0x3c=0A+#define USB_PID_ERR 0x3c=0A+#defi= ne USB_PID_SPLIT 0x78=0A+#define USB_PID_PING 0xb4=0A+#def= ine USB_PID_UNDEF_0 0xf0=0A+=0A+#define PCI_CLASS_SERIAL_USB_EHCI = 0x0c0320=0A+#define PCI_CAP_ID_EHCI_DEBUG 0x0a=0A+=0A+#define = HUB_ROOT_RESET_TIME 50 /* times are in msec */=0A+#define HUB_SHORT_RE= SET_TIME 10=0A+#define HUB_LONG_RESET_TIME 200=0A+#define HUB_RESET_TIME= OUT 500=0A+=0A+#define DBGP_MAX_PACKET 8=0A+#define DBGP_LOOPS = 1000=0A+#define DBGP_TIMEOUT (250 * 1000) /* us = */=0A+#define DBGP_CHECK_INTERVAL 100 /* us */=0A+/* This one can be set = arbitrarily - only affects input responsiveness: */=0A+#define DBGP_IDLE_IN= TERVAL 100 /* ms */=0A+=0A+struct ehci_dbgp {=0A+ struct ehci_dbg_por= t __iomem *ehci_debug;=0A+ enum dbgp_state {=0A+ dbgp_idle,=0A+ = dbgp_out,=0A+ dbgp_in,=0A+ dbgp_ctrl,=0A+ = dbgp_unsafe /* cannot use debug device during EHCI reset */=0A+ } = state;=0A+ unsigned int phys_port;=0A+ struct {=0A+ unsigned = int endpoint;=0A+ unsigned int chunk;=0A+ char buf[DBGP_MAX_P= ACKET];=0A+ } out, in;=0A+ unsigned long timeout;=0A+ struct = timer timer;=0A+ spinlock_t *lock;=0A+ bool_t reset_run;=0A+ u8 = bus, slot, func, bar;=0A+ u16 pci_cr;=0A+ u32 bar_val;=0A+ = unsigned int cap;=0A+ struct ehci_regs __iomem *ehci_regs;=0A+ = struct ehci_caps __iomem *ehci_caps;=0A+};=0A+=0A+static int ehci_dbgp_exte= rnal_startup(struct ehci_dbgp *);=0A+=0A+static void ehci_dbgp_status(struc= t ehci_dbgp *dbgp, const char *str)=0A+{=0A+#ifdef DBGP_DEBUG=0A+#define = dbgp_printk printk=0A+ if ( !dbgp->ehci_debug )=0A+ return;=0A+ = dbgp_printk("dbgp: %s\n", str);=0A+ dbgp_printk(" debug control: = %08x\n", readl(&dbgp->ehci_debug->control));=0A+ dbgp_printk(" EHCI = cmd : %08x\n", readl(&dbgp->ehci_regs->command));=0A+ dbgp_printk(" = EHCI conf flg: %08x\n",=0A+ readl(&dbgp->ehci_regs->configu= red_flag));=0A+ dbgp_printk(" EHCI status : %08x\n", readl(&dbgp->ehci= _regs->status));=0A+ dbgp_printk(" EHCI portsc : %08x\n",=0A+ = readl(&dbgp->ehci_regs->port_status[dbgp->phys_port - 1]));=0A+#endi= f=0A+}=0A+=0A+#ifndef DBGP_DEBUG=0A+static inline __attribute__ ((format = (printf, 1, 2))) void=0A+dbgp_printk(const char *fmt, ...) { }=0A+#endif=0A= +=0A+static inline u32 dbgp_len_update(u32 x, u32 len)=0A+{=0A+ return = (x & ~DBGP_LEN) | (len & DBGP_LEN) | DBGP_OUT;=0A+}=0A+=0A+static inline = u32 dbgp_pid_write_update(u32 x, u32 tok)=0A+{=0A+ static u8 data0 =3D = USB_PID_DATA1;=0A+=0A+ data0 ^=3D USB_PID_DATA0 ^ USB_PID_DATA1;=0A+ = return (x & 0xffff0000) | (data0 << 8) | (tok & 0xff);=0A+}=0A+=0A+static = inline u32 dbgp_pid_read_update(u32 x, u32 tok)=0A+{=0A+ return (x & = 0xffffff00) | (tok & 0xff);=0A+}=0A+=0A+static inline void dbgp_set_data(st= ruct ehci_dbg_port __iomem *ehci_debug,=0A+ = const void *buf, unsigned int size)=0A+{=0A+ const unsigned char = *bytes =3D buf;=0A+ u32 lo =3D 0, hi =3D 0;=0A+ unsigned int = i;=0A+=0A+ for ( i =3D 0; i < 4 && i < size; i++ )=0A+ lo |=3D = bytes[i] << (8 * i);=0A+ for ( ; i < 8 && i < size; i++ )=0A+ hi = |=3D bytes[i] << (8 * (i - 4));=0A+ writel(lo, &ehci_debug->data03);=0A+= writel(hi, &ehci_debug->data47);=0A+}=0A+=0A+static inline void = dbgp_get_data(struct ehci_dbg_port __iomem *ehci_debug,=0A+ = void *buf, int size)=0A+{=0A+ unsigned char *bytes =3D = buf;=0A+ u32 lo =3D readl(&ehci_debug->data03);=0A+ u32 hi =3D = readl(&ehci_debug->data47);=0A+ unsigned int i;=0A+=0A+ for ( i =3D = 0; i < 4 && i < size; i++ )=0A+ bytes[i] =3D (lo >> (8 * i)) & = 0xff;=0A+ for ( ; i < 8 && i < size; i++ )=0A+ bytes[i] =3D (hi = >> (8 * (i - 4))) & 0xff;=0A+}=0A+=0A+static void dbgp_issue_command(struct= ehci_dbgp *dbgp, u32 ctrl,=0A+ enum = dbgp_state state)=0A+{=0A+ u32 cmd =3D readl(&dbgp->ehci_regs->command);= =0A+=0A+ if ( unlikely(!(cmd & CMD_RUN)) )=0A+ {=0A+ /*=0A+ = * If the EHCI controller is not in the run state do extended=0A+ = * checks to see if ACPI or some other initialization also=0A+ = * reset the EHCI debug port.=0A+ */=0A+ u32 ctrl =3D = readl(&dbgp->ehci_debug->control);=0A+=0A+ if ( ctrl & DBGP_ENABLED = )=0A+ {=0A+ cmd |=3D CMD_RUN;=0A+ writel(cmd, = &dbgp->ehci_regs->command);=0A+ dbgp->reset_run =3D 1;=0A+ = }=0A+ else if ( dbgp->state !=3D dbgp_unsafe )=0A+ {=0A+ = dbgp->state =3D dbgp_unsafe;=0A+ ehci_dbgp_external_sta= rtup(dbgp);=0A+ }=0A+ }=0A+=0A+ writel(ctrl | DBGP_GO, = &dbgp->ehci_debug->control);=0A+ dbgp->timeout =3D DBGP_TIMEOUT;=0A+ = if ( dbgp->state !=3D dbgp_unsafe )=0A+ dbgp->state =3D state;=0A+}= =0A+=0A+static int dbgp_check_for_completion(struct ehci_dbgp *dbgp,=0A+ = unsigned int interval, u8 *ppid)=0A+{=0A+= u32 ctrl;=0A+ int ret;=0A+=0A+ if ( dbgp->state =3D=3D dbgp_idle = )=0A+ return 0;=0A+=0A+ ctrl =3D readl(&dbgp->ehci_debug->control= ) & ~DBGP_GO;=0A+ if ( !(ctrl & DBGP_DONE) )=0A+ {=0A+ if ( = dbgp->timeout > interval )=0A+ dbgp->timeout -=3D interval;=0A+ = else if ( interval )=0A+ {=0A+ /* See the timeout = related comment in dbgp_wait_until_done(). */=0A+ dbgp->state = =3D dbgp_unsafe;=0A+ dbgp->timeout =3D 0;=0A+ }=0A+ = return -DBGP_TIMEOUT;=0A+ }=0A+=0A+ if ( ctrl & DBGP_ERROR )=0A+ = {=0A+ ret =3D -DBGP_ERRCODE(ctrl);=0A+ if ( ret =3D=3D = -DBGP_ERR_BAD && dbgp->timeout > interval )=0A+ ctrl |=3D = DBGP_GO;=0A+ }=0A+ else=0A+ {=0A+ u8 pid =3D DBGP_PID_GET(r= eadl(&dbgp->ehci_debug->pids));=0A+=0A+ ret =3D ctrl & DBGP_LEN;=0A+= if ( ppid )=0A+ *ppid =3D pid;=0A+ else if ( = dbgp->state =3D=3D dbgp_in )=0A+ {=0A+ dbgp_get_data(dbgp= ->ehci_debug, dbgp->in.buf, ret);=0A+ dbgp->in.chunk =3D = ret;=0A+ }=0A+ else if ( pid =3D=3D USB_PID_NAK && dbgp->time= out > interval )=0A+ ctrl |=3D DBGP_GO;=0A+ }=0A+=0A+ = writel(ctrl, &dbgp->ehci_debug->control);=0A+ if ( ctrl & DBGP_GO )=0A+ = {=0A+ dbgp->timeout -=3D interval;=0A+ return -DBGP_TIMEOU= T;=0A+ }=0A+=0A+ if ( unlikely(dbgp->reset_run) )=0A+ {=0A+ = writel(readl(&dbgp->ehci_regs->command) & ~CMD_RUN,=0A+ = &dbgp->ehci_regs->command);=0A+ dbgp->reset_run =3D 0;=0A+ = }=0A+=0A+ if ( dbgp->state !=3D dbgp_unsafe )=0A+ dbgp->state = =3D dbgp_idle;=0A+=0A+ return ret;=0A+}=0A+=0A+static int dbgp_wait_unti= l_complete(struct ehci_dbgp *dbgp, u8 *ppid)=0A+{=0A+ unsigned int loop = =3D DBGP_TIMEOUT;=0A+ int ret;=0A+=0A+ do {=0A+ ret =3D = dbgp_check_for_completion(dbgp, 0, ppid);=0A+ if ( ret !=3D = -DBGP_TIMEOUT )=0A+ break;=0A+ udelay(1);=0A+ } while = ( --loop );=0A+=0A+ if ( !ppid && !loop )=0A+ dbgp->state =3D = dbgp_unsafe;=0A+=0A+ return ret;=0A+}=0A+=0A+static inline void = dbgp_mdelay(unsigned int ms)=0A+{=0A+ while ( ms-- )=0A+ {=0A+ = unsigned int i;=0A+=0A+ for ( i =3D 0; i < 1000; i++ )=0A+ = outb(0x1, 0x80);=0A+ }=0A+}=0A+=0A+static void dbgp_breathe(void)=0A+= {=0A+ /* Sleep to give the debug port a chance to breathe. */=0A+ = dbgp_mdelay(1);=0A+}=0A+=0A+static int dbgp_wait_until_done(struct = ehci_dbgp *dbgp, u32 ctrl,=0A+ unsigned int = loop)=0A+{=0A+ int ret;=0A+=0A+ dbgp->timeout =3D 0;=0A+=0A+ for = ( ; ; writel(ctrl | DBGP_GO, &dbgp->ehci_debug->control) )=0A+ {=0A+ = u8 pid;=0A+=0A+ ret =3D dbgp_wait_until_complete(dbgp, = &pid);=0A+ if ( ret < 0 )=0A+ {=0A+ /*=0A+ = * A -DBGP_TIMEOUT failure here means the device has failed,=0A+ = * perhaps because it was unplugged, in which case we do not=0A+ = * want to hang the system so the dbgp will be marked as unsafe=0A+ = * to use. EHCI reset is the only way to recover if you = unplug=0A+ * the dbgp device.=0A+ */=0A+ = if ( ret =3D=3D -DBGP_TIMEOUT )=0A+ dbgp->state =3D = dbgp_unsafe;=0A+ if ( ret !=3D -DBGP_ERR_BAD || !--loop )=0A+ = break;=0A+ }=0A+ else=0A+ {=0A+ = /*=0A+ * If the port is getting full or it has dropped = data=0A+ * start pacing ourselves, not necessary but it's = friendly.=0A+ */=0A+ if ( pid =3D=3D USB_PID_NAK || = pid =3D=3D USB_PID_NYET )=0A+ dbgp_breathe();=0A+=0A+ = /* If we got a NACK, reissue the transmission. */=0A+ if ( = pid !=3D USB_PID_NAK || !--loop )=0A+ break;=0A+ = }=0A+ }=0A+=0A+ return ret;=0A+}=0A+=0A+static int dbgp_bulk_write(st= ruct ehci_dbgp *dbgp,=0A+ unsigned int devnum, = unsigned int endpoint,=0A+ const void *bytes, = unsigned int size, u32 *pctrl)=0A+{=0A+ u32 addr, pids, ctrl;=0A+=0A+ = if ( size > DBGP_MAX_PACKET )=0A+ return -EINVAL;=0A+=0A+ addr = =3D DBGP_EPADDR(devnum, endpoint);=0A+ pids =3D dbgp_pid_write_update(re= adl(&dbgp->ehci_debug->pids), USB_PID_OUT);=0A+ ctrl =3D dbgp_len_update= (readl(&dbgp->ehci_debug->control), size);=0A+ if ( pctrl )=0A+ = *pctrl =3D ctrl;=0A+=0A+ dbgp_set_data(dbgp->ehci_debug, bytes, = size);=0A+ writel(addr, &dbgp->ehci_debug->address);=0A+ writel(pids,= &dbgp->ehci_debug->pids);=0A+ dbgp_issue_command(dbgp, ctrl, dbgp_out);= =0A+=0A+ return 0;=0A+}=0A+=0A+static int dbgp_bulk_read(struct = ehci_dbgp *dbgp,=0A+ unsigned int devnum, = unsigned int endpoint,=0A+ unsigned int size, u32 = *pctrl)=0A+{=0A+ u32 addr, pids, ctrl;=0A+=0A+ if ( size > DBGP_MAX_P= ACKET )=0A+ return -EINVAL;=0A+=0A+ addr =3D DBGP_EPADDR(devnum, = endpoint);=0A+ pids =3D dbgp_pid_read_update(readl(&dbgp->ehci_debug->pi= ds), USB_PID_IN);=0A+ ctrl =3D readl(&dbgp->ehci_debug->control) & = ~DBGP_OUT;=0A+=0A+ writel(addr, &dbgp->ehci_debug->address);=0A+ = writel(pids, &dbgp->ehci_debug->pids);=0A+ if ( likely(!pctrl) )=0A+ = dbgp_issue_command(dbgp, ctrl, dbgp_in);=0A+ else=0A+ = dbgp_issue_command(dbgp, *pctrl =3D ctrl, dbgp_ctrl);=0A+=0A+ return = 0;=0A+}=0A+=0A+static int dbgp_control_msg(struct ehci_dbgp *dbgp, = unsigned int devnum,=0A+ int requesttype, int = request, int value,=0A+ int index, void *data, = unsigned int size)=0A+{=0A+ u32 addr, pids, ctrl;=0A+ struct = usb_ctrlrequest req;=0A+ bool_t read =3D (requesttype & USB_DIR_IN) = !=3D 0;=0A+ int ret;=0A+=0A+ if ( size > (read ? DBGP_MAX_PACKET : = 0) )=0A+ return -EINVAL;=0A+=0A+ /* Compute the control message = */=0A+ req.bRequestType =3D requesttype;=0A+ req.bRequest =3D = request;=0A+ req.wValue =3D cpu_to_le16(value);=0A+ req.wIndex =3D = cpu_to_le16(index);=0A+ req.wLength =3D cpu_to_le16(size);=0A+=0A+ = pids =3D DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);=0A+ addr =3D = DBGP_EPADDR(devnum, 0);=0A+ ctrl =3D dbgp_len_update(readl(&dbgp->ehci_d= ebug->control), sizeof(req));=0A+=0A+ /* Send the setup message */=0A+ = dbgp_set_data(dbgp->ehci_debug, &req, sizeof(req));=0A+ writel(addr, = &dbgp->ehci_debug->address);=0A+ writel(pids, &dbgp->ehci_debug->pids);= =0A+ dbgp_issue_command(dbgp, ctrl, dbgp_ctrl);=0A+ ret =3D = dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS);=0A+ if ( ret < 0 )=0A+ = return ret;=0A+=0A+ /* Read the result */=0A+ ret =3D dbgp_bulk_r= ead(dbgp, devnum, 0, size, &ctrl);=0A+ if ( !ret )=0A+ ret =3D = dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS);=0A+ if ( ret > 0 )=0A+ = {=0A+ if ( size > ret )=0A+ size =3D ret;=0A+ = dbgp_get_data(dbgp->ehci_debug, data, size);=0A+ }=0A+=0A+ return = ret;=0A+}=0A+=0A+static unsigned int __init __find_dbgp(u8 bus, u8 slot, = u8 func)=0A+{=0A+ u32 class =3D pci_conf_read32(0, bus, slot, func, = PCI_CLASS_REVISION);=0A+=0A+ if ( (class >> 8) !=3D PCI_CLASS_SERIAL_USB= _EHCI )=0A+ return 0;=0A+=0A+ return pci_find_cap_offset(0, bus, = slot, func, PCI_CAP_ID_EHCI_DEBUG);=0A+}=0A+=0A+static unsigned int __init = find_dbgp(struct ehci_dbgp *dbgp,=0A+ = unsigned int ehci_num)=0A+{=0A+ unsigned int bus, slot, func;=0A+=0A+ = for ( bus =3D 0; bus < 256; bus++ )=0A+ {=0A+ for ( slot =3D 0; = slot < 32; slot++ )=0A+ {=0A+ for ( func =3D 0; func < = 8; func++ )=0A+ {=0A+ unsigned int cap;=0A+=0A+ = if ( !pci_device_detect(0, bus, slot, func) )=0A+ = {=0A+ if ( !func )=0A+ = break;=0A+ continue;=0A+ }=0A+=0A+ = cap =3D __find_dbgp(bus, slot, func);=0A+ if ( = !cap || ehci_num-- )=0A+ {=0A+ if ( = !func && !(pci_conf_read8(0, bus, slot, func,=0A+ = PCI_HEADER_TYPE) & 0x80) )=0A+ = break;=0A+ continue;=0A+ }=0A+=0A+ = dbgp->bus =3D bus;=0A+ dbgp->slot =3D = slot;=0A+ dbgp->func =3D func;=0A+ return = cap;=0A+ }=0A+ }=0A+ }=0A+=0A+ return 0;=0A+}=0A+= =0A+static int ehci_dbgp_startup(struct ehci_dbgp *dbgp)=0A+{=0A+ u32 = ctrl, cmd, status;=0A+ unsigned int loop;=0A+=0A+ /* Claim ownership,= but do not enable yet */=0A+ ctrl =3D readl(&dbgp->ehci_debug->control)= ;=0A+ ctrl |=3D DBGP_OWNER;=0A+ ctrl &=3D ~(DBGP_ENABLED | DBGP_INUSE= );=0A+ writel(ctrl, &dbgp->ehci_debug->control);=0A+ udelay(1);=0A+= =0A+ ehci_dbgp_status(dbgp, "EHCI startup");=0A+ /* Start the EHCI. = */=0A+ cmd =3D readl(&dbgp->ehci_regs->command);=0A+ cmd &=3D = ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);=0A+ cmd |=3D = CMD_RUN;=0A+ writel(cmd, &dbgp->ehci_regs->command);=0A+=0A+ /* = Ensure everything is routed to the EHCI */=0A+ writel(FLAG_CF, = &dbgp->ehci_regs->configured_flag);=0A+=0A+ /* Wait until the controller= is no longer halted. */=0A+ loop =3D 1000;=0A+ do {=0A+ = status =3D readl(&dbgp->ehci_regs->status);=0A+ if ( !(status & = STS_HALT) )=0A+ break;=0A+ udelay(1);=0A+ } while ( = --loop );=0A+=0A+ if ( !loop )=0A+ {=0A+ dbgp_printk("EHCI = cannot be started\n");=0A+ return -ENODEV;=0A+ }=0A+ = dbgp_printk("EHCI started\n");=0A+=0A+ return 0;=0A+}=0A+=0A+static int = ehci_dbgp_controller_reset(struct ehci_dbgp *dbgp)=0A+{=0A+ unsigned = int loop =3D 250 * 1000;=0A+ u32 cmd;=0A+=0A+ /* Reset the EHCI = controller */=0A+ cmd =3D readl(&dbgp->ehci_regs->command);=0A+ cmd = |=3D CMD_RESET;=0A+ writel(cmd, &dbgp->ehci_regs->command);=0A+ do = {=0A+ cmd =3D readl(&dbgp->ehci_regs->command);=0A+ } while ( = (cmd & CMD_RESET) && --loop );=0A+=0A+ if ( !loop )=0A+ {=0A+ = dbgp_printk("cannot reset EHCI\n");=0A+ return -1;=0A+ }=0A+ = ehci_dbgp_status(dbgp, "ehci reset done");=0A+=0A+ return 0;=0A+}=0A+=0A= +static int ehci_reset_port(struct ehci_dbgp *dbgp, unsigned int port)=0A+{= =0A+ u32 portsc, delay_time, delay;=0A+=0A+ ehci_dbgp_status(dbgp, = "reset port");=0A+ /* Reset the USB debug port. */=0A+ portsc =3D = readl(&dbgp->ehci_regs->port_status[port - 1]);=0A+ portsc &=3D = ~PORT_PE;=0A+ portsc |=3D PORT_RESET;=0A+ writel(portsc, &dbgp->ehci_= regs->port_status[port - 1]);=0A+=0A+ delay =3D HUB_ROOT_RESET_TIME;=0A+= for ( delay_time =3D 0; delay_time < HUB_RESET_TIMEOUT;=0A+ = delay_time +=3D delay )=0A+ {=0A+ dbgp_mdelay(delay);=0A+ = portsc =3D readl(&dbgp->ehci_regs->port_status[port - 1]);=0A+ if = (!(portsc & PORT_RESET))=0A+ break;=0A+ }=0A+=0A+ if ( = portsc & PORT_RESET )=0A+ {=0A+ /* force reset to complete = */=0A+ unsigned int loop =3D 100 * 1000;=0A+=0A+ writel(ports= c & ~(PORT_RWC_BITS | PORT_RESET),=0A+ &dbgp->ehci_regs->port= _status[port - 1]);=0A+ do {=0A+ udelay(1);=0A+ = portsc =3D readl(&dbgp->ehci_regs->port_status[port-1]);=0A+ } = while ( (portsc & PORT_RESET) && --loop );=0A+ }=0A+=0A+ /* Device = went away? */=0A+ if ( !(portsc & PORT_CONNECT) )=0A+ return = -ENOTCONN;=0A+=0A+ /* bomb out completely if something weird happened = */=0A+ if ( portsc & PORT_CSC )=0A+ return -EINVAL;=0A+=0A+ = /* If we've finished resetting, then break out of the loop */=0A+ if ( = !(portsc & PORT_RESET) && (portsc & PORT_PE) )=0A+ return 0;=0A+=0A+= return -EBUSY;=0A+}=0A+=0A+static int ehci_wait_for_port(struct = ehci_dbgp *dbgp, unsigned int port)=0A+{=0A+ u32 status;=0A+ = unsigned int reps;=0A+=0A+ for ( reps =3D 0; reps < 300; reps++ )=0A+ = {=0A+ status =3D readl(&dbgp->ehci_regs->status);=0A+ if ( = status & STS_PCD )=0A+ break;=0A+ dbgp_mdelay(1);=0A+ = }=0A+=0A+ return ehci_reset_port(dbgp, port) =3D=3D 0 ? 0 : -ENOTCONN;= =0A+}=0A+=0A+/* Return 0 on success=0A+ * Return -ENODEV for any general = failure=0A+ * Return -EIO if wait for port fails=0A+ */=0A+static int = ehci_dbgp_external_startup(struct ehci_dbgp *dbgp)=0A+{=0A+ unsigned = int devnum;=0A+ struct usb_debug_descriptor dbgp_desc;=0A+ int = ret;=0A+ u32 ctrl, portsc, cmd;=0A+ unsigned int dbg_port =3D = dbgp->phys_port;=0A+ unsigned int tries =3D 3;=0A+ unsigned int = reset_port_tries =3D 1;=0A+ bool_t try_hard_once =3D 1;=0A+=0A+try_port_= reset_again:=0A+ ret =3D ehci_dbgp_startup(dbgp);=0A+ if ( ret )=0A+ = return ret;=0A+=0A+ /* Wait for a device to show up in the debug = port */=0A+ ret =3D ehci_wait_for_port(dbgp, dbg_port);=0A+ if ( ret = < 0 )=0A+ {=0A+ portsc =3D readl(&dbgp->ehci_regs->port_status[db= g_port - 1]);=0A+ if ( !(portsc & PORT_CONNECT) && try_hard_once = )=0A+ {=0A+ /*=0A+ * Last ditch effort to = try to force enable the debug device by=0A+ * using the packet = test EHCI command to try and wake it up.=0A+ */=0A+ = try_hard_once =3D 0;=0A+ cmd =3D readl(&dbgp->ehci_regs->command= );=0A+ cmd &=3D ~CMD_RUN;=0A+ writel(cmd, &dbgp->ehci= _regs->command);=0A+ portsc =3D readl(&dbgp->ehci_regs->port_sta= tus[dbg_port - 1]);=0A+ portsc |=3D PORT_TEST_PKT;=0A+ = writel(portsc, &dbgp->ehci_regs->port_status[dbg_port - 1]);=0A+ = ehci_dbgp_status(dbgp, "Trying to force debug port online");=0A+ = mdelay(50);=0A+ ehci_dbgp_controller_reset(dbgp);=0A+ = goto try_port_reset_again;=0A+ }=0A+ else if ( = reset_port_tries-- )=0A+ goto try_port_reset_again;=0A+ = dbgp_printk("no device found in debug port\n");=0A+ return = -EIO;=0A+ }=0A+ ehci_dbgp_status(dbgp, "wait for port done");=0A+=0A+= /* Enable the debug port */=0A+ ctrl =3D readl(&dbgp->ehci_debug->co= ntrol);=0A+ ctrl |=3D DBGP_CLAIM;=0A+ writel(ctrl, &dbgp->ehci_debug-= >control);=0A+ ctrl =3D readl(&dbgp->ehci_debug->control);=0A+ if ( = (ctrl & DBGP_CLAIM) !=3D DBGP_CLAIM )=0A+ {=0A+ dbgp_printk("no = device in debug port\n");=0A+ writel(ctrl & ~DBGP_CLAIM, &dbgp->ehci= _debug->control);=0A+ return -ENODEV;=0A+ }=0A+ ehci_dbgp_stat= us(dbgp, "debug port enabled");=0A+=0A+ /* Completely transfer the = debug device to the debug controller */=0A+ portsc =3D readl(&dbgp->ehci= _regs->port_status[dbg_port - 1]);=0A+ portsc &=3D ~PORT_PE;=0A+ = writel(portsc, &dbgp->ehci_regs->port_status[dbg_port - 1]);=0A+=0A+ = dbgp_mdelay(100);=0A+=0A+try_again:=0A+ /* Find the debug device and = make it device number 127 */=0A+ for ( devnum =3D 0; devnum <=3D 127; = devnum++ )=0A+ {=0A+ ret =3D dbgp_control_msg(dbgp, devnum,=0A+ = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEV= ICE,=0A+ USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBU= G << 8), 0,=0A+ &dbgp_desc, sizeof(dbgp_desc)= );=0A+ if ( ret > 0 )=0A+ break;=0A+ }=0A+ if ( = devnum > 127 )=0A+ {=0A+ dbgp_printk("could not find attached = debug device\n");=0A+ goto err;=0A+ }=0A+ if ( ret < 0 )=0A+ = {=0A+ dbgp_printk("attached device is not a debug device\n");=0A+ = goto err;=0A+ }=0A+ dbgp->out.endpoint =3D dbgp_desc.bDebugOut= Endpoint;=0A+ dbgp->in.endpoint =3D dbgp_desc.bDebugInEndpoint;=0A+=0A+ = /* Move the device to 127 if it isn't already there. */=0A+ if ( = devnum !=3D USB_DEBUG_DEVNUM )=0A+ {=0A+ ret =3D dbgp_control_msg= (dbgp, devnum,=0A+ USB_DIR_OUT | USB_TYPE_STA= NDARD | USB_RECIP_DEVICE,=0A+ USB_REQ_SET_ADD= RESS, USB_DEBUG_DEVNUM, 0, NULL, 0);=0A+ if ( ret < 0 )=0A+ = {=0A+ dbgp_printk("could not move attached device to %d\n",=0A+ = USB_DEBUG_DEVNUM);=0A+ goto err;=0A+ = }=0A+ devnum =3D USB_DEBUG_DEVNUM;=0A+ dbgp_printk("debug = device renamed to 127\n");=0A+ }=0A+=0A+ /* Enable the debug = interface */=0A+ ret =3D dbgp_control_msg(dbgp, USB_DEBUG_DEVNUM,=0A+ = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,= =0A+ USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE,= =0A+ 0, NULL, 0);=0A+ if ( ret < 0 )=0A+ = {=0A+ dbgp_printk("could not enable the debug device\n");=0A+ = goto err;=0A+ }=0A+ dbgp_printk("debug interface enabled\n");=0A+=0A= + /* Perform a small write to get the even/odd data state in sync. = */=0A+ ret =3D dbgp_bulk_write(dbgp, USB_DEBUG_DEVNUM, dbgp->out.endpoin= t,=0A+ "\n", 1, &ctrl);=0A+ if ( !ret )=0A+ = ret =3D dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS);=0A+ if ( ret = < 0 )=0A+ {=0A+ dbgp_printk("dbgp_bulk_write failed: %d\n", = ret);=0A+ goto err;=0A+ }=0A+ dbgp_printk("small write = done\n");=0A+ dbgp->state =3D dbgp_idle;=0A+=0A+ return 0;=0A+err:=0A= + if ( tries-- )=0A+ goto try_again;=0A+ return -ENODEV;=0A+}= =0A+=0A+typedef void (*set_debug_port_t)(struct ehci_dbgp *, unsigned = int);=0A+=0A+static void default_set_debug_port(struct ehci_dbgp *dbgp, = unsigned int port)=0A+{=0A+}=0A+=0A+static set_debug_port_t __read_mostly = set_debug_port =3D default_set_debug_port;=0A+=0A+static void nvidia_set_de= bug_port(struct ehci_dbgp *dbgp, unsigned int port)=0A+{=0A+ u32 dword = =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, 0x74);=0A+=0A+ = dword &=3D ~(0x0f << 12);=0A+ dword |=3D (port & 0x0f) << 12;=0A+ = pci_conf_write32(0, dbgp->bus, dbgp->slot, dbgp->func, 0x74, dword);=0A+ = dbgp_printk("set debug port to %u\n", port);=0A+}=0A+=0A+static void = __init detect_set_debug_port(struct ehci_dbgp *dbgp)=0A+{=0A+ if ( = pci_conf_read16(0, dbgp->bus, dbgp->slot, dbgp->func,=0A+ = PCI_VENDOR_ID) =3D=3D 0x10de )=0A+ {=0A+ dbgp_printk("usin= g nvidia set_debug_port\n");=0A+ set_debug_port =3D nvidia_set_debug= _port;=0A+ }=0A+}=0A+=0A+/*=0A+ * The code in ehci_dbgp_bios_handoff() = is derived from the USB PCI=0A+ * quirk initialization in Linux.=0A+ = */=0A+#define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */=0A+#def= ine EHCI_USBLEGCTLSTS 4 /* legacy control/status */=0A+static = void ehci_dbgp_bios_handoff(struct ehci_dbgp *dbgp, u32 hcc_params)=0A+{=0A= + u32 cap;=0A+ unsigned int offset =3D HCC_EXT_CAPS(hcc_params);=0A+ = int msec;=0A+=0A+ if ( !offset )=0A+ return;=0A+=0A+ cap = =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, offset);=0A+ = dbgp_printk("dbgp: EHCI BIOS state %08x\n", cap);=0A+=0A+ if ( (cap & = 0xff) =3D=3D 1 && (cap & EHCI_USBLEGSUP_BIOS) )=0A+ {=0A+ = dbgp_printk("dbgp: BIOS handoff\n");=0A+ pci_conf_write8(0, = dbgp->bus, dbgp->slot, dbgp->func, offset + 3, 1);=0A+ }=0A+=0A+ /* = if boot firmware now owns EHCI, spin till it hands it over. */=0A+ msec = =3D 1000;=0A+ while ( (cap & EHCI_USBLEGSUP_BIOS) && (msec > 0) )=0A+ = {=0A+ mdelay(10);=0A+ msec -=3D 10;=0A+ cap =3D = pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, offset);=0A+ = }=0A+=0A+ if ( cap & EHCI_USBLEGSUP_BIOS )=0A+ {=0A+ /* well, = possibly buggy BIOS... try to shut it down,=0A+ * and hope nothing = goes too wrong */=0A+ dbgp_printk("dbgp: BIOS handoff failed: = %08x\n", cap);=0A+ pci_conf_write8(0, dbgp->bus, dbgp->slot, = dbgp->func, offset + 2, 0);=0A+ }=0A+=0A+ /* just in case, always = disable EHCI SMIs */=0A+ pci_conf_write8(0, dbgp->bus, dbgp->slot, = dbgp->func,=0A+ offset + EHCI_USBLEGCTLSTS, 0);=0A+}=0A+= =0A+static int ehci_dbgp_setup(struct ehci_dbgp *dbgp)=0A+{=0A+ u32 = ctrl, portsc, hcs_params;=0A+ unsigned int i, debug_port, new_debug_port= =3D 0, n_ports;=0A+ unsigned int port_map_tried, playtimes =3D 3;=0A+ = int ret;=0A+=0A+ ehci_dbgp_bios_handoff(dbgp, readl(&dbgp->ehci_caps->= hcc_params));=0A+=0A+try_next_time:=0A+ port_map_tried =3D 0;=0A+=0A+try= _next_port:=0A+=0A+ hcs_params =3D readl(&dbgp->ehci_caps->hcs_params);= =0A+ debug_port =3D HCS_DEBUG_PORT(hcs_params);=0A+ dbgp->phys_port = =3D debug_port;=0A+ n_ports =3D HCS_N_PORTS(hcs_params);=0A+=0A+ = dbgp_printk("debug_port: %u\n", debug_port);=0A+ dbgp_printk("n_ports: = %u\n", n_ports);=0A+ ehci_dbgp_status(dbgp, "");=0A+=0A+ for ( i = =3D 1; i <=3D n_ports; i++ )=0A+ {=0A+ portsc =3D readl(&dbgp->eh= ci_regs->port_status[i-1]);=0A+ dbgp_printk("portstatus%d: %08x\n", = i, portsc);=0A+ }=0A+=0A+ if ( port_map_tried && (new_debug_port = !=3D debug_port) )=0A+ {=0A+ if ( --playtimes )=0A+ {=0A+ = set_debug_port(dbgp, new_debug_port);=0A+ goto = try_next_time;=0A+ }=0A+ return -1;=0A+ }=0A+=0A+ /* = Only reset the controller if it is not already in the=0A+ * configured = state */=0A+ if ( readl(&dbgp->ehci_regs->configured_flag) & FLAG_CF = )=0A+ ehci_dbgp_status(dbgp, "ehci skip - already configured");=0A+ = else if ( ehci_dbgp_controller_reset(dbgp) !=3D 0 )=0A+ return = -1;=0A+=0A+ ret =3D ehci_dbgp_external_startup(dbgp);=0A+ if (ret = =3D=3D -EIO)=0A+ goto next_debug_port;=0A+=0A+ if ( ret < 0 = )=0A+ {=0A+ /* Things didn't work so remove my claim */=0A+ = ctrl =3D readl(&dbgp->ehci_debug->control);=0A+ ctrl &=3D = ~(DBGP_CLAIM | DBGP_OUT);=0A+ writel(ctrl, &dbgp->ehci_debug->contro= l);=0A+ return -1;=0A+ }=0A+=0A+ return 0;=0A+=0A+next_debug_p= ort:=0A+ port_map_tried |=3D 1 << (debug_port - 1);=0A+ new_debug_por= t =3D (debug_port % n_ports) + 1;=0A+ if ( port_map_tried !=3D ((1 << = n_ports) - 1) )=0A+ {=0A+ set_debug_port(dbgp, new_debug_port);= =0A+ goto try_next_port;=0A+ }=0A+ if ( --playtimes )=0A+ = {=0A+ set_debug_port(dbgp, new_debug_port);=0A+ goto = try_next_time;=0A+ }=0A+=0A+ return -1;=0A+}=0A+=0A+static inline = void _ehci_dbgp_flush(struct ehci_dbgp *dbgp)=0A+{=0A+ if ( dbgp_bulk_wr= ite(dbgp, USB_DEBUG_DEVNUM, dbgp->out.endpoint,=0A+ = dbgp->out.buf, dbgp->out.chunk, NULL) )=0A+ BUG();=0A+ = dbgp->out.chunk =3D 0;=0A+}=0A+=0A+static void ehci_dbgp_flush(struct = serial_port *port)=0A+{=0A+ struct ehci_dbgp *dbgp =3D port->uart;=0A+ = s_time_t goal;=0A+=0A+ if ( !dbgp->out.chunk || !dbgp->ehci_debug || = dbgp->state =3D=3D dbgp_unsafe )=0A+ return;=0A+=0A+ if ( = dbgp->state =3D=3D dbgp_idle || !port->sync )=0A+ dbgp_check_for_com= pletion(dbgp, 1, NULL);=0A+ else=0A+ dbgp_wait_until_complete(dbg= p, NULL);=0A+=0A+ if ( dbgp->state =3D=3D dbgp_idle )=0A+ {=0A+ = _ehci_dbgp_flush(dbgp);=0A+=0A+ if ( port->sync )=0A+ = {=0A+ dbgp_wait_until_complete(dbgp, NULL);=0A+ = return;=0A+ }=0A+ }=0A+=0A+ goal =3D NOW() + MICROSECS(DBGP_CH= ECK_INTERVAL);=0A+ if ( dbgp->timer.expires > goal )=0A+ = set_timer(&dbgp->timer, goal);=0A+}=0A+=0A+static void ehci_dbgp_putc(struc= t serial_port *port, char c)=0A+{=0A+ struct ehci_dbgp *dbgp =3D = port->uart;=0A+=0A+ if ( unlikely(dbgp->out.chunk >=3D DBGP_MAX_PACKET) = )=0A+ return;=0A+=0A+ dbgp->out.buf[dbgp->out.chunk++] =3D = c;=0A+=0A+ if ( dbgp->out.chunk =3D=3D DBGP_MAX_PACKET )=0A+ = ehci_dbgp_flush(port);=0A+}=0A+=0A+static int ehci_dbgp_tx_empty(struct = serial_port *port)=0A+{=0A+ struct ehci_dbgp *dbgp =3D port->uart;=0A+= =0A+ if ( unlikely(!dbgp->ehci_debug) || unlikely(dbgp->state =3D=3D = dbgp_unsafe) )=0A+ return port->sync || port->tx_log_everything || = !port->txbuf;=0A+=0A+ if ( dbgp->out.chunk =3D=3D DBGP_MAX_PACKET )=0A+ = ehci_dbgp_flush(port);=0A+ else=0A+ dbgp_check_for_complet= ion(dbgp, 1, NULL);=0A+=0A+ if ( dbgp->state !=3D dbgp_idle && = dbgp->out.chunk >=3D DBGP_MAX_PACKET )=0A+ return 0;=0A+=0A+ = port->tx_fifo_size =3D DBGP_MAX_PACKET - dbgp->out.chunk;=0A+ if ( = dbgp->state =3D=3D dbgp_idle )=0A+ port->tx_fifo_size +=3D = DBGP_MAX_PACKET;=0A+=0A+ return 1;=0A+}=0A+=0A+static int ehci_dbgp_getc= (struct serial_port *port, char *pc)=0A+{=0A+ struct ehci_dbgp *dbgp = =3D port->uart;=0A+=0A+ if ( !dbgp->in.chunk )=0A+ return = 0;=0A+=0A+ *pc =3D *dbgp->in.buf;=0A+ if ( --dbgp->in.chunk )=0A+ = memmove(dbgp->in.buf, dbgp->in.buf + 1, dbgp->in.chunk);=0A+=0A+ = return 1;=0A+}=0A+=0A+/* Safe: ehci_dbgp_poll() runs as timer handler, so = not reentrant. */=0A+static struct serial_port *poll_port;=0A+=0A+static = void _ehci_dbgp_poll(struct cpu_user_regs *regs)=0A+{=0A+ struct = serial_port *port =3D poll_port;=0A+ struct ehci_dbgp *dbgp =3D = port->uart;=0A+ unsigned long flags;=0A+ unsigned int timeout =3D = MICROSECS(DBGP_CHECK_INTERVAL);=0A+ bool_t empty =3D 0;=0A+=0A+ if ( = !dbgp->ehci_debug )=0A+ return;=0A+=0A+ if ( spin_trylock_irqsave= (&port->tx_lock, flags) )=0A+ {=0A+ if ( dbgp->state !=3D = dbgp_unsafe )=0A+ dbgp_check_for_completion(dbgp, DBGP_CHECK_INT= ERVAL, NULL);=0A+ if ( dbgp->state =3D=3D dbgp_idle && dbgp->out.chu= nk )=0A+ _ehci_dbgp_flush(dbgp);=0A+ if ( dbgp->state = =3D=3D dbgp_idle || dbgp->out.chunk < DBGP_MAX_PACKET )=0A+ = empty =3D 1;=0A+ spin_unlock_irqrestore(&port->tx_lock, flags);=0A+ = }=0A+=0A+ if ( dbgp->in.chunk )=0A+ serial_rx_interrupt(port, = regs);=0A+=0A+ if ( empty )=0A+ serial_tx_interrupt(port, = regs);=0A+=0A+ if ( spin_trylock_irqsave(&port->tx_lock, flags) )=0A+ = {=0A+ if ( dbgp->state =3D=3D dbgp_idle && !dbgp->in.chunk &&=0A+ = !dbgp->out.chunk && port->txbufp =3D=3D port->txbufc )=0A+ = {=0A+ if ( dbgp_bulk_read(dbgp, USB_DEBUG_DEVNUM, dbgp->in.end= point,=0A+ DBGP_MAX_PACKET, NULL) )=0A+ = BUG();=0A+ timeout =3D MILLISECS(DBGP_IDLE_INTERVAL);= =0A+ }=0A+ spin_unlock_irqrestore(&port->tx_lock, flags);=0A+= }=0A+=0A+ set_timer(&dbgp->timer, NOW() + timeout);=0A+}=0A+=0A+stat= ic void ehci_dbgp_poll(void *data)=0A+{=0A+ poll_port =3D data;=0A+#ifde= f run_in_exception_handler=0A+ run_in_exception_handler(_ehci_dbgp_poll)= ;=0A+#else=0A+ _ehci_dbgp_poll(guest_cpu_user_regs());=0A+#endif=0A+}=0A= +=0A+static bool_t ehci_dbgp_setup_preirq(struct ehci_dbgp *dbgp)=0A+{=0A+ = if ( !ehci_dbgp_setup(dbgp) )=0A+ return 1;=0A+=0A+ dbgp_print= k("ehci_dbgp_setup failed\n");=0A+ dbgp->ehci_debug =3D NULL;=0A+ = return 0;=0A+}=0A+=0A+static void __init ehci_dbgp_init_preirq(struct = serial_port *port)=0A+{=0A+ struct ehci_dbgp *dbgp =3D port->uart;=0A+ = u32 debug_port, offset;=0A+ void __iomem *ehci_bar;=0A+=0A+ = debug_port =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func,=0A+ = dbgp->cap);=0A+ offset =3D (debug_port >> = 16) & 0xfff;=0A+=0A+ /* double check if the mem space is enabled */=0A+ = dbgp->pci_cr =3D pci_conf_read8(0, dbgp->bus, dbgp->slot, dbgp->func,=0A= + PCI_COMMAND);=0A+ if ( !(dbgp->pci_cr= & PCI_COMMAND_MEMORY) )=0A+ {=0A+ dbgp->pci_cr |=3D PCI_COMMAND_= MEMORY;=0A+ pci_conf_write16(0, dbgp->bus, dbgp->slot, dbgp->func, = PCI_COMMAND,=0A+ dbgp->pci_cr);=0A+ = dbgp_printk("MMIO for EHCI enabled\n");=0A+ }=0A+=0A+ /*=0A+ * = FIXME I don't have the bar size so just guess PAGE_SIZE is more=0A+ * = than enough. 1k is the biggest that was seen.=0A+ */=0A+ set_fixmap= _nocache(FIX_EHCI_DBGP, dbgp->bar_val);=0A+ ehci_bar =3D (void __iomem = *)fix_to_virt(FIX_EHCI_DBGP);=0A+ ehci_bar +=3D dbgp->bar_val & = ~PAGE_MASK;=0A+ dbgp_printk("ehci_bar: %p\n", ehci_bar);=0A+=0A+ = dbgp->ehci_caps =3D ehci_bar;=0A+ dbgp->ehci_regs =3D ehci_bar +=0A+ = HC_LENGTH(readl(&dbgp->ehci_caps->hc_capbase));=0A+ = dbgp->ehci_debug =3D ehci_bar + offset;=0A+=0A+ detect_set_debug_port(db= gp);=0A+=0A+ if ( ehci_dbgp_setup_preirq(dbgp) )=0A+ ehci_dbgp_st= atus(dbgp, "ehci_dbgp_init_preirq complete");=0A+=0A+ port->tx_fifo_size= =3D DBGP_MAX_PACKET;=0A+ dbgp->lock =3D &port->tx_lock;=0A+}=0A+=0A+sta= tic void ehci_dbgp_setup_postirq(struct ehci_dbgp *dbgp)=0A+{=0A+ = set_timer(&dbgp->timer, NOW() + MILLISECS(1));=0A+}=0A+=0A+static void = __init ehci_dbgp_init_postirq(struct serial_port *port)=0A+{=0A+ struct = ehci_dbgp *dbgp =3D port->uart;=0A+=0A+ if ( !dbgp->ehci_debug )=0A+ = return;=0A+=0A+ serial_async_transmit(port);=0A+=0A+ init_timer(&= dbgp->timer, ehci_dbgp_poll, port, 0);=0A+=0A+ ehci_dbgp_setup_postirq(d= bgp);=0A+}=0A+=0A+static int ehci_dbgp_check_release(struct ehci_dbgp = *dbgp)=0A+{=0A+ struct ehci_dbg_port __iomem *ehci_debug =3D dbgp->ehci_= debug;=0A+ u32 ctrl;=0A+ unsigned int i;=0A+=0A+ if ( !ehci_debug = )=0A+ return 0;=0A+=0A+ for ( i =3D 0; i < DBGP_MAX_PACKET; ++i = )=0A+ if ( dbgp->out.buf[i] )=0A+ return 1;=0A+=0A+ = /*=0A+ * This means the console is not initialized, or should get = shutdown=0A+ * so as to allow for reuse of the USB device, which means = it is time=0A+ * to shutdown the USB debug port.=0A+ */=0A+ = printk(XENLOG_INFO "Releasing EHCI debug port at %02x:%02x.%u\n",=0A+ = dbgp->bus, dbgp->slot, dbgp->func);=0A+=0A+ kill_timer(&dbgp->timer= );=0A+ dbgp->ehci_debug =3D NULL;=0A+=0A+ ctrl =3D readl(&ehci_debug-= >control);=0A+ if ( ctrl & DBGP_ENABLED )=0A+ {=0A+ ctrl &=3D = ~DBGP_CLAIM;=0A+ writel(ctrl, &ehci_debug->control);=0A+ = }=0A+=0A+ return 0;=0A+}=0A+=0A+static void __init ehci_dbgp_endboot(str= uct serial_port *port)=0A+{=0A+ ehci_dbgp_check_release(port->uart);=0A+= }=0A+=0A+static void ehci_dbgp_suspend(struct serial_port *port)=0A+{=0A+ = struct ehci_dbgp *dbgp =3D port->uart;=0A+=0A+ if ( !dbgp->ehci_debug = )=0A+ return;=0A+=0A+ stop_timer(&dbgp->timer);=0A+ dbgp->time= r.expires =3D 0;=0A+=0A+ dbgp->pci_cr =3D pci_conf_read16(0, dbgp->bus, = dbgp->slot, dbgp->func,=0A+ PCI_COMMAND);= =0A+=0A+ dbgp->state =3D dbgp_unsafe;=0A+}=0A+=0A+static void ehci_dbgp_= resume(struct serial_port *port)=0A+{=0A+ struct ehci_dbgp *dbgp =3D = port->uart;=0A+=0A+ if ( !dbgp->ehci_debug )=0A+ return;=0A+=0A+ = pci_conf_write32(0, dbgp->bus, dbgp->slot, dbgp->func, dbgp->bar,=0A+ = dbgp->bar_val);=0A+ pci_conf_write16(0, dbgp->bus, = dbgp->slot, dbgp->func,=0A+ PCI_COMMAND, dbgp->pci_cr);= =0A+=0A+ ehci_dbgp_setup_preirq(dbgp);=0A+ ehci_dbgp_setup_postirq(db= gp);=0A+}=0A+=0A+static struct uart_driver __read_mostly ehci_dbgp_driver = =3D {=0A+ .init_preirq =3D ehci_dbgp_init_preirq,=0A+ .init_postirq = =3D ehci_dbgp_init_postirq,=0A+ .endboot =3D ehci_dbgp_endboot,=0A+= .suspend =3D ehci_dbgp_suspend,=0A+ .resume =3D = ehci_dbgp_resume,=0A+ .tx_empty =3D ehci_dbgp_tx_empty,=0A+ = .putc =3D ehci_dbgp_putc,=0A+ .flush =3D ehci_dbgp_flush,= =0A+ .getc =3D ehci_dbgp_getc=0A+};=0A+=0A+static struct = ehci_dbgp ehci_dbgp =3D { .state =3D dbgp_unsafe, .phys_port =3D 1 = };=0A+=0A+static char __initdata opt_dbgp[30];=0A+string_param("dbgp", = opt_dbgp);=0A+=0A+void __init ehci_dbgp_init(void)=0A+{=0A+ struct = ehci_dbgp *dbgp =3D &ehci_dbgp;=0A+ u32 debug_port, offset, bar_val;=0A+= const char *e;=0A+=0A+ if ( strncmp(opt_dbgp, "ehci", 4) )=0A+ = return;=0A+=0A+ if ( isdigit(opt_dbgp[4]) || !opt_dbgp[4] )=0A+ = {=0A+ unsigned int num =3D 0;=0A+=0A+ if ( opt_dbgp[4] )=0A+ = simple_strtoul(opt_dbgp + 4, &e, 10);=0A+=0A+ dbgp->cap = =3D find_dbgp(dbgp, num);=0A+ if ( !dbgp->cap )=0A+ = return;=0A+=0A+ dbgp_printk("Found EHCI debug port on %02x:%02x.%u\n= ",=0A+ dbgp->bus, dbgp->slot, dbgp->func);=0A+ }=0A+ = else if ( strncmp(opt_dbgp + 4, "@pci", 4) =3D=3D 0 )=0A+ {=0A+ = unsigned long val =3D simple_strtoul(opt_dbgp + 8, &e, 16);=0A+=0A+ = dbgp->bus =3D val;=0A+ if ( dbgp->bus !=3D val || *e !=3D ':' = )=0A+ return;=0A+=0A+ val =3D simple_strtoul(e + 1, &e, = 16);=0A+ if ( PCI_SLOT(PCI_DEVFN(val, 0)) !=3D val || *e !=3D '.' = )=0A+ return;=0A+ dbgp->slot =3D val;=0A+=0A+ val = =3D simple_strtoul(e + 1, &e, 16);=0A+ if ( PCI_FUNC(PCI_DEVFN(0, = val)) !=3D val || *e )=0A+ return;=0A+ dbgp->func =3D = val;=0A+=0A+ if ( !pci_device_detect(0, dbgp->bus, dbgp->slot, = dbgp->func) )=0A+ return;=0A+=0A+ dbgp->cap =3D = __find_dbgp(dbgp->bus, dbgp->slot, dbgp->func);=0A+ if ( !dbgp->cap = )=0A+ return;=0A+=0A+ dbgp_printk("Using EHCI debug port = on %02x:%02x.%u\n",=0A+ dbgp->bus, dbgp->slot, = dbgp->func);=0A+ }=0A+ else=0A+ return;=0A+=0A+ debug_port = =3D pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func,=0A+ = dbgp->cap);=0A+ dbgp->bar =3D (debug_port >> 29) & = 0x7;=0A+ dbgp->bar =3D ((dbgp->bar - 1) * 4) + PCI_BASE_ADDRESS_0;=0A+ = offset =3D (debug_port >> 16) & 0xfff;=0A+ dbgp_printk("bar: %02x = offset: %03x\n", dbgp->bar, offset);=0A+ if ( dbgp->bar < PCI_BASE_ADDRE= SS_0 || dbgp->bar > PCI_BASE_ADDRESS_5 )=0A+ {=0A+ dbgp_printk("u= nsupported/invalid bar\n");=0A+ return;=0A+ }=0A+=0A+ = dbgp->bar_val =3D bar_val =3D pci_conf_read32(0, dbgp->bus, dbgp->slot,=0A+= dbgp->func, dbgp->bar);=0A+ = dbgp_printk("bar_val: %08x\n", bar_val);=0A+ if ( bar_val & ~PCI_BASE_= ADDRESS_MEM_MASK )=0A+ {=0A+ dbgp_printk("only simple 32-bit = MMIO BARs supported\n");=0A+ return;=0A+ }=0A+ bar_val &=3D = PCI_BASE_ADDRESS_MEM_MASK;=0A+ if ( !bar_val || !(bar_val + (bar_val & = -bar_val)) )=0A+ {=0A+ dbgp_printk("firmware initialization of = MMIO BAR required\n");=0A+ return;=0A+ }=0A+=0A+ serial_regist= er_uart(SERHND_DBGP, &ehci_dbgp_driver, dbgp);=0A+}=0A+=0A+int dbgp_op(cons= t struct physdev_dbgp_op *op)=0A+{=0A+ if ( !ehci_dbgp.ehci_debug )=0A+ = return 0;=0A+=0A+ switch ( op->bus )=0A+ {=0A+ case = PHYSDEVOP_DBGP_BUS_UNKNOWN:=0A+ break;=0A+ case PHYSDEVOP_DBGP_BU= S_PCI:=0A+ if ( op->u.pci.seg || ehci_dbgp.bus !=3D op->u.pci.bus = ||=0A+ PCI_DEVFN(ehci_dbgp.slot, ehci_dbgp.func) !=3D op->u.pci.= devfn )=0A+ default:=0A+ return 0;=0A+ break;=0A+ = }=0A+=0A+ switch ( op->op )=0A+ {=0A+ case PHYSDEVOP_DBGP_RESET_PR= EPARE:=0A+ spin_lock_irq(ehci_dbgp.lock);=0A+ ehci_dbgp.state= =3D dbgp_unsafe;=0A+ dbgp_wait_until_complete(&ehci_dbgp, = NULL);=0A+ spin_unlock_irq(ehci_dbgp.lock);=0A+=0A+ return = ehci_dbgp_check_release(&ehci_dbgp);=0A+=0A+ case PHYSDEVOP_DBGP_RESET_D= ONE:=0A+ return ehci_dbgp_external_startup(&ehci_dbgp) ?: 1;=0A+ = }=0A+=0A+ return -ENOSYS;=0A+}=0A--- a/xen/drivers/char/serial.c=0A+++ = b/xen/drivers/char/serial.c=0A@@ -265,6 +265,14 @@ int __init serial_parse_= handle(char *con=0A {=0A int handle;=0A =0A+ if ( !strncmp(conf, = "dbgp", 4) && (!conf[4] || conf[4] =3D=3D ',') )=0A+ {=0A+ if ( = !com[SERHND_DBGP].driver )=0A+ goto fail;=0A+=0A+ return = SERHND_DBGP | SERHND_COOKED;=0A+ }=0A+=0A if ( strncmp(conf, "com", = 3) )=0A goto fail;=0A =0A--- a/xen/include/asm-x86/fixmap.h=0A+++ = b/xen/include/asm-x86/fixmap.h=0A@@ -36,7 +36,15 @@=0A * from the end of = virtual memory backwards.=0A */=0A enum fixed_addresses {=0A- = FIX_RESERVED, /* Index 0 is reserved since fix_to_virt(0) > FIXADDR_TOP. = */=0A+ /* Index 0 is reserved since fix_to_virt(0) =3D=3D FIXADDR_TOP. = */=0A+ FIX_RESERVED,=0A+ /*=0A+ * Indexes using the page tables = set up before entering __start_xen()=0A+ * must be among the first = (L1_PAGETABLE_ENTRIES - 1) entries.=0A+ * These are generally those = needed by the various console drivers.=0A+ */=0A+ FIX_EHCI_DBGP,=0A+= /* Everything else should go further down. */=0A #ifdef __i386__=0A = FIX_PAE_HIGHMEM_0,=0A FIX_PAE_HIGHMEM_END =3D FIX_PAE_HIGHMEM_0 + = NR_CPUS-1,=0A--- a/xen/include/public/physdev.h=0A+++ b/xen/include/public/= physdev.h=0A@@ -312,6 +312,24 @@ struct physdev_pci_device {=0A typedef = struct physdev_pci_device physdev_pci_device_t;=0A DEFINE_XEN_GUEST_HANDLE(= physdev_pci_device_t);=0A =0A+#define PHYSDEVOP_DBGP_RESET_PREPARE = 1=0A+#define PHYSDEVOP_DBGP_RESET_DONE 2=0A+=0A+#define PHYSDEVOP_DBG= P_BUS_UNKNOWN 0=0A+#define PHYSDEVOP_DBGP_BUS_PCI 1=0A+=0A+#d= efine PHYSDEVOP_dbgp_op 29=0A+struct physdev_dbgp_op {=0A+ = /* IN */=0A+ uint8_t op;=0A+ uint8_t bus;=0A+ union {=0A+ = struct physdev_pci_device pci;=0A+ } u;=0A+};=0A+typedef struct = physdev_dbgp_op physdev_dbgp_op_t;=0A+DEFINE_XEN_GUEST_HANDLE(physdev_dbgp_= op_t);=0A+=0A /*=0A * Notify that some PIRQ-bound event channels have = been unmasked.=0A * ** This command is obsolete since interface version = 0x00030202 and is **=0A--- a/xen/include/xen/serial.h=0A+++ b/xen/include/x= en/serial.h=0A@@ -69,9 +69,10 @@ struct uart_driver {=0A };=0A =0A /* = 'Serial handles' are composed from the following fields. */=0A-#define = SERHND_IDX (3<<0) /* COM1 or COM2? = */=0A+#define SERHND_IDX (3<<0) /* COM1, COM2, or DBGP? = */=0A # define SERHND_COM1 (0<<0)=0A # define SERHND_COM2 = (1<<0)=0A+# define SERHND_DBGP (2<<0)=0A #define SERHND_HI (1<<2) = /* Mux/demux each transferred char by MSB. */=0A #define SERHND_LO = (1<<3) /* Ditto, except that the MSB is cleared. */=0A #define SERHND_COOK= ED (1<<4) /* Newline/carriage-return translation? */=0A@@ -142,9 = +143,13 @@ struct ns16550_defaults {=0A unsigned long io_base; /* = default io_base address */=0A };=0A void ns16550_init(int index, struct = ns16550_defaults *defaults);=0A+void ehci_dbgp_init(void);=0A =0A void = pl011_init(int index, unsigned long register_base_address);=0A =0A+struct = physdev_dbgp_op;=0A+int dbgp_op(const struct physdev_dbgp_op *);=0A+=0A /* = Baud rate was pre-configured before invoking the UART driver. */=0A = #define BAUD_AUTO (-1)=0A =0A --=__Part0736157F.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part0736157F.0__=--