Coverage Report

Created: 2017-10-25 09:10

/root/src/xen/xen/drivers/char/ehci-dbgp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Standalone EHCI USB debug driver
3
 *
4
 * Hardware interface code based on the respective early console driver in
5
 * Linux; see the Linux source for authorship and copyrights.
6
 */
7
8
#include <xen/console.h>
9
#include <xen/delay.h>
10
#include <xen/errno.h>
11
#include <xen/pci.h>
12
#include <xen/serial.h>
13
#include <asm/byteorder.h>
14
#include <asm/io.h>
15
#include <asm/fixmap.h>
16
#include <public/physdev.h>
17
18
/* #define DBGP_DEBUG */
19
20
/* EHCI register interface, corresponds to EHCI Revision 0.95 specification */
21
22
/* Section 2.2 Host Controller Capability Registers */
23
struct ehci_caps {
24
    /*
25
     * These fields are specified as 8 and 16 bit registers,
26
     * but some hosts can't perform 8 or 16 bit PCI accesses.
27
     * some hosts treat caplength and hciversion as parts of a 32-bit
28
     * register, others treat them as two separate registers, this
29
     * affects the memory map for big endian controllers.
30
     */
31
    u32 hc_capbase;
32
0
#define HC_LENGTH(p)      (0x00ff & (p)) /* bits 7:0 / offset 0x00 */
33
#define HC_VERSION(p)     (0xffff & ((p) >> 16)) /* bits 31:16 / offset 0x02 */
34
35
    u32 hcs_params;       /* HCSPARAMS - offset 0x04 */
36
0
#define HCS_DEBUG_PORT(p) (((p) >> 20) & 0xf) /* bits 23:20, debug port? */
37
#define HCS_INDICATOR(p)  ((p) & (1 << 16))   /* true: has port indicators */
38
#define HCS_N_CC(p)       (((p) >> 12) & 0xf) /* bits 15:12, #companion HCs */
39
#define HCS_N_PCC(p)      (((p) >> 8) & 0xf)  /* bits 11:8, ports per CC */
40
#define HCS_PORTROUTED(p) ((p) & (1 << 7))    /* true: port routing */
41
#define HCS_PPC(p)        ((p) & (1 << 4))    /* true: port power control */
42
0
#define HCS_N_PORTS(p)    (((p) >> 0) & 0xf)  /* bits 3:0, ports on HC */
43
44
    u32 hcc_params;       /* HCCPARAMS - offset 0x08 */
45
/* EHCI 1.1 addendum */
46
#define HCC_32FRAME_PERIODIC_LIST(p) ((p) & (1 << 19))
47
#define HCC_PER_PORT_CHANGE_EVENT(p) ((p) & (1 << 18))
48
#define HCC_LPM(p)        ((p) & (1 << 17))
49
#define HCC_HW_PREFETCH(p) ((p) & (1 << 16))
50
0
#define HCC_EXT_CAPS(p)   (((p) >> 8) & 0xff) /* for pci extended caps */
51
#define HCC_ISOC_CACHE(p) ((p) & (1 << 7))    /* true: can cache isoc frame */
52
#define HCC_ISOC_THRES(p) (((p) >> 4) & 0x7)  /* bits 6:4, uframes cached */
53
#define HCC_CANPARK(p)    ((p) & (1 << 2))    /* true: can park on async qh */
54
#define HCC_PGM_FRAMELISTLEN(p) ((p) & (1 << 1)) /* true: periodic_size changes */
55
#define HCC_64BIT_ADDR(p) ((p) & 1)           /* true: can use 64-bit addr */
56
57
    u8  portroute[8];     /* nibbles for routing - offset 0x0C */
58
};
59
60
/* Section 2.3 Host Controller Operational Registers */
61
struct ehci_regs {
62
    /* USBCMD: offset 0x00 */
63
    u32 command;
64
65
/* EHCI 1.1 addendum */
66
#define CMD_HIRD        (0xf << 24) /* host initiated resume duration */
67
#define CMD_PPCEE       (1 << 15)   /* per port change event enable */
68
#define CMD_FSP         (1 << 14)   /* fully synchronized prefetch */
69
#define CMD_ASPE        (1 << 13)   /* async schedule prefetch enable */
70
#define CMD_PSPE        (1 << 12)   /* periodic schedule prefetch enable */
71
/* 23:16 is r/w intr rate, in microframes; default "8" == 1/msec */
72
#define CMD_PARK        (1 << 11)   /* enable "park" on async qh */
73
#define CMD_PARK_CNT(c) (((c) >> 8) & 3) /* how many transfers to park for */
74
0
#define CMD_LRESET      (1 << 7)    /* partial reset (no ports, etc) */
75
0
#define CMD_IAAD        (1 << 6)    /* "doorbell" interrupt async advance */
76
0
#define CMD_ASE         (1 << 5)    /* async schedule enable */
77
0
#define CMD_PSE         (1 << 4)    /* periodic schedule enable */
78
/* 3:2 is periodic frame list size */
79
0
#define CMD_RESET       (1 << 1)    /* reset HC not bus */
80
0
#define CMD_RUN         (1 << 0)    /* start/stop HC */
81
82
    /* USBSTS: offset 0x04 */
83
    u32 status;
84
#define STS_PPCE_MASK   (0xff << 16) /* Per-Port change event 1-16 */
85
#define STS_ASS         (1 << 15)   /* Async Schedule Status */
86
#define STS_PSS         (1 << 14)   /* Periodic Schedule Status */
87
#define STS_RECL        (1 << 13)   /* Reclamation */
88
0
#define STS_HALT        (1 << 12)   /* Not running (any reason) */
89
/* some bits reserved */
90
    /* these STS_* flags are also intr_enable bits (USBINTR) */
91
#define STS_IAA         (1 << 5)    /* Interrupted on async advance */
92
#define STS_FATAL       (1 << 4)    /* such as some PCI access errors */
93
#define STS_FLR         (1 << 3)    /* frame list rolled over */
94
0
#define STS_PCD         (1 << 2)    /* port change detect */
95
#define STS_ERR         (1 << 1)    /* "error" completion (overflow, ...) */
96
#define STS_INT         (1 << 0)    /* "normal" completion (short, ...) */
97
98
    /* USBINTR: offset 0x08 */
99
    u32 intr_enable;
100
101
    /* FRINDEX: offset 0x0C */
102
    u32 frame_index;    /* current microframe number */
103
    /* CTRLDSSEGMENT: offset 0x10 */
104
    u32 segment;    /* address bits 63:32 if needed */
105
    /* PERIODICLISTBASE: offset 0x14 */
106
    u32 frame_list;    /* points to periodic list */
107
    /* ASYNCLISTADDR: offset 0x18 */
108
    u32 async_next;    /* address of next async queue head */
109
110
    u32 reserved[9];
111
112
    /* CONFIGFLAG: offset 0x40 */
113
    u32 configured_flag;
114
0
#define FLAG_CF         (1 << 0)    /* true: we'll support "high speed" */
115
116
    /* PORTSC: offset 0x44 */
117
    u32 port_status[0];    /* up to N_PORTS */
118
/* EHCI 1.1 addendum */
119
#define PORTSC_SUSPEND_STS_ACK   0
120
#define PORTSC_SUSPEND_STS_NYET  1
121
#define PORTSC_SUSPEND_STS_STALL 2
122
#define PORTSC_SUSPEND_STS_ERR   3
123
124
#define PORT_DEV_ADDR   (0x7f << 25) /* device address */
125
#define PORT_SSTS       (0x3 << 23)  /* suspend status */
126
/* 31:23 reserved */
127
#define PORT_WKOC_E     (1 << 22)    /* wake on overcurrent (enable) */
128
#define PORT_WKDISC_E   (1 << 21)    /* wake on disconnect (enable) */
129
#define PORT_WKCONN_E   (1 << 20)    /* wake on connect (enable) */
130
/* 19:16 for port testing */
131
0
#define PORT_TEST(x)    (((x) & 0xf) << 16) /* Port Test Control */
132
0
#define PORT_TEST_PKT   PORT_TEST(0x4) /* Port Test Control - packet test */
133
#define PORT_TEST_FORCE PORT_TEST(0x5) /* Port Test Control - force enable */
134
#define PORT_LED_OFF    (0 << 14)
135
#define PORT_LED_AMBER  (1 << 14)
136
#define PORT_LED_GREEN  (2 << 14)
137
#define PORT_LED_MASK   (3 << 14)
138
#define PORT_OWNER      (1 << 13)    /* true: companion hc owns this port */
139
#define PORT_POWER      (1 << 12)    /* true: has power (see PPC) */
140
#define PORT_USB11(x)   (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
141
/* 11:10 for detecting lowspeed devices (reset vs release ownership) */
142
/* 9 reserved */
143
#define PORT_LPM        (1 << 9)     /* LPM transaction */
144
0
#define PORT_RESET      (1 << 8)     /* reset port */
145
#define PORT_SUSPEND    (1 << 7)     /* suspend port */
146
#define PORT_RESUME     (1 << 6)     /* resume it */
147
#define PORT_OCC        (1 << 5)     /* over current change */
148
#define PORT_OC         (1 << 4)     /* over current active */
149
#define PORT_PEC        (1 << 3)     /* port enable change */
150
0
#define PORT_PE         (1 << 2)     /* port enable */
151
0
#define PORT_CSC        (1 << 1)     /* connect status change */
152
0
#define PORT_CONNECT    (1 << 0)     /* device connected */
153
#define PORT_RWC_BITS   (PORT_CSC | PORT_PEC | PORT_OCC)
154
};
155
156
/*
157
 * Appendix C, Debug port ... intended for use with special "debug devices"
158
 * that can help if there's no serial console.  (nonstandard enumeration.)
159
 */
