1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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
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
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**************************************/
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
50 #include <linux/delay.h>
51 #include <asm/octeon/cvmx.h>
52 #include <asm/octeon/octeon.h>
53 #include <asm/octeon/cvmx-sysinfo.h>
54 #include "cvmx-usbnx-defs.h"
55 #include "cvmx-usbcx-defs.h"
57 #include <asm/octeon/cvmx-helper.h>
58 #include <asm/octeon/cvmx-helper-board.h>
60 #define CVMX_PREFETCH0(address) CVMX_PREFETCH(address, 0)
61 #define CVMX_PREFETCH128(address) CVMX_PREFETCH(address, 128)
63 #define CVMX_PREFETCH(address, offset) CVMX_PREFETCH_PREF0(address, offset)
64 // normal prefetches that use the pref instruction
65 #define CVMX_PREFETCH_PREFX(X, address, offset) asm volatile ("pref %[type], %[off](%[rbase])" : : [rbase] "d" (address), [off] "I" (offset), [type] "n" (X))
66 #define CVMX_PREFETCH_PREF0(address, offset) CVMX_PREFETCH_PREFX(0, address, offset)
67 #define CVMX_CLZ(result, input) asm ("clz %[rd],%[rs]" : [rd] "=d" (result) : [rs] "d" (input))
69 #define MAX_RETRIES 3 /* Maximum number of times to retry failed transactions */
70 #define MAX_PIPES 32 /* Maximum number of pipes that can be open at once */
71 #define MAX_TRANSACTIONS 256 /* Maximum number of outstanding transactions across all pipes */
72 #define MAX_CHANNELS 8 /* Maximum number of hardware channels supported by the USB block */
73 #define MAX_USB_ADDRESS 127 /* The highest valid USB device address */
74 #define MAX_USB_ENDPOINT 15 /* The highest valid USB endpoint number */
75 #define MAX_USB_HUB_PORT 15 /* The highest valid port number on a hub */
76 #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 */
77 #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 */
80 * These defines disable the normal read and write csr. This is so I can add
81 * extra debug stuff to the usb specific version and I won't use the normal
84 #define cvmx_read_csr use_cvmx_usb_read_csr64_instead_of_cvmx_read_csr
85 #define cvmx_write_csr use_cvmx_usb_write_csr64_instead_of_cvmx_write_csr
88 __CVMX_USB_TRANSACTION_FLAGS_IN_USE = 1<<16,
89 } cvmx_usb_transaction_flags_t;
92 USB_CLOCK_TYPE_REF_12,
93 USB_CLOCK_TYPE_REF_24,
94 USB_CLOCK_TYPE_REF_48,
95 USB_CLOCK_TYPE_CRYSTAL_12,
99 * Logical transactions may take numerous low level
100 * transactions, especially when splits are concerned. This
101 * enum represents all of the possible stages a transaction can
102 * be in. Note that split completes are always even. This is so
103 * the NAK handler can backup to the previous low level
104 * transaction with a simple clearing of bit 0.
107 CVMX_USB_STAGE_NON_CONTROL,
108 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
109 CVMX_USB_STAGE_SETUP,
110 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
112 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
113 CVMX_USB_STAGE_STATUS,
114 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
118 * This structure describes each pending USB transaction
119 * regardless of type. These are linked together to form a list
120 * of pending requests for a pipe.
122 typedef struct cvmx_usb_transaction {
123 struct cvmx_usb_transaction *prev; /**< Transaction before this one in the pipe */
124 struct cvmx_usb_transaction *next; /**< Transaction after this one in the pipe */
125 cvmx_usb_transfer_t type; /**< Type of transaction, duplicated of the pipe */
126 cvmx_usb_transaction_flags_t flags; /**< State flags for this transaction */
127 uint64_t buffer; /**< User's physical buffer address to read/write */
128 int buffer_length; /**< Size of the user's buffer in bytes */
129 uint64_t control_header; /**< For control transactions, physical address of the 8 byte standard header */
130 int iso_start_frame; /**< For ISO transactions, the starting frame number */
131 int iso_number_packets; /**< For ISO transactions, the number of packets in the request */
132 cvmx_usb_iso_packet_t *iso_packets; /**< For ISO transactions, the sub packets in the request */
136 int actual_bytes; /**< Actual bytes transfer for this transaction */
137 cvmx_usb_stage_t stage; /**< For control transactions, the current stage */
138 cvmx_usb_callback_func_t callback; /**< User's callback function when complete */
139 void *callback_data; /**< User's data */
140 } cvmx_usb_transaction_t;
143 * A pipe represents a virtual connection between Octeon and some
144 * USB device. It contains a list of pending request to the device.
146 typedef struct cvmx_usb_pipe {
147 struct cvmx_usb_pipe *prev; /**< Pipe before this one in the list */
148 struct cvmx_usb_pipe *next; /**< Pipe after this one in the list */
149 cvmx_usb_transaction_t *head; /**< The first pending transaction */
150 cvmx_usb_transaction_t *tail; /**< The last pending transaction */
151 uint64_t interval; /**< For periodic pipes, the interval between packets in frames */
152 uint64_t next_tx_frame; /**< The next frame this pipe is allowed to transmit on */
153 cvmx_usb_pipe_flags_t flags; /**< State flags for this pipe */
154 cvmx_usb_speed_t device_speed; /**< Speed of device connected to this pipe */
155 cvmx_usb_transfer_t transfer_type; /**< Type of transaction supported by this pipe */
156 cvmx_usb_direction_t transfer_dir; /**< IN or OUT. Ignored for Control */
157 int multi_count; /**< Max packet in a row for the device */
158 uint16_t max_packet; /**< The device's maximum packet size in bytes */
159 uint8_t device_addr; /**< USB device address at other end of pipe */
160 uint8_t endpoint_num; /**< USB endpoint number at other end of pipe */
161 uint8_t hub_device_addr; /**< Hub address this device is connected to */
162 uint8_t hub_port; /**< Hub port this device is connected to */
163 uint8_t pid_toggle; /**< This toggles between 0/1 on every packet send to track the data pid needed */
164 uint8_t channel; /**< Hardware DMA channel for this pipe */
165 int8_t split_sc_frame; /**< The low order bits of the frame number the split complete should be sent on */
169 cvmx_usb_pipe_t *head; /**< Head of the list, or NULL if empty */
170 cvmx_usb_pipe_t *tail; /**< Tail if the list, or NULL if empty */
171 } cvmx_usb_pipe_list_t;
178 } entry[MAX_CHANNELS+1];
181 } cvmx_usb_tx_fifo_t;
184 * The state of the USB block is stored in this structure
187 int init_flags; /**< Flags passed to initialize */
188 int index; /**< Which USB block this is for */
189 int idle_hardware_channels; /**< Bit set for every idle hardware channel */
190 cvmx_usbcx_hprt_t usbcx_hprt; /**< Stored port status so we don't need to read a CSR to determine splits */
191 cvmx_usb_pipe_t *pipe_for_channel[MAX_CHANNELS]; /**< Map channels to pipes */
192 cvmx_usb_transaction_t *free_transaction_head; /**< List of free transactions head */
193 cvmx_usb_transaction_t *free_transaction_tail; /**< List of free transactions tail */
194 cvmx_usb_pipe_t pipe[MAX_PIPES]; /**< Storage for pipes */
195 cvmx_usb_transaction_t transaction[MAX_TRANSACTIONS]; /**< Storage for transactions */
196 cvmx_usb_callback_func_t callback[__CVMX_USB_CALLBACK_END]; /**< User global callbacks */
197 void *callback_data[__CVMX_USB_CALLBACK_END]; /**< User data for each callback */
198 int indent; /**< Used by debug output to indent functions */
199 cvmx_usb_port_status_t port_status; /**< Last port status used for change notification */
200 cvmx_usb_pipe_list_t free_pipes; /**< List of all pipes that are currently closed */
201 cvmx_usb_pipe_list_t idle_pipes; /**< List of open pipes that have no transactions */
202 cvmx_usb_pipe_list_t active_pipes[4]; /**< Active pipes indexed by transfer type */
203 uint64_t frame_number; /**< Increments every SOF interrupt for time keeping */
204 cvmx_usb_transaction_t *active_split; /**< Points to the current active split, or NULL */
205 cvmx_usb_tx_fifo_t periodic;
206 cvmx_usb_tx_fifo_t nonperiodic;
207 } cvmx_usb_internal_state_t;
209 /* This macro spins on a field waiting for it to reach a value */
210 #define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
213 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
214 octeon_get_clock_rate() / 1000000; \
217 c.u32 = __cvmx_usb_read_csr32(usb, address); \
218 if (c.s.field op (value)) { \
221 } else if (cvmx_get_cycle() > done) { \
231 * This macro logically sets a single field in a CSR. It does the sequence
232 * read, modify, and write
234 #define USB_SET_FIELD32(address, type, field, value) \
237 c.u32 = __cvmx_usb_read_csr32(usb, address); \
239 __cvmx_usb_write_csr32(usb, address, c.u32); \
242 /* Returns the IO address to push/pop stuff data from the FIFOs */
243 #define USB_FIFO_ADDRESS(channel, usb_index) (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
245 static int octeon_usb_get_clock_type(void)
247 switch (cvmx_sysinfo_get()->board_type) {
248 case CVMX_BOARD_TYPE_BBGW_REF:
249 case CVMX_BOARD_TYPE_LANAI2_A:
250 case CVMX_BOARD_TYPE_LANAI2_U:
251 case CVMX_BOARD_TYPE_LANAI2_G:
252 case CVMX_BOARD_TYPE_UBNT_E100:
253 return USB_CLOCK_TYPE_CRYSTAL_12;
255 return USB_CLOCK_TYPE_REF_48;
259 * Read a USB 32bit CSR. It performs the necessary address swizzle
260 * for 32bit CSRs and logs the value in a readable format if
263 * @usb: USB block this access is for
264 * @address: 64bit address to read
266 * Returns: Result of the read
268 static inline uint32_t __cvmx_usb_read_csr32(cvmx_usb_internal_state_t *usb,
271 uint32_t result = cvmx_read64_uint32(address ^ 4);
277 * Write a USB 32bit CSR. It performs the necessary address
278 * swizzle for 32bit CSRs and logs the value in a readable format
279 * if debugging is on.
281 * @usb: USB block this access is for
282 * @address: 64bit address to write
283 * @value: Value to write
285 static inline void __cvmx_usb_write_csr32(cvmx_usb_internal_state_t *usb,
286 uint64_t address, uint32_t value)
288 cvmx_write64_uint32(address ^ 4, value);
289 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
294 * Read a USB 64bit CSR. It logs the value in a readable format if
297 * @usb: USB block this access is for
298 * @address: 64bit address to read
300 * Returns: Result of the read
302 static inline uint64_t __cvmx_usb_read_csr64(cvmx_usb_internal_state_t *usb,
305 uint64_t result = cvmx_read64_uint64(address);
311 * Write a USB 64bit CSR. It logs the value in a readable format
312 * if debugging is on.
314 * @usb: USB block this access is for
315 * @address: 64bit address to write
316 * @value: Value to write
318 static inline void __cvmx_usb_write_csr64(cvmx_usb_internal_state_t *usb,
319 uint64_t address, uint64_t value)
321 cvmx_write64_uint64(address, value);
325 * Return non zero if this pipe connects to a non HIGH speed
326 * device through a high speed hub.
328 * @usb: USB block this access is for
329 * @pipe: Pipe to check
331 * Returns: Non zero if we need to do split transactions
333 static inline int __cvmx_usb_pipe_needs_split(cvmx_usb_internal_state_t *usb, cvmx_usb_pipe_t *pipe)
335 return ((pipe->device_speed != CVMX_USB_SPEED_HIGH) && (usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH));
340 * Trivial utility function to return the correct PID for a pipe
342 * @pipe: pipe to check
344 * Returns: PID for pipe
346 static inline int __cvmx_usb_get_data_pid(cvmx_usb_pipe_t *pipe)
348 if (pipe->pid_toggle)
349 return 2; /* Data1 */
351 return 0; /* Data0 */
356 * Return the number of USB ports supported by this Octeon
357 * chip. If the chip doesn't support USB, or is not supported
358 * by this API, a zero will be returned. Most Octeon chips
359 * support one usb port, but some support two ports.
360 * cvmx_usb_initialize() must be called on independent
361 * cvmx_usb_state_t structures.
363 * Returns: Number of port, zero if usb isn't supported
365 int cvmx_usb_get_num_ports(void)
369 if (OCTEON_IS_MODEL(OCTEON_CN56XX))
371 else if (OCTEON_IS_MODEL(OCTEON_CN52XX))
373 else if (OCTEON_IS_MODEL(OCTEON_CN50XX))
375 else if (OCTEON_IS_MODEL(OCTEON_CN31XX))
377 else if (OCTEON_IS_MODEL(OCTEON_CN30XX))
387 * Allocate a usb transaction for use
389 * @usb: USB device state populated by
390 * cvmx_usb_initialize().
392 * Returns: Transaction or NULL
394 static inline cvmx_usb_transaction_t *__cvmx_usb_alloc_transaction(cvmx_usb_internal_state_t *usb)
396 cvmx_usb_transaction_t *t;
397 t = usb->free_transaction_head;
399 usb->free_transaction_head = t->next;
400 if (!usb->free_transaction_head)
401 usb->free_transaction_tail = NULL;
404 memset(t, 0, sizeof(*t));
405 t->flags = __CVMX_USB_TRANSACTION_FLAGS_IN_USE;
412 * Free a usb transaction
414 * @usb: USB device state populated by
415 * cvmx_usb_initialize().
417 * Transaction to free
419 static inline void __cvmx_usb_free_transaction(cvmx_usb_internal_state_t *usb,
420 cvmx_usb_transaction_t *transaction)
422 transaction->flags = 0;
423 transaction->prev = NULL;
424 transaction->next = NULL;
425 if (usb->free_transaction_tail)
426 usb->free_transaction_tail->next = transaction;
428 usb->free_transaction_head = transaction;
429 usb->free_transaction_tail = transaction;
434 * Add a pipe to the tail of a list
435 * @list: List to add pipe to
438 static inline void __cvmx_usb_append_pipe(cvmx_usb_pipe_list_t *list, cvmx_usb_pipe_t *pipe)
441 pipe->prev = list->tail;
443 list->tail->next = pipe;
451 * Remove a pipe from a list
452 * @list: List to remove pipe from
453 * @pipe: Pipe to remove
455 static inline void __cvmx_usb_remove_pipe(cvmx_usb_pipe_list_t *list, cvmx_usb_pipe_t *pipe)
457 if (list->head == pipe) {
458 list->head = pipe->next;
461 list->head->prev = NULL;
464 } else if (list->tail == pipe) {
465 list->tail = pipe->prev;
466 list->tail->next = NULL;
469 pipe->prev->next = pipe->next;
470 pipe->next->prev = pipe->prev;
478 * Initialize a USB port for use. This must be called before any
479 * other access to the Octeon USB port is made. The port starts
480 * off in the disabled state.
482 * @state: Pointer to an empty cvmx_usb_state_t structure
483 * that will be populated by the initialize call.
484 * This structure is then passed to all other USB
487 * Which Octeon USB port to initialize.
488 * @flags: Flags to control hardware initialization. See
489 * cvmx_usb_initialize_flags_t for the flag
490 * definitions. Some flags are mandatory.
492 * Returns: 0 or a negative error code.
494 int cvmx_usb_initialize(cvmx_usb_state_t *state, int usb_port_number,
495 cvmx_usb_initialize_flags_t flags)
497 cvmx_usbnx_clk_ctl_t usbn_clk_ctl;
498 cvmx_usbnx_usbp_ctl_status_t usbn_usbp_ctl_status;
499 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
501 usb->init_flags = flags;
503 /* Make sure that state is large enough to store the internal state */
504 if (sizeof(*state) < sizeof(*usb))
506 /* At first allow 0-1 for the usb port number */
507 if ((usb_port_number < 0) || (usb_port_number > 1))
509 /* For all chips except 52XX there is only one port */
510 if (!OCTEON_IS_MODEL(OCTEON_CN52XX) && (usb_port_number > 0))
512 /* Try to determine clock type automatically */
513 if ((flags & (CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI |
514 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND)) == 0) {
515 if (octeon_usb_get_clock_type() == USB_CLOCK_TYPE_CRYSTAL_12)
516 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI; /* Only 12 MHZ crystals are supported */
518 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
521 if (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
522 /* Check for auto ref clock frequency */
523 if (!(flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK))
524 switch (octeon_usb_get_clock_type()) {
525 case USB_CLOCK_TYPE_REF_12:
526 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
528 case USB_CLOCK_TYPE_REF_24:
529 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
531 case USB_CLOCK_TYPE_REF_48:
532 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
540 memset(usb, 0, sizeof(usb));
541 usb->init_flags = flags;
543 /* Initialize the USB state structure */
546 usb->index = usb_port_number;
548 /* Initialize the transaction double linked list */
549 usb->free_transaction_head = NULL;
550 usb->free_transaction_tail = NULL;
551 for (i = 0; i < MAX_TRANSACTIONS; i++)
552 __cvmx_usb_free_transaction(usb, usb->transaction + i);
553 for (i = 0; i < MAX_PIPES; i++)
554 __cvmx_usb_append_pipe(&usb->free_pipes, usb->pipe + i);
558 * Power On Reset and PHY Initialization
560 * 1. Wait for DCOK to assert (nothing to do)
562 * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
563 * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
565 usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
566 usbn_clk_ctl.s.por = 1;
567 usbn_clk_ctl.s.hrst = 0;
568 usbn_clk_ctl.s.prst = 0;
569 usbn_clk_ctl.s.hclk_rst = 0;
570 usbn_clk_ctl.s.enable = 0;
572 * 2b. Select the USB reference clock/crystal parameters by writing
573 * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
575 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
577 * The USB port uses 12/24/48MHz 2.5V board clock
578 * source at USB_XO. USB_XI should be tied to GND.
579 * Most Octeon evaluation boards require this setting
581 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
582 usbn_clk_ctl.cn31xx.p_rclk = 1; /* From CN31XX,CN30XX manual */
583 usbn_clk_ctl.cn31xx.p_xenbn = 0;
584 } else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
585 usbn_clk_ctl.cn56xx.p_rtype = 2; /* From CN56XX,CN50XX manual */
587 usbn_clk_ctl.cn52xx.p_rtype = 1; /* From CN52XX manual */
589 switch (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
590 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
591 usbn_clk_ctl.s.p_c_sel = 0;
593 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
594 usbn_clk_ctl.s.p_c_sel = 1;
596 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
597 usbn_clk_ctl.s.p_c_sel = 2;
602 * The USB port uses a 12MHz crystal as clock source
603 * at USB_XO and USB_XI
605 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
606 usbn_clk_ctl.cn31xx.p_rclk = 1; /* From CN31XX,CN30XX manual */
607 usbn_clk_ctl.cn31xx.p_xenbn = 1;
608 } else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
609 usbn_clk_ctl.cn56xx.p_rtype = 0; /* From CN56XX,CN50XX manual */
611 usbn_clk_ctl.cn52xx.p_rtype = 0; /* From CN52XX manual */
613 usbn_clk_ctl.s.p_c_sel = 0;
616 * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
617 * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
618 * such that USB is as close as possible to 125Mhz
621 int divisor = (octeon_get_clock_rate()+125000000-1)/125000000;
622 if (divisor < 4) /* Lower than 4 doesn't seem to work properly */
624 usbn_clk_ctl.s.divide = divisor;
625 usbn_clk_ctl.s.divide2 = 0;
627 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
629 /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
630 usbn_clk_ctl.s.hclk_rst = 1;
631 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
633 /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
636 * 3. Program the power-on reset field in the USBN clock-control
638 * USBN_CLK_CTL[POR] = 0
640 usbn_clk_ctl.s.por = 0;
641 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
643 /* 4. Wait 1 ms for PHY clock to start */
646 * 5. Program the Reset input from automatic test equipment field in the
647 * USBP control and status register:
648 * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
650 usbn_usbp_ctl_status.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index));
651 usbn_usbp_ctl_status.s.ate_reset = 1;
652 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
653 usbn_usbp_ctl_status.u64);
654 /* 6. Wait 10 cycles */
657 * 7. Clear ATE_RESET field in the USBN clock-control register:
658 * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
660 usbn_usbp_ctl_status.s.ate_reset = 0;
661 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
662 usbn_usbp_ctl_status.u64);
664 * 8. Program the PHY reset field in the USBN clock-control register:
665 * USBN_CLK_CTL[PRST] = 1
667 usbn_clk_ctl.s.prst = 1;
668 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
671 * 9. Program the USBP control and status register to select host or
672 * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
675 usbn_usbp_ctl_status.s.hst_mode = 0;
676 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
677 usbn_usbp_ctl_status.u64);
681 * 11. Program the hreset_n field in the USBN clock-control register:
682 * USBN_CLK_CTL[HRST] = 1
684 usbn_clk_ctl.s.hrst = 1;
685 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
687 /* 12. Proceed to USB core initialization */
688 usbn_clk_ctl.s.enable = 1;
689 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
694 * USB Core Initialization
696 * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
697 * determine USB core configuration parameters.
701 * 2. Program the following fields in the global AHB configuration
702 * register (USBC_GAHBCFG)
703 * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
704 * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
705 * Nonperiodic TxFIFO empty level (slave mode only),
706 * USBC_GAHBCFG[NPTXFEMPLVL]
707 * Periodic TxFIFO empty level (slave mode only),
708 * USBC_GAHBCFG[PTXFEMPLVL]
709 * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
712 cvmx_usbcx_gahbcfg_t usbcx_gahbcfg;
713 /* Due to an errata, CN31XX doesn't support DMA */
714 if (OCTEON_IS_MODEL(OCTEON_CN31XX))
715 usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
716 usbcx_gahbcfg.u32 = 0;
717 usbcx_gahbcfg.s.dmaen = !(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
718 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
719 usb->idle_hardware_channels = 0x1; /* Only use one channel with non DMA */
720 else if (OCTEON_IS_MODEL(OCTEON_CN5XXX))
721 usb->idle_hardware_channels = 0xf7; /* CN5XXX have an errata with channel 3 */
723 usb->idle_hardware_channels = 0xff;
724 usbcx_gahbcfg.s.hbstlen = 0;
725 usbcx_gahbcfg.s.nptxfemplvl = 1;
726 usbcx_gahbcfg.s.ptxfemplvl = 1;
727 usbcx_gahbcfg.s.glblintrmsk = 1;
728 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
732 * 3. Program the following fields in USBC_GUSBCFG register.
733 * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
734 * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
735 * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
736 * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
739 cvmx_usbcx_gusbcfg_t usbcx_gusbcfg;
740 usbcx_gusbcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index));
741 usbcx_gusbcfg.s.toutcal = 0;
742 usbcx_gusbcfg.s.ddrsel = 0;
743 usbcx_gusbcfg.s.usbtrdtim = 0x5;
744 usbcx_gusbcfg.s.phylpwrclksel = 0;
745 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
749 * 4. The software must unmask the following bits in the USBC_GINTMSK
751 * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
752 * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
755 cvmx_usbcx_gintmsk_t usbcx_gintmsk;
758 usbcx_gintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTMSK(usb->index));
759 usbcx_gintmsk.s.otgintmsk = 1;
760 usbcx_gintmsk.s.modemismsk = 1;
761 usbcx_gintmsk.s.hchintmsk = 1;
762 usbcx_gintmsk.s.sofmsk = 0;
763 /* We need RX FIFO interrupts if we don't have DMA */
764 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
765 usbcx_gintmsk.s.rxflvlmsk = 1;
766 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
769 /* Disable all channel interrupts. We'll enable them per channel later */
770 for (channel = 0; channel < 8; channel++)
771 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
776 * Host Port Initialization
778 * 1. Program the host-port interrupt-mask field to unmask,
779 * USBC_GINTMSK[PRTINT] = 1
781 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t,
783 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t,
786 * 2. Program the USBC_HCFG register to select full-speed host
787 * or high-speed host.
790 cvmx_usbcx_hcfg_t usbcx_hcfg;
791 usbcx_hcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
792 usbcx_hcfg.s.fslssupp = 0;
793 usbcx_hcfg.s.fslspclksel = 0;
794 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
797 * 3. Program the port power bit to drive VBUS on the USB,
798 * USBC_HPRT[PRTPWR] = 1
800 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtpwr, 1);
803 * Steps 4-15 from the manual are done later in the port enable
812 * Shutdown a USB port after a call to cvmx_usb_initialize().
813 * The port should be disabled with all pipes closed when this
814 * function is called.
816 * @state: USB device state populated by
817 * cvmx_usb_initialize().
819 * Returns: 0 or a negative error code.
821 int cvmx_usb_shutdown(cvmx_usb_state_t *state)
823 cvmx_usbnx_clk_ctl_t usbn_clk_ctl;
824 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
826 /* Make sure all pipes are closed */
827 if (usb->idle_pipes.head ||
828 usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS].head ||
829 usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT].head ||
830 usb->active_pipes[CVMX_USB_TRANSFER_CONTROL].head ||
831 usb->active_pipes[CVMX_USB_TRANSFER_BULK].head)
834 /* Disable the clocks and put them in power on reset */
835 usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
836 usbn_clk_ctl.s.enable = 1;
837 usbn_clk_ctl.s.por = 1;
838 usbn_clk_ctl.s.hclk_rst = 1;
839 usbn_clk_ctl.s.prst = 0;
840 usbn_clk_ctl.s.hrst = 0;
841 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
848 * Enable a USB port. After this call succeeds, the USB port is
849 * online and servicing requests.
851 * @state: USB device state populated by
852 * cvmx_usb_initialize().
854 * Returns: 0 or a negative error code.
856 int cvmx_usb_enable(cvmx_usb_state_t *state)
858 cvmx_usbcx_ghwcfg3_t usbcx_ghwcfg3;
859 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
861 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
864 * If the port is already enabled the just return. We don't need to do
867 if (usb->usbcx_hprt.s.prtena)
870 /* If there is nothing plugged into the port then fail immediately */
871 if (!usb->usbcx_hprt.s.prtconnsts) {
875 /* Program the port reset bit to start the reset process */
876 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtrst, 1);
879 * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
880 * process to complete.
884 /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
885 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtrst, 0);
887 /* Wait for the USBC_HPRT[PRTENA]. */
888 if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t,
889 prtena, ==, 1, 100000))
892 /* Read the port speed field to get the enumerated speed, USBC_HPRT[PRTSPD]. */
893 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
894 usbcx_ghwcfg3.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GHWCFG3(usb->index));
897 * 13. Program the USBC_GRXFSIZ register to select the size of the
898 * receive FIFO (25%).
900 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), cvmx_usbcx_grxfsiz_t,
901 rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
903 * 14. Program the USBC_GNPTXFSIZ register to select the size and the
904 * start address of the non- periodic transmit FIFO for nonperiodic
905 * transactions (50%).
908 cvmx_usbcx_gnptxfsiz_t siz;
909 siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
910 siz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
911 siz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
912 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), siz.u32);
915 * 15. Program the USBC_HPTXFSIZ register to select the size and start
916 * address of the periodic transmit FIFO for periodic transactions
920 cvmx_usbcx_hptxfsiz_t siz;
921 siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
922 siz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
923 siz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
924 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), siz.u32);
926 /* Flush all FIFOs */
927 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t, txfnum, 0x10);
928 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t, txfflsh, 1);
929 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t,
930 txfflsh, ==, 0, 100);
931 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t, rxfflsh, 1);
932 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), cvmx_usbcx_grstctl_t,
933 rxfflsh, ==, 0, 100);
940 * Disable a USB port. After this call the USB port will not
941 * generate data transfers and will not generate events.
942 * Transactions in process will fail and call their
943 * associated callbacks.
945 * @state: USB device state populated by
946 * cvmx_usb_initialize().
948 * Returns: 0 or a negative error code.
950 int cvmx_usb_disable(cvmx_usb_state_t *state)
952 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
954 /* Disable the port */
955 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt_t, prtena, 1);
961 * Get the current state of the USB port. Use this call to
962 * determine if the usb port has anything connected, is enabled,
963 * or has some sort of error condition. The return value of this
964 * call has "changed" bits to signal of the value of some fields
965 * have changed between calls. These "changed" fields are based
966 * on the last call to cvmx_usb_set_status(). In order to clear
967 * them, you must update the status through cvmx_usb_set_status().
969 * @state: USB device state populated by
970 * cvmx_usb_initialize().
972 * Returns: Port status information
974 cvmx_usb_port_status_t cvmx_usb_get_status(cvmx_usb_state_t *state)
976 cvmx_usbcx_hprt_t usbc_hprt;
977 cvmx_usb_port_status_t result;
978 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
980 memset(&result, 0, sizeof(result));
982 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
983 result.port_enabled = usbc_hprt.s.prtena;
984 result.port_over_current = usbc_hprt.s.prtovrcurract;
985 result.port_powered = usbc_hprt.s.prtpwr;
986 result.port_speed = usbc_hprt.s.prtspd;
987 result.connected = usbc_hprt.s.prtconnsts;
988 result.connect_change = (result.connected != usb->port_status.connected);
995 * Set the current state of the USB port. The status is used as
996 * a reference for the "changed" bits returned by
997 * cvmx_usb_get_status(). Other than serving as a reference, the
998 * status passed to this function is not used. No fields can be
999 * changed through this call.
1001 * @state: USB device state populated by
1002 * cvmx_usb_initialize().
1004 * Port status to set, most like returned by cvmx_usb_get_status()
1006 void cvmx_usb_set_status(cvmx_usb_state_t *state, cvmx_usb_port_status_t port_status)
1008 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
1009 usb->port_status = port_status;
1015 * Convert a USB transaction into a handle
1017 * @usb: USB device state populated by
1018 * cvmx_usb_initialize().
1020 * Transaction to get handle for
1024 static inline int __cvmx_usb_get_submit_handle(cvmx_usb_internal_state_t *usb,
1025 cvmx_usb_transaction_t *transaction)
1027 return ((unsigned long)transaction - (unsigned long)usb->transaction) /
1028 sizeof(*transaction);
1033 * Convert a USB pipe into a handle
1035 * @usb: USB device state populated by
1036 * cvmx_usb_initialize().
1037 * @pipe: Pipe to get handle for
1041 static inline int __cvmx_usb_get_pipe_handle(cvmx_usb_internal_state_t *usb,
1042 cvmx_usb_pipe_t *pipe)
1044 return ((unsigned long)pipe - (unsigned long)usb->pipe) / sizeof(*pipe);
1049 * Open a virtual pipe between the host and a USB device. A pipe
1050 * must be opened before data can be transferred between a device
1053 * @state: USB device state populated by
1054 * cvmx_usb_initialize().
1055 * @flags: Optional pipe flags defined in
1056 * cvmx_usb_pipe_flags_t.
1058 * USB device address to open the pipe to
1061 * USB endpoint number to open the pipe to
1064 * The speed of the device the pipe is going
1065 * to. This must match the device's speed,
1066 * which may be different than the port speed.
1067 * @max_packet: The maximum packet length the device can
1068 * transmit/receive (low speed=0-8, full
1069 * speed=0-1023, high speed=0-1024). This value
1070 * comes from the standard endpoint descriptor
1071 * field wMaxPacketSize bits <10:0>.
1073 * The type of transfer this pipe is for.
1075 * The direction the pipe is in. This is not
1076 * used for control pipes.
1077 * @interval: For ISOCHRONOUS and INTERRUPT transfers,
1078 * this is how often the transfer is scheduled
1079 * for. All other transfers should specify
1080 * zero. The units are in frames (8000/sec at
1081 * high speed, 1000/sec for full speed).
1083 * For high speed devices, this is the maximum
1084 * allowed number of packet per microframe.
1085 * Specify zero for non high speed devices. This
1086 * value comes from the standard endpoint descriptor
1087 * field wMaxPacketSize bits <12:11>.
1089 * Hub device address this device is connected
1090 * to. Devices connected directly to Octeon
1091 * use zero. This is only used when the device
1092 * is full/low speed behind a high speed hub.
1093 * The address will be of the high speed hub,
1094 * not and full speed hubs after it.
1095 * @hub_port: Which port on the hub the device is
1096 * connected. Use zero for devices connected
1097 * directly to Octeon. Like hub_device_addr,
1098 * this is only used for full/low speed
1099 * devices behind a high speed hub.
1101 * Returns: A non negative value is a pipe handle. Negative
1102 * values are error codes.
1104 int cvmx_usb_open_pipe(cvmx_usb_state_t *state, cvmx_usb_pipe_flags_t flags,
1105 int device_addr, int endpoint_num,
1106 cvmx_usb_speed_t device_speed, int max_packet,
1107 cvmx_usb_transfer_t transfer_type,
1108 cvmx_usb_direction_t transfer_dir, int interval,
1109 int multi_count, int hub_device_addr, int hub_port)
1111 cvmx_usb_pipe_t *pipe;
1112 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
1114 if (unlikely((device_addr < 0) || (device_addr > MAX_USB_ADDRESS)))
1116 if (unlikely((endpoint_num < 0) || (endpoint_num > MAX_USB_ENDPOINT)))
1118 if (unlikely(device_speed > CVMX_USB_SPEED_LOW))
1120 if (unlikely((max_packet <= 0) || (max_packet > 1024)))
1122 if (unlikely(transfer_type > CVMX_USB_TRANSFER_INTERRUPT))
1124 if (unlikely((transfer_dir != CVMX_USB_DIRECTION_OUT) &&
1125 (transfer_dir != CVMX_USB_DIRECTION_IN)))
1127 if (unlikely(interval < 0))
1129 if (unlikely((transfer_type == CVMX_USB_TRANSFER_CONTROL) && interval))
1131 if (unlikely(multi_count < 0))
1133 if (unlikely((device_speed != CVMX_USB_SPEED_HIGH) &&
1134 (multi_count != 0)))
1136 if (unlikely((hub_device_addr < 0) || (hub_device_addr > MAX_USB_ADDRESS)))
1138 if (unlikely((hub_port < 0) || (hub_port > MAX_USB_HUB_PORT)))
1141 /* Find a free pipe */
1142 pipe = usb->free_pipes.head;
1145 __cvmx_usb_remove_pipe(&usb->free_pipes, pipe);
1146 pipe->flags = flags | __CVMX_USB_PIPE_FLAGS_OPEN;
1147 if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1148 (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1149 (transfer_type == CVMX_USB_TRANSFER_BULK))
1150 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
1151 pipe->device_addr = device_addr;
1152 pipe->endpoint_num = endpoint_num;
1153 pipe->device_speed = device_speed;
1154 pipe->max_packet = max_packet;
1155 pipe->transfer_type = transfer_type;
1156 pipe->transfer_dir = transfer_dir;
1158 * All pipes use interval to rate limit NAK processing. Force an
1159 * interval if one wasn't supplied
1163 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1164 pipe->interval = interval*8;
1165 /* Force start splits to be schedule on uFrame 0 */
1166 pipe->next_tx_frame = ((usb->frame_number+7)&~7) + pipe->interval;
1168 pipe->interval = interval;
1169 pipe->next_tx_frame = usb->frame_number + pipe->interval;
1171 pipe->multi_count = multi_count;
1172 pipe->hub_device_addr = hub_device_addr;
1173 pipe->hub_port = hub_port;
1174 pipe->pid_toggle = 0;
1175 pipe->split_sc_frame = -1;
1176 __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
1179 * We don't need to tell the hardware about this pipe yet since
1180 * it doesn't have any submitted requests
1183 return __cvmx_usb_get_pipe_handle(usb, pipe);
1188 * Poll the RX FIFOs and remove data as needed. This function is only used
1189 * in non DMA mode. It is very important that this function be called quickly
1190 * enough to prevent FIFO overflow.
1192 * @usb: USB device state populated by
1193 * cvmx_usb_initialize().
1195 static void __cvmx_usb_poll_rx_fifo(cvmx_usb_internal_state_t *usb)
1197 cvmx_usbcx_grxstsph_t rx_status;
1203 rx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GRXSTSPH(usb->index));
1204 /* Only read data if IN data is there */
1205 if (rx_status.s.pktsts != 2)
1207 /* Check if no data is available */
1208 if (!rx_status.s.bcnt)
1211 channel = rx_status.s.chnum;
1212 bytes = rx_status.s.bcnt;
1216 /* Get where the DMA engine would have written this data */
1217 address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8);
1218 ptr = cvmx_phys_to_ptr(address);
1219 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, address + bytes);
1221 /* Loop writing the FIFO data for this packet into memory */
1223 *ptr++ = __cvmx_usb_read_csr32(usb, USB_FIFO_ADDRESS(channel, usb->index));
1233 * Fill the TX hardware fifo with data out of the software
1236 * @usb: USB device state populated by
1237 * cvmx_usb_initialize().
1238 * @fifo: Software fifo to use
1239 * @available: Amount of space in the hardware fifo
1241 * Returns: Non zero if the hardware fifo was too small and needs
1242 * to be serviced again.
1244 static int __cvmx_usb_fill_tx_hw(cvmx_usb_internal_state_t *usb, cvmx_usb_tx_fifo_t *fifo, int available)
1247 * We're done either when there isn't anymore space or the software FIFO
1250 while (available && (fifo->head != fifo->tail)) {
1252 const uint32_t *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1253 uint64_t csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel, usb->index) ^ 4;
1254 int words = available;
1256 /* Limit the amount of data to waht the SW fifo has */
1257 if (fifo->entry[i].size <= available) {
1258 words = fifo->entry[i].size;
1260 if (fifo->tail > MAX_CHANNELS)
1264 /* Update the next locations and counts */
1266 fifo->entry[i].address += words * 4;
1267 fifo->entry[i].size -= words;
1270 * Write the HW fifo data. The read every three writes is due
1271 * to an errata on CN3XXX chips
1274 cvmx_write64_uint32(csr_address, *ptr++);
1275 cvmx_write64_uint32(csr_address, *ptr++);
1276 cvmx_write64_uint32(csr_address, *ptr++);
1277 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1280 cvmx_write64_uint32(csr_address, *ptr++);
1282 cvmx_write64_uint32(csr_address, *ptr++);
1284 cvmx_write64_uint32(csr_address, *ptr++);
1286 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1288 return fifo->head != fifo->tail;
1293 * Check the hardware FIFOs and fill them as needed
1295 * @usb: USB device state populated by
1296 * cvmx_usb_initialize().
1298 static void __cvmx_usb_poll_tx_fifo(cvmx_usb_internal_state_t *usb)
1300 if (usb->periodic.head != usb->periodic.tail) {
1301 cvmx_usbcx_hptxsts_t tx_status;
1302 tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXSTS(usb->index));
1303 if (__cvmx_usb_fill_tx_hw(usb, &usb->periodic, tx_status.s.ptxfspcavail))
1304 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, ptxfempmsk, 1);
1306 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, ptxfempmsk, 0);
1309 if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1310 cvmx_usbcx_gnptxsts_t tx_status;
1311 tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXSTS(usb->index));
1312 if (__cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic, tx_status.s.nptxfspcavail))
1313 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, nptxfempmsk, 1);
1315 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, nptxfempmsk, 0);
1323 * Fill the TX FIFO with an outgoing packet
1325 * @usb: USB device state populated by
1326 * cvmx_usb_initialize().
1327 * @channel: Channel number to get packet from
1329 static void __cvmx_usb_fill_tx_fifo(cvmx_usb_internal_state_t *usb, int channel)
1331 cvmx_usbcx_hccharx_t hcchar;
1332 cvmx_usbcx_hcspltx_t usbc_hcsplt;
1333 cvmx_usbcx_hctsizx_t usbc_hctsiz;
1334 cvmx_usb_tx_fifo_t *fifo;
1336 /* We only need to fill data on outbound channels */
1337 hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
1338 if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1341 /* OUT Splits only have data on the start and not the complete */
1342 usbc_hcsplt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index));
1343 if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1346 /* Find out how many bytes we need to fill and convert it into 32bit words */
1347 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1348 if (!usbc_hctsiz.s.xfersize)
1351 if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1352 (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1353 fifo = &usb->periodic;
1355 fifo = &usb->nonperiodic;
1357 fifo->entry[fifo->head].channel = channel;
1358 fifo->entry[fifo->head].address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8);
1359 fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize+3)>>2;
1361 if (fifo->head > MAX_CHANNELS)
1364 __cvmx_usb_poll_tx_fifo(usb);
1370 * Perform channel specific setup for Control transactions. All
1371 * the generic stuff will already have been done in
1372 * __cvmx_usb_start_channel()
1374 * @usb: USB device state populated by
1375 * cvmx_usb_initialize().
1376 * @channel: Channel to setup
1377 * @pipe: Pipe for control transaction
1379 static void __cvmx_usb_start_channel_control(cvmx_usb_internal_state_t *usb,
1381 cvmx_usb_pipe_t *pipe)
1383 cvmx_usb_transaction_t *transaction = pipe->head;
1384 cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(transaction->control_header);
1385 int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1386 int packets_to_transfer;
1387 cvmx_usbcx_hctsizx_t usbc_hctsiz;
1389 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1391 switch (transaction->stage) {
1392 case CVMX_USB_STAGE_NON_CONTROL:
1393 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1394 cvmx_dprintf("%s: ERROR - Non control stage\n", __FUNCTION__);
1396 case CVMX_USB_STAGE_SETUP:
1397 usbc_hctsiz.s.pid = 3; /* Setup */
1398 bytes_to_transfer = sizeof(*header);
1399 /* All Control operations start with a setup going OUT */
1400 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir, CVMX_USB_DIRECTION_OUT);
1402 * Setup send the control header instead of the buffer data. The
1403 * buffer data will be used in the next stage
1405 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, transaction->control_header);
1407 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1408 usbc_hctsiz.s.pid = 3; /* Setup */
1409 bytes_to_transfer = 0;
1410 /* All Control operations start with a setup going OUT */
1411 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir, CVMX_USB_DIRECTION_OUT);
1412 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), cvmx_usbcx_hcspltx_t, compsplt, 1);
1414 case CVMX_USB_STAGE_DATA:
1415 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1416 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1417 if (header->s.request_type & 0x80)
1418 bytes_to_transfer = 0;
1419 else if (bytes_to_transfer > pipe->max_packet)
1420 bytes_to_transfer = pipe->max_packet;
1422 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1423 cvmx_usbcx_hccharx_t, epdir,
1424 ((header->s.request_type & 0x80) ?
1425 CVMX_USB_DIRECTION_IN :
1426 CVMX_USB_DIRECTION_OUT));
1428 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1429 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1430 if (!(header->s.request_type & 0x80))
1431 bytes_to_transfer = 0;
1432 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1433 cvmx_usbcx_hccharx_t, epdir,
1434 ((header->s.request_type & 0x80) ?
1435 CVMX_USB_DIRECTION_IN :
1436 CVMX_USB_DIRECTION_OUT));
1437 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), cvmx_usbcx_hcspltx_t, compsplt, 1);
1439 case CVMX_USB_STAGE_STATUS:
1440 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1441 bytes_to_transfer = 0;
1442 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir,
1443 ((header->s.request_type & 0x80) ?
1444 CVMX_USB_DIRECTION_OUT :
1445 CVMX_USB_DIRECTION_IN));
1447 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1448 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1449 bytes_to_transfer = 0;
1450 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, epdir,
1451 ((header->s.request_type & 0x80) ?
1452 CVMX_USB_DIRECTION_OUT :
1453 CVMX_USB_DIRECTION_IN));
1454 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), cvmx_usbcx_hcspltx_t, compsplt, 1);
1459 * Make sure the transfer never exceeds the byte limit of the hardware.
1460 * Further bytes will be sent as continued transactions
1462 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1463 /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1464 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1465 bytes_to_transfer *= pipe->max_packet;
1469 * Calculate the number of packets to transfer. If the length is zero
1470 * we still need to transfer one packet
1472 packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1473 if (packets_to_transfer == 0)
1474 packets_to_transfer = 1;
1475 else if ((packets_to_transfer > 1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1477 * Limit to one packet when not using DMA. Channels must be
1478 * restarted between every packet for IN transactions, so there
1479 * is no reason to do multiple packets in a row
1481 packets_to_transfer = 1;
1482 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1483 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1485 * Limit the number of packet and data transferred to what the
1486 * hardware can handle
1488 packets_to_transfer = MAX_TRANSFER_PACKETS;
1489 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1492 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1493 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1495 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1501 * Start a channel to perform the pipe's head transaction
1503 * @usb: USB device state populated by
1504 * cvmx_usb_initialize().
1505 * @channel: Channel to setup
1506 * @pipe: Pipe to start
1508 static void __cvmx_usb_start_channel(cvmx_usb_internal_state_t *usb,
1510 cvmx_usb_pipe_t *pipe)
1512 cvmx_usb_transaction_t *transaction = pipe->head;
1514 /* Make sure all writes to the DMA region get flushed */
1517 /* Attach the channel to the pipe */
1518 usb->pipe_for_channel[channel] = pipe;
1519 pipe->channel = channel;
1520 pipe->flags |= __CVMX_USB_PIPE_FLAGS_SCHEDULED;
1522 /* Mark this channel as in use */
1523 usb->idle_hardware_channels &= ~(1<<channel);
1525 /* Enable the channel interrupt bits */
1527 cvmx_usbcx_hcintx_t usbc_hcint;
1528 cvmx_usbcx_hcintmskx_t usbc_hcintmsk;
1529 cvmx_usbcx_haintmsk_t usbc_haintmsk;
1531 /* Clear all channel status bits */
1532 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
1533 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index), usbc_hcint.u32);
1535 usbc_hcintmsk.u32 = 0;
1536 usbc_hcintmsk.s.chhltdmsk = 1;
1537 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1538 /* Channels need these extra interrupts when we aren't in DMA mode */
1539 usbc_hcintmsk.s.datatglerrmsk = 1;
1540 usbc_hcintmsk.s.frmovrunmsk = 1;
1541 usbc_hcintmsk.s.bblerrmsk = 1;
1542 usbc_hcintmsk.s.xacterrmsk = 1;
1543 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1544 /* Splits don't generate xfercompl, so we need ACK and NYET */
1545 usbc_hcintmsk.s.nyetmsk = 1;
1546 usbc_hcintmsk.s.ackmsk = 1;
1548 usbc_hcintmsk.s.nakmsk = 1;
1549 usbc_hcintmsk.s.stallmsk = 1;
1550 usbc_hcintmsk.s.xfercomplmsk = 1;
1552 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), usbc_hcintmsk.u32);
1554 /* Enable the channel interrupt to propagate */
1555 usbc_haintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index));
1556 usbc_haintmsk.s.haintmsk |= 1<<channel;
1557 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index), usbc_haintmsk.u32);
1560 /* Setup the locations the DMA engines use */
1562 uint64_t dma_address = transaction->buffer + transaction->actual_bytes;
1563 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1564 dma_address = transaction->buffer + transaction->iso_packets[0].offset + transaction->actual_bytes;
1565 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, dma_address);
1566 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, dma_address);
1569 /* Setup both the size of the transfer and the SPLIT characteristics */
1571 cvmx_usbcx_hcspltx_t usbc_hcsplt = {.u32 = 0};
1572 cvmx_usbcx_hctsizx_t usbc_hctsiz = {.u32 = 0};
1573 int packets_to_transfer;
1574 int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1577 * ISOCHRONOUS transactions store each individual transfer size
1578 * in the packet structure, not the global buffer_length
1580 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1581 bytes_to_transfer = transaction->iso_packets[0].length - transaction->actual_bytes;
1584 * We need to do split transactions when we are talking to non
1585 * high speed devices that are behind a high speed hub
1587 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1589 * On the start split phase (stage is even) record the
1590 * frame number we will need to send the split complete.
1591 * We only store the lower two bits since the time ahead
1592 * can only be two frames
1594 if ((transaction->stage&1) == 0) {
1595 if (transaction->type == CVMX_USB_TRANSFER_BULK)
1596 pipe->split_sc_frame = (usb->frame_number + 1) & 0x7f;
1598 pipe->split_sc_frame = (usb->frame_number + 2) & 0x7f;
1600 pipe->split_sc_frame = -1;
1602 usbc_hcsplt.s.spltena = 1;
1603 usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1604 usbc_hcsplt.s.prtaddr = pipe->hub_port;
1605 usbc_hcsplt.s.compsplt = (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1608 * SPLIT transactions can only ever transmit one data
1609 * packet so limit the transfer size to the max packet
1612 if (bytes_to_transfer > pipe->max_packet)
1613 bytes_to_transfer = pipe->max_packet;
1616 * ISOCHRONOUS OUT splits are unique in that they limit
1617 * data transfers to 188 byte chunks representing the
1618 * begin/middle/end of the data or all
1620 if (!usbc_hcsplt.s.compsplt &&
1621 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1622 (pipe->transfer_type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1624 * Clear the split complete frame number as
1625 * there isn't going to be a split complete
1627 pipe->split_sc_frame = -1;
1629 * See if we've started this transfer and sent
1632 if (transaction->actual_bytes == 0) {
1634 * Nothing sent yet, this is either a
1635 * begin or the entire payload
1637 if (bytes_to_transfer <= 188)
1638 usbc_hcsplt.s.xactpos = 3; /* Entire payload in one go */
1640 usbc_hcsplt.s.xactpos = 2; /* First part of payload */
1643 * Continuing the previous data, we must
1644 * either be in the middle or at the end
1646 if (bytes_to_transfer <= 188)
1647 usbc_hcsplt.s.xactpos = 1; /* End of payload */
1649 usbc_hcsplt.s.xactpos = 0; /* Middle of payload */
1652 * Again, the transfer size is limited to 188
1655 if (bytes_to_transfer > 188)
1656 bytes_to_transfer = 188;
1661 * Make sure the transfer never exceeds the byte limit of the
1662 * hardware. Further bytes will be sent as continued
1665 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1667 * Round MAX_TRANSFER_BYTES to a multiple of out packet
1670 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1671 bytes_to_transfer *= pipe->max_packet;
1675 * Calculate the number of packets to transfer. If the length is
1676 * zero we still need to transfer one packet
1678 packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1679 if (packets_to_transfer == 0)
1680 packets_to_transfer = 1;
1681 else if ((packets_to_transfer > 1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1683 * Limit to one packet when not using DMA. Channels must
1684 * be restarted between every packet for IN
1685 * transactions, so there is no reason to do multiple
1688 packets_to_transfer = 1;
1689 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1690 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1692 * Limit the number of packet and data transferred to
1693 * what the hardware can handle
1695 packets_to_transfer = MAX_TRANSFER_PACKETS;
1696 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1699 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1700 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1702 /* Update the DATA0/DATA1 toggle */
1703 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1705 * High speed pipes may need a hardware ping before they start
1707 if (pipe->flags & __CVMX_USB_PIPE_FLAGS_NEED_PING)
1708 usbc_hctsiz.s.dopng = 1;
1710 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index), usbc_hcsplt.u32);
1711 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1714 /* Setup the Host Channel Characteristics Register */
1716 cvmx_usbcx_hccharx_t usbc_hcchar = {.u32 = 0};
1719 * Set the startframe odd/even properly. This is only used for
1722 usbc_hcchar.s.oddfrm = usb->frame_number&1;
1725 * Set the number of back to back packets allowed by this
1726 * endpoint. Split transactions interpret "ec" as the number of
1727 * immediate retries of failure. These retries happen too
1728 * quickly, so we disable these entirely for splits
1730 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1731 usbc_hcchar.s.ec = 1;
1732 else if (pipe->multi_count < 1)
1733 usbc_hcchar.s.ec = 1;
1734 else if (pipe->multi_count > 3)
1735 usbc_hcchar.s.ec = 3;
1737 usbc_hcchar.s.ec = pipe->multi_count;
1739 /* Set the rest of the endpoint specific settings */
1740 usbc_hcchar.s.devaddr = pipe->device_addr;
1741 usbc_hcchar.s.eptype = transaction->type;
1742 usbc_hcchar.s.lspddev = (pipe->device_speed == CVMX_USB_SPEED_LOW);
1743 usbc_hcchar.s.epdir = pipe->transfer_dir;
1744 usbc_hcchar.s.epnum = pipe->endpoint_num;
1745 usbc_hcchar.s.mps = pipe->max_packet;
1746 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
1749 /* Do transaction type specific fixups as needed */
1750 switch (transaction->type) {
1751 case CVMX_USB_TRANSFER_CONTROL:
1752 __cvmx_usb_start_channel_control(usb, channel, pipe);
1754 case CVMX_USB_TRANSFER_BULK:
1755 case CVMX_USB_TRANSFER_INTERRUPT:
1757 case CVMX_USB_TRANSFER_ISOCHRONOUS:
1758 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
1760 * ISO transactions require different PIDs depending on
1761 * direction and how many packets are needed
1763 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
1764 if (pipe->multi_count < 2) /* Need DATA0 */
1765 USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), cvmx_usbcx_hctsizx_t, pid, 0);
1766 else /* Need MDATA */
1767 USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), cvmx_usbcx_hctsizx_t, pid, 3);
1773 cvmx_usbcx_hctsizx_t usbc_hctsiz = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index))};
1774 transaction->xfersize = usbc_hctsiz.s.xfersize;
1775 transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1777 /* Remeber when we start a split transaction */
1778 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1779 usb->active_split = transaction;
1780 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), cvmx_usbcx_hccharx_t, chena, 1);
1781 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1782 __cvmx_usb_fill_tx_fifo(usb, channel);
1788 * Find a pipe that is ready to be scheduled to hardware.
1789 * @usb: USB device state populated by
1790 * cvmx_usb_initialize().
1791 * @list: Pipe list to search
1793 * Frame counter to use as a time reference.
1795 * Returns: Pipe or NULL if none are ready
1797 static 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)
1799 cvmx_usb_pipe_t *pipe = list->head;
1801 if (!(pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED) && pipe->head &&
1802 (pipe->next_tx_frame <= current_frame) &&
1803 ((pipe->split_sc_frame == -1) || ((((int)current_frame - (int)pipe->split_sc_frame) & 0x7f) < 0x40)) &&
1804 (!usb->active_split || (usb->active_split == pipe->head))) {
1805 CVMX_PREFETCH(pipe, 128);
1806 CVMX_PREFETCH(pipe->head, 0);
1816 * Called whenever a pipe might need to be scheduled to the
1819 * @usb: USB device state populated by
1820 * cvmx_usb_initialize().
1821 * @is_sof: True if this schedule was called on a SOF interrupt.
1823 static void __cvmx_usb_schedule(cvmx_usb_internal_state_t *usb, int is_sof)
1826 cvmx_usb_pipe_t *pipe;
1828 cvmx_usb_transfer_t ttype;
1830 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1831 /* Without DMA we need to be careful to not schedule something at the end of a frame and cause an overrun */
1832 cvmx_usbcx_hfnum_t hfnum = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index))};
1833 cvmx_usbcx_hfir_t hfir = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFIR(usb->index))};
1834 if (hfnum.s.frrem < hfir.s.frint/4)
1838 while (usb->idle_hardware_channels) {
1839 /* Find an idle channel */
1840 CVMX_CLZ(channel, usb->idle_hardware_channels);
1841 channel = 31 - channel;
1842 if (unlikely(channel > 7))
1845 /* Find a pipe needing service */
1849 * Only process periodic pipes on SOF interrupts. This
1850 * way we are sure that the periodic data is sent in the
1851 * beginning of the frame
1853 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_ISOCHRONOUS, usb->frame_number);
1855 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_INTERRUPT, usb->frame_number);
1857 if (likely(!pipe)) {
1858 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_CONTROL, usb->frame_number);
1860 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_BULK, usb->frame_number);
1865 __cvmx_usb_start_channel(usb, channel, pipe);
1870 * Only enable SOF interrupts when we have transactions pending in the
1871 * future that might need to be scheduled
1874 for (ttype = CVMX_USB_TRANSFER_CONTROL; ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
1875 pipe = usb->active_pipes[ttype].head;
1877 if (pipe->next_tx_frame > usb->frame_number) {
1884 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), cvmx_usbcx_gintmsk_t, sofmsk, need_sof);
1890 * Call a user's callback for a specific reason.
1892 * @usb: USB device state populated by
1893 * cvmx_usb_initialize().
1894 * @pipe: Pipe the callback is for or NULL
1896 * Transaction the callback is for or NULL
1897 * @reason: Reason this callback is being called
1899 * Completion code for the transaction, if any
1901 static void __cvmx_usb_perform_callback(cvmx_usb_internal_state_t *usb,
1902 cvmx_usb_pipe_t *pipe,
1903 cvmx_usb_transaction_t *transaction,
1904 cvmx_usb_callback_t reason,
1905 cvmx_usb_complete_t complete_code)
1907 cvmx_usb_callback_func_t callback = usb->callback[reason];
1908 void *user_data = usb->callback_data[reason];
1909 int submit_handle = -1;
1910 int pipe_handle = -1;
1911 int bytes_transferred = 0;
1914 pipe_handle = __cvmx_usb_get_pipe_handle(usb, pipe);
1917 submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
1918 bytes_transferred = transaction->actual_bytes;
1919 /* Transactions are allowed to override the default callback */
1920 if ((reason == CVMX_USB_CALLBACK_TRANSFER_COMPLETE) && transaction->callback) {
1921 callback = transaction->callback;
1922 user_data = transaction->callback_data;
1929 callback((cvmx_usb_state_t *)usb, reason, complete_code, pipe_handle, submit_handle,
1930 bytes_transferred, user_data);
1935 * Signal the completion of a transaction and free it. The
1936 * transaction will be removed from the pipe transaction list.
1938 * @usb: USB device state populated by
1939 * cvmx_usb_initialize().
1940 * @pipe: Pipe the transaction is on
1942 * Transaction that completed
1946 static void __cvmx_usb_perform_complete(cvmx_usb_internal_state_t *usb,
1947 cvmx_usb_pipe_t *pipe,
1948 cvmx_usb_transaction_t *transaction,
1949 cvmx_usb_complete_t complete_code)
1951 /* If this was a split then clear our split in progress marker */
1952 if (usb->active_split == transaction)
1953 usb->active_split = NULL;
1956 * Isochronous transactions need extra processing as they might not be
1957 * done after a single data transfer
1959 if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1960 /* Update the number of bytes transferred in this ISO packet */
1961 transaction->iso_packets[0].length = transaction->actual_bytes;
1962 transaction->iso_packets[0].status = complete_code;
1965 * If there are more ISOs pending and we succeeded, schedule the
1968 if ((transaction->iso_number_packets > 1) && (complete_code == CVMX_USB_COMPLETE_SUCCESS)) {
1969 transaction->actual_bytes = 0; /* No bytes transferred for this packet as of yet */
1970 transaction->iso_number_packets--; /* One less ISO waiting to transfer */
1971 transaction->iso_packets++; /* Increment to the next location in our packet array */
1972 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
1977 /* Remove the transaction from the pipe list */
1978 if (transaction->next)
1979 transaction->next->prev = transaction->prev;
1981 pipe->tail = transaction->prev;
1982 if (transaction->prev)
1983 transaction->prev->next = transaction->next;
1985 pipe->head = transaction->next;
1987 __cvmx_usb_remove_pipe(usb->active_pipes + pipe->transfer_type, pipe);
1988 __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
1991 __cvmx_usb_perform_callback(usb, pipe, transaction,
1992 CVMX_USB_CALLBACK_TRANSFER_COMPLETE,
1994 __cvmx_usb_free_transaction(usb, transaction);
2001 * Submit a usb transaction to a pipe. Called for all types
2006 * Which pipe to submit to. Will be validated in this function.
2007 * @type: Transaction type
2008 * @flags: Flags for the transaction
2009 * @buffer: User buffer for the transaction
2011 * User buffer's length in bytes
2013 * For control transactions, the 8 byte standard header
2015 * For ISO transactions, the start frame
2016 * @iso_number_packets:
2017 * For ISO, the number of packet in the transaction.
2019 * A description of each ISO packet
2020 * @callback: User callback to call when the transaction completes
2021 * @user_data: User's data for the callback
2023 * Returns: Submit handle or negative on failure. Matches the result
2024 * in the external API.
2026 static int __cvmx_usb_submit_transaction(cvmx_usb_internal_state_t *usb,
2028 cvmx_usb_transfer_t type,
2032 uint64_t control_header,
2033 int iso_start_frame,
2034 int iso_number_packets,
2035 cvmx_usb_iso_packet_t *iso_packets,
2036 cvmx_usb_callback_func_t callback,
2040 cvmx_usb_transaction_t *transaction;
2041 cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2043 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2045 /* Fail if the pipe isn't open */
2046 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2048 if (unlikely(pipe->transfer_type != type))
2051 transaction = __cvmx_usb_alloc_transaction(usb);
2052 if (unlikely(!transaction))
2055 transaction->type = type;
2056 transaction->flags |= flags;
2057 transaction->buffer = buffer;
2058 transaction->buffer_length = buffer_length;
2059 transaction->control_header = control_header;
2060 transaction->iso_start_frame = iso_start_frame; // FIXME: This is not used, implement it
2061 transaction->iso_number_packets = iso_number_packets;
2062 transaction->iso_packets = iso_packets;
2063 transaction->callback = callback;
2064 transaction->callback_data = user_data;
2065 if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2066 transaction->stage = CVMX_USB_STAGE_SETUP;
2068 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2070 transaction->next = NULL;
2072 transaction->prev = pipe->tail;
2073 transaction->prev->next = transaction;
2075 if (pipe->next_tx_frame < usb->frame_number)
2076 pipe->next_tx_frame = usb->frame_number + pipe->interval -
2077 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2078 transaction->prev = NULL;
2079 pipe->head = transaction;
2080 __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2081 __cvmx_usb_append_pipe(usb->active_pipes + pipe->transfer_type, pipe);
2083 pipe->tail = transaction;
2085 submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
2087 /* We may need to schedule the pipe if this was the head of the pipe */
2088 if (!transaction->prev)
2089 __cvmx_usb_schedule(usb, 0);
2091 return submit_handle;
2096 * Call to submit a USB Bulk transfer to a pipe.
2098 * @state: USB device state populated by
2099 * cvmx_usb_initialize().
2101 * Handle to the pipe for the transfer.
2102 * @buffer: Physical address of the data buffer in
2103 * memory. Note that this is NOT A POINTER, but
2104 * the full 64bit physical address of the
2105 * buffer. This may be zero if buffer_length is
2108 * Length of buffer in bytes.
2109 * @callback: Function to call when this transaction
2110 * completes. If the return value of this
2111 * function isn't an error, then this function
2112 * is guaranteed to be called when the
2113 * transaction completes. If this parameter is
2114 * NULL, then the generic callback registered
2115 * through cvmx_usb_register_callback is
2116 * called. If both are NULL, then there is no
2117 * way to know when a transaction completes.
2118 * @user_data: User supplied data returned when the
2119 * callback is called. This is only used if
2120 * callback in not NULL.
2122 * Returns: A submitted transaction handle or negative on
2123 * failure. Negative values are error codes.
2125 int cvmx_usb_submit_bulk(cvmx_usb_state_t *state, int pipe_handle,
2126 uint64_t buffer, int buffer_length,
2127 cvmx_usb_callback_func_t callback,
2131 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2133 /* Pipe handle checking is done later in a common place */
2134 if (unlikely(!buffer))
2136 if (unlikely(buffer_length < 0))
2139 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2140 CVMX_USB_TRANSFER_BULK,
2144 0, /* control_header */
2145 0, /* iso_start_frame */
2146 0, /* iso_number_packets */
2147 NULL, /* iso_packets */
2150 return submit_handle;
2155 * Call to submit a USB Interrupt transfer to a pipe.
2157 * @state: USB device state populated by
2158 * cvmx_usb_initialize().
2160 * Handle to the pipe for the transfer.
2161 * @buffer: Physical address of the data buffer in
2162 * memory. Note that this is NOT A POINTER, but
2163 * the full 64bit physical address of the
2164 * buffer. This may be zero if buffer_length is
2167 * Length of buffer in bytes.
2168 * @callback: Function to call when this transaction
2169 * completes. If the return value of this
2170 * function isn't an error, then this function
2171 * is guaranteed to be called when the
2172 * transaction completes. If this parameter is
2173 * NULL, then the generic callback registered
2174 * through cvmx_usb_register_callback is
2175 * called. If both are NULL, then there is no
2176 * way to know when a transaction completes.
2177 * @user_data: User supplied data returned when the
2178 * callback is called. This is only used if
2179 * callback in not NULL.
2181 * Returns: A submitted transaction handle or negative on
2182 * failure. Negative values are error codes.
2184 int cvmx_usb_submit_interrupt(cvmx_usb_state_t *state, int pipe_handle,
2185 uint64_t buffer, int buffer_length,
2186 cvmx_usb_callback_func_t callback,
2190 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2192 /* Pipe handle checking is done later in a common place */
2193 if (unlikely(!buffer))
2195 if (unlikely(buffer_length < 0))
2198 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2199 CVMX_USB_TRANSFER_INTERRUPT,
2203 0, /* control_header */
2204 0, /* iso_start_frame */
2205 0, /* iso_number_packets */
2206 NULL, /* iso_packets */
2209 return submit_handle;
2214 * Call to submit a USB Control transfer to a pipe.
2216 * @state: USB device state populated by
2217 * cvmx_usb_initialize().
2219 * Handle to the pipe for the transfer.
2221 * USB 8 byte control header physical address.
2222 * Note that this is NOT A POINTER, but the
2223 * full 64bit physical address of the buffer.
2224 * @buffer: Physical address of the data buffer in
2225 * memory. Note that this is NOT A POINTER, but
2226 * the full 64bit physical address of the
2227 * buffer. This may be zero if buffer_length is
2230 * Length of buffer in bytes.
2231 * @callback: Function to call when this transaction
2232 * completes. If the return value of this
2233 * function isn't an error, then this function
2234 * is guaranteed to be called when the
2235 * transaction completes. If this parameter is
2236 * NULL, then the generic callback registered
2237 * through cvmx_usb_register_callback is
2238 * called. If both are NULL, then there is no
2239 * way to know when a transaction completes.
2240 * @user_data: User supplied data returned when the
2241 * callback is called. This is only used if
2242 * callback in not NULL.
2244 * Returns: A submitted transaction handle or negative on
2245 * failure. Negative values are error codes.
2247 int cvmx_usb_submit_control(cvmx_usb_state_t *state, int pipe_handle,
2248 uint64_t control_header,
2249 uint64_t buffer, int buffer_length,
2250 cvmx_usb_callback_func_t callback,
2254 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2255 cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(control_header);
2257 /* Pipe handle checking is done later in a common place */
2258 if (unlikely(!control_header))
2260 /* Some drivers send a buffer with a zero length. God only knows why */
2261 if (unlikely(buffer && (buffer_length < 0)))
2263 if (unlikely(!buffer && (buffer_length != 0)))
2265 if ((header->s.request_type & 0x80) == 0)
2266 buffer_length = le16_to_cpu(header->s.length);
2268 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2269 CVMX_USB_TRANSFER_CONTROL,
2274 0, /* iso_start_frame */
2275 0, /* iso_number_packets */
2276 NULL, /* iso_packets */
2279 return submit_handle;
2284 * Call to submit a USB Isochronous transfer to a pipe.
2286 * @state: USB device state populated by
2287 * cvmx_usb_initialize().
2289 * Handle to the pipe for the transfer.
2291 * Number of frames into the future to schedule
2293 * @flags: Flags to control the transfer. See
2294 * cvmx_usb_isochronous_flags_t for the flag
2297 * Number of sequential packets to transfer.
2298 * "packets" is a pointer to an array of this
2299 * many packet structures.
2300 * @packets: Description of each transfer packet as
2301 * defined by cvmx_usb_iso_packet_t. The array
2302 * pointed to here must stay valid until the
2303 * complete callback is called.
2304 * @buffer: Physical address of the data buffer in
2305 * memory. Note that this is NOT A POINTER, but
2306 * the full 64bit physical address of the
2307 * buffer. This may be zero if buffer_length is
2310 * Length of buffer in bytes.
2311 * @callback: Function to call when this transaction
2312 * completes. If the return value of this
2313 * function isn't an error, then this function
2314 * is guaranteed to be called when the
2315 * transaction completes. If this parameter is
2316 * NULL, then the generic callback registered
2317 * through cvmx_usb_register_callback is
2318 * called. If both are NULL, then there is no
2319 * way to know when a transaction completes.
2320 * @user_data: User supplied data returned when the
2321 * callback is called. This is only used if
2322 * callback in not NULL.
2324 * Returns: A submitted transaction handle or negative on
2325 * failure. Negative values are error codes.
2327 int cvmx_usb_submit_isochronous(cvmx_usb_state_t *state, int pipe_handle,
2328 int start_frame, int flags,
2330 cvmx_usb_iso_packet_t packets[],
2331 uint64_t buffer, int buffer_length,
2332 cvmx_usb_callback_func_t callback,
2336 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2338 /* Pipe handle checking is done later in a common place */
2339 if (unlikely(start_frame < 0))
2341 if (unlikely(flags & ~(CVMX_USB_ISOCHRONOUS_FLAGS_ALLOW_SHORT | CVMX_USB_ISOCHRONOUS_FLAGS_ASAP)))
2343 if (unlikely(number_packets < 1))
2345 if (unlikely(!packets))
2347 if (unlikely(!buffer))
2349 if (unlikely(buffer_length < 0))
2352 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2353 CVMX_USB_TRANSFER_ISOCHRONOUS,
2357 0, /* control_header */
2363 return submit_handle;
2368 * Cancel one outstanding request in a pipe. Canceling a request
2369 * can fail if the transaction has already completed before cancel
2370 * is called. Even after a successful cancel call, it may take
2371 * a frame or two for the cvmx_usb_poll() function to call the
2372 * associated callback.
2374 * @state: USB device state populated by
2375 * cvmx_usb_initialize().
2377 * Pipe handle to cancel requests in.
2379 * Handle to transaction to cancel, returned by the submit function.
2381 * Returns: 0 or a negative error code.
2383 int cvmx_usb_cancel(cvmx_usb_state_t *state, int pipe_handle, int submit_handle)
2385 cvmx_usb_transaction_t *transaction;
2386 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2387 cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2389 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2391 if (unlikely((submit_handle < 0) || (submit_handle >= MAX_TRANSACTIONS)))
2394 /* Fail if the pipe isn't open */
2395 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2398 transaction = usb->transaction + submit_handle;
2400 /* Fail if this transaction already completed */
2401 if (unlikely((transaction->flags & __CVMX_USB_TRANSACTION_FLAGS_IN_USE) == 0))
2405 * If the transaction is the HEAD of the queue and scheduled. We need to
2408 if ((pipe->head == transaction) &&
2409 (pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
2410 cvmx_usbcx_hccharx_t usbc_hcchar;
2412 usb->pipe_for_channel[pipe->channel] = NULL;
2413 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2417 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
2418 /* If the channel isn't enabled then the transaction already completed */
2419 if (usbc_hcchar.s.chena) {
2420 usbc_hcchar.s.chdis = 1;
2421 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index), usbc_hcchar.u32);
2424 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_CANCEL);
2430 * Cancel all outstanding requests in a pipe. Logically all this
2431 * does is call cvmx_usb_cancel() in a loop.
2433 * @state: USB device state populated by
2434 * cvmx_usb_initialize().
2436 * Pipe handle to cancel requests in.
2438 * Returns: 0 or a negative error code.
2440 int cvmx_usb_cancel_all(cvmx_usb_state_t *state, int pipe_handle)
2442 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2443 cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2445 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2448 /* Fail if the pipe isn't open */
2449 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2452 /* Simply loop through and attempt to cancel each transaction */
2453 while (pipe->head) {
2454 int result = cvmx_usb_cancel(state, pipe_handle,
2455 __cvmx_usb_get_submit_handle(usb, pipe->head));
2456 if (unlikely(result != 0))
2464 * Close a pipe created with cvmx_usb_open_pipe().
2466 * @state: USB device state populated by
2467 * cvmx_usb_initialize().
2469 * Pipe handle to close.
2471 * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
2472 * outstanding transfers.
2474 int cvmx_usb_close_pipe(cvmx_usb_state_t *state, int pipe_handle)
2476 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2477 cvmx_usb_pipe_t *pipe = usb->pipe + pipe_handle;
2479 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2482 /* Fail if the pipe isn't open */
2483 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2486 /* Fail if the pipe has pending transactions */
2487 if (unlikely(pipe->head))
2491 __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2492 __cvmx_usb_append_pipe(&usb->free_pipes, pipe);
2499 * Register a function to be called when various USB events occur.
2501 * @state: USB device state populated by
2502 * cvmx_usb_initialize().
2503 * @reason: Which event to register for.
2504 * @callback: Function to call when the event occurs.
2505 * @user_data: User data parameter to the function.
2507 * Returns: 0 or a negative error code.
2509 int cvmx_usb_register_callback(cvmx_usb_state_t *state,
2510 cvmx_usb_callback_t reason,
2511 cvmx_usb_callback_func_t callback,
2514 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2516 if (unlikely(reason >= __CVMX_USB_CALLBACK_END))
2518 if (unlikely(!callback))
2521 usb->callback[reason] = callback;
2522 usb->callback_data[reason] = user_data;
2529 * Get the current USB protocol level frame number. The frame
2530 * number is always in the range of 0-0x7ff.
2532 * @state: USB device state populated by
2533 * cvmx_usb_initialize().
2535 * Returns: USB frame number
2537 int cvmx_usb_get_frame_number(cvmx_usb_state_t *state)
2540 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
2541 cvmx_usbcx_hfnum_t usbc_hfnum;
2543 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2544 frame_number = usbc_hfnum.s.frnum;
2546 return frame_number;
2551 * Poll a channel for status
2554 * @channel: Channel to poll
2556 * Returns: Zero on success
2558 static int __cvmx_usb_poll_channel(cvmx_usb_internal_state_t *usb, int channel)
2560 cvmx_usbcx_hcintx_t usbc_hcint;
2561 cvmx_usbcx_hctsizx_t usbc_hctsiz;
2562 cvmx_usbcx_hccharx_t usbc_hcchar;
2563 cvmx_usb_pipe_t *pipe;
2564 cvmx_usb_transaction_t *transaction;
2565 int bytes_this_transfer;
2566 int bytes_in_last_packet;
2567 int packets_processed;
2568 int buffer_space_left;
2570 /* Read the interrupt status bits for the channel */
2571 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
2573 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2574 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2576 if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2578 * There seems to be a bug in CN31XX which can cause
2579 * interrupt IN transfers to get stuck until we do a
2580 * write of HCCHARX without changing things
2582 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2587 * In non DMA mode the channels don't halt themselves. We need
2588 * to manually disable channels that are left running
2590 if (!usbc_hcint.s.chhltd) {
2591 if (usbc_hcchar.s.chena) {
2592 cvmx_usbcx_hcintmskx_t hcintmsk;
2593 /* Disable all interrupts except CHHLTD */
2595 hcintmsk.s.chhltdmsk = 1;
2596 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), hcintmsk.u32);
2597 usbc_hcchar.s.chdis = 1;
2598 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2600 } else if (usbc_hcint.s.xfercompl) {
2601 /* Successful IN/OUT with transfer complete. Channel halt isn't needed */
2603 cvmx_dprintf("USB%d: Channel %d interrupt without halt\n", usb->index, channel);
2609 * There is are no interrupts that we need to process when the
2610 * channel is still running
2612 if (!usbc_hcint.s.chhltd)
2616 /* Disable the channel interrupts now that it is done */
2617 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
2618 usb->idle_hardware_channels |= (1<<channel);
2620 /* Make sure this channel is tied to a valid pipe */
2621 pipe = usb->pipe_for_channel[channel];
2622 CVMX_PREFETCH(pipe, 0);
2623 CVMX_PREFETCH(pipe, 128);
2626 transaction = pipe->head;
2627 CVMX_PREFETCH0(transaction);
2630 * Disconnect this pipe from the HW channel. Later the schedule
2631 * function will figure out which pipe needs to go
2633 usb->pipe_for_channel[channel] = NULL;
2634 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2637 * Read the channel config info so we can figure out how much data
2640 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2641 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
2644 * Calculating the number of bytes successfully transferred is dependent
2645 * on the transfer direction
2647 packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2648 if (usbc_hcchar.s.epdir) {
2650 * IN transactions are easy. For every byte received the
2651 * hardware decrements xfersize. All we need to do is subtract
2652 * the current value of xfersize from its starting value and we
2653 * know how many bytes were written to the buffer
2655 bytes_this_transfer = transaction->xfersize - usbc_hctsiz.s.xfersize;
2658 * OUT transaction don't decrement xfersize. Instead pktcnt is
2659 * decremented on every successful packet send. The hardware
2660 * does this when it receives an ACK, or NYET. If it doesn't
2661 * receive one of these responses pktcnt doesn't change
2663 bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2665 * The last packet may not be a full transfer if we didn't have
2668 if (bytes_this_transfer > transaction->xfersize)
2669 bytes_this_transfer = transaction->xfersize;
2671 /* Figure out how many bytes were in the last packet of the transfer */
2672 if (packets_processed)
2673 bytes_in_last_packet = bytes_this_transfer - (packets_processed-1) * usbc_hcchar.s.mps;
2675 bytes_in_last_packet = bytes_this_transfer;
2678 * As a special case, setup transactions output the setup header, not
2679 * the user's data. For this reason we don't count setup data as bytes
2682 if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2683 (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2684 bytes_this_transfer = 0;
2687 * Add the bytes transferred to the running total. It is important that
2688 * bytes_this_transfer doesn't count any data that needs to be
2691 transaction->actual_bytes += bytes_this_transfer;
2692 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
2693 buffer_space_left = transaction->iso_packets[0].length - transaction->actual_bytes;
2695 buffer_space_left = transaction->buffer_length - transaction->actual_bytes;
2698 * We need to remember the PID toggle state for the next transaction.
2699 * The hardware already updated it for the next transaction
2701 pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2704 * For high speed bulk out, assume the next transaction will need to do
2705 * a ping before proceeding. If this isn't true the ACK processing below
2706 * will clear this flag
2708 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2709 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2710 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2711 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
2713 if (usbc_hcint.s.stall) {
2715 * STALL as a response means this transaction cannot be
2716 * completed because the device can't process transactions. Tell
2717 * the user. Any data that was transferred will be counted on
2718 * the actual bytes transferred
2720 pipe->pid_toggle = 0;
2721 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_STALL);
2722 } else if (usbc_hcint.s.xacterr) {
2724 * We know at least one packet worked if we get a ACK or NAK.
2725 * Reset the retry counter
2727 if (usbc_hcint.s.nak || usbc_hcint.s.ack)
2728 transaction->retries = 0;
2729 transaction->retries++;
2730 if (transaction->retries > MAX_RETRIES) {
2732 * XactErr as a response means the device signaled
2733 * something wrong with the transfer. For example, PID
2734 * toggle errors cause these
2736 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_XACTERR);
2739 * If this was a split then clear our split in progress
2742 if (usb->active_split == transaction)
2743 usb->active_split = NULL;
2745 * Rewind to the beginning of the transaction by anding
2746 * off the split complete bit
2748 transaction->stage &= ~1;
2749 pipe->split_sc_frame = -1;
2750 pipe->next_tx_frame += pipe->interval;
2751 if (pipe->next_tx_frame < usb->frame_number)
2752 pipe->next_tx_frame = usb->frame_number + pipe->interval -
2753 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2755 } else if (usbc_hcint.s.bblerr) {
2756 /* Babble Error (BblErr) */
2757 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_BABBLEERR);
2758 } else if (usbc_hcint.s.datatglerr) {
2759 /* We'll retry the exact same transaction again */
2760 transaction->retries++;
2761 } else if (usbc_hcint.s.nyet) {
2763 * NYET as a response is only allowed in three cases: as a
2764 * response to a ping, as a response to a split transaction, and
2765 * as a response to a bulk out. The ping case is handled by
2766 * hardware, so we only have splits and bulk out
2768 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
2769 transaction->retries = 0;
2771 * If there is more data to go then we need to try
2772 * again. Otherwise this transaction is complete
2774 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet))
2775 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2778 * Split transactions retry the split complete 4 times
2779 * then rewind to the start split and do the entire
2780 * transactions again
2782 transaction->retries++;
2783 if ((transaction->retries & 0x3) == 0) {
2785 * Rewind to the beginning of the transaction by
2786 * anding off the split complete bit
2788 transaction->stage &= ~1;
2789 pipe->split_sc_frame = -1;
2792 } else if (usbc_hcint.s.ack) {
2793 transaction->retries = 0;
2795 * The ACK bit can only be checked after the other error bits.
2796 * This is because a multi packet transfer may succeed in a
2797 * number of packets and then get a different response on the
2798 * last packet. In this case both ACK and the last response bit
2799 * will be set. If none of the other response bits is set, then
2800 * the last packet must have been an ACK
2802 * Since we got an ACK, we know we don't need to do a ping on
2805 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_NEED_PING;
2807 switch (transaction->type) {
2808 case CVMX_USB_TRANSFER_CONTROL:
2809 switch (transaction->stage) {
2810 case CVMX_USB_STAGE_NON_CONTROL:
2811 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2812 /* This should be impossible */
2813 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
2815 case CVMX_USB_STAGE_SETUP:
2816 pipe->pid_toggle = 1;
2817 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2818 transaction->stage = CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
2820 cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(transaction->control_header);
2821 if (header->s.length)
2822 transaction->stage = CVMX_USB_STAGE_DATA;
2824 transaction->stage = CVMX_USB_STAGE_STATUS;
2827 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2829 cvmx_usb_control_header_t *header = cvmx_phys_to_ptr(transaction->control_header);
2830 if (header->s.length)
2831 transaction->stage = CVMX_USB_STAGE_DATA;
2833 transaction->stage = CVMX_USB_STAGE_STATUS;
2836 case CVMX_USB_STAGE_DATA:
2837 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2838 transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
2840 * For setup OUT data that are splits,
2841 * the hardware doesn't appear to count
2842 * transferred data. Here we manually
2843 * update the data transferred
2845 if (!usbc_hcchar.s.epdir) {
2846 if (buffer_space_left < pipe->max_packet)
2847 transaction->actual_bytes += buffer_space_left;
2849 transaction->actual_bytes += pipe->max_packet;
2851 } else if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2852 pipe->pid_toggle = 1;
2853 transaction->stage = CVMX_USB_STAGE_STATUS;
2856 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
2857 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2858 pipe->pid_toggle = 1;
2859 transaction->stage = CVMX_USB_STAGE_STATUS;
2861 transaction->stage = CVMX_USB_STAGE_DATA;
2864 case CVMX_USB_STAGE_STATUS:
2865 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2866 transaction->stage = CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
2868 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2870 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
2871 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2875 case CVMX_USB_TRANSFER_BULK:
2876 case CVMX_USB_TRANSFER_INTERRUPT:
2878 * The only time a bulk transfer isn't complete when it
2879 * finishes with an ACK is during a split transaction.
2880 * For splits we need to continue the transfer if more
2883 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2884 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
2885 transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2887 if (buffer_space_left && (bytes_in_last_packet == pipe->max_packet))
2888 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2890 if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
2891 pipe->next_tx_frame += pipe->interval;
2892 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2896 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2897 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2898 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
2900 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
2901 if (!buffer_space_left || (bytes_in_last_packet < pipe->max_packet)) {
2902 if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
2903 pipe->next_tx_frame += pipe->interval;
2904 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2908 case CVMX_USB_TRANSFER_ISOCHRONOUS:
2909 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2911 * ISOCHRONOUS OUT splits don't require a
2912 * complete split stage. Instead they use a
2913 * sequence of begin OUT splits to transfer the
2914 * data 188 bytes at a time. Once the transfer
2915 * is complete, the pipe sleeps until the next
2918 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
2920 * If no space left or this wasn't a max
2921 * size packet then this transfer is
2922 * complete. Otherwise start it again to
2923 * send the next 188 bytes
2925 if (!buffer_space_left || (bytes_this_transfer < 188)) {
2926 pipe->next_tx_frame += pipe->interval;
2927 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2930 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
2932 * We are in the incoming data
2933 * phase. Keep getting data
2934 * until we run out of space or
2935 * get a small packet
2937 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2938 pipe->next_tx_frame += pipe->interval;
2939 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2942 transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2945 pipe->next_tx_frame += pipe->interval;
2946 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2950 } else if (usbc_hcint.s.nak) {
2951 /* If this was a split then clear our split in progress marker */
2952 if (usb->active_split == transaction)
2953 usb->active_split = NULL;
2955 * NAK as a response means the device couldn't accept the
2956 * transaction, but it should be retried in the future. Rewind
2957 * to the beginning of the transaction by anding off the split
2958 * complete bit. Retry in the next interval
2960 transaction->retries = 0;
2961 transaction->stage &= ~1;
2962 pipe->next_tx_frame += pipe->interval;
2963 if (pipe->next_tx_frame < usb->frame_number)
2964 pipe->next_tx_frame = usb->frame_number + pipe->interval -
2965 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2967 cvmx_usb_port_status_t port;
2968 port = cvmx_usb_get_status((cvmx_usb_state_t *)usb);
2969 if (port.port_enabled) {
2970 /* We'll retry the exact same transaction again */
2971 transaction->retries++;
2974 * We get channel halted interrupts with no result bits
2975 * sets when the cable is unplugged
2977 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
2985 * Poll the USB block for status and call all needed callback
2986 * handlers. This function is meant to be called in the interrupt
2987 * handler for the USB controller. It can also be called
2988 * periodically in a loop for non-interrupt based operation.
2990 * @state: USB device state populated by
2991 * cvmx_usb_initialize().
2993 * Returns: 0 or a negative error code.
2995 int cvmx_usb_poll(cvmx_usb_state_t *state)
2997 cvmx_usbcx_hfnum_t usbc_hfnum;
2998 cvmx_usbcx_gintsts_t usbc_gintsts;
2999 cvmx_usb_internal_state_t *usb = (cvmx_usb_internal_state_t *)state;
3001 CVMX_PREFETCH(usb, 0);
3002 CVMX_PREFETCH(usb, 1*128);
3003 CVMX_PREFETCH(usb, 2*128);
3004 CVMX_PREFETCH(usb, 3*128);
3005 CVMX_PREFETCH(usb, 4*128);
3007 /* Update the frame counter */
3008 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
3009 if ((usb->frame_number&0x3fff) > usbc_hfnum.s.frnum)
3010 usb->frame_number += 0x4000;
3011 usb->frame_number &= ~0x3fffull;
3012 usb->frame_number |= usbc_hfnum.s.frnum;
3014 /* Read the pending interrupts */
3015 usbc_gintsts.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTSTS(usb->index));
3017 /* Clear the interrupts now that we know about them */
3018 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index), usbc_gintsts.u32);
3020 if (usbc_gintsts.s.rxflvl) {
3022 * RxFIFO Non-Empty (RxFLvl)
3023 * Indicates that there is at least one packet pending to be
3024 * read from the RxFIFO.
3026 * In DMA mode this is handled by hardware
3028 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3029 __cvmx_usb_poll_rx_fifo(usb);
3031 if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
3032 /* Fill the Tx FIFOs when not in DMA mode */
3033 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3034 __cvmx_usb_poll_tx_fifo(usb);
3036 if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
3037 cvmx_usbcx_hprt_t usbc_hprt;
3039 * Disconnect Detected Interrupt (DisconnInt)
3040 * Asserted when a device disconnect is detected.
3042 * Host Port Interrupt (PrtInt)
3043 * The core sets this bit to indicate a change in port status of
3044 * one of the O2P USB core ports in Host mode. The application
3045 * must read the Host Port Control and Status (HPRT) register to
3046 * determine the exact event that caused this interrupt. The
3047 * application must clear the appropriate status bit in the Host
3048 * Port Control and Status register to clear this bit.
3050 * Call the user's port callback
3052 __cvmx_usb_perform_callback(usb, NULL, NULL,
3053 CVMX_USB_CALLBACK_PORT_CHANGED,
3054 CVMX_USB_COMPLETE_SUCCESS);
3055 /* Clear the port change bits */
3056 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
3057 usbc_hprt.s.prtena = 0;
3058 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index), usbc_hprt.u32);
3060 if (usbc_gintsts.s.hchint) {
3062 * Host Channels Interrupt (HChInt)
3063 * The core sets this bit to indicate that an interrupt is
3064 * pending on one of the channels of the core (in Host mode).
3065 * The application must read the Host All Channels Interrupt
3066 * (HAINT) register to determine the exact number of the channel
3067 * on which the interrupt occurred, and then read the
3068 * corresponding Host Channel-n Interrupt (HCINTn) register to
3069 * determine the exact cause of the interrupt. The application
3070 * must clear the appropriate status bit in the HCINTn register
3071 * to clear this bit.
3073 cvmx_usbcx_haint_t usbc_haint;
3074 usbc_haint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINT(usb->index));
3075 while (usbc_haint.u32) {
3077 CVMX_CLZ(channel, usbc_haint.u32);
3078 channel = 31 - channel;
3079 __cvmx_usb_poll_channel(usb, channel);
3080 usbc_haint.u32 ^= 1<<channel;
3084 __cvmx_usb_schedule(usb, usbc_gintsts.s.sof);