cvmx-usb.c revision 215990
1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Networks (support@cavium.com). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17
18 *   * Neither the name of Cavium Networks nor the names of
19 *     its contributors may be used to endorse or promote products
20 *     derived from this software without specific prior written
21 *     permission.
22
23 * This Software, including technical data, may be subject to U.S. export  control
24 * laws, including the U.S. Export Administration Act and its  associated
25 * regulations, and may be subject to export or import  regulations in other
26 * countries.
27
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM  NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40
41/**
42 * @file
43 *
44 * "cvmx-usb.c" defines a set of low level USB functions to help
45 * developers create Octeon USB drivers for various operating
46 * systems. These functions provide a generic API to the Octeon
47 * USB blocks, hiding the internal hardware specific
48 * operations.
49 *
50 * <hr>$Revision: 32636 $<hr>
51 */
52#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
53#include <asm/octeon/cvmx.h>
54#include <asm/octeon/cvmx-clock.h>
55#include <asm/octeon/cvmx-sysinfo.h>
56#include <asm/octeon/cvmx-usbnx-defs.h>
57#include <asm/octeon/cvmx-usbcx-defs.h>
58#include <asm/octeon/cvmx-usb.h>
59#include <asm/octeon/cvmx-helper.h>
60#include <asm/octeon/cvmx-helper-board.h>
61#include <asm/octeon/cvmx-swap.h>
62#if 0
63   /* Do not use cvmx-error.h for now. When the cvmx-error.h is properly
64    * ported, remove the above #if 0, and all #ifdef __CVMX_ERROR_H__ within
65    * this file */
66   #include <asm/octeon/cvmx-error.h>
67#endif
68#else
69#include "cvmx.h"
70#include "cvmx-clock.h"
71#include "cvmx-sysinfo.h"
72#include "cvmx-usb.h"
73#include "cvmx-helper.h"
74#include "cvmx-helper-board.h"
75#include "cvmx-csr-db.h"
76#include "cvmx-swap.h"
77#include "cvmx-error.h"
78#endif
79
80#define MAX_RETRIES         3   /* Maximum number of times to retry failed transactions */
81#define MAX_PIPES           32  /* Maximum number of pipes that can be open at once */
82#define MAX_TRANSACTIONS    256 /* Maximum number of outstanding transactions across all pipes */
83#define MAX_CHANNELS        8   /* Maximum number of hardware channels supported by the USB block */
84#define MAX_USB_ADDRESS     127 /* The highest valid USB device address */
85#define MAX_USB_ENDPOINT    15  /* The highest valid USB endpoint number */
86#define MAX_USB_HUB_PORT    15  /* The highest valid port number on a hub */
87#define MAX_TRANSFER_BYTES  ((1<<19)-1) /* The low level hardware can transfer a maximum of this number of bytes in each transfer. The field is 19 bits wide */
88#define MAX_TRANSFER_PACKETS ((1<<10)-1) /* The low level hardware can transfer a maximum of this number of packets in each transfer. The field is 10 bits wide */
89#define ALLOW_CSR_DECODES   0   /* CSR decoding when CVMX_USB_INITIALIZE_FLAGS_DEBUG_CSRS is set
90                                    enlarges the code a lot. This define overrides the ability to do CSR
91                                    decoding since it isn't necessary 99% of the time. Change this to a
92                                    one if you need CSR decoding */
93
94/* These defines disable the normal read and write csr. This is so I can add
95    extra debug stuff to the usb specific version and I won't use the normal
96    version by mistake */
97#define cvmx_read_csr use_cvmx_usb_read_csr64_instead_of_cvmx_read_csr
98#define cvmx_write_csr use_cvmx_usb_write_csr64_instead_of_cvmx_write_csr
99
100typedef enum
101{
102    __CVMX_USB_TRANSACTION_FLAGS_IN_USE = 1<<16,
103} cvmx_usb_transaction_flags_t;
104
105/**
106 * Logical transactions may take numerous low level
107 * transactions, especially when splits are concerned. This
108 * enum represents all of the possible stages a transaction can
109 * be in. Note that split completes are always even. This is so
110 * the NAK handler can backup to the previous low level
111 * transaction with a simple clearing of bit 0.
112 */
113typedef enum
114{
115    CVMX_USB_STAGE_NON_CONTROL,
116    CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
117    CVMX_USB_STAGE_SETUP,
118    CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
119    CVMX_USB_STAGE_DATA,
120    CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
121    CVMX_USB_STAGE_STATUS,
122    CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
123} cvmx_usb_stage_t;
124
125/**
126 * This structure describes each pending USB transaction
127 * regardless of type. These are linked together to form a list
128 * of pending requests for a pipe.
129 */
130typedef struct cvmx_usb_transaction
131{
132    struct cvmx_usb_transaction *prev;  /**< Transaction before this one in the pipe */
133    struct cvmx_usb_transaction *next;  /**< Transaction after this one in the pipe */
134    cvmx_usb_transfer_t type;           /**< Type of transaction, duplicated of the pipe */
135    cvmx_usb_transaction_flags_t flags; /**< State flags for this transaction */
136    uint64_t buffer;                    /**< User's physical buffer address to read/write */
137    int buffer_length;                  /**< Size of the user's buffer in bytes */
138    uint64_t control_header;            /**< For control transactions, physical address of the 8 byte standard header */
139    int iso_start_frame;                /**< For ISO transactions, the starting frame number */
140    int iso_number_packets;             /**< For ISO transactions, the number of packets in the request */
141    cvmx_usb_iso_packet_t *iso_packets; /**< For ISO transactions, the sub packets in the request */
142    int xfersize;
143    int pktcnt;
144    int retries;
145    int actual_bytes;                   /**< Actual bytes transfer for this transaction */
146    cvmx_usb_stage_t stage;             /**< For control transactions, the current stage */
147    cvmx_usb_callback_func_t callback;  /**< User's callback function when complete */
148    void *callback_data;                /**< User's data */
149} cvmx_usb_transaction_t;
150
151/**
152 * A pipe represents a virtual connection between Octeon and some
153 * USB device. It contains a list of pending request to the device.
154 */
155typedef struct cvmx_usb_pipe
156{
157    struct cvmx_usb_pipe *prev;         /**< Pipe before this one in the list */
158    struct cvmx_usb_pipe *next;         /**< Pipe after this one in the list */
159    cvmx_usb_transaction_t *head;       /**< The first pending transaction */
160    cvmx_usb_transaction_t *tail;       /**< The last pending transaction */
161    uint64_t interval;                  /**< For periodic pipes, the interval between packets in frames */
162    uint64_t next_tx_frame;             /**< The next frame this pipe is allowed to transmit on */
163    cvmx_usb_pipe_flags_t flags;        /**< State flags for this pipe */
164    cvmx_usb_speed_t device_speed;      /**< Speed of device connected to this pipe */
165    cvmx_usb_transfer_t transfer_type;  /**< Type of transaction supported by this pipe */
166    cvmx_usb_direction_t transfer_dir;  /**< IN or OUT. Ignored for Control */
167    int multi_count;                    /**< Max packet in a row for the device */
168    uint16_t max_packet;                /**< The device's maximum packet size in bytes */
169    uint8_t device_addr;                /**< USB device address at other end of pipe */
170    uint8_t endpoint_num;               /**< USB endpoint number at other end of pipe */
171    uint8_t hub_device_addr;            /**< Hub address this device is connected to */
172    uint8_t hub_port;                   /**< Hub port this device is connected to */
173    uint8_t pid_toggle;                 /**< This toggles between 0/1 on every packet send to track the data pid needed */
174    uint8_t channel;                    /**< Hardware DMA channel for this pipe */
175    int8_t  split_sc_frame;             /**< The low order bits of the frame number the split complete should be sent on */
176} cvmx_usb_pipe_t;
177
178typedef struct
179{
180    cvmx_usb_pipe_t *head;              /**< Head of the list, or NULL if empty */
181    cvmx_usb_pipe_t *tail;              /**< Tail if the list, or NULL if empty */
182} cvmx_usb_pipe_list_t;
183
184typedef struct
185{
186    struct
187    {
188        int channel;
189        int size;
190        uint64_t address;
191    } entry[MAX_CHANNELS+1];
192    int head;
193    int tail;
194} cvmx_usb_tx_fifo_t;
195
196/**
197 * The state of the USB block is stored in this structure
198 */
199typedef struct
200{
201    int init_flags;                     /**< Flags passed to initialize */
202    int index;                          /**< Which USB block this is for */
203    int idle_hardware_channels;         /**< Bit set for every idle hardware channel */
204    cvmx_usbcx_hprt_t usbcx_hprt;       /**< Stored port status so we don't need to read a CSR to determine splits */
205    cvmx_usb_pipe_t *pipe_for_channel[MAX_CHANNELS];    /**< Map channels to pipes */
206    cvmx_usb_transaction_t *free_transaction_head;      /**< List of free transactions head */
207    cvmx_usb_transaction_t *free_transaction_tail;      /**< List of free transactions tail */
208    cvmx_usb_pipe_t pipe[MAX_PIPES];                    /**< Storage for pipes */
209    cvmx_usb_transaction_t transaction[MAX_TRANSACTIONS];       /**< Storage for transactions */
210    cvmx_usb_callback_func_t callback[__CVMX_USB_CALLBACK_END]; /**< User global callbacks */
211    void *callback_data[__CVMX_USB_CALLBACK_END];               /**< User data for each callback */
212    int indent;                         /**< Used by debug output to indent functions */
213    cvmx_usb_port_status_t port_status; /**< Last port status used for change notification */
214    cvmx_usb_pipe_list_t free_pipes;    /**< List of all pipes that are currently closed */
215    cvmx_usb_pipe_list_t idle_pipes;    /**< List of open pipes that have no transactions */
216    cvmx_usb_pipe_list_t active_pipes[4]; /**< Active pipes indexed by transfer type */
217    uint64_t frame_number;              /**< Increments every SOF interrupt for time keeping */
218    cvmx_usb_transaction_t *active_split; /**< Points to the current active split, or NULL */
219    cvmx_usb_tx_fifo_t periodic;
220    cvmx_usb_tx_fifo_t nonperiodic;
221} cvmx_usb_internal_state_t;
222
223/* This macro logs out whenever a function is called if debugging is on */
224#define CVMX_USB_LOG_CALLED() \
225    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CALLS)) \
226        cvmx_dprintf("%*s%s: called\n", 2*usb->indent++, "", __FUNCTION__);
227
228/* This macro logs out each function parameter if debugging is on */
229#define CVMX_USB_LOG_PARAM(format, param) \
230    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CALLS)) \
231        cvmx_dprintf("%*s%s: param %s = " format "\n", 2*usb->indent, "", __FUNCTION__, #param, param);
232
233/* This macro logs out when a function returns a value */
234#define CVMX_USB_RETURN(v)                                              \
235    do {                                                                \
236        __typeof(v) r = v;                                              \
237        if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CALLS))    \
238            cvmx_dprintf("%*s%s: returned %s(%d)\n", 2*--usb->indent, "", __FUNCTION__, #v, r); \
239        return r;                                                       \
240    } while (0);
241
242/* This macro logs out when a function doesn't return a value */
243#define CVMX_USB_RETURN_NOTHING()                                       \
244    do {                                                                \
245        if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CALLS))    \
246            cvmx_dprintf("%*s%s: returned\n", 2*--usb->indent, "", __FUNCTION__); \
247        return;                                                         \
248    } while (0);
249
250/* This macro spins on a field waiting for it to reach a value */
251#define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
252    ({int result;                                                       \
253    do {                                                                \
254        uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec *     \
255                           cvmx_clock_get_rate(CVMX_CLOCK_CORE) / 1000000;  \
256        type c;                                                         \
257        while (1)                                                       \
258        {                                                               \
259            c.u32 = __cvmx_usb_read_csr32(usb, address);                \
260            if (c.s.field op (value)) {                                 \
261                result = 0;                                             \
262                break;                                                  \
263            } else if (cvmx_get_cycle() > done) {                       \
264                result = -1;                                            \
265                break;                                                  \
266            } else                                                      \
267                cvmx_wait(100);                                         \
268        }                                                               \
269    } while (0);                                                        \
270    result;})
271
272/* This macro logically sets a single field in a CSR. It does the sequence
273    read, modify, and write */
274#define USB_SET_FIELD32(address, type, field, value)\
275    do {                                            \
276        type c;                                     \
277        c.u32 = __cvmx_usb_read_csr32(usb, address);\
278        c.s.field = value;                          \
279        __cvmx_usb_write_csr32(usb, address, c.u32);\
280    } while (0)
281
282/* Returns the IO address to push/pop stuff data from the FIFOs */
283#define USB_FIFO_ADDRESS(channel, usb_index) (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
284
285/**
286 * @INTERNAL
287 * Read a USB 32bit CSR. It performs the necessary address swizzle
288 * for 32bit CSRs and logs the value in a readable format if
289 * debugging is on.
290 *
291 * @param usb     USB block this access is for
292 * @param address 64bit address to read
293 *
294 * @return Result of the read
295 */
296static inline uint32_t __cvmx_usb_read_csr32(cvmx_usb_internal_state_t *usb,
297                                             uint64_t address)
298{
299    uint32_t result = cvmx_read64_uint32(address ^ 4);
300#if ALLOW_CSR_DECODES
301    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CSRS))
302    {
303        cvmx_dprintf("Read: ");
304        cvmx_csr_db_decode(cvmx_get_proc_id(), address, result);
305    }
306#endif
307    return result;
308}
309
310
311/**
312 * @INTERNAL
313 * Write a USB 32bit CSR. It performs the necessary address
314 * swizzle for 32bit CSRs and logs the value in a readable format
315 * if debugging is on.
316 *
317 * @param usb     USB block this access is for
318 * @param address 64bit address to write
319 * @param value   Value to write
320 */
321static inline void __cvmx_usb_write_csr32(cvmx_usb_internal_state_t *usb,
322                                          uint64_t address, uint32_t value)
323{
324#if ALLOW_CSR_DECODES
325    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CSRS))
326    {
327        cvmx_dprintf("Write: ");
328        cvmx_csr_db_decode(cvmx_get_proc_id(), address, value);
329    }
330#endif
331    cvmx_write64_uint32(address ^ 4, value);
332    cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
333}
334
335
336/**
337 * @INTERNAL
338 * Read a USB 64bit CSR. It logs the value in a readable format if
339 * debugging is on.
340 *
341 * @param usb     USB block this access is for
342 * @param address 64bit address to read
343 *
344 * @return Result of the read
345 */
346static inline uint64_t __cvmx_usb_read_csr64(cvmx_usb_internal_state_t *usb,
347                                             uint64_t address)
348{
349    uint64_t result = cvmx_read64_uint64(address);
350#if ALLOW_CSR_DECODES
351    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CSRS))
352    {
353        cvmx_dprintf("Read: ");
354        cvmx_csr_db_decode(cvmx_get_proc_id(), address, result);
355    }
356#endif
357    return result;
358}
359
360
361/**
362 * @INTERNAL
363 * Write a USB 64bit CSR. It logs the value in a readable format
364 * if debugging is on.
365 *
366 * @param usb     USB block this access is for
367 * @param address 64bit address to write
368 * @param value   Value to write
369 */
370static inline void __cvmx_usb_write_csr64(cvmx_usb_internal_state_t *usb,
371                                          uint64_t address, uint64_t value)
372{
373#if ALLOW_CSR_DECODES
374    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CSRS))
375    {
376        cvmx_dprintf("Write: ");
377        cvmx_csr_db_decode(cvmx_get_proc_id(), address, value);
378    }
379#endif
380    cvmx_write64_uint64(address, value);
381}
382
383
384/**
385 * @INTERNAL
386 * Utility function to convert complete codes into strings
387 *
388 * @param complete_code
389 *               Code to convert
390 *
391 * @return Human readable string
392 */
393static const char *__cvmx_usb_complete_to_string(cvmx_usb_complete_t complete_code)
394{
395    switch (complete_code)
396    {
397        case CVMX_USB_COMPLETE_SUCCESS: return "SUCCESS";
398        case CVMX_USB_COMPLETE_SHORT:   return "SHORT";
399        case CVMX_USB_COMPLETE_CANCEL:  return "CANCEL";
400        case CVMX_USB_COMPLETE_ERROR:   return "ERROR";
401        case CVMX_USB_COMPLETE_STALL:   return "STALL";
402        case CVMX_USB_COMPLETE_XACTERR: return "XACTERR";
403        case CVMX_USB_COMPLETE_DATATGLERR: return "DATATGLERR";
404        case CVMX_USB_COMPLETE_BABBLEERR: return "BABBLEERR";
405        case CVMX_USB_COMPLETE_FRAMEERR: return "FRAMEERR";
406    }
407    return "Update __cvmx_usb_complete_to_string";
408}
409
410
411/**
412 * @INTERNAL
413 * Return non zero if this pipe connects to a non HIGH speed
414 * device through a high speed hub.
415 *
416 * @param usb    USB block this access is for
417 * @param pipe   Pipe to check
418 *
419 * @return Non zero if we need to do split transactions
420 */
421static inline int __cvmx_usb_pipe_needs_split(cvmx_usb_internal_state_t *usb, cvmx_usb_pipe_t *pipe)
422{
423    return ((pipe->device_speed != CVMX_USB_SPEED_HIGH) && (usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH));
424}
425
426
427/**
428 * @INTERNAL
429 * Trivial utility function to return the correct PID for a pipe
430 *
431 * @param pipe   pipe to check
432 *
433 * @return PID for pipe
434 */
435static inline int __cvmx_usb_get_data_pid(cvmx_usb_pipe_t *pipe)
436{
437    if (pipe->pid_toggle)
438        return 2; /* Data1 */
439    else
440        return 0; /* Data0 */
441}
442
443
444/**
445 * Return the number of USB ports supported by this Octeon
446 * chip. If the chip doesn't support USB, or is not supported
447 * by this API, a zero will be returned. Most Octeon chips
448 * support one usb port, but some support two ports.
449 * cvmx_usb_initialize() must be called on independent
450 * cvmx_usb_state_t structures.
451 *
452 * This utilizes cvmx_helper_board_usb_get_num_ports()
453 * to get any board specific variations.
454 *
455 * @return Number of port, zero if usb isn't supported
456 */
457int cvmx_usb_get_num_ports(void)
458{
459    int arch_ports = 0;
460
461    if (OCTEON_IS_MODEL(OCTEON_CN56XX))
462        arch_ports = 1;
463    else if (OCTEON_IS_MODEL(OCTEON_CN52XX))
464        arch_ports = 2;
465    else if (OCTEON_IS_MODEL(OCTEON_CN50XX))
466        arch_ports = 1;
467    else if (OCTEON_IS_MODEL(OCTEON_CN31XX))
468        arch_ports = 1;
469    else if (OCTEON_IS_MODEL(OCTEON_CN30XX))
470        arch_ports = 1;
471    else
472        arch_ports = 0;
473
474    return __cvmx_helper_board_usb_get_num_ports(arch_ports);
475}
476#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
477EXPORT_SYMBOL(cvmx_usb_get_num_ports);
478#endif
479
480
481/**
482 * @INTERNAL
483 * Allocate a usb transaction for use
484 *
485 * @param usb    USB device state populated by
486 *               cvmx_usb_initialize().
487 *
488 * @return Transaction or NULL
489 */
490static inline cvmx_usb_transaction_t *__cvmx_usb_alloc_transaction(cvmx_usb_internal_state_t *usb)
491{
492    cvmx_usb_transaction_t *t;
493    t = usb->free_transaction_head;
494    if (t)
495    {
496        usb->free_transaction_head = t->next;
497        if (!usb->free_transaction_head)
498            usb->free_transaction_tail = NULL;
499    }
500    else if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_INFO))
501        cvmx_dprintf("%s: Failed to allocate a transaction\n", __FUNCTION__);
502    if (t)
503    {
504        memset(t, 0, sizeof(*t));
505        t->flags = __CVMX_USB_TRANSACTION_FLAGS_IN_USE;
506    }
507    return t;
508}
509
510
511/**
512 * @INTERNAL
513 * Free a usb transaction
514 *
515 * @param usb    USB device state populated by
516 *               cvmx_usb_initialize().
517 * @param transaction
518 *               Transaction to free
519 */
520static inline void __cvmx_usb_free_transaction(cvmx_usb_internal_state_t *usb,
521                                        cvmx_usb_transaction_t *transaction)
522{
523    transaction->flags = 0;
524    transaction->prev = NULL;
525    transaction->next = NULL;
526    if (usb->free_transaction_tail)
527        usb->free_transaction_tail->next = transaction;
528    else
529        usb->free_transaction_head = transaction;
530    usb->free_transaction_tail = transaction;
531}
532
533
534/**
535 * @INTERNAL
536 * Add a pipe to the tail of a list
537 * @param list   List to add pipe to
538 * @param pipe   Pipe to add
539 */
540static inline void __cvmx_usb_append_pipe(cvmx_usb_pipe_list_t *list, cvmx_usb_pipe_t *pipe)
541{
542    pipe->next = NULL;
543    pipe->prev = list->tail;
544    if (list->tail)
545        list->tail->next = pipe;
546    else
547        list->head = pipe;
548    list->tail = pipe;
549}
550
551
552/**
553 * @INTERNAL
554 * Remove a pipe from a list
555 * @param list   List to remove pipe from
556 * @param pipe   Pipe to remove
557 */
558static inline void __cvmx_usb_remove_pipe(cvmx_usb_pipe_list_t *list, cvmx_usb_pipe_t *pipe)
559{
560    if (list->head == pipe)
561    {
562        list->head = pipe->next;
563        pipe->next = NULL;
564        if (list->head)
565            list->head->prev = NULL;
566        else
567            list->tail = NULL;
568    }
569    else if (list->tail == pipe)
570    {
571        list->tail = pipe->prev;
572        list->tail->next = NULL;
573        pipe->prev = NULL;
574    }
575    else
576    {
577        pipe->prev->next = pipe->next;
578        pipe->next->prev = pipe->prev;
579        pipe->prev = NULL;
580        pipe->next = NULL;
581    }
582}
583
584
585/**
586 * Initialize a USB port for use. This must be called before any
587 * other access to the Octeon USB port is made. The port starts
588 * off in the disabled state.
589 *
590 * @param state  Pointer to an empty cvmx_usb_state_t structure
591 *               that will be populated by the initialize call.
592 *               This structure is then passed to all other USB
593 *               functions.
594 * @param usb_port_number
595 *               Which Octeon USB port to initialize.
596 * @param flags  Flags to control hardware initialization. See
597 *               cvmx_usb_initialize_flags_t for the flag
598 *               definitions. Some flags are mandatory.
599 *
600 * @return CVMX_USB_SUCCESS or a negative error code defined in
601 *         cvmx_usb_status_t.
602 */
603cvmx_usb_status_t cvmx_usb_initialize(cvmx_usb_state_t *state,
604                                      int usb_port_number,
605                                      cvmx_usb_initialize_flags_t flags)
606{
607    cvmx_usbnx_clk_ctl_t usbn_clk_ctl;
608    cvmx_usbnx_usbp_ctl_status_t usbn_usbp_ctl_status;
609    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
610
611    usb->init_flags = flags;
612    CVMX_USB_LOG_CALLED();
613    CVMX_USB_LOG_PARAM("%p", state);
614    CVMX_USB_LOG_PARAM("%d", usb_port_number);
615    CVMX_USB_LOG_PARAM("0x%x", flags);
616
617    /* Make sure that state is large enough to store the internal state */
618    if (sizeof(*state) < sizeof(*usb))
619        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
620    /* At first allow 0-1 for the usb port number */
621    if ((usb_port_number < 0) || (usb_port_number > 1))
622        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
623    /* For all chips except 52XX there is only one port */
624    if (!OCTEON_IS_MODEL(OCTEON_CN52XX) && (usb_port_number > 0))
625        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
626    /* Try to determine clock type automatically */
627    if ((flags & (CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI |
628                  CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND)) == 0)
629    {
630        if (__cvmx_helper_board_usb_get_clock_type() == USB_CLOCK_TYPE_CRYSTAL_12)
631            flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;  /* Only 12 MHZ crystals are supported */
632        else
633            flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
634    }
635
636    if (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND)
637    {
638        /* Check for auto ref clock frequency */
639        if (!(flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK))
640            switch (__cvmx_helper_board_usb_get_clock_type())
641            {
642                case USB_CLOCK_TYPE_REF_12:
643                    flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
644                    break;
645                case USB_CLOCK_TYPE_REF_24:
646                    flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
647                    break;
648                case USB_CLOCK_TYPE_REF_48:
649                    flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
650                    break;
651                default:
652                    CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
653                    break;
654            }
655    }
656
657    memset(usb, 0, sizeof(usb));
658    usb->init_flags = flags;
659
660    /* Initialize the USB state structure */
661    {
662        int i;
663        usb->index = usb_port_number;
664
665        /* Initialize the transaction double linked list */
666        usb->free_transaction_head = NULL;
667        usb->free_transaction_tail = NULL;
668        for (i=0; i<MAX_TRANSACTIONS; i++)
669            __cvmx_usb_free_transaction(usb, usb->transaction + i);
670        for (i=0; i<MAX_PIPES; i++)
671            __cvmx_usb_append_pipe(&usb->free_pipes, usb->pipe + i);
672    }
673
674    /* Power On Reset and PHY Initialization */
675
676    /* 1. Wait for DCOK to assert (nothing to do) */
677    /* 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
678        USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0 */
679    usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
680    usbn_clk_ctl.s.por = 1;
681    usbn_clk_ctl.s.hrst = 0;
682    usbn_clk_ctl.s.prst = 0;
683    usbn_clk_ctl.s.hclk_rst = 0;
684    usbn_clk_ctl.s.enable = 0;
685    /* 2b. Select the USB reference clock/crystal parameters by writing
686        appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON] */
687    if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND)
688    {
689        /* The USB port uses 12/24/48MHz 2.5V board clock
690            source at USB_XO. USB_XI should be tied to GND.
691            Most Octeon evaluation boards require this setting */
692        if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
693        {
694            usbn_clk_ctl.cn31xx.p_rclk  = 1; /* From CN31XX,CN30XX manual */
695            usbn_clk_ctl.cn31xx.p_xenbn = 0;
696        }
697        else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
698            usbn_clk_ctl.cn56xx.p_rtype = 2; /* From CN56XX,CN50XX manual */
699        else
700            usbn_clk_ctl.cn52xx.p_rtype = 1; /* From CN52XX manual */
701
702        switch (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK)
703        {
704            case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
705                usbn_clk_ctl.s.p_c_sel = 0;
706                break;
707            case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
708                usbn_clk_ctl.s.p_c_sel = 1;
709                break;
710            case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
711                usbn_clk_ctl.s.p_c_sel = 2;
712                break;
713        }
714    }
715    else
716    {
717        /* The USB port uses a 12MHz crystal as clock source
718            at USB_XO and USB_XI */
719        if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
720        {
721            usbn_clk_ctl.cn31xx.p_rclk  = 1; /* From CN31XX,CN30XX manual */
722            usbn_clk_ctl.cn31xx.p_xenbn = 1;
723        }
724        else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
725            usbn_clk_ctl.cn56xx.p_rtype = 0; /* From CN56XX,CN50XX manual */
726        else
727            usbn_clk_ctl.cn52xx.p_rtype = 0; /* From CN52XX manual */
728
729        usbn_clk_ctl.s.p_c_sel = 0;
730    }
731    /* 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
732        setting USBN0/1_CLK_CTL[ENABLE] = 1.  Divide the core clock down such
733        that USB is as close as possible to 125Mhz */
734    {
735        int divisor = (cvmx_clock_get_rate(CVMX_CLOCK_CORE)+125000000-1)/125000000;
736        if (divisor < 4)  /* Lower than 4 doesn't seem to work properly */
737            divisor = 4;
738        usbn_clk_ctl.s.divide = divisor;
739        usbn_clk_ctl.s.divide2 = 0;
740    }
741    __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
742                           usbn_clk_ctl.u64);
743    /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
744    usbn_clk_ctl.s.hclk_rst = 1;
745    __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
746                           usbn_clk_ctl.u64);
747    /* 2e.  Wait 64 core-clock cycles for HCLK to stabilize */
748    cvmx_wait(64);
749    /* 3. Program the power-on reset field in the USBN clock-control register:
750        USBN_CLK_CTL[POR] = 0 */
751    usbn_clk_ctl.s.por = 0;
752    __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
753                           usbn_clk_ctl.u64);
754    /* 4. Wait 1 ms for PHY clock to start */
755    cvmx_wait_usec(1000);
756    /* 5. Program the Reset input from automatic test equipment field in the
757        USBP control and status register: USBN_USBP_CTL_STATUS[ATE_RESET] = 1 */
758    usbn_usbp_ctl_status.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index));
759    usbn_usbp_ctl_status.s.ate_reset = 1;
760    __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
761                           usbn_usbp_ctl_status.u64);
762    /* 6. Wait 10 cycles */
763    cvmx_wait(10);
764    /* 7. Clear ATE_RESET field in the USBN clock-control register:
765        USBN_USBP_CTL_STATUS[ATE_RESET] = 0 */
766    usbn_usbp_ctl_status.s.ate_reset = 0;
767    __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
768                           usbn_usbp_ctl_status.u64);
769    /* 8. Program the PHY reset field in the USBN clock-control register:
770        USBN_CLK_CTL[PRST] = 1 */
771    usbn_clk_ctl.s.prst = 1;
772    __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
773                           usbn_clk_ctl.u64);
774    /* 9. Program the USBP control and status register to select host or
775        device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
776        device */
777    usbn_usbp_ctl_status.s.hst_mode = 0;
778    __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
779                           usbn_usbp_ctl_status.u64);
780    /* 10. Wait 1 �s */
781    cvmx_wait_usec(1);
782    /* 11. Program the hreset_n field in the USBN clock-control register:
783        USBN_CLK_CTL[HRST] = 1 */
784    usbn_clk_ctl.s.hrst = 1;
785    __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
786                           usbn_clk_ctl.u64);
787    /* 12. Proceed to USB core initialization */
788    usbn_clk_ctl.s.enable = 1;
789    __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
790                           usbn_clk_ctl.u64);
791    cvmx_wait_usec(1);
792
793    /* USB Core Initialization */
794
795    /* 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
796        determine USB core configuration parameters. */
797    /* Nothing needed */
798    /* 2. Program the following fields in the global AHB configuration
799        register (USBC_GAHBCFG)
800        DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
801        Burst length, USBC_GAHBCFG[HBSTLEN] = 0
802        Nonperiodic TxFIFO empty level (slave mode only),
803        USBC_GAHBCFG[NPTXFEMPLVL]
804        Periodic TxFIFO empty level (slave mode only),
805        USBC_GAHBCFG[PTXFEMPLVL]
806        Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1 */
807    {
808        cvmx_usbcx_gahbcfg_t usbcx_gahbcfg;
809        /* Due to an errata, CN31XX doesn't support DMA */
810        if (OCTEON_IS_MODEL(OCTEON_CN31XX))
811            usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
812        usbcx_gahbcfg.u32 = 0;
813        usbcx_gahbcfg.s.dmaen = !(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
814        if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
815            usb->idle_hardware_channels = 0x1;  /* Only use one channel with non DMA */
816        else if (OCTEON_IS_MODEL(OCTEON_CN5XXX))
817            usb->idle_hardware_channels = 0xf7; /* CN5XXX have an errata with channel 3 */
818        else
819            usb->idle_hardware_channels = 0xff;
820        usbcx_gahbcfg.s.hbstlen = 0;
821        usbcx_gahbcfg.s.nptxfemplvl = 1;
822        usbcx_gahbcfg.s.ptxfemplvl = 1;
823        usbcx_gahbcfg.s.glblintrmsk = 1;
824        __cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
825                               usbcx_gahbcfg.u32);
826    }
827    /* 3. Program the following fields in USBC_GUSBCFG register.
828        HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
829        ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
830        USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
831        PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0 */
832    {
833        cvmx_usbcx_gusbcfg_t usbcx_gusbcfg;
834        usbcx_gusbcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index));
835        usbcx_gusbcfg.s.toutcal = 0;
836        usbcx_gusbcfg.s.ddrsel = 0;
837        usbcx_gusbcfg.s.usbtrdtim = 0x5;
838        usbcx_gusbcfg.s.phylpwrclksel = 0;
839        __cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
840                               usbcx_gusbcfg.u32);
841    }
842    /* 4. The software must unmask the following bits in the USBC_GINTMSK
843        register.
844        OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
845        Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1 */
846    {
847        cvmx_usbcx_gintmsk_t usbcx_gintmsk;
848        int channel;
849
850        usbcx_gintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTMSK(usb->index));
851        usbcx_gintmsk.s.otgintmsk = 1;
852        usbcx_gintmsk.s.modemismsk = 1;
853        usbcx_gintmsk.s.hchintmsk = 1;
854        usbcx_gintmsk.s.sofmsk = 0;
855        /* We need RX FIFO interrupts if we don't have DMA */
856        if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
857            usbcx_gintmsk.s.rxflvlmsk = 1;
858        __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
859                               usbcx_gintmsk.u32);
860
861        /* Disable all channel interrupts. We'll enable them per channel later */
862        for (channel=0; channel<8; channel++)
863            __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
864    }
865
866    {
867        /* Host Port Initialization */
868        if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_INFO))
869            cvmx_dprintf("%s: USB%d is in host mode\n", __FUNCTION__, usb->index);
870
871        /* 1. Program the host-port interrupt-mask field to unmask,
872            USBC_GINTMSK[PRTINT] = 1 */
873        USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t,
874                        prtintmsk, 1);
875        USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t,
876                        disconnintmsk, 1);
877        /* 2. Program the USBC_HCFG register to select full-speed host or
878            high-speed host. */
879        {
880            cvmx_usbcx_hcfg_t usbcx_hcfg;
881            usbcx_hcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
882            usbcx_hcfg.s.fslssupp = 0;
883            usbcx_hcfg.s.fslspclksel = 0;
884            __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
885        }
886        /* 3. Program the port power bit to drive VBUS on the USB,
887            USBC_HPRT[PRTPWR] = 1 */
888        USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtpwr, 1);
889
890        /* Steps 4-15 from the manual are done later in the port enable */
891    }
892
893#ifdef __CVMX_ERROR_H__
894    cvmx_error_enable_group(CVMX_ERROR_GROUP_USB, usb->index);
895#endif
896    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
897}
898#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
899EXPORT_SYMBOL(cvmx_usb_initialize);
900#endif
901
902
903/**
904 * Shutdown a USB port after a call to cvmx_usb_initialize().
905 * The port should be disabled with all pipes closed when this
906 * function is called.
907 *
908 * @param state  USB device state populated by
909 *               cvmx_usb_initialize().
910 *
911 * @return CVMX_USB_SUCCESS or a negative error code defined in
912 *         cvmx_usb_status_t.
913 */
914cvmx_usb_status_t cvmx_usb_shutdown(cvmx_usb_state_t *state)
915{
916    cvmx_usbnx_clk_ctl_t usbn_clk_ctl;
917    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
918
919    CVMX_USB_LOG_CALLED();
920    CVMX_USB_LOG_PARAM("%p", state);
921
922    /* Make sure all pipes are closed */
923    if (usb->idle_pipes.head ||
924        usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS].head ||
925        usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT].head ||
926        usb->active_pipes[CVMX_USB_TRANSFER_CONTROL].head ||
927        usb->active_pipes[CVMX_USB_TRANSFER_BULK].head)
928        CVMX_USB_RETURN(CVMX_USB_BUSY);
929
930#ifdef __CVMX_ERROR_H__
931    cvmx_error_disable_group(CVMX_ERROR_GROUP_USB, usb->index);
932#endif
933
934    /* Disable the clocks and put them in power on reset */
935    usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
936    usbn_clk_ctl.s.enable = 1;
937    usbn_clk_ctl.s.por = 1;
938    usbn_clk_ctl.s.hclk_rst = 1;
939    usbn_clk_ctl.s.prst = 0;
940    usbn_clk_ctl.s.hrst = 0;
941    __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
942                           usbn_clk_ctl.u64);
943    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
944}
945#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
946EXPORT_SYMBOL(cvmx_usb_shutdown);
947#endif
948
949
950/**
951 * Enable a USB port. After this call succeeds, the USB port is
952 * online and servicing requests.
953 *
954 * @param state  USB device state populated by
955 *               cvmx_usb_initialize().
956 *
957 * @return CVMX_USB_SUCCESS or a negative error code defined in
958 *         cvmx_usb_status_t.
959 */
960cvmx_usb_status_t cvmx_usb_enable(cvmx_usb_state_t *state)
961{
962    cvmx_usbcx_ghwcfg3_t usbcx_ghwcfg3;
963    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
964
965    CVMX_USB_LOG_CALLED();
966    CVMX_USB_LOG_PARAM("%p", state);
967
968    usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
969
970    /* If the port is already enabled the just return. We don't need to do
971        anything */
972    if (usb->usbcx_hprt.s.prtena)
973        CVMX_USB_RETURN(CVMX_USB_SUCCESS);
974
975    /* If there is nothing plugged into the port then fail immediately */
976    if (!usb->usbcx_hprt.s.prtconnsts)
977    {
978        if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_INFO))
979            cvmx_dprintf("%s: USB%d Nothing plugged into the port\n", __FUNCTION__, usb->index);
980        CVMX_USB_RETURN(CVMX_USB_TIMEOUT);
981    }
982
983    /* Program the port reset bit to start the reset process */
984    USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtrst, 1);
985
986    /* Wait at least 50ms (high speed), or 10ms (full speed) for the reset
987        process to complete. */
988    cvmx_wait_usec(50000);
989
990    /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
991    USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtrst, 0);
992
993    /* Wait for the USBC_HPRT[PRTENA]. */
994    if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t,
995                              prtena, ==, 1, 100000))
996    {
997        if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_INFO))
998            cvmx_dprintf("%s: Timeout waiting for the port to finish reset\n",
999                         __FUNCTION__);
1000        CVMX_USB_RETURN(CVMX_USB_TIMEOUT);
1001    }
1002
1003    /* Read the port speed field to get the enumerated speed, USBC_HPRT[PRTSPD]. */
1004    usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1005    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_INFO))
1006        cvmx_dprintf("%s: USB%d is in %s speed mode\n", __FUNCTION__, usb->index,
1007                     (usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH) ? "high" :
1008                     (usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_FULL) ? "full" :
1009                     "low");
1010
1011    usbcx_ghwcfg3.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GHWCFG3(usb->index));
1012
1013    /* 13. Program the USBC_GRXFSIZ register to select the size of the receive
1014        FIFO (25%). */
1015    USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), cvmx_usbcx_grxfsiz_t,
1016                    rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
1017    /* 14. Program the USBC_GNPTXFSIZ register to select the size and the
1018        start address of the non- periodic transmit FIFO for nonperiodic
1019        transactions (50%). */
1020    {
1021        cvmx_usbcx_gnptxfsiz_t siz;
1022        siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
1023        siz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
1024        siz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
1025        __cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), siz.u32);
1026    }
1027    /* 15. Program the USBC_HPTXFSIZ register to select the size and start
1028        address of the periodic transmit FIFO for periodic transactions (25%). */
1029    {
1030        cvmx_usbcx_hptxfsiz_t siz;
1031        siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
1032        siz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
1033        siz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
1034        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), siz.u32);
1035    }
1036    /* Flush all FIFOs */
1037    USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t, txfnum, 0x10);
1038    USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t, txfflsh, 1);
1039    CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t,
1040                          txfflsh, ==, 0, 100);
1041    USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t, rxfflsh, 1);
1042    CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t,
1043                          rxfflsh, ==, 0, 100);
1044
1045    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
1046}
1047#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
1048EXPORT_SYMBOL(cvmx_usb_enable);
1049#endif
1050
1051
1052/**
1053 * Disable a USB port. After this call the USB port will not
1054 * generate data transfers and will not generate events.
1055 * Transactions in process will fail and call their
1056 * associated callbacks.
1057 *
1058 * @param state  USB device state populated by
1059 *               cvmx_usb_initialize().
1060 *
1061 * @return CVMX_USB_SUCCESS or a negative error code defined in
1062 *         cvmx_usb_status_t.
1063 */
1064cvmx_usb_status_t cvmx_usb_disable(cvmx_usb_state_t *state)
1065{
1066    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
1067
1068    CVMX_USB_LOG_CALLED();
1069    CVMX_USB_LOG_PARAM("%p", state);
1070
1071    /* Disable the port */
1072    USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtena, 1);
1073    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
1074}
1075#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
1076EXPORT_SYMBOL(cvmx_usb_disable);
1077#endif
1078
1079
1080/**
1081 * Get the current state of the USB port. Use this call to
1082 * determine if the usb port has anything connected, is enabled,
1083 * or has some sort of error condition. The return value of this
1084 * call has "changed" bits to signal of the value of some fields
1085 * have changed between calls. These "changed" fields are based
1086 * on the last call to cvmx_usb_set_status(). In order to clear
1087 * them, you must update the status through cvmx_usb_set_status().
1088 *
1089 * @param state  USB device state populated by
1090 *               cvmx_usb_initialize().
1091 *
1092 * @return Port status information
1093 */
1094cvmx_usb_port_status_t cvmx_usb_get_status(cvmx_usb_state_t *state)
1095{
1096    cvmx_usbcx_hprt_t usbc_hprt;
1097    cvmx_usb_port_status_t result;
1098    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
1099
1100    memset(&result, 0, sizeof(result));
1101
1102    CVMX_USB_LOG_CALLED();
1103    CVMX_USB_LOG_PARAM("%p", state);
1104
1105    usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1106    result.port_enabled = usbc_hprt.s.prtena;
1107    result.port_over_current = usbc_hprt.s.prtovrcurract;
1108    result.port_powered = usbc_hprt.s.prtpwr;
1109    result.port_speed = usbc_hprt.s.prtspd;
1110    result.connected = usbc_hprt.s.prtconnsts;
1111    result.connect_change = (result.connected != usb->port_status.connected);
1112
1113    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CALLS))
1114        cvmx_dprintf("%*s%s: returned port enabled=%d, over_current=%d, powered=%d, speed=%d, connected=%d, connect_change=%d\n",
1115                     2*(--usb->indent), "", __FUNCTION__,
1116                     result.port_enabled,
1117                     result.port_over_current,
1118                     result.port_powered,
1119                     result.port_speed,
1120                     result.connected,
1121                     result.connect_change);
1122    return result;
1123}
1124#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
1125EXPORT_SYMBOL(cvmx_usb_get_status);
1126#endif
1127
1128
1129/**
1130 * Set the current state of the USB port. The status is used as
1131 * a reference for the "changed" bits returned by
1132 * cvmx_usb_get_status(). Other than serving as a reference, the
1133 * status passed to this function is not used. No fields can be
1134 * changed through this call.
1135 *
1136 * @param state  USB device state populated by
1137 *               cvmx_usb_initialize().
1138 * @param port_status
1139 *               Port status to set, most like returned by cvmx_usb_get_status()
1140 */
1141void cvmx_usb_set_status(cvmx_usb_state_t *state, cvmx_usb_port_status_t port_status)
1142{
1143    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
1144    CVMX_USB_LOG_CALLED();
1145    CVMX_USB_LOG_PARAM("%p", state);
1146    usb->port_status = port_status;
1147    CVMX_USB_RETURN_NOTHING();
1148}
1149#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
1150EXPORT_SYMBOL(cvmx_usb_set_status);
1151#endif
1152
1153
1154/**
1155 * @INTERNAL
1156 * Convert a USB transaction into a handle
1157 *
1158 * @param usb    USB device state populated by
1159 *               cvmx_usb_initialize().
1160 * @param transaction
1161 *               Transaction to get handle for
1162 *
1163 * @return Handle
1164 */
1165static inline int __cvmx_usb_get_submit_handle(cvmx_usb_internal_state_t *usb,
1166                                        cvmx_usb_transaction_t *transaction)
1167{
1168    return ((unsigned long)transaction - (unsigned long)usb->transaction) /
1169            sizeof(*transaction);
1170}
1171
1172
1173/**
1174 * @INTERNAL
1175 * Convert a USB pipe into a handle
1176 *
1177 * @param usb    USB device state populated by
1178 *               cvmx_usb_initialize().
1179 * @param pipe   Pipe to get handle for
1180 *
1181 * @return Handle
1182 */
1183static inline int __cvmx_usb_get_pipe_handle(cvmx_usb_internal_state_t *usb,
1184                                        cvmx_usb_pipe_t *pipe)
1185{
1186    return ((unsigned long)pipe - (unsigned long)usb->pipe) / sizeof(*pipe);
1187}
1188
1189
1190/**
1191 * Open a virtual pipe between the host and a USB device. A pipe
1192 * must be opened before data can be transferred between a device
1193 * and Octeon.
1194 *
1195 * @param state      USB device state populated by
1196 *                   cvmx_usb_initialize().
1197 * @param flags      Optional pipe flags defined in
1198 *                   cvmx_usb_pipe_flags_t.
1199 * @param device_addr
1200 *                   USB device address to open the pipe to
1201 *                   (0-127).
1202 * @param endpoint_num
1203 *                   USB endpoint number to open the pipe to
1204 *                   (0-15).
1205 * @param device_speed
1206 *                   The speed of the device the pipe is going
1207 *                   to. This must match the device's speed,
1208 *                   which may be different than the port speed.
1209 * @param max_packet The maximum packet length the device can
1210 *                   transmit/receive (low speed=0-8, full
1211 *                   speed=0-1023, high speed=0-1024). This value
1212 *                   comes from the standard endpoint descriptor
1213 *                   field wMaxPacketSize bits <10:0>.
1214 * @param transfer_type
1215 *                   The type of transfer this pipe is for.
1216 * @param transfer_dir
1217 *                   The direction the pipe is in. This is not
1218 *                   used for control pipes.
1219 * @param interval   For ISOCHRONOUS and INTERRUPT transfers,
1220 *                   this is how often the transfer is scheduled
1221 *                   for. All other transfers should specify
1222 *                   zero. The units are in frames (8000/sec at
1223 *                   high speed, 1000/sec for full speed).
1224 * @param multi_count
1225 *                   For high speed devices, this is the maximum
1226 *                   allowed number of packet per microframe.
1227 *                   Specify zero for non high speed devices. This
1228 *                   value comes from the standard endpoint descriptor
1229 *                   field wMaxPacketSize bits <12:11>.
1230 * @param hub_device_addr
1231 *                   Hub device address this device is connected
1232 *                   to. Devices connected directly to Octeon
1233 *                   use zero. This is only used when the device
1234 *                   is full/low speed behind a high speed hub.
1235 *                   The address will be of the high speed hub,
1236 *                   not and full speed hubs after it.
1237 * @param hub_port   Which port on the hub the device is
1238 *                   connected. Use zero for devices connected
1239 *                   directly to Octeon. Like hub_device_addr,
1240 *                   this is only used for full/low speed
1241 *                   devices behind a high speed hub.
1242 *
1243 * @return A non negative value is a pipe handle. Negative
1244 *         values are failure codes from cvmx_usb_status_t.
1245 */
1246int cvmx_usb_open_pipe(cvmx_usb_state_t *state, cvmx_usb_pipe_flags_t flags,
1247                       int device_addr, int endpoint_num,
1248                       cvmx_usb_speed_t device_speed, int max_packet,
1249                       cvmx_usb_transfer_t transfer_type,
1250                       cvmx_usb_direction_t transfer_dir, int interval,
1251                       int multi_count, int hub_device_addr, int hub_port)
1252{
1253    cvmx_usb_pipe_t *pipe;
1254    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
1255
1256    CVMX_USB_LOG_CALLED();
1257    CVMX_USB_LOG_PARAM("%p", state);
1258    CVMX_USB_LOG_PARAM("0x%x", flags);
1259    CVMX_USB_LOG_PARAM("%d", device_addr);
1260    CVMX_USB_LOG_PARAM("%d", endpoint_num);
1261    CVMX_USB_LOG_PARAM("%d", device_speed);
1262    CVMX_USB_LOG_PARAM("%d", max_packet);
1263    CVMX_USB_LOG_PARAM("%d", transfer_type);
1264    CVMX_USB_LOG_PARAM("%d", transfer_dir);
1265    CVMX_USB_LOG_PARAM("%d", interval);
1266    CVMX_USB_LOG_PARAM("%d", multi_count);
1267    CVMX_USB_LOG_PARAM("%d", hub_device_addr);
1268    CVMX_USB_LOG_PARAM("%d", hub_port);
1269
1270    if (cvmx_unlikely((device_addr < 0) || (device_addr > MAX_USB_ADDRESS)))
1271        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1272    if (cvmx_unlikely((endpoint_num < 0) || (endpoint_num > MAX_USB_ENDPOINT)))
1273        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1274    if (cvmx_unlikely(device_speed > CVMX_USB_SPEED_LOW))
1275        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1276    if (cvmx_unlikely((max_packet <= 0) || (max_packet > 1024)))
1277        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1278    if (cvmx_unlikely(transfer_type > CVMX_USB_TRANSFER_INTERRUPT))
1279        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1280    if (cvmx_unlikely((transfer_dir != CVMX_USB_DIRECTION_OUT) &&
1281        (transfer_dir != CVMX_USB_DIRECTION_IN)))
1282        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1283    if (cvmx_unlikely(interval < 0))
1284        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1285    if (cvmx_unlikely((transfer_type == CVMX_USB_TRANSFER_CONTROL) && interval))
1286        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1287    if (cvmx_unlikely(multi_count < 0))
1288        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1289    if (cvmx_unlikely((device_speed != CVMX_USB_SPEED_HIGH) &&
1290        (multi_count != 0)))
1291        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1292    if (cvmx_unlikely((hub_device_addr < 0) || (hub_device_addr > MAX_USB_ADDRESS)))
1293        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1294    if (cvmx_unlikely((hub_port < 0) || (hub_port > MAX_USB_HUB_PORT)))
1295        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
1296
1297    /* Find a free pipe */
1298    pipe = usb->free_pipes.head;
1299    if (!pipe)
1300        CVMX_USB_RETURN(CVMX_USB_NO_MEMORY);
1301    __cvmx_usb_remove_pipe(&usb->free_pipes, pipe);
1302    pipe->flags = flags | __CVMX_USB_PIPE_FLAGS_OPEN;
1303    if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1304        (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1305        (transfer_type == CVMX_USB_TRANSFER_BULK))
1306        pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
1307    pipe->device_addr = device_addr;
1308    pipe->endpoint_num = endpoint_num;
1309    pipe->device_speed = device_speed;
1310    pipe->max_packet = max_packet;
1311    pipe->transfer_type = transfer_type;
1312    pipe->transfer_dir = transfer_dir;
1313    /* All pipes use interval to rate limit NAK processing. Force an interval
1314        if one wasn't supplied */
1315    if (!interval)
1316        interval = 1;
1317    if (__cvmx_usb_pipe_needs_split(usb, pipe))
1318    {
1319        pipe->interval = interval*8;
1320        /* Force start splits to be schedule on uFrame 0 */
1321        pipe->next_tx_frame = ((usb->frame_number+7)&~7) + pipe->interval;
1322    }
1323    else
1324    {
1325        pipe->interval = interval;
1326        pipe->next_tx_frame = usb->frame_number + pipe->interval;
1327    }
1328    pipe->multi_count = multi_count;
1329    pipe->hub_device_addr = hub_device_addr;
1330    pipe->hub_port = hub_port;
1331    pipe->pid_toggle = 0;
1332    pipe->split_sc_frame = -1;
1333    __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
1334
1335    /* We don't need to tell the hardware about this pipe yet since
1336        it doesn't have any submitted requests */
1337
1338    CVMX_USB_RETURN(__cvmx_usb_get_pipe_handle(usb, pipe));
1339}
1340#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
1341EXPORT_SYMBOL(cvmx_usb_open_pipe);
1342#endif
1343
1344
1345/**
1346 * @INTERNAL
1347 * Poll the RX FIFOs and remove data as needed. This function is only used
1348 * in non DMA mode. It is very important that this function be called quickly
1349 * enough to prevent FIFO overflow.
1350 *
1351 * @param usb     USB device state populated by
1352 *                cvmx_usb_initialize().
1353 */
1354static void __cvmx_usb_poll_rx_fifo(cvmx_usb_internal_state_t *usb)
1355{
1356    cvmx_usbcx_grxstsph_t rx_status;
1357    int channel;
1358    int bytes;
1359    uint64_t address;
1360    uint32_t *ptr;
1361
1362    CVMX_USB_LOG_CALLED();
1363    CVMX_USB_LOG_PARAM("%p", usb);
1364
1365    rx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GRXSTSPH(usb->index));
1366    /* Only read data if IN data is there */
1367    if (rx_status.s.pktsts != 2)
1368        CVMX_USB_RETURN_NOTHING();
1369    /* Check if no data is available */
1370    if (!rx_status.s.bcnt)
1371        CVMX_USB_RETURN_NOTHING();
1372
1373    channel = rx_status.s.chnum;
1374    bytes = rx_status.s.bcnt;
1375    if (!bytes)
1376        CVMX_USB_RETURN_NOTHING();
1377
1378    /* Get where the DMA engine would have written this data */
1379    address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8);
1380    ptr = cvmx_phys_to_ptr(address);
1381    __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, address + bytes);
1382
1383    /* Loop writing the FIFO data for this packet into memory */
1384    while (bytes > 0)
1385    {
1386        *ptr++ = __cvmx_usb_read_csr32(usb, USB_FIFO_ADDRESS(channel, usb->index));
1387        bytes -= 4;
1388    }
1389    CVMX_SYNCW;
1390
1391    CVMX_USB_RETURN_NOTHING();
1392}
1393
1394
1395/**
1396 * Fill the TX hardware fifo with data out of the software
1397 * fifos
1398 *
1399 * @param usb       USB device state populated by
1400 *                  cvmx_usb_initialize().
1401 * @param fifo      Software fifo to use
1402 * @param available Amount of space in the hardware fifo
1403 *
1404 * @return Non zero if the hardware fifo was too small and needs
1405 *         to be serviced again.
1406 */
1407static int __cvmx_usb_fill_tx_hw(cvmx_usb_internal_state_t *usb, cvmx_usb_tx_fifo_t *fifo, int available)
1408{
1409    CVMX_USB_LOG_CALLED();
1410    CVMX_USB_LOG_PARAM("%p", usb);
1411    CVMX_USB_LOG_PARAM("%p", fifo);
1412    CVMX_USB_LOG_PARAM("%d", available);
1413
1414    /* We're done either when there isn't anymore space or the software FIFO
1415        is empty */
1416    while (available && (fifo->head != fifo->tail))
1417    {
1418        int i = fifo->tail;
1419        const uint32_t *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1420        uint64_t csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel, usb->index) ^ 4;
1421        int words = available;
1422
1423        /* Limit the amount of data to waht the SW fifo has */
1424        if (fifo->entry[i].size <= available)
1425        {
1426            words = fifo->entry[i].size;
1427            fifo->tail++;
1428            if (fifo->tail > MAX_CHANNELS)
1429                fifo->tail = 0;
1430        }
1431
1432        /* Update the next locations and counts */
1433        available -= words;
1434        fifo->entry[i].address += words * 4;
1435        fifo->entry[i].size -= words;
1436
1437        /* Write the HW fifo data. The read every three writes is due
1438            to an errata on CN3XXX chips */
1439        while (words > 3)
1440        {
1441            cvmx_write64_uint32(csr_address, *ptr++);
1442            cvmx_write64_uint32(csr_address, *ptr++);
1443            cvmx_write64_uint32(csr_address, *ptr++);
1444            cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1445            words -= 3;
1446        }
1447        cvmx_write64_uint32(csr_address, *ptr++);
1448        if (--words)
1449        {
1450            cvmx_write64_uint32(csr_address, *ptr++);
1451            if (--words)
1452                cvmx_write64_uint32(csr_address, *ptr++);
1453        }
1454        cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1455    }
1456    CVMX_USB_RETURN(fifo->head != fifo->tail);
1457}
1458
1459
1460/**
1461 * Check the hardware FIFOs and fill them as needed
1462 *
1463 * @param usb    USB device state populated by
1464 *               cvmx_usb_initialize().
1465 */
1466static void __cvmx_usb_poll_tx_fifo(cvmx_usb_internal_state_t *usb)
1467{
1468    CVMX_USB_LOG_CALLED();
1469    CVMX_USB_LOG_PARAM("%p", usb);
1470
1471    if (usb->periodic.head != usb->periodic.tail)
1472    {
1473        cvmx_usbcx_hptxsts_t tx_status;
1474        tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXSTS(usb->index));
1475        if (__cvmx_usb_fill_tx_hw(usb, &usb->periodic, tx_status.s.ptxfspcavail))
1476            USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, ptxfempmsk, 1);
1477        else
1478            USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, ptxfempmsk, 0);
1479    }
1480
1481    if (usb->nonperiodic.head != usb->nonperiodic.tail)
1482    {
1483        cvmx_usbcx_gnptxsts_t tx_status;
1484        tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXSTS(usb->index));
1485        if (__cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic, tx_status.s.nptxfspcavail))
1486            USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, nptxfempmsk, 1);
1487        else
1488            USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, nptxfempmsk, 0);
1489    }
1490
1491    CVMX_USB_RETURN_NOTHING();
1492}
1493
1494
1495/**
1496 * @INTERNAL
1497 * Fill the TX FIFO with an outgoing packet
1498 *
1499 * @param usb     USB device state populated by
1500 *                cvmx_usb_initialize().
1501 * @param channel Channel number to get packet from
1502 */
1503static void __cvmx_usb_fill_tx_fifo(cvmx_usb_internal_state_t *usb, int channel)
1504{
1505    cvmx_usbcx_hccharx_t hcchar;
1506    cvmx_usbcx_hcspltx_t usbc_hcsplt;
1507    cvmx_usbcx_hctsizx_t usbc_hctsiz;
1508    cvmx_usb_tx_fifo_t *fifo;
1509
1510    CVMX_USB_LOG_CALLED();
1511    CVMX_USB_LOG_PARAM("%p", usb);
1512    CVMX_USB_LOG_PARAM("%d", channel);
1513
1514    /* We only need to fill data on outbound channels */
1515    hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
1516    if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1517        CVMX_USB_RETURN_NOTHING();
1518
1519    /* OUT Splits only have data on the start and not the complete */
1520    usbc_hcsplt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index));
1521    if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1522        CVMX_USB_RETURN_NOTHING();
1523
1524    /* Find out how many bytes we need to fill and convert it into 32bit words */
1525    usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1526    if (!usbc_hctsiz.s.xfersize)
1527        CVMX_USB_RETURN_NOTHING();
1528
1529    if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1530        (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1531        fifo = &usb->periodic;
1532    else
1533        fifo = &usb->nonperiodic;
1534
1535    fifo->entry[fifo->head].channel = channel;
1536    fifo->entry[fifo->head].address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8);
1537    fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize+3)>>2;
1538    fifo->head++;
1539    if (fifo->head > MAX_CHANNELS)
1540        fifo->head = 0;
1541
1542    __cvmx_usb_poll_tx_fifo(usb);
1543
1544    CVMX_USB_RETURN_NOTHING();
1545}
1546
1547/**
1548 * @INTERNAL
1549 * Perform channel specific setup for Control transactions. All
1550 * the generic stuff will already have been done in
1551 * __cvmx_usb_start_channel()
1552 *
1553 * @param usb     USB device state populated by
1554 *                cvmx_usb_initialize().
1555 * @param channel Channel to setup
1556 * @param pipe    Pipe for control transaction
1557 */
1558static void __cvmx_usb_start_channel_control(cvmx_usb_internal_state_t *usb,
1559                                             int channel,
1560                                             cvmx_usb_pipe_t *pipe)
1561{
1562    cvmx_usb_transaction_t *transaction = pipe->head;
1563    cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(transaction->control_header);
1564    int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1565    int packets_to_transfer;
1566    cvmx_usbcx_hctsizx_t usbc_hctsiz;
1567
1568    CVMX_USB_LOG_CALLED();
1569    CVMX_USB_LOG_PARAM("%p", usb);
1570    CVMX_USB_LOG_PARAM("%d", channel);
1571    CVMX_USB_LOG_PARAM("%p", pipe);
1572
1573    usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1574
1575    switch (transaction->stage)
1576    {
1577        case CVMX_USB_STAGE_NON_CONTROL:
1578        case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1579            cvmx_dprintf("%s: ERROR - Non control stage\n", __FUNCTION__);
1580            break;
1581        case CVMX_USB_STAGE_SETUP:
1582            usbc_hctsiz.s.pid = 3; /* Setup */
1583            bytes_to_transfer = sizeof(*header);
1584            /* All Control operations start with a setup going OUT */
1585            USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir, CVMX_USB_DIRECTION_OUT);
1586            /* Setup send the control header instead of the buffer data. The
1587                buffer data will be used in the next stage */
1588            __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, transaction->control_header);
1589            break;
1590        case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1591            usbc_hctsiz.s.pid = 3; /* Setup */
1592            bytes_to_transfer = 0;
1593            /* All Control operations start with a setup going OUT */
1594            USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir, CVMX_USB_DIRECTION_OUT);
1595            USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), cvmx_usbcx_hcspltx_t, compsplt, 1);
1596            break;
1597        case CVMX_USB_STAGE_DATA:
1598            usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1599            if (__cvmx_usb_pipe_needs_split(usb, pipe))
1600            {
1601                if (header->s.request_type & 0x80)
1602                    bytes_to_transfer = 0;
1603                else if (bytes_to_transfer > pipe->max_packet)
1604                    bytes_to_transfer = pipe->max_packet;
1605            }
1606            USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1607                            cvmx_usbcx_hccharx_t, epdir,
1608                            ((header->s.request_type & 0x80) ?
1609                             CVMX_USB_DIRECTION_IN :
1610                             CVMX_USB_DIRECTION_OUT));
1611            break;
1612        case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1613            usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1614            if (!(header->s.request_type & 0x80))
1615                bytes_to_transfer = 0;
1616            USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1617                            cvmx_usbcx_hccharx_t, epdir,
1618                            ((header->s.request_type & 0x80) ?
1619                             CVMX_USB_DIRECTION_IN :
1620                             CVMX_USB_DIRECTION_OUT));
1621            USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), cvmx_usbcx_hcspltx_t, compsplt, 1);
1622            break;
1623        case CVMX_USB_STAGE_STATUS:
1624            usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1625            bytes_to_transfer = 0;
1626            USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir,
1627                            ((header->s.request_type & 0x80) ?
1628                             CVMX_USB_DIRECTION_OUT :
1629                             CVMX_USB_DIRECTION_IN));
1630            break;
1631        case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1632            usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1633            bytes_to_transfer = 0;
1634            USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir,
1635                            ((header->s.request_type & 0x80) ?
1636                             CVMX_USB_DIRECTION_OUT :
1637                             CVMX_USB_DIRECTION_IN));
1638            USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), cvmx_usbcx_hcspltx_t, compsplt, 1);
1639            break;
1640    }
1641
1642    /* Make sure the transfer never exceeds the byte limit of the hardware.
1643        Further bytes will be sent as continued transactions */
1644    if (bytes_to_transfer > MAX_TRANSFER_BYTES)
1645    {
1646        /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1647        bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1648        bytes_to_transfer *= pipe->max_packet;
1649    }
1650
1651    /* Calculate the number of packets to transfer. If the length is zero
1652        we still need to transfer one packet */
1653    packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1654    if (packets_to_transfer == 0)
1655        packets_to_transfer = 1;
1656    else if ((packets_to_transfer>1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA))
1657    {
1658        /* Limit to one packet when not using DMA. Channels must be restarted
1659            between every packet for IN transactions, so there is no reason to
1660            do multiple packets in a row */
1661        packets_to_transfer = 1;
1662        bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1663    }
1664    else if (packets_to_transfer > MAX_TRANSFER_PACKETS)
1665    {
1666        /* Limit the number of packet and data transferred to what the
1667            hardware can handle */
1668        packets_to_transfer = MAX_TRANSFER_PACKETS;
1669        bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1670    }
1671
1672    usbc_hctsiz.s.xfersize = bytes_to_transfer;
1673    usbc_hctsiz.s.pktcnt = packets_to_transfer;
1674
1675    __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1676    CVMX_USB_RETURN_NOTHING();
1677}
1678
1679
1680/**
1681 * @INTERNAL
1682 * Start a channel to perform the pipe's head transaction
1683 *
1684 * @param usb     USB device state populated by
1685 *                cvmx_usb_initialize().
1686 * @param channel Channel to setup
1687 * @param pipe    Pipe to start
1688 */
1689static void __cvmx_usb_start_channel(cvmx_usb_internal_state_t *usb,
1690                                     int channel,
1691                                     cvmx_usb_pipe_t *pipe)
1692{
1693    cvmx_usb_transaction_t *transaction = pipe->head;
1694
1695    CVMX_USB_LOG_CALLED();
1696    CVMX_USB_LOG_PARAM("%p", usb);
1697    CVMX_USB_LOG_PARAM("%d", channel);
1698    CVMX_USB_LOG_PARAM("%p", pipe);
1699
1700    if (cvmx_unlikely((usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_TRANSFERS) ||
1701        (pipe->flags & CVMX_USB_PIPE_FLAGS_DEBUG_TRANSFERS)))
1702        cvmx_dprintf("%s: Channel %d started. Pipe %d transaction %d stage %d\n",
1703                     __FUNCTION__, channel, __cvmx_usb_get_pipe_handle(usb, pipe),
1704                     __cvmx_usb_get_submit_handle(usb, transaction),
1705                     transaction->stage);
1706
1707    /* Make sure all writes to the DMA region get flushed */
1708    CVMX_SYNCW;
1709
1710    /* Attach the channel to the pipe */
1711    usb->pipe_for_channel[channel] = pipe;
1712    pipe->channel = channel;
1713    pipe->flags |= __CVMX_USB_PIPE_FLAGS_SCHEDULED;
1714
1715    /* Mark this channel as in use */
1716    usb->idle_hardware_channels &= ~(1<<channel);
1717
1718    /* Enable the channel interrupt bits */
1719    {
1720        cvmx_usbcx_hcintx_t usbc_hcint;
1721        cvmx_usbcx_hcintmskx_t usbc_hcintmsk;
1722        cvmx_usbcx_haintmsk_t usbc_haintmsk;
1723
1724        /* Clear all channel status bits */
1725        usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
1726        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index), usbc_hcint.u32);
1727
1728        usbc_hcintmsk.u32 = 0;
1729        usbc_hcintmsk.s.chhltdmsk = 1;
1730        if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1731        {
1732            /* Channels need these extra interrupts when we aren't in DMA mode */
1733            usbc_hcintmsk.s.datatglerrmsk = 1;
1734            usbc_hcintmsk.s.frmovrunmsk = 1;
1735            usbc_hcintmsk.s.bblerrmsk = 1;
1736            usbc_hcintmsk.s.xacterrmsk = 1;
1737            if (__cvmx_usb_pipe_needs_split(usb, pipe))
1738            {
1739                /* Splits don't generate xfercompl, so we need ACK and NYET */
1740                usbc_hcintmsk.s.nyetmsk = 1;
1741                usbc_hcintmsk.s.ackmsk = 1;
1742            }
1743            usbc_hcintmsk.s.nakmsk = 1;
1744            usbc_hcintmsk.s.stallmsk = 1;
1745            usbc_hcintmsk.s.xfercomplmsk = 1;
1746        }
1747        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), usbc_hcintmsk.u32);
1748
1749        /* Enable the channel interrupt to propagate */
1750        usbc_haintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index));
1751        usbc_haintmsk.s.haintmsk |= 1<<channel;
1752        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index), usbc_haintmsk.u32);
1753    }
1754
1755    /* Setup the locations the DMA engines use  */
1756    {
1757        uint64_t dma_address = transaction->buffer + transaction->actual_bytes;
1758        if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1759            dma_address = transaction->buffer + transaction->iso_packets[0].offset + transaction->actual_bytes;
1760        __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, dma_address);
1761        __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, dma_address);
1762    }
1763
1764    /* Setup both the size of the transfer and the SPLIT characteristics */
1765    {
1766        cvmx_usbcx_hcspltx_t usbc_hcsplt = {.u32 = 0};
1767        cvmx_usbcx_hctsizx_t usbc_hctsiz = {.u32 = 0};
1768        int packets_to_transfer;
1769        int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1770
1771        /* ISOCHRONOUS transactions store each individual transfer size in the
1772            packet structure, not the global buffer_length */
1773        if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1774            bytes_to_transfer = transaction->iso_packets[0].length - transaction->actual_bytes;
1775
1776        /* We need to do split transactions when we are talking to non high
1777            speed devices that are behind a high speed hub */
1778        if (__cvmx_usb_pipe_needs_split(usb, pipe))
1779        {
1780            /* On the start split phase (stage is even) record the frame number we
1781                will need to send the split complete. We only store the lower two bits
1782                since the time ahead can only be two frames */
1783            if ((transaction->stage&1) == 0)
1784            {
1785                if (transaction->type == CVMX_USB_TRANSFER_BULK)
1786                    pipe->split_sc_frame = (usb->frame_number + 1) & 0x7f;
1787                else
1788                    pipe->split_sc_frame = (usb->frame_number + 2) & 0x7f;
1789            }
1790            else
1791                pipe->split_sc_frame = -1;
1792
1793            usbc_hcsplt.s.spltena = 1;
1794            usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1795            usbc_hcsplt.s.prtaddr = pipe->hub_port;
1796            usbc_hcsplt.s.compsplt = (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1797
1798            /* SPLIT transactions can only ever transmit one data packet so
1799                limit the transfer size to the max packet size */
1800            if (bytes_to_transfer > pipe->max_packet)
1801                bytes_to_transfer = pipe->max_packet;
1802
1803            /* ISOCHRONOUS OUT splits are unique in that they limit
1804                data transfers to 188 byte chunks representing the
1805                begin/middle/end of the data or all */
1806            if (!usbc_hcsplt.s.compsplt &&
1807                (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1808                (pipe->transfer_type == CVMX_USB_TRANSFER_ISOCHRONOUS))
1809            {
1810                /* Clear the split complete frame number as there isn't going
1811                    to be a split complete */
1812                pipe->split_sc_frame = -1;
1813                /* See if we've started this transfer and sent data */
1814                if (transaction->actual_bytes == 0)
1815                {
1816                    /* Nothing sent yet, this is either a begin or the
1817                        entire payload */
1818                    if (bytes_to_transfer <= 188)
1819                        usbc_hcsplt.s.xactpos = 3; /* Entire payload in one go */
1820                    else
1821                        usbc_hcsplt.s.xactpos = 2; /* First part of payload */
1822                }
1823                else
1824                {
1825                    /* Continuing the previous data, we must either be
1826                        in the middle or at the end */
1827                    if (bytes_to_transfer <= 188)
1828                        usbc_hcsplt.s.xactpos = 1; /* End of payload */
1829                    else
1830                        usbc_hcsplt.s.xactpos = 0; /* Middle of payload */
1831                }
1832                /* Again, the transfer size is limited to 188 bytes */
1833                if (bytes_to_transfer > 188)
1834                    bytes_to_transfer = 188;
1835            }
1836        }
1837
1838        /* Make sure the transfer never exceeds the byte limit of the hardware.
1839            Further bytes will be sent as continued transactions */
1840        if (bytes_to_transfer > MAX_TRANSFER_BYTES)
1841        {
1842            /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1843            bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1844            bytes_to_transfer *= pipe->max_packet;
1845        }
1846
1847        /* Calculate the number of packets to transfer. If the length is zero
1848            we still need to transfer one packet */
1849        packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1850        if (packets_to_transfer == 0)
1851            packets_to_transfer = 1;
1852        else if ((packets_to_transfer>1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA))
1853        {
1854            /* Limit to one packet when not using DMA. Channels must be restarted
1855                between every packet for IN transactions, so there is no reason to
1856                do multiple packets in a row */
1857            packets_to_transfer = 1;
1858            bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1859        }
1860        else if (packets_to_transfer > MAX_TRANSFER_PACKETS)
1861        {
1862            /* Limit the number of packet and data transferred to what the
1863                hardware can handle */
1864            packets_to_transfer = MAX_TRANSFER_PACKETS;
1865            bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1866        }
1867
1868        usbc_hctsiz.s.xfersize = bytes_to_transfer;
1869        usbc_hctsiz.s.pktcnt = packets_to_transfer;
1870
1871        /* Update the DATA0/DATA1 toggle */
1872        usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1873        /* High speed pipes may need a hardware ping before they start */
1874        if (pipe->flags & __CVMX_USB_PIPE_FLAGS_NEED_PING)
1875            usbc_hctsiz.s.dopng = 1;
1876
1877        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index), usbc_hcsplt.u32);
1878        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1879    }
1880
1881    /* Setup the Host Channel Characteristics Register */
1882    {
1883        cvmx_usbcx_hccharx_t usbc_hcchar = {.u32 = 0};
1884
1885        /* Set the startframe odd/even properly. This is only used for periodic */
1886        usbc_hcchar.s.oddfrm = usb->frame_number&1;
1887
1888        /* Set the number of back to back packets allowed by this endpoint.
1889            Split transactions interpret "ec" as the number of immediate
1890            retries of failure. These retries happen too quickly, so we
1891            disable these entirely for splits */
1892        if (__cvmx_usb_pipe_needs_split(usb, pipe))
1893            usbc_hcchar.s.ec = 1;
1894        else if (pipe->multi_count < 1)
1895            usbc_hcchar.s.ec = 1;
1896        else if (pipe->multi_count > 3)
1897            usbc_hcchar.s.ec = 3;
1898        else
1899            usbc_hcchar.s.ec = pipe->multi_count;
1900
1901        /* Set the rest of the endpoint specific settings */
1902        usbc_hcchar.s.devaddr = pipe->device_addr;
1903        usbc_hcchar.s.eptype = transaction->type;
1904        usbc_hcchar.s.lspddev = (pipe->device_speed == CVMX_USB_SPEED_LOW);
1905        usbc_hcchar.s.epdir = pipe->transfer_dir;
1906        usbc_hcchar.s.epnum = pipe->endpoint_num;
1907        usbc_hcchar.s.mps = pipe->max_packet;
1908        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
1909    }
1910
1911    /* Do transaction type specific fixups as needed */
1912    switch (transaction->type)
1913    {
1914        case CVMX_USB_TRANSFER_CONTROL:
1915            __cvmx_usb_start_channel_control(usb, channel, pipe);
1916            break;
1917        case CVMX_USB_TRANSFER_BULK:
1918        case CVMX_USB_TRANSFER_INTERRUPT:
1919            break;
1920        case CVMX_USB_TRANSFER_ISOCHRONOUS:
1921            if (!__cvmx_usb_pipe_needs_split(usb, pipe))
1922            {
1923                /* ISO transactions require different PIDs depending on direction
1924                    and how many packets are needed */
1925                if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT)
1926                {
1927                    if (pipe->multi_count < 2) /* Need DATA0 */
1928                        USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), cvmx_usbcx_hctsizx_t, pid, 0);
1929                    else /* Need MDATA */
1930                        USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), cvmx_usbcx_hctsizx_t, pid, 3);
1931                }
1932            }
1933            break;
1934    }
1935    {
1936        cvmx_usbcx_hctsizx_t usbc_hctsiz = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index))};
1937        transaction->xfersize = usbc_hctsiz.s.xfersize;
1938        transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1939    }
1940    /* Remeber when we start a split transaction */
1941    if (__cvmx_usb_pipe_needs_split(usb, pipe))
1942        usb->active_split = transaction;
1943    USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, chena, 1);
1944    if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1945        __cvmx_usb_fill_tx_fifo(usb, channel);
1946    CVMX_USB_RETURN_NOTHING();
1947}
1948
1949
1950/**
1951 * @INTERNAL
1952 * Find a pipe that is ready to be scheduled to hardware.
1953 * @param usb    USB device state populated by
1954 *               cvmx_usb_initialize().
1955 * @param list   Pipe list to search
1956 * @param current_frame
1957 *               Frame counter to use as a time reference.
1958 *
1959 * @return Pipe or NULL if none are ready
1960 */
1961static cvmx_usb_pipe_t *__cvmx_usb_find_ready_pipe(cvmx_usb_internal_state_t *usb, cvmx_usb_pipe_list_t *list, uint64_t current_frame)
1962{
1963    cvmx_usb_pipe_t *pipe = list->head;
1964    while (pipe)
1965    {
1966        if (!(pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED) && pipe->head &&
1967            (pipe->next_tx_frame <= current_frame) &&
1968            ((pipe->split_sc_frame == -1) || ((((int)current_frame - (int)pipe->split_sc_frame) & 0x7f) < 0x40)) &&
1969            (!usb->active_split || (usb->active_split == pipe->head)))
1970        {
1971            CVMX_PREFETCH(pipe, 128);
1972            CVMX_PREFETCH(pipe->head, 0);
1973            return pipe;
1974        }
1975        pipe = pipe->next;
1976    }
1977    return NULL;
1978}
1979
1980
1981/**
1982 * @INTERNAL
1983 * Called whenever a pipe might need to be scheduled to the
1984 * hardware.
1985 *
1986 * @param usb    USB device state populated by
1987 *               cvmx_usb_initialize().
1988 * @param is_sof True if this schedule was called on a SOF interrupt.
1989 */
1990static void __cvmx_usb_schedule(cvmx_usb_internal_state_t *usb, int is_sof)
1991{
1992    int channel;
1993    cvmx_usb_pipe_t *pipe;
1994    int need_sof;
1995    cvmx_usb_transfer_t ttype;
1996
1997    CVMX_USB_LOG_CALLED();
1998    CVMX_USB_LOG_PARAM("%p", usb);
1999
2000    if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
2001    {
2002        /* Without DMA we need to be careful to not schedule something at the end of a frame and cause an overrun */
2003        cvmx_usbcx_hfnum_t hfnum = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index))};
2004        cvmx_usbcx_hfir_t hfir = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFIR(usb->index))};
2005        if (hfnum.s.frrem < hfir.s.frint/4)
2006            goto done;
2007    }
2008
2009    while (usb->idle_hardware_channels)
2010    {
2011        /* Find an idle channel */
2012        CVMX_CLZ(channel, usb->idle_hardware_channels);
2013        channel = 31 - channel;
2014        if (cvmx_unlikely(channel > 7))
2015        {
2016            if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_INFO))
2017                cvmx_dprintf("%s: Idle hardware channels has a channel higher than 7. This is wrong\n", __FUNCTION__);
2018            break;
2019        }
2020
2021        /* Find a pipe needing service */
2022        pipe = NULL;
2023        if (is_sof)
2024        {
2025            /* Only process periodic pipes on SOF interrupts. This way we are
2026                sure that the periodic data is sent in the beginning of the
2027                frame */
2028            pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_ISOCHRONOUS, usb->frame_number);
2029            if (cvmx_likely(!pipe))
2030                pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_INTERRUPT, usb->frame_number);
2031        }
2032        if (cvmx_likely(!pipe))
2033        {
2034            pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_CONTROL, usb->frame_number);
2035            if (cvmx_likely(!pipe))
2036                pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_BULK, usb->frame_number);
2037        }
2038        if (!pipe)
2039            break;
2040
2041        CVMX_USB_LOG_PARAM("%d", channel);
2042        CVMX_USB_LOG_PARAM("%p", pipe);
2043
2044        if (cvmx_unlikely((usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_TRANSFERS) ||
2045            (pipe->flags & CVMX_USB_PIPE_FLAGS_DEBUG_TRANSFERS)))
2046        {
2047            cvmx_usb_transaction_t *transaction = pipe->head;
2048            const cvmx_usb_control_header_t *header = (transaction->control_header) ? cvmx_phys_to_ptr(transaction->control_header) : NULL;
2049            const char *dir = (pipe->transfer_dir == CVMX_USB_DIRECTION_IN) ? "IN" : "OUT";
2050            const char *type;
2051            switch (pipe->transfer_type)
2052            {
2053                case CVMX_USB_TRANSFER_CONTROL:
2054                    type = "SETUP";
2055                    dir = (header->s.request_type & 0x80) ? "IN" : "OUT";
2056                    break;
2057                case CVMX_USB_TRANSFER_ISOCHRONOUS:
2058                    type = "ISOCHRONOUS";
2059                    break;
2060                case CVMX_USB_TRANSFER_BULK:
2061                    type = "BULK";
2062                    break;
2063                default: /* CVMX_USB_TRANSFER_INTERRUPT */
2064                    type = "INTERRUPT";
2065                    break;
2066            }
2067            cvmx_dprintf("%s: Starting pipe %d, transaction %d on channel %d. %s %s len=%d header=0x%llx\n",
2068                         __FUNCTION__, __cvmx_usb_get_pipe_handle(usb, pipe),
2069                         __cvmx_usb_get_submit_handle(usb, transaction),
2070                         channel, type, dir,
2071                         transaction->buffer_length,
2072                         (header) ? (unsigned long long)header->u64 : 0ull);
2073        }
2074        __cvmx_usb_start_channel(usb, channel, pipe);
2075    }
2076
2077done:
2078    /* Only enable SOF interrupts when we have transactions pending in the
2079        future that might need to be scheduled */
2080    need_sof = 0;
2081    for (ttype=CVMX_USB_TRANSFER_CONTROL; ttype<=CVMX_USB_TRANSFER_INTERRUPT; ttype++)
2082    {
2083        pipe = usb->active_pipes[ttype].head;
2084        while (pipe)
2085        {
2086            if (pipe->next_tx_frame > usb->frame_number)
2087            {
2088                need_sof = 1;
2089                break;
2090            }
2091            pipe=pipe->next;
2092        }
2093    }
2094    USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, sofmsk, need_sof);
2095    CVMX_USB_RETURN_NOTHING();
2096}
2097
2098
2099/**
2100 * @INTERNAL
2101 * Call a user's callback for a specific reason.
2102 *
2103 * @param usb    USB device state populated by
2104 *               cvmx_usb_initialize().
2105 * @param pipe   Pipe the callback is for or NULL
2106 * @param transaction
2107 *               Transaction the callback is for or NULL
2108 * @param reason Reason this callback is being called
2109 * @param complete_code
2110 *               Completion code for the transaction, if any
2111 */
2112static void __cvmx_usb_perform_callback(cvmx_usb_internal_state_t *usb,
2113                                        cvmx_usb_pipe_t *pipe,
2114                                        cvmx_usb_transaction_t *transaction,
2115                                        cvmx_usb_callback_t reason,
2116                                        cvmx_usb_complete_t complete_code)
2117{
2118    cvmx_usb_callback_func_t callback = usb->callback[reason];
2119    void *user_data = usb->callback_data[reason];
2120    int submit_handle = -1;
2121    int pipe_handle = -1;
2122    int bytes_transferred = 0;
2123
2124    if (pipe)
2125        pipe_handle = __cvmx_usb_get_pipe_handle(usb, pipe);
2126
2127    if (transaction)
2128    {
2129        submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
2130        bytes_transferred = transaction->actual_bytes;
2131        /* Transactions are allowed to override the default callback */
2132        if ((reason == CVMX_USB_CALLBACK_TRANSFER_COMPLETE) && transaction->callback)
2133        {
2134            callback = transaction->callback;
2135            user_data = transaction->callback_data;
2136        }
2137    }
2138
2139    if (!callback)
2140        return;
2141
2142    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CALLBACKS))
2143        cvmx_dprintf("%*s%s: calling callback %p(usb=%p, complete_code=%s, "
2144                     "pipe_handle=%d, submit_handle=%d, bytes_transferred=%d, user_data=%p);\n",
2145                     2*usb->indent, "", __FUNCTION__, callback, usb,
2146                     __cvmx_usb_complete_to_string(complete_code),
2147                     pipe_handle, submit_handle, bytes_transferred, user_data);
2148
2149    callback((cvmx_usb_state_t *)usb, reason, complete_code, pipe_handle, submit_handle,
2150             bytes_transferred, user_data);
2151
2152    if (cvmx_unlikely(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_CALLBACKS))
2153        cvmx_dprintf("%*s%s: callback %p complete\n", 2*usb->indent, "",
2154                      __FUNCTION__, callback);
2155}
2156
2157
2158/**
2159 * @INTERNAL
2160 * Signal the completion of a transaction and free it. The
2161 * transaction will be removed from the pipe transaction list.
2162 *
2163 * @param usb    USB device state populated by
2164 *               cvmx_usb_initialize().
2165 * @param pipe   Pipe the transaction is on
2166 * @param transaction
2167 *               Transaction that completed
2168 * @param complete_code
2169 *               Completion code
2170 */
2171static void __cvmx_usb_perform_complete(cvmx_usb_internal_state_t * usb,
2172                                        cvmx_usb_pipe_t *pipe,
2173                                        cvmx_usb_transaction_t *transaction,
2174                                        cvmx_usb_complete_t complete_code)
2175{
2176    CVMX_USB_LOG_CALLED();
2177    CVMX_USB_LOG_PARAM("%p", usb);
2178    CVMX_USB_LOG_PARAM("%p", pipe);
2179    CVMX_USB_LOG_PARAM("%p", transaction);
2180    CVMX_USB_LOG_PARAM("%d", complete_code);
2181
2182    /* If this was a split then clear our split in progress marker */
2183    if (usb->active_split == transaction)
2184        usb->active_split = NULL;
2185
2186    /* Isochronous transactions need extra processing as they might not be done
2187        after a single data transfer */
2188    if (cvmx_unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS))
2189    {
2190        /* Update the number of bytes transferred in this ISO packet */
2191        transaction->iso_packets[0].length = transaction->actual_bytes;
2192        transaction->iso_packets[0].status = complete_code;
2193
2194        /* If there are more ISOs pending and we succeeded, schedule the next
2195            one */
2196        if ((transaction->iso_number_packets > 1) && (complete_code == CVMX_USB_COMPLETE_SUCCESS))
2197        {
2198            transaction->actual_bytes = 0;      /* No bytes transferred for this packet as of yet */
2199            transaction->iso_number_packets--;  /* One less ISO waiting to transfer */
2200            transaction->iso_packets++;         /* Increment to the next location in our packet array */
2201            transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2202            goto done;
2203        }
2204    }
2205
2206    /* Remove the transaction from the pipe list */
2207    if (transaction->next)
2208        transaction->next->prev = transaction->prev;
2209    else
2210        pipe->tail = transaction->prev;
2211    if (transaction->prev)
2212        transaction->prev->next = transaction->next;
2213    else
2214        pipe->head = transaction->next;
2215    if (!pipe->head)
2216    {
2217        __cvmx_usb_remove_pipe(usb->active_pipes + pipe->transfer_type, pipe);
2218        __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
2219
2220    }
2221    __cvmx_usb_perform_callback(usb, pipe, transaction,
2222                                CVMX_USB_CALLBACK_TRANSFER_COMPLETE,
2223                                complete_code);
2224    __cvmx_usb_free_transaction(usb, transaction);
2225done:
2226    CVMX_USB_RETURN_NOTHING();
2227}
2228
2229
2230/**
2231 * @INTERNAL
2232 * Submit a usb transaction to a pipe. Called for all types
2233 * of transactions.
2234 *
2235 * @param usb
2236 * @param pipe_handle
2237 *                  Which pipe to submit to. Will be validated in this function.
2238 * @param type      Transaction type
2239 * @param flags     Flags for the transaction
2240 * @param buffer    User buffer for the transaction
2241 * @param buffer_length
2242 *                  User buffer's length in bytes
2243 * @param control_header
2244 *                  For control transactions, the 8 byte standard header
2245 * @param iso_start_frame
2246 *                  For ISO transactions, the start frame
2247 * @param iso_number_packets
2248 *                  For ISO, the number of packet in the transaction.
2249 * @param iso_packets
2250 *                  A description of each ISO packet
2251 * @param callback  User callback to call when the transaction completes
2252 * @param user_data User's data for the callback
2253 *
2254 * @return Submit handle or negative on failure. Matches the result
2255 *         in the external API.
2256 */
2257static int __cvmx_usb_submit_transaction(cvmx_usb_internal_state_t *usb,
2258                                         int pipe_handle,
2259                                         cvmx_usb_transfer_t type,
2260                                         int flags,
2261                                         uint64_t buffer,
2262                                         int buffer_length,
2263                                         uint64_t control_header,
2264                                         int iso_start_frame,
2265                                         int iso_number_packets,
2266                                         cvmx_usb_iso_packet_t *iso_packets,
2267                                         cvmx_usb_callback_func_t callback,
2268                                         void *user_data)
2269{
2270    int submit_handle;
2271    cvmx_usb_transaction_t *transaction;
2272    cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2273
2274    CVMX_USB_LOG_CALLED();
2275    if (cvmx_unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2276        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2277    /* Fail if the pipe isn't open */
2278    if (cvmx_unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2279        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2280    if (cvmx_unlikely(pipe->transfer_type != type))
2281        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2282
2283    transaction = __cvmx_usb_alloc_transaction(usb);
2284    if (cvmx_unlikely(!transaction))
2285        CVMX_USB_RETURN(CVMX_USB_NO_MEMORY);
2286
2287    transaction->type = type;
2288    transaction->flags |= flags;
2289    transaction->buffer = buffer;
2290    transaction->buffer_length = buffer_length;
2291    transaction->control_header = control_header;
2292    transaction->iso_start_frame = iso_start_frame; // FIXME: This is not used, implement it
2293    transaction->iso_number_packets = iso_number_packets;
2294    transaction->iso_packets = iso_packets;
2295    transaction->callback = callback;
2296    transaction->callback_data = user_data;
2297    if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2298        transaction->stage = CVMX_USB_STAGE_SETUP;
2299    else
2300        transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2301
2302    transaction->next = NULL;
2303    if (pipe->tail)
2304    {
2305        transaction->prev = pipe->tail;
2306        transaction->prev->next = transaction;
2307    }
2308    else
2309    {
2310        if (pipe->next_tx_frame < usb->frame_number)
2311            pipe->next_tx_frame = usb->frame_number + pipe->interval -
2312                (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2313        transaction->prev = NULL;
2314        pipe->head = transaction;
2315        __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2316        __cvmx_usb_append_pipe(usb->active_pipes + pipe->transfer_type, pipe);
2317    }
2318    pipe->tail = transaction;
2319
2320    submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
2321
2322    /* We may need to schedule the pipe if this was the head of the pipe */
2323    if (!transaction->prev)
2324        __cvmx_usb_schedule(usb, 0);
2325
2326    CVMX_USB_RETURN(submit_handle);
2327}
2328
2329
2330/**
2331 * Call to submit a USB Bulk transfer to a pipe.
2332 *
2333 * @param state     USB device state populated by
2334 *                  cvmx_usb_initialize().
2335 * @param pipe_handle
2336 *                  Handle to the pipe for the transfer.
2337 * @param buffer    Physical address of the data buffer in
2338 *                  memory. Note that this is NOT A POINTER, but
2339 *                  the full 64bit physical address of the
2340 *                  buffer. This may be zero if buffer_length is
2341 *                  zero.
2342 * @param buffer_length
2343 *                  Length of buffer in bytes.
2344 * @param callback  Function to call when this transaction
2345 *                  completes. If the return value of this
2346 *                  function isn't an error, then this function
2347 *                  is guaranteed to be called when the
2348 *                  transaction completes. If this parameter is
2349 *                  NULL, then the generic callback registered
2350 *                  through cvmx_usb_register_callback is
2351 *                  called. If both are NULL, then there is no
2352 *                  way to know when a transaction completes.
2353 * @param user_data User supplied data returned when the
2354 *                  callback is called. This is only used if
2355 *                  callback in not NULL.
2356 *
2357 * @return A submitted transaction handle or negative on
2358 *         failure. Negative values are failure codes from
2359 *         cvmx_usb_status_t.
2360 */
2361int cvmx_usb_submit_bulk(cvmx_usb_state_t *state, int pipe_handle,
2362                                uint64_t buffer, int buffer_length,
2363                                cvmx_usb_callback_func_t callback,
2364                                void *user_data)
2365{
2366    int submit_handle;
2367    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2368
2369    CVMX_USB_LOG_CALLED();
2370    CVMX_USB_LOG_PARAM("%p", state);
2371    CVMX_USB_LOG_PARAM("%d", pipe_handle);
2372    CVMX_USB_LOG_PARAM("0x%llx", (unsigned long long)buffer);
2373    CVMX_USB_LOG_PARAM("%d", buffer_length);
2374
2375    /* Pipe handle checking is done later in a common place */
2376    if (cvmx_unlikely(!buffer))
2377        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2378    if (cvmx_unlikely(buffer_length < 0))
2379        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2380
2381    submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2382                                         CVMX_USB_TRANSFER_BULK,
2383                                         0, /* flags */
2384                                         buffer,
2385                                         buffer_length,
2386                                         0, /* control_header */
2387                                         0, /* iso_start_frame */
2388                                         0, /* iso_number_packets */
2389                                         NULL, /* iso_packets */
2390                                         callback,
2391                                         user_data);
2392    CVMX_USB_RETURN(submit_handle);
2393}
2394#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2395EXPORT_SYMBOL(cvmx_usb_submit_bulk);
2396#endif
2397
2398
2399/**
2400 * Call to submit a USB Interrupt transfer to a pipe.
2401 *
2402 * @param state     USB device state populated by
2403 *                  cvmx_usb_initialize().
2404 * @param pipe_handle
2405 *                  Handle to the pipe for the transfer.
2406 * @param buffer    Physical address of the data buffer in
2407 *                  memory. Note that this is NOT A POINTER, but
2408 *                  the full 64bit physical address of the
2409 *                  buffer. This may be zero if buffer_length is
2410 *                  zero.
2411 * @param buffer_length
2412 *                  Length of buffer in bytes.
2413 * @param callback  Function to call when this transaction
2414 *                  completes. If the return value of this
2415 *                  function isn't an error, then this function
2416 *                  is guaranteed to be called when the
2417 *                  transaction completes. If this parameter is
2418 *                  NULL, then the generic callback registered
2419 *                  through cvmx_usb_register_callback is
2420 *                  called. If both are NULL, then there is no
2421 *                  way to know when a transaction completes.
2422 * @param user_data User supplied data returned when the
2423 *                  callback is called. This is only used if
2424 *                  callback in not NULL.
2425 *
2426 * @return A submitted transaction handle or negative on
2427 *         failure. Negative values are failure codes from
2428 *         cvmx_usb_status_t.
2429 */
2430int cvmx_usb_submit_interrupt(cvmx_usb_state_t *state, int pipe_handle,
2431                              uint64_t buffer, int buffer_length,
2432                              cvmx_usb_callback_func_t callback,
2433                              void *user_data)
2434{
2435    int submit_handle;
2436    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2437
2438    CVMX_USB_LOG_CALLED();
2439    CVMX_USB_LOG_PARAM("%p", state);
2440    CVMX_USB_LOG_PARAM("%d", pipe_handle);
2441    CVMX_USB_LOG_PARAM("0x%llx", (unsigned long long)buffer);
2442    CVMX_USB_LOG_PARAM("%d", buffer_length);
2443
2444    /* Pipe handle checking is done later in a common place */
2445    if (cvmx_unlikely(!buffer))
2446        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2447    if (cvmx_unlikely(buffer_length < 0))
2448        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2449
2450    submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2451                                         CVMX_USB_TRANSFER_INTERRUPT,
2452                                         0, /* flags */
2453                                         buffer,
2454                                         buffer_length,
2455                                         0, /* control_header */
2456                                         0, /* iso_start_frame */
2457                                         0, /* iso_number_packets */
2458                                         NULL, /* iso_packets */
2459                                         callback,
2460                                         user_data);
2461    CVMX_USB_RETURN(submit_handle);
2462}
2463#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2464EXPORT_SYMBOL(cvmx_usb_submit_interrupt);
2465#endif
2466
2467
2468/**
2469 * Call to submit a USB Control transfer to a pipe.
2470 *
2471 * @param state     USB device state populated by
2472 *                  cvmx_usb_initialize().
2473 * @param pipe_handle
2474 *                  Handle to the pipe for the transfer.
2475 * @param control_header
2476 *                  USB 8 byte control header physical address.
2477 *                  Note that this is NOT A POINTER, but the
2478 *                  full 64bit physical address of the buffer.
2479 * @param buffer    Physical address of the data buffer in
2480 *                  memory. Note that this is NOT A POINTER, but
2481 *                  the full 64bit physical address of the
2482 *                  buffer. This may be zero if buffer_length is
2483 *                  zero.
2484 * @param buffer_length
2485 *                  Length of buffer in bytes.
2486 * @param callback  Function to call when this transaction
2487 *                  completes. If the return value of this
2488 *                  function isn't an error, then this function
2489 *                  is guaranteed to be called when the
2490 *                  transaction completes. If this parameter is
2491 *                  NULL, then the generic callback registered
2492 *                  through cvmx_usb_register_callback is
2493 *                  called. If both are NULL, then there is no
2494 *                  way to know when a transaction completes.
2495 * @param user_data User supplied data returned when the
2496 *                  callback is called. This is only used if
2497 *                  callback in not NULL.
2498 *
2499 * @return A submitted transaction handle or negative on
2500 *         failure. Negative values are failure codes from
2501 *         cvmx_usb_status_t.
2502 */
2503int cvmx_usb_submit_control(cvmx_usb_state_t *state, int pipe_handle,
2504                            uint64_t control_header,
2505                            uint64_t buffer, int buffer_length,
2506                            cvmx_usb_callback_func_t callback,
2507                            void *user_data)
2508{
2509    int submit_handle;
2510    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2511    cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(control_header);
2512
2513    CVMX_USB_LOG_CALLED();
2514    CVMX_USB_LOG_PARAM("%p", state);
2515    CVMX_USB_LOG_PARAM("%d", pipe_handle);
2516    CVMX_USB_LOG_PARAM("0x%llx", (unsigned long long)control_header);
2517    CVMX_USB_LOG_PARAM("0x%llx", (unsigned long long)buffer);
2518    CVMX_USB_LOG_PARAM("%d", buffer_length);
2519
2520    /* Pipe handle checking is done later in a common place */
2521    if (cvmx_unlikely(!control_header))
2522        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2523    /* Some drivers send a buffer with a zero length. God only knows why */
2524    if (cvmx_unlikely(buffer && (buffer_length < 0)))
2525        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2526    if (cvmx_unlikely(!buffer && (buffer_length != 0)))
2527        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2528    if ((header->s.request_type & 0x80) == 0)
2529        buffer_length = cvmx_le16_to_cpu(header->s.length);
2530
2531    submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2532                                         CVMX_USB_TRANSFER_CONTROL,
2533                                         0, /* flags */
2534                                         buffer,
2535                                         buffer_length,
2536                                         control_header,
2537                                         0, /* iso_start_frame */
2538                                         0, /* iso_number_packets */
2539                                         NULL, /* iso_packets */
2540                                         callback,
2541                                         user_data);
2542    CVMX_USB_RETURN(submit_handle);
2543}
2544#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2545EXPORT_SYMBOL(cvmx_usb_submit_control);
2546#endif
2547
2548
2549/**
2550 * Call to submit a USB Isochronous transfer to a pipe.
2551 *
2552 * @param state     USB device state populated by
2553 *                  cvmx_usb_initialize().
2554 * @param pipe_handle
2555 *                  Handle to the pipe for the transfer.
2556 * @param start_frame
2557 *                  Number of frames into the future to schedule
2558 *                  this transaction.
2559 * @param flags     Flags to control the transfer. See
2560 *                  cvmx_usb_isochronous_flags_t for the flag
2561 *                  definitions.
2562 * @param number_packets
2563 *                  Number of sequential packets to transfer.
2564 *                  "packets" is a pointer to an array of this
2565 *                  many packet structures.
2566 * @param packets   Description of each transfer packet as
2567 *                  defined by cvmx_usb_iso_packet_t. The array
2568 *                  pointed to here must stay valid until the
2569 *                  complete callback is called.
2570 * @param buffer    Physical address of the data buffer in
2571 *                  memory. Note that this is NOT A POINTER, but
2572 *                  the full 64bit physical address of the
2573 *                  buffer. This may be zero if buffer_length is
2574 *                  zero.
2575 * @param buffer_length
2576 *                  Length of buffer in bytes.
2577 * @param callback  Function to call when this transaction
2578 *                  completes. If the return value of this
2579 *                  function isn't an error, then this function
2580 *                  is guaranteed to be called when the
2581 *                  transaction completes. If this parameter is
2582 *                  NULL, then the generic callback registered
2583 *                  through cvmx_usb_register_callback is
2584 *                  called. If both are NULL, then there is no
2585 *                  way to know when a transaction completes.
2586 * @param user_data User supplied data returned when the
2587 *                  callback is called. This is only used if
2588 *                  callback in not NULL.
2589 *
2590 * @return A submitted transaction handle or negative on
2591 *         failure. Negative values are failure codes from
2592 *         cvmx_usb_status_t.
2593 */
2594int cvmx_usb_submit_isochronous(cvmx_usb_state_t *state, int pipe_handle,
2595                                int start_frame, int flags,
2596                                int number_packets,
2597                                cvmx_usb_iso_packet_t packets[],
2598                                uint64_t buffer, int buffer_length,
2599                                cvmx_usb_callback_func_t callback,
2600                                void *user_data)
2601{
2602    int submit_handle;
2603    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2604
2605    CVMX_USB_LOG_CALLED();
2606    CVMX_USB_LOG_PARAM("%p", state);
2607    CVMX_USB_LOG_PARAM("%d", pipe_handle);
2608    CVMX_USB_LOG_PARAM("%d", start_frame);
2609    CVMX_USB_LOG_PARAM("0x%x", flags);
2610    CVMX_USB_LOG_PARAM("%d", number_packets);
2611    CVMX_USB_LOG_PARAM("%p", packets);
2612    CVMX_USB_LOG_PARAM("0x%llx", (unsigned long long)buffer);
2613    CVMX_USB_LOG_PARAM("%d", buffer_length);
2614
2615    /* Pipe handle checking is done later in a common place */
2616    if (cvmx_unlikely(start_frame < 0))
2617        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2618    if (cvmx_unlikely(flags & ~(CVMX_USB_ISOCHRONOUS_FLAGS_ALLOW_SHORT | CVMX_USB_ISOCHRONOUS_FLAGS_ASAP)))
2619        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2620    if (cvmx_unlikely(number_packets < 1))
2621        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2622    if (cvmx_unlikely(!packets))
2623        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2624    if (cvmx_unlikely(!buffer))
2625        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2626    if (cvmx_unlikely(buffer_length < 0))
2627        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2628
2629    submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2630                                         CVMX_USB_TRANSFER_ISOCHRONOUS,
2631                                         flags,
2632                                         buffer,
2633                                         buffer_length,
2634                                         0, /* control_header */
2635                                         start_frame,
2636                                         number_packets,
2637                                         packets,
2638                                         callback,
2639                                         user_data);
2640    CVMX_USB_RETURN(submit_handle);
2641}
2642#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2643EXPORT_SYMBOL(cvmx_usb_submit_isochronous);
2644#endif
2645
2646
2647/**
2648 * Cancel one outstanding request in a pipe. Canceling a request
2649 * can fail if the transaction has already completed before cancel
2650 * is called. Even after a successful cancel call, it may take
2651 * a frame or two for the cvmx_usb_poll() function to call the
2652 * associated callback.
2653 *
2654 * @param state  USB device state populated by
2655 *               cvmx_usb_initialize().
2656 * @param pipe_handle
2657 *               Pipe handle to cancel requests in.
2658 * @param submit_handle
2659 *               Handle to transaction to cancel, returned by the submit function.
2660 *
2661 * @return CVMX_USB_SUCCESS or a negative error code defined in
2662 *         cvmx_usb_status_t.
2663 */
2664cvmx_usb_status_t cvmx_usb_cancel(cvmx_usb_state_t *state, int pipe_handle,
2665                                  int submit_handle)
2666{
2667    cvmx_usb_transaction_t *transaction;
2668    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2669    cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2670
2671    CVMX_USB_LOG_CALLED();
2672    CVMX_USB_LOG_PARAM("%p", state);
2673    CVMX_USB_LOG_PARAM("%d", pipe_handle);
2674    CVMX_USB_LOG_PARAM("%d", submit_handle);
2675
2676    if (cvmx_unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2677        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2678    if (cvmx_unlikely((submit_handle < 0) || (submit_handle >= MAX_TRANSACTIONS)))
2679        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2680
2681    /* Fail if the pipe isn't open */
2682    if (cvmx_unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2683        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2684
2685    transaction = usb->transaction + submit_handle;
2686
2687    /* Fail if this transaction already completed */
2688    if (cvmx_unlikely((transaction->flags & __CVMX_USB_TRANSACTION_FLAGS_IN_USE) == 0))
2689        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2690
2691    /* If the transaction is the HEAD of the queue and scheduled. We need to
2692        treat it special */
2693    if ((pipe->head == transaction) &&
2694        (pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED))
2695    {
2696        cvmx_usbcx_hccharx_t usbc_hcchar;
2697
2698        usb->pipe_for_channel[pipe->channel] = NULL;
2699        pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2700
2701        CVMX_SYNCW;
2702
2703        usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
2704        /* If the channel isn't enabled then the transaction already completed */
2705        if (usbc_hcchar.s.chena)
2706        {
2707            usbc_hcchar.s.chdis = 1;
2708            __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index), usbc_hcchar.u32);
2709        }
2710    }
2711    __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_CANCEL);
2712    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
2713}
2714#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2715EXPORT_SYMBOL(cvmx_usb_cancel);
2716#endif
2717
2718
2719/**
2720 * Cancel all outstanding requests in a pipe. Logically all this
2721 * does is call cvmx_usb_cancel() in a loop.
2722 *
2723 * @param state  USB device state populated by
2724 *               cvmx_usb_initialize().
2725 * @param pipe_handle
2726 *               Pipe handle to cancel requests in.
2727 *
2728 * @return CVMX_USB_SUCCESS or a negative error code defined in
2729 *         cvmx_usb_status_t.
2730 */
2731cvmx_usb_status_t cvmx_usb_cancel_all(cvmx_usb_state_t *state, int pipe_handle)
2732{
2733    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2734    cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2735
2736    CVMX_USB_LOG_CALLED();
2737    CVMX_USB_LOG_PARAM("%p", state);
2738    CVMX_USB_LOG_PARAM("%d", pipe_handle);
2739    if (cvmx_unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2740        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2741
2742    /* Fail if the pipe isn't open */
2743    if (cvmx_unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2744        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2745
2746    /* Simply loop through and attempt to cancel each transaction */
2747    while (pipe->head)
2748    {
2749        cvmx_usb_status_t result = cvmx_usb_cancel(state, pipe_handle,
2750            __cvmx_usb_get_submit_handle(usb, pipe->head));
2751        if (cvmx_unlikely(result != CVMX_USB_SUCCESS))
2752            CVMX_USB_RETURN(result);
2753    }
2754    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
2755}
2756#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2757EXPORT_SYMBOL(cvmx_usb_cancel_all);
2758#endif
2759
2760
2761/**
2762 * Close a pipe created with cvmx_usb_open_pipe().
2763 *
2764 * @param state  USB device state populated by
2765 *               cvmx_usb_initialize().
2766 * @param pipe_handle
2767 *               Pipe handle to close.
2768 *
2769 * @return CVMX_USB_SUCCESS or a negative error code defined in
2770 *         cvmx_usb_status_t. CVMX_USB_BUSY is returned if the
2771 *         pipe has outstanding transfers.
2772 */
2773cvmx_usb_status_t cvmx_usb_close_pipe(cvmx_usb_state_t *state, int pipe_handle)
2774{
2775    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2776    cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2777
2778    CVMX_USB_LOG_CALLED();
2779    CVMX_USB_LOG_PARAM("%p", state);
2780    CVMX_USB_LOG_PARAM("%d", pipe_handle);
2781    if (cvmx_unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2782        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2783
2784    /* Fail if the pipe isn't open */
2785    if (cvmx_unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2786        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2787
2788    /* Fail if the pipe has pending transactions */
2789    if (cvmx_unlikely(pipe->head))
2790        CVMX_USB_RETURN(CVMX_USB_BUSY);
2791
2792    pipe->flags = 0;
2793    __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2794    __cvmx_usb_append_pipe(&usb->free_pipes, pipe);
2795
2796    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
2797}
2798#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2799EXPORT_SYMBOL(cvmx_usb_close_pipe);
2800#endif
2801
2802
2803/**
2804 * Register a function to be called when various USB events occur.
2805 *
2806 * @param state     USB device state populated by
2807 *                  cvmx_usb_initialize().
2808 * @param reason    Which event to register for.
2809 * @param callback  Function to call when the event occurs.
2810 * @param user_data User data parameter to the function.
2811 *
2812 * @return CVMX_USB_SUCCESS or a negative error code defined in
2813 *         cvmx_usb_status_t.
2814 */
2815cvmx_usb_status_t cvmx_usb_register_callback(cvmx_usb_state_t *state,
2816                                             cvmx_usb_callback_t reason,
2817                                             cvmx_usb_callback_func_t callback,
2818                                             void *user_data)
2819{
2820    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2821
2822    CVMX_USB_LOG_CALLED();
2823    CVMX_USB_LOG_PARAM("%p", state);
2824    CVMX_USB_LOG_PARAM("%d", reason);
2825    CVMX_USB_LOG_PARAM("%p", callback);
2826    CVMX_USB_LOG_PARAM("%p", user_data);
2827    if (cvmx_unlikely(reason >= __CVMX_USB_CALLBACK_END))
2828        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2829    if (cvmx_unlikely(!callback))
2830        CVMX_USB_RETURN(CVMX_USB_INVALID_PARAM);
2831
2832    usb->callback[reason] = callback;
2833    usb->callback_data[reason] = user_data;
2834
2835    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
2836}
2837#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2838EXPORT_SYMBOL(cvmx_usb_register_callback);
2839#endif
2840
2841
2842/**
2843 * Get the current USB protocol level frame number. The frame
2844 * number is always in the range of 0-0x7ff.
2845 *
2846 * @param state  USB device state populated by
2847 *               cvmx_usb_initialize().
2848 *
2849 * @return USB frame number
2850 */
2851int cvmx_usb_get_frame_number(cvmx_usb_state_t *state)
2852{
2853    int frame_number;
2854    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
2855    cvmx_usbcx_hfnum_t usbc_hfnum;
2856
2857    CVMX_USB_LOG_CALLED();
2858    CVMX_USB_LOG_PARAM("%p", state);
2859
2860    usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2861    frame_number = usbc_hfnum.s.frnum;
2862
2863    CVMX_USB_RETURN(frame_number);
2864}
2865#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
2866EXPORT_SYMBOL(cvmx_usb_get_frame_number);
2867#endif
2868
2869
2870/**
2871 * @INTERNAL
2872 * Poll a channel for status
2873 *
2874 * @param usb     USB device
2875 * @param channel Channel to poll
2876 *
2877 * @return Zero on success
2878 */
2879static int __cvmx_usb_poll_channel(cvmx_usb_internal_state_t *usb, int channel)
2880{
2881    cvmx_usbcx_hcintx_t usbc_hcint;
2882    cvmx_usbcx_hctsizx_t usbc_hctsiz;
2883    cvmx_usbcx_hccharx_t usbc_hcchar;
2884    cvmx_usb_pipe_t *pipe;
2885    cvmx_usb_transaction_t *transaction;
2886    int bytes_this_transfer;
2887    int bytes_in_last_packet;
2888    int packets_processed;
2889    int buffer_space_left;
2890    CVMX_USB_LOG_CALLED();
2891    CVMX_USB_LOG_PARAM("%p", usb);
2892    CVMX_USB_LOG_PARAM("%d", channel);
2893
2894    /* Read the interrupt status bits for the channel */
2895    usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
2896
2897#if 0
2898    cvmx_dprintf("Channel %d%s%s%s%s%s%s%s%s%s%s%s\n", channel,
2899        (usbc_hcint.s.datatglerr) ? " DATATGLERR" : "",
2900        (usbc_hcint.s.frmovrun) ? " FRMOVRUN" : "",
2901        (usbc_hcint.s.bblerr) ? " BBLERR" : "",
2902        (usbc_hcint.s.xacterr) ? " XACTERR" : "",
2903        (usbc_hcint.s.nyet) ? " NYET" : "",
2904        (usbc_hcint.s.ack) ? " ACK" : "",
2905        (usbc_hcint.s.nak) ? " NAK" : "",
2906        (usbc_hcint.s.stall) ? " STALL" : "",
2907        (usbc_hcint.s.ahberr) ? " AHBERR" : "",
2908        (usbc_hcint.s.chhltd) ? " CHHLTD" : "",
2909        (usbc_hcint.s.xfercompl) ? " XFERCOMPL" : "");
2910#endif
2911
2912    if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
2913    {
2914        usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2915
2916        if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis)
2917        {
2918            /* There seems to be a bug in CN31XX which can cause interrupt
2919                IN transfers to get stuck until we do a write of HCCHARX
2920                without changing things */
2921            __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2922            CVMX_USB_RETURN(0);
2923        }
2924
2925        /* In non DMA mode the channels don't halt themselves. We need to
2926            manually disable channels that are left running */
2927        if (!usbc_hcint.s.chhltd)
2928        {
2929            if (usbc_hcchar.s.chena)
2930            {
2931                cvmx_usbcx_hcintmskx_t hcintmsk;
2932                /* Disable all interrupts except CHHLTD */
2933                hcintmsk.u32 = 0;
2934                hcintmsk.s.chhltdmsk = 1;
2935                __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), hcintmsk.u32);
2936                usbc_hcchar.s.chdis = 1;
2937                __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2938                CVMX_USB_RETURN(0);
2939            }
2940            else if (usbc_hcint.s.xfercompl)
2941            {
2942                /* Successful IN/OUT with transfer complete. Channel halt isn't needed */
2943            }
2944            else
2945            {
2946                cvmx_dprintf("USB%d: Channel %d interrupt without halt\n", usb->index, channel);
2947                CVMX_USB_RETURN(0);
2948            }
2949        }
2950    }
2951    else
2952    {
2953        /* There is are no interrupts that we need to process when the channel is
2954            still running */
2955        if (!usbc_hcint.s.chhltd)
2956            CVMX_USB_RETURN(0);
2957    }
2958
2959    /* Disable the channel interrupts now that it is done */
2960    __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
2961    usb->idle_hardware_channels |= (1<<channel);
2962
2963    /* Make sure this channel is tied to a valid pipe */
2964    pipe = usb->pipe_for_channel[channel];
2965    CVMX_PREFETCH(pipe, 0);
2966    CVMX_PREFETCH(pipe, 128);
2967    if (!pipe)
2968        CVMX_USB_RETURN(0);
2969    transaction = pipe->head;
2970    CVMX_PREFETCH0(transaction);
2971
2972    /* Disconnect this pipe from the HW channel. Later the schedule function will
2973        figure out which pipe needs to go */
2974    usb->pipe_for_channel[channel] = NULL;
2975    pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2976
2977    /* Read the channel config info so we can figure out how much data
2978        transfered */
2979    usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2980    usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
2981
2982    /* Calculating the number of bytes successfully transferred is dependent on
2983        the transfer direction */
2984    packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2985    if (usbc_hcchar.s.epdir)
2986    {
2987        /* IN transactions are easy. For every byte received the hardware
2988            decrements xfersize. All we need to do is subtract the current
2989            value of xfersize from its starting value and we know how many
2990            bytes were written to the buffer */
2991        bytes_this_transfer = transaction->xfersize - usbc_hctsiz.s.xfersize;
2992    }
2993    else
2994    {
2995        /* OUT transaction don't decrement xfersize. Instead pktcnt is
2996            decremented on every successful packet send. The hardware does
2997            this when it receives an ACK, or NYET. If it doesn't
2998            receive one of these responses pktcnt doesn't change */
2999        bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
3000        /* The last packet may not be a full transfer if we didn't have
3001            enough data */
3002        if (bytes_this_transfer > transaction->xfersize)
3003            bytes_this_transfer = transaction->xfersize;
3004    }
3005    /* Figure out how many bytes were in the last packet of the transfer */
3006    if (packets_processed)
3007        bytes_in_last_packet = bytes_this_transfer - (packets_processed-1) * usbc_hcchar.s.mps;
3008    else
3009        bytes_in_last_packet = bytes_this_transfer;
3010
3011    /* As a special case, setup transactions output the setup header, not
3012        the user's data. For this reason we don't count setup data as bytes
3013        transferred */
3014    if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
3015        (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
3016        bytes_this_transfer = 0;
3017
3018    /* Optional debug output */
3019    if (cvmx_unlikely((usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_DEBUG_TRANSFERS) ||
3020        (pipe->flags & CVMX_USB_PIPE_FLAGS_DEBUG_TRANSFERS)))
3021        cvmx_dprintf("%s: Channel %d halted. Pipe %d transaction %d stage %d bytes=%d\n",
3022                     __FUNCTION__, channel,
3023                     __cvmx_usb_get_pipe_handle(usb, pipe),
3024                     __cvmx_usb_get_submit_handle(usb, transaction),
3025                     transaction->stage, bytes_this_transfer);
3026
3027    /* Add the bytes transferred to the running total. It is important that
3028        bytes_this_transfer doesn't count any data that needs to be
3029        retransmitted */
3030    transaction->actual_bytes += bytes_this_transfer;
3031    if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
3032        buffer_space_left = transaction->iso_packets[0].length - transaction->actual_bytes;
3033    else
3034        buffer_space_left = transaction->buffer_length - transaction->actual_bytes;
3035
3036    /* We need to remember the PID toggle state for the next transaction. The
3037        hardware already updated it for the next transaction */
3038    pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
3039
3040    /* For high speed bulk out, assume the next transaction will need to do a
3041        ping before proceeding. If this isn't true the ACK processing below
3042        will clear this flag */
3043    if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
3044        (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
3045        (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
3046        pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
3047
3048    if (usbc_hcint.s.stall)
3049    {
3050        /* STALL as a response means this transaction cannot be completed
3051            because the device can't process transactions. Tell the user. Any
3052            data that was transferred will be counted on the actual bytes
3053            transferred */
3054        pipe->pid_toggle = 0;
3055        __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_STALL);
3056    }
3057    else if (usbc_hcint.s.xacterr)
3058    {
3059        /* We know at least one packet worked if we get a ACK or NAK. Reset the retry counter */
3060        if (usbc_hcint.s.nak || usbc_hcint.s.ack)
3061            transaction->retries = 0;
3062        transaction->retries++;
3063        if (transaction->retries > MAX_RETRIES)
3064        {
3065            /* XactErr as a response means the device signaled something wrong with
3066                the transfer. For example, PID toggle errors cause these */
3067            __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_XACTERR);
3068        }
3069        else
3070        {
3071            /* If this was a split then clear our split in progress marker */
3072            if (usb->active_split == transaction)
3073                usb->active_split = NULL;
3074            /* Rewind to the beginning of the transaction by anding off the
3075                split complete bit */
3076            transaction->stage &= ~1;
3077            pipe->split_sc_frame = -1;
3078            pipe->next_tx_frame += pipe->interval;
3079            if (pipe->next_tx_frame < usb->frame_number)
3080                pipe->next_tx_frame = usb->frame_number + pipe->interval -
3081                    (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
3082        }
3083    }
3084    else if (usbc_hcint.s.bblerr)
3085    {
3086        /* Babble Error (BblErr) */
3087        __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_BABBLEERR);
3088    }
3089    else if (usbc_hcint.s.datatglerr)
3090    {
3091        /* We'll retry the exact same transaction again */
3092        transaction->retries++;
3093    }
3094    else if (usbc_hcint.s.nyet)
3095    {
3096        /* NYET as a response is only allowed in three cases: as a response to
3097            a ping, as a response to a split transaction, and as a response to
3098            a bulk out. The ping case is handled by hardware, so we only have
3099            splits and bulk out */
3100        if (!__cvmx_usb_pipe_needs_split(usb, pipe))
3101        {
3102            transaction->retries = 0;
3103            /* If there is more data to go then we need to try again. Otherwise
3104                this transaction is complete */
3105            if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet))
3106                __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3107        }
3108        else
3109        {
3110            /* Split transactions retry the split complete 4 times then rewind
3111                to the start split and do the entire transactions again */
3112            transaction->retries++;
3113            if ((transaction->retries & 0x3) == 0)
3114            {
3115                /* Rewind to the beginning of the transaction by anding off the
3116                    split complete bit */
3117                transaction->stage &= ~1;
3118                pipe->split_sc_frame = -1;
3119            }
3120        }
3121    }
3122    else if (usbc_hcint.s.ack)
3123    {
3124        transaction->retries = 0;
3125        /* The ACK bit can only be checked after the other error bits. This is
3126            because a multi packet transfer may succeed in a number of packets
3127            and then get a different response on the last packet. In this case
3128            both ACK and the last response bit will be set. If none of the
3129            other response bits is set, then the last packet must have been an
3130            ACK */
3131
3132        /* Since we got an ACK, we know we don't need to do a ping on this
3133            pipe */
3134        pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_NEED_PING;
3135
3136        switch (transaction->type)
3137        {
3138            case CVMX_USB_TRANSFER_CONTROL:
3139                switch (transaction->stage)
3140                {
3141                    case CVMX_USB_STAGE_NON_CONTROL:
3142                    case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
3143                        /* This should be impossible */
3144                        __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
3145                        break;
3146                    case CVMX_USB_STAGE_SETUP:
3147                        pipe->pid_toggle = 1;
3148                        if (__cvmx_usb_pipe_needs_split(usb, pipe))
3149                            transaction->stage = CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
3150                        else
3151                        {
3152                            cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(transaction->control_header);
3153                            if (header->s.length)
3154                                transaction->stage = CVMX_USB_STAGE_DATA;
3155                            else
3156                                transaction->stage = CVMX_USB_STAGE_STATUS;
3157                        }
3158                        break;
3159                    case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
3160                        {
3161                            cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(transaction->control_header);
3162                            if (header->s.length)
3163                                transaction->stage = CVMX_USB_STAGE_DATA;
3164                            else
3165                                transaction->stage = CVMX_USB_STAGE_STATUS;
3166                        }
3167                        break;
3168                    case CVMX_USB_STAGE_DATA:
3169                        if (__cvmx_usb_pipe_needs_split(usb, pipe))
3170                        {
3171                            transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
3172                            /* For setup OUT data that are splits, the hardware
3173                                doesn't appear to count transferred data. Here
3174                                we manually update the data transferred */
3175                            if (!usbc_hcchar.s.epdir)
3176                            {
3177                                if (buffer_space_left < pipe->max_packet)
3178                                    transaction->actual_bytes += buffer_space_left;
3179                                else
3180                                    transaction->actual_bytes += pipe->max_packet;
3181                            }
3182                        }
3183                        else if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet))
3184                        {
3185                            pipe->pid_toggle = 1;
3186                            transaction->stage = CVMX_USB_STAGE_STATUS;
3187                        }
3188                        break;
3189                    case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
3190                        if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet))
3191                        {
3192                            pipe->pid_toggle = 1;
3193                            transaction->stage = CVMX_USB_STAGE_STATUS;
3194                        }
3195                        else
3196                        {
3197                            transaction->stage = CVMX_USB_STAGE_DATA;
3198                        }
3199                        break;
3200                    case CVMX_USB_STAGE_STATUS:
3201                        if (__cvmx_usb_pipe_needs_split(usb, pipe))
3202                            transaction->stage = CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
3203                        else
3204                            __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3205                        break;
3206                    case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
3207                        __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3208                        break;
3209                }
3210                break;
3211            case CVMX_USB_TRANSFER_BULK:
3212            case CVMX_USB_TRANSFER_INTERRUPT:
3213                /* The only time a bulk transfer isn't complete when
3214                    it finishes with an ACK is during a split transaction. For
3215                    splits we need to continue the transfer if more data is
3216                    needed */
3217                if (__cvmx_usb_pipe_needs_split(usb, pipe))
3218                {
3219                    if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
3220                        transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
3221                    else
3222                    {
3223                        if (buffer_space_left && (bytes_in_last_packet == pipe->max_packet))
3224                            transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
3225                        else
3226                        {
3227                            if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
3228                                pipe->next_tx_frame += pipe->interval;
3229                            __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3230                        }
3231                    }
3232                }
3233                else
3234                {
3235                    if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
3236                        (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
3237                        (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
3238                        (usbc_hcint.s.nak))
3239                        pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
3240                    if (!buffer_space_left || (bytes_in_last_packet < pipe->max_packet))
3241                    {
3242                        if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
3243                            pipe->next_tx_frame += pipe->interval;
3244                        __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3245                    }
3246                }
3247                break;
3248            case CVMX_USB_TRANSFER_ISOCHRONOUS:
3249                if (__cvmx_usb_pipe_needs_split(usb, pipe))
3250                {
3251                    /* ISOCHRONOUS OUT splits don't require a complete split stage.
3252                        Instead they use a sequence of begin OUT splits to transfer
3253                        the data 188 bytes at a time. Once the transfer is complete,
3254                        the pipe sleeps until the next schedule interval */
3255                    if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT)
3256                    {
3257                        /* If no space left or this wasn't a max size packet then
3258                            this transfer is complete. Otherwise start it again
3259                            to send the next 188 bytes */
3260                        if (!buffer_space_left || (bytes_this_transfer < 188))
3261                        {
3262                            pipe->next_tx_frame += pipe->interval;
3263                            __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3264                        }
3265                    }
3266                    else
3267                    {
3268                        if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE)
3269                        {
3270                            /* We are in the incoming data phase. Keep getting
3271                                data until we run out of space or get a small
3272                                packet */
3273                            if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet))
3274                            {
3275                                pipe->next_tx_frame += pipe->interval;
3276                                __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3277                            }
3278                        }
3279                        else
3280                            transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
3281                    }
3282                }
3283                else
3284                {
3285                    pipe->next_tx_frame += pipe->interval;
3286                    __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3287                }
3288                break;
3289        }
3290    }
3291    else if (usbc_hcint.s.nak)
3292    {
3293        /* If this was a split then clear our split in progress marker */
3294        if (usb->active_split == transaction)
3295            usb->active_split = NULL;
3296        /* NAK as a response means the device couldn't accept the transaction,
3297            but it should be retried in the future. Rewind to the beginning of
3298            the transaction by anding off the split complete bit. Retry in the
3299            next interval */
3300        transaction->retries = 0;
3301        transaction->stage &= ~1;
3302        pipe->next_tx_frame += pipe->interval;
3303        if (pipe->next_tx_frame < usb->frame_number)
3304            pipe->next_tx_frame = usb->frame_number + pipe->interval -
3305                (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
3306    }
3307    else
3308    {
3309        cvmx_usb_port_status_t port;
3310        port = cvmx_usb_get_status((cvmx_usb_state_t *)usb);
3311        if (port.port_enabled)
3312        {
3313            /* We'll retry the exact same transaction again */
3314            transaction->retries++;
3315        }
3316        else
3317        {
3318            /* We get channel halted interrupts with no result bits sets when the
3319                cable is unplugged */
3320            __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
3321        }
3322    }
3323    CVMX_USB_RETURN(0);
3324}
3325
3326
3327/**
3328 * Poll the USB block for status and call all needed callback
3329 * handlers. This function is meant to be called in the interrupt
3330 * handler for the USB controller. It can also be called
3331 * periodically in a loop for non-interrupt based operation.
3332 *
3333 * @param state  USB device state populated by
3334 *               cvmx_usb_initialize().
3335 *
3336 * @return CVMX_USB_SUCCESS or a negative error code defined in
3337 *         cvmx_usb_status_t.
3338 */
3339cvmx_usb_status_t cvmx_usb_poll(cvmx_usb_state_t *state)
3340{
3341    cvmx_usbcx_hfnum_t usbc_hfnum;
3342    cvmx_usbcx_gintsts_t usbc_gintsts;
3343    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
3344
3345    CVMX_PREFETCH(usb, 0);
3346    CVMX_PREFETCH(usb, 1*128);
3347    CVMX_PREFETCH(usb, 2*128);
3348    CVMX_PREFETCH(usb, 3*128);
3349    CVMX_PREFETCH(usb, 4*128);
3350
3351    CVMX_USB_LOG_CALLED();
3352    CVMX_USB_LOG_PARAM("%p", state);
3353
3354    /* Update the frame counter */
3355    usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
3356    if ((usb->frame_number&0x3fff) > usbc_hfnum.s.frnum)
3357        usb->frame_number += 0x4000;
3358    usb->frame_number &= ~0x3fffull;
3359    usb->frame_number |= usbc_hfnum.s.frnum;
3360
3361    /* Read the pending interrupts */
3362    usbc_gintsts.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTSTS(usb->index));
3363
3364    /* Clear the interrupts now that we know about them */
3365    __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index), usbc_gintsts.u32);
3366
3367    if (usbc_gintsts.s.rxflvl)
3368    {
3369        /* RxFIFO Non-Empty (RxFLvl)
3370            Indicates that there is at least one packet pending to be read
3371            from the RxFIFO. */
3372        /* In DMA mode this is handled by hardware */
3373        if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3374            __cvmx_usb_poll_rx_fifo(usb);
3375    }
3376    if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp)
3377    {
3378        /* Fill the Tx FIFOs when not in DMA mode */
3379        if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3380            __cvmx_usb_poll_tx_fifo(usb);
3381    }
3382    if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint)
3383    {
3384        cvmx_usbcx_hprt_t usbc_hprt;
3385        /* Disconnect Detected Interrupt (DisconnInt)
3386            Asserted when a device disconnect is detected. */
3387
3388        /* Host Port Interrupt (PrtInt)
3389            The core sets this bit to indicate a change in port status of one
3390            of the O2P USB core ports in Host mode. The application must
3391            read the Host Port Control and Status (HPRT) register to
3392            determine the exact event that caused this interrupt. The
3393            application must clear the appropriate status bit in the Host Port
3394            Control and Status register to clear this bit. */
3395
3396        /* Call the user's port callback */
3397        __cvmx_usb_perform_callback(usb, NULL, NULL,
3398                                    CVMX_USB_CALLBACK_PORT_CHANGED,
3399                                    CVMX_USB_COMPLETE_SUCCESS);
3400        /* Clear the port change bits */
3401        usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
3402        usbc_hprt.s.prtena = 0;
3403        __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index), usbc_hprt.u32);
3404    }
3405    if (usbc_gintsts.s.hchint)
3406    {
3407        /* Host Channels Interrupt (HChInt)
3408            The core sets this bit to indicate that an interrupt is pending on
3409            one of the channels of the core (in Host mode). The application
3410            must read the Host All Channels Interrupt (HAINT) register to
3411            determine the exact number of the channel on which the
3412            interrupt occurred, and then read the corresponding Host
3413            Channel-n Interrupt (HCINTn) register to determine the exact
3414            cause of the interrupt. The application must clear the
3415            appropriate status bit in the HCINTn register to clear this bit. */
3416        cvmx_usbcx_haint_t usbc_haint;
3417        usbc_haint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINT(usb->index));
3418        while (usbc_haint.u32)
3419        {
3420            int channel;
3421            CVMX_CLZ(channel, usbc_haint.u32);
3422            channel = 31 - channel;
3423            __cvmx_usb_poll_channel(usb, channel);
3424            usbc_haint.u32 ^= 1<<channel;
3425        }
3426    }
3427
3428    __cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
3429
3430    CVMX_USB_RETURN(CVMX_USB_SUCCESS);
3431}
3432#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
3433EXPORT_SYMBOL(cvmx_usb_poll);
3434#endif
3435
3436extern void cvmx_usb_set_toggle(cvmx_usb_state_t *state, int endpoint_num, int toggle)
3437{
3438    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
3439    cvmx_usb_pipe_t *pipe = usb->pipe + endpoint_num;
3440
3441    pipe->pid_toggle = !!toggle;
3442}
3443
3444extern int cvmx_usb_get_toggle(cvmx_usb_state_t *state, int endpoint_num)
3445{
3446    cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t*)state;
3447    cvmx_usb_pipe_t *pipe = usb->pipe + endpoint_num;
3448
3449    if (pipe->pid_toggle)
3450	    return (1);
3451    return (0);
3452}
3453
3454