160
struct ehci_dbg_port {
161
    u32 control;
162
0
#define DBGP_OWNER      (1 << 30)
163
0
#define DBGP_ENABLED    (1 << 28)
164
0
#define DBGP_DONE       (1 << 16)
165
0
#define DBGP_INUSE      (1 << 10)
166
0
#define DBGP_ERRCODE(x) (((x) >> 7) & 0x07)
167
0
# define DBGP_ERR_BAD    1
168
# define DBGP_ERR_SIGNAL 2
169
0
#define DBGP_ERROR      (1 << 6)
170
0
#define DBGP_GO         (1 << 5)
171
0
#define DBGP_OUT        (1 << 4)
172
0
#define DBGP_LEN        (0xf << 0)
173
0
#define DBGP_CLAIM      (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
174
    u32 pids;
175
0
#define DBGP_PID_GET(x)         (((x) >> 16) & 0xff)
176
0
#define DBGP_PID_SET(data, tok) (((data) << 8) | (tok))
177
    u32 data03;
178
    u32 data47;
179
    u32 address;
180
0
#define DBGP_EPADDR(dev, ep) (((dev) << 8) | (ep))
181
};
182
183
/* CONTROL REQUEST SUPPORT */
184
185
/*
186
 * USB directions
187
 *
188
 * This bit flag is used in endpoint descriptors' bEndpointAddress field.
189
 * It's also one of three fields in control requests bRequestType.
190
 */
191
0
#define USB_DIR_OUT 0           /* to device */
192
0
#define USB_DIR_IN  0x80        /* to host */
193
194
/*
195
 * USB types, the second of three bRequestType fields
196
 */
197
#define USB_TYPE_MASK     (0x03 << 5)
198
0
#define USB_TYPE_STANDARD (0x00 << 5)
199
#define USB_TYPE_CLASS    (0x01 << 5)
200
#define USB_TYPE_VENDOR   (0x02 << 5)
201
#define USB_TYPE_RESERVED (0x03 << 5)
202
203
/*
204
 * USB recipients, the third of three bRequestType fields
205
 */
206
#define USB_RECIP_MASK      0x1f
207
0
#define USB_RECIP_DEVICE    0x00
208
#define USB_RECIP_INTERFACE 0x01
209
#define USB_RECIP_ENDPOINT  0x02
210
#define USB_RECIP_OTHER     0x03
211
/* From Wireless USB 1.0 */
212
#define USB_RECIP_PORT      0x04
213
#define USB_RECIP_RPIPE     0x05
214
215
/*
216
 * Standard requests, for the bRequest field of a SETUP packet.
217
 *
218
 * These are qualified by the bRequestType field, so that for example
219
 * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved
220
 * by a GET_STATUS request.
221
 */
222
#define USB_REQ_GET_STATUS        0x00
223
#define USB_REQ_CLEAR_FEATURE     0x01
224
0
#define USB_REQ_SET_FEATURE       0x03
225
0
#define USB_REQ_SET_ADDRESS       0x05
226
0
#define USB_REQ_GET_DESCRIPTOR    0x06
227
#define USB_REQ_SET_DESCRIPTOR    0x07
228
#define USB_REQ_GET_CONFIGURATION 0x08
229
#define USB_REQ_SET_CONFIGURATION 0x09
230
#define USB_REQ_GET_INTERFACE     0x0A
231
#define USB_REQ_SET_INTERFACE     0x0B
232
#define USB_REQ_SYNCH_FRAME       0x0C
233
234
0
#define USB_DEVICE_DEBUG_MODE        6    /* (special devices only) */
235
236
/**
237
 * struct usb_ctrlrequest - SETUP data for a USB device control request
238
 * @bRequestType: matches the USB bmRequestType field
239
 * @bRequest: matches the USB bRequest field
240
 * @wValue: matches the USB wValue field (le16 byte order)
241
 * @wIndex: matches the USB wIndex field (le16 byte order)
242
 * @wLength: matches the USB wLength field (le16 byte order)
243
 *
244
 * This structure is used to send control requests to a USB device.  It matches
245
 * the different fields of the USB 2.0 Spec section 9.3, table 9-2.  See the
246
 * USB spec for a fuller description of the different fields, and what they are
247
 * used for.
248
 *
249
 * Note that the driver for any interface can issue control requests.
250
 * For most devices, interfaces don't coordinate with each other, so
251
 * such requests may be made at any time.
252
 */
253
struct __packed usb_ctrlrequest {
254
    u8 bRequestType;
255
    u8 bRequest;
256
    __le16 wValue;
257
    __le16 wIndex;
258
    __le16 wLength;
259
};
260
261
/* USB_DT_DEBUG: for special highspeed devices, replacing serial console */
262
263
0
#define USB_DT_DEBUG    0x0a
264
265
struct __packed usb_debug_descriptor {
266
    u8 bLength;
267
    u8 bDescriptorType;
268
    /* bulk endpoints with 8 byte maxpacket */
269
    u8 bDebugInEndpoint;
270
    u8 bDebugOutEndpoint;
271
};
272
273
0
#define USB_DEBUG_DEVNUM 127
274
275
/*
276
 * USB Packet IDs (PIDs)
277
 */
278
279
/* token */
280
0
#define USB_PID_OUT           0xe1
281
0
#define USB_PID_IN            0x69
282
#define USB_PID_SOF           0xa5
283
#define USB_PID_SETUP         0x2d
284
/* handshake */
285
#define USB_PID_ACK           0xd2
286
0
#define USB_PID_NAK           0x5a
287
#define USB_PID_STALL         0x1e
288
0
#define USB_PID_NYET          0x96
289
/* data */
290
0
#define USB_PID_DATA0         0xc3
291
0
#define USB_PID_DATA1         0x4b
292
#define USB_PID_DATA2         0x87
293
#define USB_PID_MDATA         0x0f
294
/* Special */
295
#define USB_PID_PREAMBLE      0x3c
296
#define USB_PID_ERR           0x3c
297
#define USB_PID_SPLIT         0x78
298
#define USB_PID_PING          0xb4
299
#define USB_PID_UNDEF_0       0xf0
300
301
0
#define PCI_CLASS_SERIAL_USB_EHCI 0x0c0320
302
0
#define PCI_CAP_ID_EHCI_DEBUG     0x0a
303
304
0
#define HUB_ROOT_RESET_TIME   50    /* times are in msec */
305
#define HUB_SHORT_RESET_TIME  10
306
#define HUB_LONG_RESET_TIME   200
307
0
#define HUB_RESET_TIMEOUT     500
308
309
0
#define DBGP_MAX_PACKET       8
310
0
#define DBGP_LOOPS            1000
311
0
#define DBGP_TIMEOUT          (250 * 1000) /* us */
312
0
#define DBGP_CHECK_INTERVAL   100 /* us */
313
/* This one can be set arbitrarily - only affects input responsiveness: */
314
#define DBGP_IDLE_INTERVAL    100 /* ms */
315
316
struct ehci_dbgp {
317
    struct ehci_dbg_port __iomem *ehci_debug;
318
    enum dbgp_state {
319
        dbgp_idle,
320
        dbgp_out,
321
        dbgp_in,
322
        dbgp_ctrl,
323
        dbgp_unsafe /* cannot use debug device during EHCI reset */
324
    } state;
325
    unsigned int phys_port;
326
    struct {
327
        unsigned int endpoint;
328
        unsigned int chunk;
329
        char buf[DBGP_MAX_PACKET];
330
    } out, in;
331
    unsigned long timeout;
332
    struct timer timer;
333
    spinlock_t *lock;
334
    bool_t reset_run;
335
    u8 bus, slot, func, bar;
336
    u16 pci_cr;
337
    u32 bar_val;
338
    unsigned int cap;
339
    struct ehci_regs __iomem *ehci_regs;
340
    struct ehci_caps __iomem *ehci_caps;
341
};
342
343
static int ehci_dbgp_external_startup(struct ehci_dbgp *);
344
345
static void ehci_dbgp_status(struct ehci_dbgp *dbgp, const char *str)
346
0
{
347
0
#ifdef DBGP_DEBUG
348
#define dbgp_printk printk
349
    if ( !dbgp->ehci_debug )
350
        return;
351
    dbgp_printk("dbgp: %s\n", str);
352
    dbgp_printk("  debug control: %08x\n", readl(&dbgp->ehci_debug->control));
353
    dbgp_printk("  EHCI cmd     : %08x\n", readl(&dbgp->ehci_regs->command));
354
    dbgp_printk("  EHCI conf flg: %08x\n",
355
                readl(&dbgp->ehci_regs->configured_flag));
356
    dbgp_printk("  EHCI status  : %08x\n", readl(&dbgp->ehci_regs->status));
357
    dbgp_printk("  EHCI portsc  : %08x\n",
358
                readl(&dbgp->ehci_regs->port_status[dbgp->phys_port - 1]));
359
#endif
360
0
}
361
362
#ifndef DBGP_DEBUG
363
static inline __attribute__ ((format (printf, 1, 2))) void
364
0
dbgp_printk(const char *fmt, ...) { }
365
#endif
366
367
static inline u32 dbgp_len_update(u32 x, u32 len)
368
0
{
369
0
    return (x & ~DBGP_LEN) | (len & DBGP_LEN) | DBGP_OUT;
370
0
}
371
372
static inline u32 dbgp_pid_write_update(u32 x, u32 tok)
373
0
{
374
0
    static u8 data0 = USB_PID_DATA1;
375
0
376
0
    data0 ^= USB_PID_DATA0 ^ USB_PID_DATA1;
377
0
    return (x & 0xffff0000) | (data0 << 8) | (tok & 0xff);
378
0
}
379
380
static inline u32 dbgp_pid_read_update(u32 x, u32 tok)
381
0
{
382
0
    return (x & 0xffffff00) | (tok & 0xff);
383
0
}
384
385
static inline void dbgp_set_data(struct ehci_dbg_port __iomem *ehci_debug,
386
                                 const void *buf, unsigned int size)
387
0
{
388
0
    const unsigned char *bytes = buf;
389
0
    u32 lo = 0, hi = 0;
390
0
    unsigned int i;
391
0
392
0
    for ( i = 0; i < 4 && i < size; i++ )
393
0
        lo |= bytes[i] << (8 * i);
394
0
    for ( ; i < 8 && i < size; i++ )
395
0
        hi |= bytes[i] << (8 * (i - 4));
396
0
    writel(lo, &ehci_debug->data03);
397
0
    writel(hi, &ehci_debug->data47);
398
0
}
399
400
static inline void dbgp_get_data(struct ehci_dbg_port __iomem *ehci_debug,
401
                                 void *buf, int size)
402
0
{
403
0
    unsigned char *bytes = buf;
404
0
    u32 lo = readl(&ehci_debug->data03);
405
0
    u32 hi = readl(&ehci_debug->data47);
406
0
    unsigned int i;
407
0
408
0
    for ( i = 0; i < 4 && i < size; i++ )
409
0
        bytes[i] = (lo >> (8 * i)) & 0xff;
410
0
    for ( ; i < 8 && i < size; i++ )
411
0
        bytes[i] = (hi >> (8 * (i - 4))) & 0xff;
412
0
}
413
414
static void dbgp_issue_command(struct ehci_dbgp *dbgp, u32 ctrl,
415
                               enum dbgp_state state)
416
0
{
417
0
    u32 cmd = readl(&dbgp->ehci_regs->command);
418
0
419
0
    if ( unlikely(!(cmd & CMD_RUN)) )
420
0
    {
421
0
        /*
422
0
         * If the EHCI controller is not in the run state do extended
423
0
         * checks to see if ACPI or some other initialization also
424
0
         * reset the EHCI debug port.
425
0
         */
426
0
        u32 ctrl = readl(&dbgp->ehci_debug->control);
427
0
428
0
        if ( ctrl & DBGP_ENABLED )
429
0
        {
430
0
            cmd |= CMD_RUN;
431
0
            writel(cmd, &dbgp->ehci_regs->command);
432
0
            dbgp->reset_run = 1;
433
0
        }
434
0
        else if ( dbgp->state != dbgp_unsafe )
435
0
        {
436
0
            dbgp->state = dbgp_unsafe;
437
0
            ehci_dbgp_external_startup(dbgp);
438
0
        }
439
0
    }
440
0
441
0
    writel(ctrl | DBGP_GO, &dbgp->ehci_debug->control);
442
0
    dbgp->timeout = DBGP_TIMEOUT;
443
0
    if ( dbgp->state != dbgp_unsafe )
444
0
        dbgp->state = state;
445
0
}
446
447
static int dbgp_check_for_completion(struct ehci_dbgp *dbgp,
448
                                     unsigned int interval, u8 *ppid)
449
0
{
450
0
    u32 ctrl;
451
0
    int ret;
452
0
453
0
    if ( dbgp->state == dbgp_idle )
454
0
        return 0;
455
0
456
0
    ctrl = readl(&dbgp->ehci_debug->control) & ~DBGP_GO;
457
0
    if ( !(ctrl & DBGP_DONE) )
458
0
    {
459
0
        if ( dbgp->timeout > interval )
460
0
            dbgp->timeout -= interval;
461
0
        else if ( interval )
462
0
        {
463
0
            /* See the timeout related comment in dbgp_wait_until_done(). */
464
0
            dbgp->state = dbgp_unsafe;
465
0
            dbgp->timeout = 0;
466
0
        }
467
0
        return -DBGP_TIMEOUT;
468
0
    }
469
0
470
0
    if ( ctrl & DBGP_ERROR )
471
0
    {
472
0
        ret = -DBGP_ERRCODE(ctrl);
473
0
        if ( ret == -DBGP_ERR_BAD && dbgp->timeout > interval )
474
0
            ctrl |= DBGP_GO;
475
0
    }
476
0
    else
477
0
    {
478
0
        u8 pid = DBGP_PID_GET(readl(&dbgp->ehci_debug->pids));
479
0
480
0
        ret = ctrl & DBGP_LEN;
481
0
        if ( ppid )
482
0
            *ppid = pid;
483
0
        else if ( dbgp->state == dbgp_in )
484
0
        {
485
0
            dbgp_get_data(dbgp->ehci_debug, dbgp->in.buf, ret);
486
0
            dbgp->in.chunk = ret;
487
0
        }
488
0
        else if ( pid == USB_PID_NAK && dbgp->timeout > interval )
489
0
            ctrl |= DBGP_GO;
490
0
    }
491
0
492
0
    writel(ctrl, &dbgp->ehci_debug->control);
493
0
    if ( ctrl & DBGP_GO )
494
0
    {
495
0
        dbgp->timeout -= interval;
496
0
        return -DBGP_TIMEOUT;
497
0
    }
498
0
499
0
    if ( unlikely(dbgp->reset_run) )
500
0
    {
501
0
        writel(readl(&dbgp->ehci_regs->command) & ~CMD_RUN,
502
0
               &dbgp->ehci_regs->command);
503
0
        dbgp->reset_run = 0;
504
0
    }
505
0
506
0
    if ( dbgp->state != dbgp_unsafe )
507
0
        dbgp->state = dbgp_idle;
508
0
509
0
    return ret;
510
0
}
511
512
static int dbgp_wait_until_complete(struct ehci_dbgp *dbgp, u8 *ppid)
513
0
{
514
0
    unsigned int loop = DBGP_TIMEOUT;
515
0
    int ret;
516
0
517
0
    do {
518
0
        ret = dbgp_check_for_completion(dbgp, 0, ppid);
519
0
        if ( ret != -DBGP_TIMEOUT )
520
0
            break;
521
0
        udelay(1);
522
0
    } while ( --loop );
523
0
524
0
    if ( !ppid && !loop )
525
0
        dbgp->state = dbgp_unsafe;
526
0
527
0
    return ret;
528
0
}
529
530
static inline void dbgp_mdelay(unsigned int ms)
531
0
{
532
0
    while ( ms-- )
533
0
    {
534
0
        unsigned int i;
535
0
536
0
        for ( i = 0; i < 1000; i++ )
537
0
            outb(0x1, 0x80);
538
0
    }
539
0
}
540
541
static void dbgp_breathe(void)
542
0
{
543
0
    /* Sleep to give the debug port a chance to breathe. */
544
0
    dbgp_mdelay(1);
545
0
}
546
547
static int dbgp_wait_until_done(struct ehci_dbgp *dbgp, u32 ctrl,
548
                                unsigned int loop)
549
0
{
550
0
    int ret;
551
0
552
0
    dbgp->timeout = 0;
553
0
554
0
    for ( ; ; writel(ctrl | DBGP_GO, &dbgp->ehci_debug->control) )
555
0
    {
556
0
        u8 pid;
557
0
558
0
        ret = dbgp_wait_until_complete(dbgp, &pid);
559
0
        if ( ret < 0 )
560
0
        {
561
0
            /*
562
0
             * A -DBGP_TIMEOUT failure here means the device has failed,
563
0
             * perhaps because it was unplugged, in which case we do not
564
0
             * want to hang the system so the dbgp will be marked as unsafe
565
0
             * to use. EHCI reset is the only way to recover if you unplug
566
0
             * the dbgp device.
567
0
             */
568
0
            if ( ret == -DBGP_TIMEOUT )
569
0
                dbgp->state = dbgp_unsafe;
570
0
            if ( ret != -DBGP_ERR_BAD || !--loop )
571
0
                break;
572
0
        }
573
0
        else
574
0
        {
575
0
            /*
576
0
             * If the port is getting full or it has dropped data
577
0
             * start pacing ourselves, not necessary but it's friendly.
578
0
             */
579
0
            if ( pid == USB_PID_NAK || pid == USB_PID_NYET )
580
0
                dbgp_breathe();
581
0
582
0
            /* If we got a NACK, reissue the transmission. */
583
0
            if ( pid != USB_PID_NAK || !--loop )
584
0
                break;
585
0
        }
586
0
    }
587
0
588
0
    return ret;
589
0
}
590
591
static int dbgp_bulk_write(struct ehci_dbgp *dbgp,
592
                           unsigned int devnum, unsigned int endpoint,
593
                           const void *bytes, unsigned int size, u32 *pctrl)
594
0
{
595
0
    u32 addr, pids, ctrl;
596
0
597
0
    if ( size > DBGP_MAX_PACKET )
598
0
        return -EINVAL;
599
0
600
0
    addr = DBGP_EPADDR(devnum, endpoint);
601
0
    pids = dbgp_pid_write_update(readl(&dbgp->ehci_debug->pids), USB_PID_OUT);
602
0
    ctrl = dbgp_len_update(readl(&dbgp->ehci_debug->control), size);
603
0
    if ( pctrl )
604
0
        *pctrl = ctrl;
605
0
606
0
    dbgp_set_data(dbgp->ehci_debug, bytes, size);
607
0
    writel(addr, &dbgp->ehci_debug->address);
608
0
    writel(pids, &dbgp->ehci_debug->pids);
609
0
    dbgp_issue_command(dbgp, ctrl, dbgp_out);
610
0
611
0
    return 0;
612
0
}
613
614
static int dbgp_bulk_read(struct ehci_dbgp *dbgp,
615
                          unsigned int devnum, unsigned int endpoint,
616
                          unsigned int size, u32 *pctrl)
617
0
{
618
0
    u32 addr, pids, ctrl;
619
0
620
0
    if ( size > DBGP_MAX_PACKET )
621
0
        return -EINVAL;
622
0
623
0
    addr = DBGP_EPADDR(devnum, endpoint);
624
0
    pids = dbgp_pid_read_update(readl(&dbgp->ehci_debug->pids), USB_PID_IN);
625
0
    ctrl = readl(&dbgp->ehci_debug->control) & ~DBGP_OUT;
626
0
627
0
    writel(addr, &dbgp->ehci_debug->address);
628
0
    writel(pids, &dbgp->ehci_debug->pids);
629
0
    if ( likely(!pctrl) )
630
0
        dbgp_issue_command(dbgp, ctrl, dbgp_in);
631
0
    else
632
0
        dbgp_issue_command(dbgp, *pctrl = ctrl, dbgp_ctrl);
633
0
634
0
    return 0;
635
0
}
636
637
static int dbgp_control_msg(struct ehci_dbgp *dbgp, unsigned int devnum,
638
                            int requesttype, int request, int value,
639
                            int index, void *data, unsigned int size)
640
0
{
641
0
    u32 addr, pids, ctrl;
642
0
    struct usb_ctrlrequest req;
643
0
    bool_t read = (requesttype & USB_DIR_IN) != 0;
644
0
    int ret;
645
0
646
0
    if ( size > (read ? DBGP_MAX_PACKET : 0) )
647
0
        return -EINVAL;
648
0
649
0
    /* Compute the control message */
650
0
    req.bRequestType = requesttype;
651
0
    req.bRequest = request;
652
0
    req.wValue = cpu_to_le16(value);
653
0
    req.wIndex = cpu_to_le16(index);
654
0
    req.wLength = cpu_to_le16(size);
655
0
656
0
    pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
657
0
    addr = DBGP_EPADDR(devnum, 0);
658
0
    ctrl = dbgp_len_update(readl(&dbgp->ehci_debug->control), sizeof(req));
659
0
660
0
    /* Send the setup message */
661
0
    dbgp_set_data(dbgp->ehci_debug, &req, sizeof(req));
662
0
    writel(addr, &dbgp->ehci_debug->address);
663
0
    writel(pids, &dbgp->ehci_debug->pids);
664
0
    dbgp_issue_command(dbgp, ctrl, dbgp_ctrl);
665
0
    ret = dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS);
666
0
    if ( ret < 0 )
667
0
        return ret;
668
0
669
0
    /* Read the result */
670
0
    ret = dbgp_bulk_read(dbgp, devnum, 0, size, &ctrl);
671
0
    if ( !ret )
672
0
        ret = dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS);
673
0
    if ( ret > 0 )
674
0
    {
675
0
        if ( size > ret )
676
0
            size = ret;
677
0
        dbgp_get_data(dbgp->ehci_debug, data, size);
678
0
    }
679
0
680
0
    return ret;
681
0
}
682
683
static unsigned int __init __find_dbgp(u8 bus, u8 slot, u8 func)
684
0
{
685
0
    u32 class = pci_conf_read32(0, bus, slot, func, PCI_CLASS_REVISION);
686
0
687
0
    if ( (class >> 8) != PCI_CLASS_SERIAL_USB_EHCI )
688
0
        return 0;
689
0
690
0
    return pci_find_cap_offset(0, bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
691
0
}
692
693
static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp,
694
                                     unsigned int ehci_num)
695
0
{
696
0
    unsigned int bus, slot, func;
697
0
698
0
    for ( bus = 0; bus < 256; bus++ )
699
0
    {
700
0
        for ( slot = 0; slot < 32; slot++ )
701
0
        {
702
0
            for ( func = 0; func < 8; func++ )
703
0
            {
704
0
                unsigned int cap;
705
0
706
0
                if ( !pci_device_detect(0, bus, slot, func) )
707
0
                {
708
0
                    if ( !func )
709
0
                        break;
710
0
                    continue;
711
0
                }
712
0
713
0
                cap = __find_dbgp(bus, slot, func);
714
0
                if ( !cap || ehci_num-- )
715
0
                {
716
0
                    if ( !func && !(pci_conf_read8(0, bus, slot, func,
717
0
                                                   PCI_HEADER_TYPE) & 0x80) )
718
0
                        break;
719
0
                    continue;
720
0
                }
721
0
722
0
                dbgp->bus = bus;
723
0
                dbgp->slot = slot;
724
0
                dbgp->func = func;
725
0
                return cap;
726
0
            }
727
0
        }
728
0
    }
729
0
730
0
    return 0;
731
0
}
732
733
static int ehci_dbgp_startup(struct ehci_dbgp *dbgp)
734
0
{
735
0
    u32 ctrl, cmd, status;
736
0
    unsigned int loop;
737
0
738
0
    /* Claim ownership, but do not enable yet */
739
0
    ctrl = readl(&dbgp->ehci_debug->control);
740
0
    ctrl |= DBGP_OWNER;
741
0
    ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
742
0
    writel(ctrl, &dbgp->ehci_debug->control);
743
0
    udelay(1);
744
0
745
0
    ehci_dbgp_status(dbgp, "EHCI startup");
746
0
    /* Start the EHCI. */
747
0
    cmd = readl(&dbgp->ehci_regs->command);
748
0
    cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
749
0
    cmd |= CMD_RUN;
750
0
    writel(cmd, &dbgp->ehci_regs->command);
751
0
752
0
    /* Ensure everything is routed to the EHCI */
753
0
    writel(FLAG_CF, &dbgp->ehci_regs->configured_flag);
754
0
755
0
    /* Wait until the controller is no longer halted. */
756
0
    loop = 1000;
757
0
    do {
758
0
        status = readl(&dbgp->ehci_regs->status);
759
0
        if ( !(status & STS_HALT) )
760
0
            break;
761
0
        udelay(1);
762
0
    } while ( --loop );
763
0
764
0
    if ( !loop )
765
0
    {
766
0
        dbgp_printk("EHCI cannot be started\n");
767
0
        return -ENODEV;
768
0
    }
769
0
    dbgp_printk("EHCI started\n");
770
0
771
0
    return 0;
772
0
}
773
774
static int ehci_dbgp_controller_reset(struct ehci_dbgp *dbgp)
775
0
{
776
0
    unsigned int loop = 250 * 1000;
777
0
    u32 cmd;
778
0
779
0
    /* Reset the EHCI controller */
780
0
    cmd = readl(&dbgp->ehci_regs->command);
781
0
    cmd |= CMD_RESET;
782
0
    writel(cmd, &dbgp->ehci_regs->command);
783
0
    do {
784
0
        cmd = readl(&dbgp->ehci_regs->command);
785
0
    } while ( (cmd & CMD_RESET) && --loop );
786
0
787
0
    if ( !loop )
788
0
    {
789
0
        dbgp_printk("cannot reset EHCI\n");
790
0
        return -1;
791
0
    }
792
0
    ehci_dbgp_status(dbgp, "ehci reset done");
793
0
794
0
    return 0;
795
0
}
796
797
static int ehci_reset_port(struct ehci_dbgp *dbgp, unsigned int port)
798
0
{
799
0
    u32 portsc, delay_time, delay;
800
0
801
0
    ehci_dbgp_status(dbgp, "reset port");
802
0
    /* Reset the USB debug port. */
803
0
    portsc = readl(&dbgp->ehci_regs->port_status[port - 1]);
804
0
    portsc &= ~PORT_PE;
805
0
    portsc |= PORT_RESET;
806
0
    writel(portsc, &dbgp->ehci_regs->port_status[port - 1]);
807
0
808
0
    delay = HUB_ROOT_RESET_TIME;
809
0
    for ( delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
810
0
          delay_time += delay )
811
0
    {
812
0
        dbgp_mdelay(delay);
813
0
        portsc = readl(&dbgp->ehci_regs->port_status[port - 1]);
814
0
        if (!(portsc & PORT_RESET))
815
0
            break;
816
0
    }
817
0
818
0
    if ( portsc & PORT_RESET )
819
0
    {
820
0
        /* force reset to complete */
821
0
        unsigned int loop = 100 * 1000;
822
0
823
0
        writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
824
0
               &dbgp->ehci_regs->port_status[port - 1]);
825
0
        do {
826
0
            udelay(1);
827
0
            portsc = readl(&dbgp->ehci_regs->port_status[port-1]);
828
0
        } while ( (portsc & PORT_RESET) && --loop );
829
0
    }
830
0
831
0
    /* Device went away? */
832
0
    if ( !(portsc & PORT_CONNECT) )
833
0
        return -ENOTCONN;
834
0
835
0
    /* bomb out completely if something weird happened */
836
0
    if ( portsc & PORT_CSC )
837
0
        return -EINVAL;
838
0
839
0
    /* If we've finished resetting, then break out of the loop */
840
0
    if ( !(portsc & PORT_RESET) && (portsc & PORT_PE) )
841
0
        return 0;
842
0
843
0
    return -EBUSY;
844
0
}
845
846
static int ehci_wait_for_port(struct ehci_dbgp *dbgp, unsigned int port)
847
0
{
848
0
    u32 status;
849
0
    unsigned int reps;
850
0
851
0
    for ( reps = 0; reps < 300; reps++ )
852
0
    {
853
0
        status = readl(&dbgp->ehci_regs->status);
854
0
        if ( status & STS_PCD )
855
0
            break;
856
0
        dbgp_mdelay(1);
857
0
    }
858
0
859
0
    return ehci_reset_port(dbgp, port) == 0 ? 0 : -ENOTCONN;
860
0
}
861
862
/* Return 0 on success
863
 * Return -ENODEV for any general failure
864
 * Return -EIO if wait for port fails
865
 */
866
static int ehci_dbgp_external_startup(struct ehci_dbgp *dbgp)
867
0
{
868
0
    unsigned int devnum;
869
0
    struct usb_debug_descriptor dbgp_desc;
870
0
    int ret;
871
0
    u32 ctrl, portsc, cmd;
872
0
    unsigned int dbg_port = dbgp->phys_port;
873
0
    unsigned int tries = 3;
874
0
    unsigned int reset_port_tries = 1;
875
0
    bool_t try_hard_once = 1;
876
0
877
0
try_port_reset_again:
878
0
    ret = ehci_dbgp_startup(dbgp);
879
0
    if ( ret )
880
0
        return ret;
881
0
882
0
    /* Wait for a device to show up in the debug port */
883
0
    ret = ehci_wait_for_port(dbgp, dbg_port);
884
0
    if ( ret < 0 )
885
0
    {
886
0
        portsc = readl(&dbgp->ehci_regs->port_status[dbg_port - 1]);
887
0
        if ( !(portsc & PORT_CONNECT) && try_hard_once )
888
0
        {
889
0
            /*
890
0
             * Last ditch effort to try to force enable the debug device by
891
0
             * using the packet test EHCI command to try and wake it up.
892
0
             */
893
0
            try_hard_once = 0;
894
0
            cmd = readl(&dbgp->ehci_regs->command);
895
0
            cmd &= ~CMD_RUN;
896
0
            writel(cmd, &dbgp->ehci_regs->command);
897
0
            portsc = readl(&dbgp->ehci_regs->port_status[dbg_port - 1]);
898
0
            portsc |= PORT_TEST_PKT;
899
0
            writel(portsc, &dbgp->ehci_regs->port_status[dbg_port - 1]);
900
0
            ehci_dbgp_status(dbgp, "Trying to force debug port online");
901
0
            mdelay(50);
902
0
            ehci_dbgp_controller_reset(dbgp);
903
0
            goto try_port_reset_again;
904
0
        }
905
0
        else if ( reset_port_tries-- )
906
0
            goto try_port_reset_again;
907
0
        dbgp_printk("no device found in debug port\n");
908
0
        return -EIO;
909
0
    }
910
0
    ehci_dbgp_status(dbgp, "wait for port done");
911
0
912
0
    /* Enable the debug port */
913
0
    ctrl = readl(&dbgp->ehci_debug->control);
914
0
    ctrl |= DBGP_CLAIM;
915
0
    writel(ctrl, &dbgp->ehci_debug->control);
916
0
    ctrl = readl(&dbgp->ehci_debug->control);
917
0
    if ( (ctrl & DBGP_CLAIM) != DBGP_CLAIM )
918
0
    {
919
0
        dbgp_printk("no device in debug port\n");
920
0
        writel(ctrl & ~DBGP_CLAIM, &dbgp->ehci_debug->control);
921
0
        return -ENODEV;
922
0
    }
923
0
    ehci_dbgp_status(dbgp, "debug port enabled");
924
0
925
0
    /* Completely transfer the debug device to the debug controller */
926
0
    portsc = readl(&dbgp->ehci_regs->port_status[dbg_port - 1]);
927
0
    portsc &= ~PORT_PE;
928
0
    writel(portsc, &dbgp->ehci_regs->port_status[dbg_port - 1]);
929
0
930
0
    dbgp_mdelay(100);
931
0
932
0
try_again:
933
0
    /* Find the debug device and make it device number 127 */
934
0
    for ( devnum = 0; devnum <= 127; devnum++ )
935
0
    {
936
0
        ret = dbgp_control_msg(dbgp, devnum,
937
0
                               USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
938
0
                               USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
939
0
                               &dbgp_desc, sizeof(dbgp_desc));
940
0
        if ( ret > 0 )
941
0
            break;
942
0
    }
943
0
    if ( devnum > 127 )
944
0
    {
945
0
        dbgp_printk("could not find attached debug device\n");
946
0
        goto err;
947
0
    }
948
0
    dbgp->out.endpoint = dbgp_desc.bDebugOutEndpoint;
949
0
    dbgp->in.endpoint = dbgp_desc.bDebugInEndpoint;
950
0
951
0
    /* Move the device to 127 if it isn't already there. */
952
0
    if ( devnum != USB_DEBUG_DEVNUM )
953
0
    {
954
0
        ret = dbgp_control_msg(dbgp, devnum,
955
0
                               USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
956
0
                               USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
957
0
        if ( ret < 0 )
958
0
        {
959
0
            dbgp_printk("could not move attached device to %d\n",
960
0
                        USB_DEBUG_DEVNUM);
961
0
            goto err;
962
0
        }
963
0
        devnum = USB_DEBUG_DEVNUM;
964
0
        dbgp_printk("debug device renamed to 127\n");
965
0
    }
966
0
967
0
    /* Enable the debug interface */
968
0
    ret = dbgp_control_msg(dbgp, USB_DEBUG_DEVNUM,
969
0
                           USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
970
0
                           USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE,
971
0
                           0, NULL, 0);
972
0
    if ( ret < 0 )
973
0
    {
974
0
        dbgp_printk("could not enable the debug device\n");
975
0
        goto err;
976
0
    }
977
0
    dbgp_printk("debug interface enabled\n");
978
0
979
0
    /* Perform a small write to get the even/odd data state in sync. */
980
0
    ret = dbgp_bulk_write(dbgp, USB_DEBUG_DEVNUM, dbgp->out.endpoint,
981
0
                          "\n", 1, &ctrl);
982
0
    if ( !ret )
983
0
        ret = dbgp_wait_until_done(dbgp, ctrl, DBGP_LOOPS);
984
0
    if ( ret < 0 )
985
0
    {
986
0
        dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
987
0
        goto err;
988
0
    }
989
0
    dbgp_printk("small write done\n");
990
0
    dbgp->state = dbgp_idle;
991
0
992
0
    return 0;
993
0
err:
994
0
    if ( tries-- )
995
0
        goto try_again;
996
0
    return -ENODEV;
997
0
}
998
999
typedef void (*set_debug_port_t)(struct ehci_dbgp *, unsigned int);
1000
1001
static void default_set_debug_port(struct ehci_dbgp *dbgp, unsigned int port)
1002
0
{
1003
0
}
1004
1005
static set_debug_port_t __read_mostly set_debug_port = default_set_debug_port;
1006
1007
static void nvidia_set_debug_port(struct ehci_dbgp *dbgp, unsigned int port)
1008
0
{
1009
0
    u32 dword = pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, 0x74);
1010
0
1011
0
    dword &= ~(0x0f << 12);
1012
0
    dword |= (port & 0x0f) << 12;
1013
0
    pci_conf_write32(0, dbgp->bus, dbgp->slot, dbgp->func, 0x74, dword);
1014
0
    dbgp_printk("set debug port to %u\n", port);
1015
0
}
1016
1017
static void __init detect_set_debug_port(struct ehci_dbgp *dbgp)
1018
0
{
1019
0
    if ( pci_conf_read16(0, dbgp->bus, dbgp->slot, dbgp->func,
1020
0
                         PCI_VENDOR_ID) == 0x10de )
1021
0
    {
1022
0
        dbgp_printk("using nvidia set_debug_port\n");
1023
0
        set_debug_port = nvidia_set_debug_port;
1024
0
    }
1025
0
}
1026
1027
/*
1028
 * The code in ehci_dbgp_bios_handoff() is derived from the USB PCI
1029
 * quirk initialization in Linux.
1030
 */
1031
0
#define EHCI_USBLEGSUP_BIOS    (1 << 16) /* BIOS semaphore */
1032
0
#define EHCI_USBLEGCTLSTS      4        /* legacy control/status */
1033
static void ehci_dbgp_bios_handoff(struct ehci_dbgp *dbgp, u32 hcc_params)
1034
0
{
1035
0
    u32 cap;
1036
0
    unsigned int offset = HCC_EXT_CAPS(hcc_params);
1037
0
    int msec;
1038
0
1039
0
    if ( !offset )
1040
0
        return;
1041
0
1042
0
    cap = pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, offset);
1043
0
    dbgp_printk("dbgp: EHCI BIOS state %08x\n", cap);
1044
0
1045
0
    if ( (cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS) )
1046
0
    {
1047
0
        dbgp_printk("dbgp: BIOS handoff\n");
1048
0
        pci_conf_write8(0, dbgp->bus, dbgp->slot, dbgp->func, offset + 3, 1);
1049
0
    }
1050
0
1051
0
    /* if boot firmware now owns EHCI, spin till it hands it over. */
1052
0
    msec = 1000;
1053
0
    while ( (cap & EHCI_USBLEGSUP_BIOS) && (msec > 0) )
1054
0
    {
1055
0
        mdelay(10);
1056
0
        msec -= 10;
1057
0
        cap = pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func, offset);
1058
0
    }
1059
0
1060
0
    if ( cap & EHCI_USBLEGSUP_BIOS )
1061
0
    {
1062
0
        /* well, possibly buggy BIOS... try to shut it down,
1063
0
         * and hope nothing goes too wrong */
1064
0
        dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
1065
0
        pci_conf_write8(0, dbgp->bus, dbgp->slot, dbgp->func, offset + 2, 0);
1066
0
    }
1067
0
1068
0
    /* just in case, always disable EHCI SMIs */
1069
0
    pci_conf_write8(0, dbgp->bus, dbgp->slot, dbgp->func,
1070
0
                    offset + EHCI_USBLEGCTLSTS, 0);
1071
0
}
1072
1073
static int ehci_dbgp_setup(struct ehci_dbgp *dbgp)
1074
0
{
1075
0
    u32 ctrl, portsc, hcs_params;
1076
0
    unsigned int i, debug_port, new_debug_port = 0, n_ports;
1077
0
    unsigned int port_map_tried, playtimes = 3;
1078
0
    int ret;
1079
0
1080
0
    ehci_dbgp_bios_handoff(dbgp, readl(&dbgp->ehci_caps->hcc_params));
1081
0
1082
0
try_next_time:
1083
0
    port_map_tried = 0;
1084
0
1085
0
try_next_port:
1086
0
1087
0
    hcs_params = readl(&dbgp->ehci_caps->hcs_params);
1088
0
    debug_port = HCS_DEBUG_PORT(hcs_params);
1089
0
    dbgp->phys_port = debug_port;
1090
0
    n_ports = HCS_N_PORTS(hcs_params);
1091
0
1092
0
    dbgp_printk("debug_port: %u\n", debug_port);
1093
0
    dbgp_printk("n_ports:    %u\n", n_ports);
1094
0
    ehci_dbgp_status(dbgp, "");
1095
0
1096
0
    if ( n_ports == 0 )
1097
0
        return -1;
1098
0
1099
0
    for ( i = 1; i <= n_ports; i++ )
1100
0
    {
1101
0
        portsc = readl(&dbgp->ehci_regs->port_status[i-1]);
1102
0
        dbgp_printk("portstatus%d: %08x\n", i, portsc);
1103
0
    }
1104
0
1105
0
    if ( port_map_tried && (new_debug_port != debug_port) )
1106
0
    {
1107
0
        if ( --playtimes )
1108
0
        {
1109
0
            set_debug_port(dbgp, new_debug_port);
1110
0
            goto try_next_time;
1111
0
        }
1112
0
        return -1;
1113
0
    }
1114
0
1115
0
    /* Only reset the controller if it is not already in the
1116
0
     * configured state */
1117
0
    if ( readl(&dbgp->ehci_regs->configured_flag) & FLAG_CF )
1118
0
        ehci_dbgp_status(dbgp, "ehci skip - already configured");
1119
0
    else if ( ehci_dbgp_controller_reset(dbgp) != 0 )
1120
0
        return -1;
1121
0
1122
0
    ret = ehci_dbgp_external_startup(dbgp);
1123
0
    if (ret == -EIO)
1124
0
        goto next_debug_port;
1125
0
1126
0
    if ( ret < 0 )
1127
0
    {
1128
0
        /* Things didn't work so remove my claim */
1129
0
        ctrl = readl(&dbgp->ehci_debug->control);
1130
0
        ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
1131
0
        writel(ctrl, &dbgp->ehci_debug->control);
1132
0
        return -1;
1133
0
    }
1134
0
1135
0
    return 0;
1136
0
1137
0
next_debug_port:
1138
0
    port_map_tried |= 1 << (debug_port - 1);
1139
0
    new_debug_port = (debug_port % n_ports) + 1;
1140
0
    if ( port_map_tried != ((1 << n_ports) - 1) )
1141
0
    {
1142
0
        set_debug_port(dbgp, new_debug_port);
1143
0
        goto try_next_port;
1144
0
    }
1145
0
    if ( --playtimes )
1146
0
    {
1147
0
        set_debug_port(dbgp, new_debug_port);
1148
0
        goto try_next_time;
1149
0
    }
1150
0
1151
0
    return -1;
1152
0
}
1153
1154
static inline void _ehci_dbgp_flush(struct ehci_dbgp *dbgp)
1155
0
{
1156
0
    if ( dbgp_bulk_write(dbgp, USB_DEBUG_DEVNUM, dbgp->out.endpoint,
1157
0
                         dbgp->out.buf, dbgp->out.chunk, NULL) )
1158
0
        BUG();
1159
0
    dbgp->out.chunk = 0;
1160
0
}
1161
1162
static void ehci_dbgp_flush(struct serial_port *port)
1163
0
{
1164
0
    struct ehci_dbgp *dbgp = port->uart;
1165
0
    s_time_t goal;
1166
0
1167
0
    if ( !dbgp->out.chunk || !dbgp->ehci_debug || dbgp->state == dbgp_unsafe )
1168
0
        return;
1169
0
1170
0
    if ( dbgp->state == dbgp_idle || !port->sync )
1171
0
        dbgp_check_for_completion(dbgp, 1, NULL);
1172
0
    else
1173
0
        dbgp_wait_until_complete(dbgp, NULL);
1174
0
1175
0
    if ( dbgp->state == dbgp_idle )
1176
0
    {
1177
0
        _ehci_dbgp_flush(dbgp);
1178
0
1179
0
        if ( port->sync )
1180
0
        {
1181
0
            dbgp_wait_until_complete(dbgp, NULL);
1182
0
            return;
1183
0
        }
1184
0
    }
1185
0
1186
0
    goal = NOW() + MICROSECS(DBGP_CHECK_INTERVAL);
1187
0
    if ( dbgp->timer.expires > goal )
1188
0
       set_timer(&dbgp->timer, goal);
1189
0
}
1190
1191
static void ehci_dbgp_putc(struct serial_port *port, char c)
1192
0
{
1193
0
    struct ehci_dbgp *dbgp = port->uart;
1194
0
1195
0
    if ( unlikely(dbgp->out.chunk >= DBGP_MAX_PACKET) )
1196
0
        return;
1197
0
1198
0
    dbgp->out.buf[dbgp->out.chunk++] = c;
1199
0
1200
0
    if ( dbgp->out.chunk == DBGP_MAX_PACKET )
1201
0
        ehci_dbgp_flush(port);
1202
0
}
1203
1204
static int ehci_dbgp_tx_ready(struct serial_port *port)
1205
0
{
1206
0
    struct ehci_dbgp *dbgp = port->uart;
1207
0
1208
0
    if ( unlikely(!dbgp->ehci_debug) || unlikely(dbgp->state == dbgp_unsafe) )
1209
0
        return port->sync || port->tx_log_everything || !port->txbuf;
1210
0
1211
0
    if ( dbgp->out.chunk == DBGP_MAX_PACKET )
1212
0
        ehci_dbgp_flush(port);
1213
0
    else
1214
0
        dbgp_check_for_completion(dbgp, 1, NULL);
1215
0
1216
0
    if ( dbgp->state != dbgp_idle && dbgp->out.chunk >= DBGP_MAX_PACKET )
1217
0
        return 0;
1218
0
1219
0
    return DBGP_MAX_PACKET - dbgp->out.chunk +
1220
0
           (dbgp->state == dbgp_idle) * DBGP_MAX_PACKET;
1221
0
}
1222
1223
static int ehci_dbgp_getc(struct serial_port *port, char *pc)
1224
0
{
1225
0
    struct ehci_dbgp *dbgp = port->uart;
1226
0
1227
0
    if ( !dbgp->in.chunk )
1228
0
        return 0;
1229
0
1230
0
    *pc = *dbgp->in.buf;
1231
0
    if ( --dbgp->in.chunk )
1232
0
        memmove(dbgp->in.buf, dbgp->in.buf + 1, dbgp->in.chunk);
1233
0
1234
0
    return 1;
1235
0
}
1236
1237
/* Safe: ehci_dbgp_poll() runs as timer handler, so not reentrant. */
1238
static struct serial_port *poll_port;
1239
1240
static void _ehci_dbgp_poll(struct cpu_user_regs *regs)
1241
0
{
1242
0
    struct serial_port *port = poll_port;
1243
0
    struct ehci_dbgp *dbgp = port->uart;
1244
0
    unsigned long flags;
1245
0
    unsigned int timeout = MICROSECS(DBGP_CHECK_INTERVAL);
1246
0
    bool_t empty = 0;
1247
0
1248
0
    if ( !dbgp->ehci_debug )
1249
0
        return;
1250
0
1251
0
    if ( spin_trylock_irqsave(&port->tx_lock, flags) )
1252
0
    {
1253
0
        if ( dbgp->state != dbgp_unsafe )
1254
0
            dbgp_check_for_completion(dbgp, DBGP_CHECK_INTERVAL, NULL);
1255
0
        if ( dbgp->state == dbgp_idle && dbgp->out.chunk )
1256
0
            _ehci_dbgp_flush(dbgp);
1257
0
        if ( dbgp->state == dbgp_idle || dbgp->out.chunk < DBGP_MAX_PACKET )
1258
0
            empty = 1;
1259
0
        spin_unlock_irqrestore(&port->tx_lock, flags);
1260
0
    }
1261
0
1262
0
    if ( dbgp->in.chunk )
1263
0
        serial_rx_interrupt(port, regs);
1264
0
1265
0
    if ( empty )
1266
0
        serial_tx_interrupt(port, regs);
1267
0
1268
0
    if ( spin_trylock_irqsave(&port->tx_lock, flags) )
1269
0
    {
1270
0
        if ( dbgp->state == dbgp_idle && !dbgp->in.chunk &&
1271
0
             !dbgp->out.chunk && port->txbufp == port->txbufc )
1272
0
        {
1273
0
            if ( dbgp_bulk_read(dbgp, USB_DEBUG_DEVNUM, dbgp->in.endpoint,
1274
0
                                DBGP_MAX_PACKET, NULL) )
1275
0
                BUG();
1276
0
            timeout = MILLISECS(DBGP_IDLE_INTERVAL);
1277
0
        }
1278
0
        spin_unlock_irqrestore(&port->tx_lock, flags);
1279
0
    }
1280
0
1281
0
    set_timer(&dbgp->timer, NOW() + timeout);
1282
0
}
1283
1284
static void ehci_dbgp_poll(void *data)
1285
0
{
1286
0
    poll_port = data;
1287
0
#ifdef run_in_exception_handler
1288
0
    run_in_exception_handler(_ehci_dbgp_poll);
1289
0
#else
1290
    _ehci_dbgp_poll(guest_cpu_user_regs());
1291
#endif
1292
0
}
1293
1294
static bool_t ehci_dbgp_setup_preirq(struct ehci_dbgp *dbgp)
1295
0
{
1296
0
    if ( !ehci_dbgp_setup(dbgp) )
1297
0
        return 1;
1298
0
1299
0
    dbgp_printk("ehci_dbgp_setup failed\n");
1300
0
    dbgp->ehci_debug = NULL;
1301
0
    return 0;
1302
0
}
1303
1304
static void __init ehci_dbgp_init_preirq(struct serial_port *port)
1305
0
{
1306
0
    struct ehci_dbgp *dbgp = port->uart;
1307
0
    u32 debug_port, offset;
1308
0
    void __iomem *ehci_bar;
1309
0
1310
0
    debug_port = pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func,
1311
0
                                 dbgp->cap);
1312
0
    offset = (debug_port >> 16) & 0xfff;
1313
0
1314
0
    /* double check if the mem space is enabled */
1315
0
    dbgp->pci_cr = pci_conf_read8(0, dbgp->bus, dbgp->slot, dbgp->func,
1316
0
                                  PCI_COMMAND);
1317
0
    if ( !(dbgp->pci_cr & PCI_COMMAND_MEMORY) )
1318
0
    {
1319
0
        dbgp->pci_cr |= PCI_COMMAND_MEMORY;
1320
0
        pci_conf_write16(0, dbgp->bus, dbgp->slot, dbgp->func, PCI_COMMAND,
1321
0
                         dbgp->pci_cr);
1322
0
        dbgp_printk("MMIO for EHCI enabled\n");
1323
0
    }
1324
0
1325
0
    /*
1326
0
     * FIXME I don't have the bar size so just guess PAGE_SIZE is more
1327
0
     * than enough.  1k is the biggest that was seen.
1328
0
     */
1329
0
    set_fixmap_nocache(FIX_EHCI_DBGP, dbgp->bar_val);
1330
0
    ehci_bar = (void __iomem *)fix_to_virt(FIX_EHCI_DBGP);
1331
0
    ehci_bar += dbgp->bar_val & ~PAGE_MASK;
1332
0
    dbgp_printk("ehci_bar: %p\n", ehci_bar);
1333
0
1334
0
    dbgp->ehci_caps = ehci_bar;
1335
0
    dbgp->ehci_regs = ehci_bar +
1336
0
                      HC_LENGTH(readl(&dbgp->ehci_caps->hc_capbase));
1337
0
    dbgp->ehci_debug = ehci_bar + offset;
1338
0
1339
0
    detect_set_debug_port(dbgp);
1340
0
1341
0
    if ( ehci_dbgp_setup_preirq(dbgp) )
1342
0
        ehci_dbgp_status(dbgp, "ehci_dbgp_init_preirq complete");
1343
0
1344
0
    dbgp->lock = &port->tx_lock;
1345
0
}
1346
1347
static void ehci_dbgp_setup_postirq(struct ehci_dbgp *dbgp)
1348
0
{
1349
0
    set_timer(&dbgp->timer, NOW() + MILLISECS(1));
1350
0
}
1351
1352
static void __init ehci_dbgp_init_postirq(struct serial_port *port)
1353
0
{
1354
0
    struct ehci_dbgp *dbgp = port->uart;
1355
0
1356
0
    if ( !dbgp->ehci_debug )
1357
0
        return;
1358
0
1359
0
    serial_async_transmit(port);
1360
0
1361
0
    init_timer(&dbgp->timer, ehci_dbgp_poll, port, 0);
1362
0
1363
0
    ehci_dbgp_setup_postirq(dbgp);
1364
0
1365
0
    pci_hide_device(dbgp->bus, PCI_DEVFN(dbgp->slot, dbgp->func));
1366
0
}
1367
1368
static int ehci_dbgp_check_release(struct ehci_dbgp *dbgp)
1369
0
{
1370
0
    struct ehci_dbg_port __iomem *ehci_debug = dbgp->ehci_debug;
1371
0
    u32 ctrl;
1372
0
    unsigned int i;
1373
0
1374
0
    if ( !ehci_debug )
1375
0
        return 0;
1376
0
1377
0
    for ( i = 0; i < DBGP_MAX_PACKET; ++i )
1378
0
        if ( dbgp->out.buf[i] )
1379
0
            return 1;
1380
0
1381
0
    /*
1382
0
     * This means the console is not initialized, or should get shutdown
1383
0
     * so as to allow for reuse of the USB device, which means it is time
1384
0
     * to shutdown the USB debug port.
1385
0
     */
1386
0
    printk(XENLOG_INFO "Releasing EHCI debug port at %02x:%02x.%u\n",
1387
0
           dbgp->bus, dbgp->slot, dbgp->func);
1388
0
1389
0
    if ( dbgp->timer.function )
1390
0
        kill_timer(&dbgp->timer);
1391
0
    dbgp->ehci_debug = NULL;
1392
0
1393
0
    ctrl = readl(&ehci_debug->control);
1394
0
    if ( ctrl & DBGP_ENABLED )
1395
0
    {
1396
0
        ctrl &= ~DBGP_CLAIM;
1397
0
        writel(ctrl, &ehci_debug->control);
1398
0
    }
1399
0
1400
0
    return 0;
1401
0
}
1402
1403
static void __init ehci_dbgp_endboot(struct serial_port *port)
1404
0
{
1405
0
    ehci_dbgp_check_release(port->uart);
1406
0
}
1407
1408
static void ehci_dbgp_suspend(struct serial_port *port)
1409
0
{
1410
0
    struct ehci_dbgp *dbgp = port->uart;
1411
0
1412
0
    if ( !dbgp->ehci_debug )
1413
0
        return;
1414
0
1415
0
    stop_timer(&dbgp->timer);
1416
0
    dbgp->timer.expires = 0;
1417
0
1418
0
    dbgp->pci_cr = pci_conf_read16(0, dbgp->bus, dbgp->slot, dbgp->func,
1419
0
                                   PCI_COMMAND);
1420
0
1421
0
    dbgp->state = dbgp_unsafe;
1422
0
}
1423
1424
static void ehci_dbgp_resume(struct serial_port *port)
1425
0
{
1426
0
    struct ehci_dbgp *dbgp = port->uart;
1427
0
1428
0
    if ( !dbgp->ehci_debug )
1429
0
        return;
1430
0
1431
0
    pci_conf_write32(0, dbgp->bus, dbgp->slot, dbgp->func, dbgp->bar,
1432
0
                     dbgp->bar_val);
1433
0
    pci_conf_write16(0, dbgp->bus, dbgp->slot, dbgp->func,
1434
0
                     PCI_COMMAND, dbgp->pci_cr);
1435
0
1436
0
    ehci_dbgp_setup_preirq(dbgp);
1437
0
    ehci_dbgp_setup_postirq(dbgp);
1438
0
}
1439
1440
static struct uart_driver __read_mostly ehci_dbgp_driver = {
1441
    .init_preirq  = ehci_dbgp_init_preirq,
1442
    .init_postirq = ehci_dbgp_init_postirq,
1443
    .endboot      = ehci_dbgp_endboot,
1444
    .suspend      = ehci_dbgp_suspend,
1445
    .resume       = ehci_dbgp_resume,
1446
    .tx_ready     = ehci_dbgp_tx_ready,
1447
    .putc         = ehci_dbgp_putc,
1448
    .flush        = ehci_dbgp_flush,
1449
    .getc         = ehci_dbgp_getc
1450
};
1451
1452
static struct ehci_dbgp ehci_dbgp = { .state = dbgp_unsafe, .phys_port = 1 };
1453
1454
static char __initdata opt_dbgp[30];
1455
string_param("dbgp", opt_dbgp);
1456
1457
void __init ehci_dbgp_init(void)
1458
1
{
1459
1
    struct ehci_dbgp *dbgp = &ehci_dbgp;
1460
1
    u32 debug_port, offset, bar_val;
1461
1
    const char *e;
1462
1
1463
1
    if ( strncmp(opt_dbgp, "ehci", 4) )
1464
1
        return;
1465
1
1466
0
    if ( isdigit(opt_dbgp[4]) || !opt_dbgp[4] )
1467
0
    {
1468
0
        unsigned int num = 0;
1469
0
1470
0
        if ( opt_dbgp[4] )
1471
0
            simple_strtoul(opt_dbgp + 4, &e, 10);
1472
0
1473
0
        dbgp->cap = find_dbgp(dbgp, num);
1474
0
        if ( !dbgp->cap )
1475
0
            return;
1476
0
1477
0
        dbgp_printk("Found EHCI debug port on %02x:%02x.%u\n",
1478
0
                    dbgp->bus, dbgp->slot, dbgp->func);
1479
0
    }
1480
0
    else if ( strncmp(opt_dbgp + 4, "@pci", 4) == 0 )
1481
0
    {
1482
0
        unsigned int bus, slot, func;
1483
0
1484
0
        e = parse_pci(opt_dbgp + 8, NULL, &bus, &slot, &func);
1485
0
        if ( !e || *e )
1486
0
            return;
1487
0
1488
0
        dbgp->bus = bus;
1489
0
        dbgp->slot = slot;
1490
0
        dbgp->func = func;
1491
0
1492
0
        if ( !pci_device_detect(0, bus, slot, func) )
1493
0
            return;
1494
0
1495
0
        dbgp->cap = __find_dbgp(bus, slot, func);
1496
0
        if ( !dbgp->cap )
1497
0
            return;
1498
0
1499
0
        dbgp_printk("Using EHCI debug port on %02x:%02x.%u\n",
1500
0
                    bus, slot, func);
1501
0
    }
1502
0
    else
1503
0
        return;
1504
0
1505
0
    debug_port = pci_conf_read32(0, dbgp->bus, dbgp->slot, dbgp->func,
1506
0
                                 dbgp->cap);
1507
0
    dbgp->bar = (debug_port >> 29) & 0x7;
1508
0
    dbgp->bar = ((dbgp->bar - 1) * 4) + PCI_BASE_ADDRESS_0;
1509
0
    offset = (debug_port >> 16) & 0xfff;
1510
0
    dbgp_printk("bar: %02x offset: %03x\n", dbgp->bar, offset);
1511
0
    if ( dbgp->bar < PCI_BASE_ADDRESS_0 || dbgp->bar > PCI_BASE_ADDRESS_5 )
1512
0
    {
1513
0
        dbgp_printk("unsupported/invalid bar\n");
1514
0
        return;
1515
0
    }
1516
0
1517
0
    dbgp->bar_val = bar_val = pci_conf_read32(0, dbgp->bus, dbgp->slot,
1518
0
                                              dbgp->func, dbgp->bar);
1519
0
    dbgp_printk("bar_val: %08x\n", bar_val);
1520
0
    if ( bar_val & ~PCI_BASE_ADDRESS_MEM_MASK )
1521
0
    {
1522
0
        dbgp_printk("only simple 32-bit MMIO BARs supported\n");
1523
0
        return;
1524
0
    }
1525
0
    bar_val &= PCI_BASE_ADDRESS_MEM_MASK;
1526
0
    if ( !bar_val || !(bar_val + (bar_val & -bar_val)) )
1527
0
    {
1528
0
        dbgp_printk("firmware initialization of MMIO BAR required\n");
1529
0
        return;
1530
0
    }
1531
0
1532
0
    serial_register_uart(SERHND_DBGP, &ehci_dbgp_driver, dbgp);
1533
0
}
1534
1535
int dbgp_op(const struct physdev_dbgp_op *op)
1536
0
{
1537
0
    if ( !ehci_dbgp.ehci_debug )
1538
0
        return 0;
1539
0
1540
0
    switch ( op->bus )
1541
0
    {
1542
0
    case PHYSDEVOP_DBGP_BUS_UNKNOWN:
1543
0
        break;
1544
0
    case PHYSDEVOP_DBGP_BUS_PCI:
1545
0
        if ( op->u.pci.seg || ehci_dbgp.bus != op->u.pci.bus ||
1546
0
            PCI_DEVFN(ehci_dbgp.slot, ehci_dbgp.func) != op->u.pci.devfn )
1547
0
    default:
1548
0
            return 0;
1549
0
        break;
1550
0
    }
1551
0
1552
0
    switch ( op->op )
1553
0
    {
1554
0
    case PHYSDEVOP_DBGP_RESET_PREPARE:
1555
0
        spin_lock_irq(ehci_dbgp.lock);
1556
0
        ehci_dbgp.state = dbgp_unsafe;
1557
0
        dbgp_wait_until_complete(&ehci_dbgp, NULL);
1558
0
        spin_unlock_irq(ehci_dbgp.lock);
1559
0
1560
0
        return ehci_dbgp_check_release(&ehci_dbgp);
1561
0
1562
0
    case PHYSDEVOP_DBGP_RESET_DONE:
1563
0
        return ehci_dbgp_external_startup(&ehci_dbgp) ?: 1;
1564
0
    }
1565
0
1566
0
    return -ENOSYS;
1567
0
}