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_PREFETCH128(address) CVMX_PREFETCH(address, 128)
62 #define CVMX_PREFETCH(address, offset) CVMX_PREFETCH_PREF0(address, offset)
63 // normal prefetches that use the pref instruction
64 #define CVMX_PREFETCH_PREFX(X, address, offset) asm volatile ("pref %[type], %[off](%[rbase])" : : [rbase] "d" (address), [off] "I" (offset), [type] "n" (X))
65 #define CVMX_PREFETCH_PREF0(address, offset) CVMX_PREFETCH_PREFX(0, address, offset)
67 #define MAX_RETRIES 3 /* Maximum number of times to retry failed transactions */
68 #define MAX_PIPES 32 /* Maximum number of pipes that can be open at once */
69 #define MAX_TRANSACTIONS 256 /* Maximum number of outstanding transactions across all pipes */
70 #define MAX_CHANNELS 8 /* Maximum number of hardware channels supported by the USB block */
71 #define MAX_USB_ADDRESS 127 /* The highest valid USB device address */
72 #define MAX_USB_ENDPOINT 15 /* The highest valid USB endpoint number */
73 #define MAX_USB_HUB_PORT 15 /* The highest valid port number on a hub */
74 #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 */
75 #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 */
78 * These defines disable the normal read and write csr. This is so I can add
79 * extra debug stuff to the usb specific version and I won't use the normal
82 #define cvmx_read_csr use_cvmx_usb_read_csr64_instead_of_cvmx_read_csr
83 #define cvmx_write_csr use_cvmx_usb_write_csr64_instead_of_cvmx_write_csr
85 enum cvmx_usb_transaction_flags {
86 __CVMX_USB_TRANSACTION_FLAGS_IN_USE = 1<<16,
90 USB_CLOCK_TYPE_REF_12,
91 USB_CLOCK_TYPE_REF_24,
92 USB_CLOCK_TYPE_REF_48,
93 USB_CLOCK_TYPE_CRYSTAL_12,
97 * Logical transactions may take numerous low level
98 * transactions, especially when splits are concerned. This
99 * enum represents all of the possible stages a transaction can
100 * be in. Note that split completes are always even. This is so
101 * the NAK handler can backup to the previous low level
102 * transaction with a simple clearing of bit 0.
104 enum cvmx_usb_stage {
105 CVMX_USB_STAGE_NON_CONTROL,
106 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
107 CVMX_USB_STAGE_SETUP,
108 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
110 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
111 CVMX_USB_STAGE_STATUS,
112 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
116 * struct cvmx_usb_transaction - describes each pending USB transaction
117 * regardless of type. These are linked together
118 * to form a list of pending requests for a pipe.
120 * @prev: Transaction before this one in the pipe.
121 * @next: Transaction after this one in the pipe.
122 * @type: Type of transaction, duplicated of the pipe.
123 * @flags: State flags for this transaction.
124 * @buffer: User's physical buffer address to read/write.
125 * @buffer_length: Size of the user's buffer in bytes.
126 * @control_header: For control transactions, physical address of the 8
127 * byte standard header.
128 * @iso_start_frame: For ISO transactions, the starting frame number.
129 * @iso_number_packets: For ISO transactions, the number of packets in the
131 * @iso_packets: For ISO transactions, the sub packets in the request.
132 * @actual_bytes: Actual bytes transfer for this transaction.
133 * @stage: For control transactions, the current stage.
134 * @callback: User's callback function when complete.
135 * @callback_data: User's data.
137 struct cvmx_usb_transaction {
138 struct cvmx_usb_transaction *prev;
139 struct cvmx_usb_transaction *next;
140 enum cvmx_usb_transfer type;
141 enum cvmx_usb_transaction_flags flags;
144 uint64_t control_header;
146 int iso_number_packets;
147 struct cvmx_usb_iso_packet *iso_packets;
152 enum cvmx_usb_stage stage;
153 cvmx_usb_callback_func_t callback;
158 * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
159 * and some USB device. It contains a list of pending
160 * request to the device.
162 * @prev: Pipe before this one in the list
163 * @next: Pipe after this one in the list
164 * @head: The first pending transaction
165 * @tail: The last pending transaction
166 * @interval: For periodic pipes, the interval between packets in
168 * @next_tx_frame: The next frame this pipe is allowed to transmit on
169 * @flags: State flags for this pipe
170 * @device_speed: Speed of device connected to this pipe
171 * @transfer_type: Type of transaction supported by this pipe
172 * @transfer_dir: IN or OUT. Ignored for Control
173 * @multi_count: Max packet in a row for the device
174 * @max_packet: The device's maximum packet size in bytes
175 * @device_addr: USB device address at other end of pipe
176 * @endpoint_num: USB endpoint number at other end of pipe
177 * @hub_device_addr: Hub address this device is connected to
178 * @hub_port: Hub port this device is connected to
179 * @pid_toggle: This toggles between 0/1 on every packet send to track
180 * the data pid needed
181 * @channel: Hardware DMA channel for this pipe
182 * @split_sc_frame: The low order bits of the frame number the split
183 * complete should be sent on
185 struct cvmx_usb_pipe {
186 struct cvmx_usb_pipe *prev;
187 struct cvmx_usb_pipe *next;
188 struct cvmx_usb_transaction *head;
189 struct cvmx_usb_transaction *tail;
191 uint64_t next_tx_frame;
192 enum cvmx_usb_pipe_flags flags;
193 enum cvmx_usb_speed device_speed;
194 enum cvmx_usb_transfer transfer_type;
195 enum cvmx_usb_direction transfer_dir;
199 uint8_t endpoint_num;
200 uint8_t hub_device_addr;
204 int8_t split_sc_frame;
208 * struct cvmx_usb_pipe_list
210 * @head: Head of the list, or NULL if empty.
211 * @tail: Tail if the list, or NULL if empty.
213 struct cvmx_usb_pipe_list {
214 struct cvmx_usb_pipe *head;
215 struct cvmx_usb_pipe *tail;
218 struct cvmx_usb_tx_fifo {
223 } entry[MAX_CHANNELS+1];
229 * struct cvmx_usb_internal_state - the state of the USB block
231 * init_flags: Flags passed to initialize.
232 * index: Which USB block this is for.
233 * idle_hardware_channels: Bit set for every idle hardware channel.
234 * usbcx_hprt: Stored port status so we don't need to read a CSR to
236 * pipe_for_channel: Map channels to pipes.
237 * free_transaction_head: List of free transactions head.
238 * free_transaction_tail: List of free transactions tail.
239 * pipe: Storage for pipes.
240 * transaction: Storage for transactions.
241 * callback: User global callbacks.
242 * callback_data: User data for each callback.
243 * indent: Used by debug output to indent functions.
244 * port_status: Last port status used for change notification.
245 * free_pipes: List of all pipes that are currently closed.
246 * idle_pipes: List of open pipes that have no transactions.
247 * active_pipes: Active pipes indexed by transfer type.
248 * frame_number: Increments every SOF interrupt for time keeping.
249 * active_split: Points to the current active split, or NULL.
251 struct cvmx_usb_internal_state {
254 int idle_hardware_channels;
255 union cvmx_usbcx_hprt usbcx_hprt;
256 struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
257 struct cvmx_usb_transaction *free_transaction_head;
258 struct cvmx_usb_transaction *free_transaction_tail;
259 struct cvmx_usb_pipe pipe[MAX_PIPES];
260 struct cvmx_usb_transaction transaction[MAX_TRANSACTIONS];
261 cvmx_usb_callback_func_t callback[__CVMX_USB_CALLBACK_END];
262 void *callback_data[__CVMX_USB_CALLBACK_END];
264 struct cvmx_usb_port_status port_status;
265 struct cvmx_usb_pipe_list free_pipes;
266 struct cvmx_usb_pipe_list idle_pipes;
267 struct cvmx_usb_pipe_list active_pipes[4];
268 uint64_t frame_number;
269 struct cvmx_usb_transaction *active_split;
270 struct cvmx_usb_tx_fifo periodic;
271 struct cvmx_usb_tx_fifo nonperiodic;
274 /* This macro spins on a field waiting for it to reach a value */
275 #define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
278 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
279 octeon_get_clock_rate() / 1000000; \
282 c.u32 = __cvmx_usb_read_csr32(usb, address); \
283 if (c.s.field op (value)) { \
286 } else if (cvmx_get_cycle() > done) { \
296 * This macro logically sets a single field in a CSR. It does the sequence
297 * read, modify, and write
299 #define USB_SET_FIELD32(address, type, field, value) \
302 c.u32 = __cvmx_usb_read_csr32(usb, address); \
304 __cvmx_usb_write_csr32(usb, address, c.u32); \
307 /* Returns the IO address to push/pop stuff data from the FIFOs */
308 #define USB_FIFO_ADDRESS(channel, usb_index) (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
310 static int octeon_usb_get_clock_type(void)
312 switch (cvmx_sysinfo_get()->board_type) {
313 case CVMX_BOARD_TYPE_BBGW_REF:
314 case CVMX_BOARD_TYPE_LANAI2_A:
315 case CVMX_BOARD_TYPE_LANAI2_U:
316 case CVMX_BOARD_TYPE_LANAI2_G:
317 case CVMX_BOARD_TYPE_UBNT_E100:
318 return USB_CLOCK_TYPE_CRYSTAL_12;
320 return USB_CLOCK_TYPE_REF_48;
324 * Read a USB 32bit CSR. It performs the necessary address swizzle
325 * for 32bit CSRs and logs the value in a readable format if
328 * @usb: USB block this access is for
329 * @address: 64bit address to read
331 * Returns: Result of the read
333 static inline uint32_t __cvmx_usb_read_csr32(struct cvmx_usb_internal_state *usb,
336 uint32_t result = cvmx_read64_uint32(address ^ 4);
342 * Write a USB 32bit CSR. It performs the necessary address
343 * swizzle for 32bit CSRs and logs the value in a readable format
344 * if debugging is on.
346 * @usb: USB block this access is for
347 * @address: 64bit address to write
348 * @value: Value to write
350 static inline void __cvmx_usb_write_csr32(struct cvmx_usb_internal_state *usb,
351 uint64_t address, uint32_t value)
353 cvmx_write64_uint32(address ^ 4, value);
354 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
359 * Read a USB 64bit CSR. It logs the value in a readable format if
362 * @usb: USB block this access is for
363 * @address: 64bit address to read
365 * Returns: Result of the read
367 static inline uint64_t __cvmx_usb_read_csr64(struct cvmx_usb_internal_state *usb,
370 uint64_t result = cvmx_read64_uint64(address);
376 * Write a USB 64bit CSR. It logs the value in a readable format
377 * if debugging is on.
379 * @usb: USB block this access is for
380 * @address: 64bit address to write
381 * @value: Value to write
383 static inline void __cvmx_usb_write_csr64(struct cvmx_usb_internal_state *usb,
384 uint64_t address, uint64_t value)
386 cvmx_write64_uint64(address, value);
390 * Return non zero if this pipe connects to a non HIGH speed
391 * device through a high speed hub.
393 * @usb: USB block this access is for
394 * @pipe: Pipe to check
396 * Returns: Non zero if we need to do split transactions
398 static inline int __cvmx_usb_pipe_needs_split(struct cvmx_usb_internal_state *usb, struct cvmx_usb_pipe *pipe)
400 return ((pipe->device_speed != CVMX_USB_SPEED_HIGH) && (usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH));
405 * Trivial utility function to return the correct PID for a pipe
407 * @pipe: pipe to check
409 * Returns: PID for pipe
411 static inline int __cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
413 if (pipe->pid_toggle)
414 return 2; /* Data1 */
416 return 0; /* Data0 */
421 * Return the number of USB ports supported by this Octeon
422 * chip. If the chip doesn't support USB, or is not supported
423 * by this API, a zero will be returned. Most Octeon chips
424 * support one usb port, but some support two ports.
425 * cvmx_usb_initialize() must be called on independent
426 * struct cvmx_usb_state.
428 * Returns: Number of port, zero if usb isn't supported
430 int cvmx_usb_get_num_ports(void)
434 if (OCTEON_IS_MODEL(OCTEON_CN56XX))
436 else if (OCTEON_IS_MODEL(OCTEON_CN52XX))
438 else if (OCTEON_IS_MODEL(OCTEON_CN50XX))
440 else if (OCTEON_IS_MODEL(OCTEON_CN31XX))
442 else if (OCTEON_IS_MODEL(OCTEON_CN30XX))
452 * Allocate a usb transaction for use
454 * @usb: USB device state populated by
455 * cvmx_usb_initialize().
457 * Returns: Transaction or NULL
459 static inline struct cvmx_usb_transaction *__cvmx_usb_alloc_transaction(struct cvmx_usb_internal_state *usb)
461 struct cvmx_usb_transaction *t;
462 t = usb->free_transaction_head;
464 usb->free_transaction_head = t->next;
465 if (!usb->free_transaction_head)
466 usb->free_transaction_tail = NULL;
469 memset(t, 0, sizeof(*t));
470 t->flags = __CVMX_USB_TRANSACTION_FLAGS_IN_USE;
477 * Free a usb transaction
479 * @usb: USB device state populated by
480 * cvmx_usb_initialize().
482 * Transaction to free
484 static inline void __cvmx_usb_free_transaction(struct cvmx_usb_internal_state *usb,
485 struct cvmx_usb_transaction *transaction)
487 transaction->flags = 0;
488 transaction->prev = NULL;
489 transaction->next = NULL;
490 if (usb->free_transaction_tail)
491 usb->free_transaction_tail->next = transaction;
493 usb->free_transaction_head = transaction;
494 usb->free_transaction_tail = transaction;
499 * Add a pipe to the tail of a list
500 * @list: List to add pipe to
503 static inline void __cvmx_usb_append_pipe(struct cvmx_usb_pipe_list *list, struct cvmx_usb_pipe *pipe)
506 pipe->prev = list->tail;
508 list->tail->next = pipe;
516 * Remove a pipe from a list
517 * @list: List to remove pipe from
518 * @pipe: Pipe to remove
520 static inline void __cvmx_usb_remove_pipe(struct cvmx_usb_pipe_list *list, struct cvmx_usb_pipe *pipe)
522 if (list->head == pipe) {
523 list->head = pipe->next;
526 list->head->prev = NULL;
529 } else if (list->tail == pipe) {
530 list->tail = pipe->prev;
531 list->tail->next = NULL;
534 pipe->prev->next = pipe->next;
535 pipe->next->prev = pipe->prev;
543 * Initialize a USB port for use. This must be called before any
544 * other access to the Octeon USB port is made. The port starts
545 * off in the disabled state.
547 * @state: Pointer to an empty struct cvmx_usb_state
548 * that will be populated by the initialize call.
549 * This structure is then passed to all other USB
552 * Which Octeon USB port to initialize.
554 * Returns: 0 or a negative error code.
556 int cvmx_usb_initialize(struct cvmx_usb_state *state, int usb_port_number)
558 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
559 union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
560 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
561 enum cvmx_usb_initialize_flags flags = 0;
563 /* Make sure that state is large enough to store the internal state */
564 if (sizeof(*state) < sizeof(*usb))
566 /* At first allow 0-1 for the usb port number */
567 if ((usb_port_number < 0) || (usb_port_number > 1))
569 /* For all chips except 52XX there is only one port */
570 if (!OCTEON_IS_MODEL(OCTEON_CN52XX) && (usb_port_number > 0))
572 /* Try to determine clock type automatically */
573 if (octeon_usb_get_clock_type() == USB_CLOCK_TYPE_CRYSTAL_12) {
574 /* Only 12 MHZ crystals are supported */
575 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
577 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
579 switch (octeon_usb_get_clock_type()) {
580 case USB_CLOCK_TYPE_REF_12:
581 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
583 case USB_CLOCK_TYPE_REF_24:
584 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
586 case USB_CLOCK_TYPE_REF_48:
587 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
595 memset(usb, 0, sizeof(*usb));
596 usb->init_flags = flags;
598 /* Initialize the USB state structure */
601 usb->index = usb_port_number;
603 /* Initialize the transaction double linked list */
604 usb->free_transaction_head = NULL;
605 usb->free_transaction_tail = NULL;
606 for (i = 0; i < MAX_TRANSACTIONS; i++)
607 __cvmx_usb_free_transaction(usb, usb->transaction + i);
608 for (i = 0; i < MAX_PIPES; i++)
609 __cvmx_usb_append_pipe(&usb->free_pipes, usb->pipe + i);
613 * Power On Reset and PHY Initialization
615 * 1. Wait for DCOK to assert (nothing to do)
617 * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
618 * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
620 usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
621 usbn_clk_ctl.s.por = 1;
622 usbn_clk_ctl.s.hrst = 0;
623 usbn_clk_ctl.s.prst = 0;
624 usbn_clk_ctl.s.hclk_rst = 0;
625 usbn_clk_ctl.s.enable = 0;
627 * 2b. Select the USB reference clock/crystal parameters by writing
628 * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
630 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
632 * The USB port uses 12/24/48MHz 2.5V board clock
633 * source at USB_XO. USB_XI should be tied to GND.
634 * Most Octeon evaluation boards require this setting
636 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
637 usbn_clk_ctl.cn31xx.p_rclk = 1; /* From CN31XX,CN30XX manual */
638 usbn_clk_ctl.cn31xx.p_xenbn = 0;
639 } else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
640 usbn_clk_ctl.cn56xx.p_rtype = 2; /* From CN56XX,CN50XX manual */
642 usbn_clk_ctl.cn52xx.p_rtype = 1; /* From CN52XX manual */
644 switch (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
645 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
646 usbn_clk_ctl.s.p_c_sel = 0;
648 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
649 usbn_clk_ctl.s.p_c_sel = 1;
651 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
652 usbn_clk_ctl.s.p_c_sel = 2;
657 * The USB port uses a 12MHz crystal as clock source
658 * at USB_XO and USB_XI
660 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
661 usbn_clk_ctl.cn31xx.p_rclk = 1; /* From CN31XX,CN30XX manual */
662 usbn_clk_ctl.cn31xx.p_xenbn = 1;
663 } else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
664 usbn_clk_ctl.cn56xx.p_rtype = 0; /* From CN56XX,CN50XX manual */
666 usbn_clk_ctl.cn52xx.p_rtype = 0; /* From CN52XX manual */
668 usbn_clk_ctl.s.p_c_sel = 0;
671 * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
672 * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
673 * such that USB is as close as possible to 125Mhz
676 int divisor = (octeon_get_clock_rate()+125000000-1)/125000000;
677 if (divisor < 4) /* Lower than 4 doesn't seem to work properly */
679 usbn_clk_ctl.s.divide = divisor;
680 usbn_clk_ctl.s.divide2 = 0;
682 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
684 /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
685 usbn_clk_ctl.s.hclk_rst = 1;
686 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
688 /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
691 * 3. Program the power-on reset field in the USBN clock-control
693 * USBN_CLK_CTL[POR] = 0
695 usbn_clk_ctl.s.por = 0;
696 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
698 /* 4. Wait 1 ms for PHY clock to start */
701 * 5. Program the Reset input from automatic test equipment field in the
702 * USBP control and status register:
703 * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
705 usbn_usbp_ctl_status.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index));
706 usbn_usbp_ctl_status.s.ate_reset = 1;
707 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
708 usbn_usbp_ctl_status.u64);
709 /* 6. Wait 10 cycles */
712 * 7. Clear ATE_RESET field in the USBN clock-control register:
713 * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
715 usbn_usbp_ctl_status.s.ate_reset = 0;
716 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
717 usbn_usbp_ctl_status.u64);
719 * 8. Program the PHY reset field in the USBN clock-control register:
720 * USBN_CLK_CTL[PRST] = 1
722 usbn_clk_ctl.s.prst = 1;
723 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
726 * 9. Program the USBP control and status register to select host or
727 * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
730 usbn_usbp_ctl_status.s.hst_mode = 0;
731 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
732 usbn_usbp_ctl_status.u64);
736 * 11. Program the hreset_n field in the USBN clock-control register:
737 * USBN_CLK_CTL[HRST] = 1
739 usbn_clk_ctl.s.hrst = 1;
740 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
742 /* 12. Proceed to USB core initialization */
743 usbn_clk_ctl.s.enable = 1;
744 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
749 * USB Core Initialization
751 * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
752 * determine USB core configuration parameters.
756 * 2. Program the following fields in the global AHB configuration
757 * register (USBC_GAHBCFG)
758 * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
759 * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
760 * Nonperiodic TxFIFO empty level (slave mode only),
761 * USBC_GAHBCFG[NPTXFEMPLVL]
762 * Periodic TxFIFO empty level (slave mode only),
763 * USBC_GAHBCFG[PTXFEMPLVL]
764 * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
767 union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
768 /* Due to an errata, CN31XX doesn't support DMA */
769 if (OCTEON_IS_MODEL(OCTEON_CN31XX))
770 usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
771 usbcx_gahbcfg.u32 = 0;
772 usbcx_gahbcfg.s.dmaen = !(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
773 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
774 usb->idle_hardware_channels = 0x1; /* Only use one channel with non DMA */
775 else if (OCTEON_IS_MODEL(OCTEON_CN5XXX))
776 usb->idle_hardware_channels = 0xf7; /* CN5XXX have an errata with channel 3 */
778 usb->idle_hardware_channels = 0xff;
779 usbcx_gahbcfg.s.hbstlen = 0;
780 usbcx_gahbcfg.s.nptxfemplvl = 1;
781 usbcx_gahbcfg.s.ptxfemplvl = 1;
782 usbcx_gahbcfg.s.glblintrmsk = 1;
783 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
787 * 3. Program the following fields in USBC_GUSBCFG register.
788 * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
789 * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
790 * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
791 * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
794 union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
795 usbcx_gusbcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index));
796 usbcx_gusbcfg.s.toutcal = 0;
797 usbcx_gusbcfg.s.ddrsel = 0;
798 usbcx_gusbcfg.s.usbtrdtim = 0x5;
799 usbcx_gusbcfg.s.phylpwrclksel = 0;
800 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
804 * 4. The software must unmask the following bits in the USBC_GINTMSK
806 * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
807 * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
810 union cvmx_usbcx_gintmsk usbcx_gintmsk;
813 usbcx_gintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTMSK(usb->index));
814 usbcx_gintmsk.s.otgintmsk = 1;
815 usbcx_gintmsk.s.modemismsk = 1;
816 usbcx_gintmsk.s.hchintmsk = 1;
817 usbcx_gintmsk.s.sofmsk = 0;
818 /* We need RX FIFO interrupts if we don't have DMA */
819 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
820 usbcx_gintmsk.s.rxflvlmsk = 1;
821 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
824 /* Disable all channel interrupts. We'll enable them per channel later */
825 for (channel = 0; channel < 8; channel++)
826 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
831 * Host Port Initialization
833 * 1. Program the host-port interrupt-mask field to unmask,
834 * USBC_GINTMSK[PRTINT] = 1
836 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk,
838 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk,
841 * 2. Program the USBC_HCFG register to select full-speed host
842 * or high-speed host.
845 union cvmx_usbcx_hcfg usbcx_hcfg;
846 usbcx_hcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
847 usbcx_hcfg.s.fslssupp = 0;
848 usbcx_hcfg.s.fslspclksel = 0;
849 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
852 * 3. Program the port power bit to drive VBUS on the USB,
853 * USBC_HPRT[PRTPWR] = 1
855 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtpwr, 1);
858 * Steps 4-15 from the manual are done later in the port enable
867 * Shutdown a USB port after a call to cvmx_usb_initialize().
868 * The port should be disabled with all pipes closed when this
869 * function is called.
871 * @state: USB device state populated by
872 * cvmx_usb_initialize().
874 * Returns: 0 or a negative error code.
876 int cvmx_usb_shutdown(struct cvmx_usb_state *state)
878 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
879 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
881 /* Make sure all pipes are closed */
882 if (usb->idle_pipes.head ||
883 usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS].head ||
884 usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT].head ||
885 usb->active_pipes[CVMX_USB_TRANSFER_CONTROL].head ||
886 usb->active_pipes[CVMX_USB_TRANSFER_BULK].head)
889 /* Disable the clocks and put them in power on reset */
890 usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
891 usbn_clk_ctl.s.enable = 1;
892 usbn_clk_ctl.s.por = 1;
893 usbn_clk_ctl.s.hclk_rst = 1;
894 usbn_clk_ctl.s.prst = 0;
895 usbn_clk_ctl.s.hrst = 0;
896 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
903 * Enable a USB port. After this call succeeds, the USB port is
904 * online and servicing requests.
906 * @state: USB device state populated by
907 * cvmx_usb_initialize().
909 * Returns: 0 or a negative error code.
911 int cvmx_usb_enable(struct cvmx_usb_state *state)
913 union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
914 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
916 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
919 * If the port is already enabled the just return. We don't need to do
922 if (usb->usbcx_hprt.s.prtena)
925 /* If there is nothing plugged into the port then fail immediately */
926 if (!usb->usbcx_hprt.s.prtconnsts) {
930 /* Program the port reset bit to start the reset process */
931 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtrst, 1);
934 * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
935 * process to complete.
939 /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
940 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtrst, 0);
942 /* Wait for the USBC_HPRT[PRTENA]. */
943 if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
944 prtena, ==, 1, 100000))
947 /* Read the port speed field to get the enumerated speed, USBC_HPRT[PRTSPD]. */
948 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
949 usbcx_ghwcfg3.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GHWCFG3(usb->index));
952 * 13. Program the USBC_GRXFSIZ register to select the size of the
953 * receive FIFO (25%).
955 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), union cvmx_usbcx_grxfsiz,
956 rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
958 * 14. Program the USBC_GNPTXFSIZ register to select the size and the
959 * start address of the non- periodic transmit FIFO for nonperiodic
960 * transactions (50%).
963 union cvmx_usbcx_gnptxfsiz siz;
964 siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
965 siz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
966 siz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
967 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), siz.u32);
970 * 15. Program the USBC_HPTXFSIZ register to select the size and start
971 * address of the periodic transmit FIFO for periodic transactions
975 union cvmx_usbcx_hptxfsiz siz;
976 siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
977 siz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
978 siz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
979 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), siz.u32);
981 /* Flush all FIFOs */
982 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl, txfnum, 0x10);
983 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl, txfflsh, 1);
984 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl,
985 txfflsh, ==, 0, 100);
986 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl, rxfflsh, 1);
987 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl,
988 rxfflsh, ==, 0, 100);
995 * Disable a USB port. After this call the USB port will not
996 * generate data transfers and will not generate events.
997 * Transactions in process will fail and call their
998 * associated callbacks.
1000 * @state: USB device state populated by
1001 * cvmx_usb_initialize().
1003 * Returns: 0 or a negative error code.
1005 int cvmx_usb_disable(struct cvmx_usb_state *state)
1007 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
1009 /* Disable the port */
1010 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtena, 1);
1016 * Get the current state of the USB port. Use this call to
1017 * determine if the usb port has anything connected, is enabled,
1018 * or has some sort of error condition. The return value of this
1019 * call has "changed" bits to signal of the value of some fields
1020 * have changed between calls. These "changed" fields are based
1021 * on the last call to cvmx_usb_set_status(). In order to clear
1022 * them, you must update the status through cvmx_usb_set_status().
1024 * @state: USB device state populated by
1025 * cvmx_usb_initialize().
1027 * Returns: Port status information
1029 struct cvmx_usb_port_status cvmx_usb_get_status(struct cvmx_usb_state *state)
1031 union cvmx_usbcx_hprt usbc_hprt;
1032 struct cvmx_usb_port_status result;
1033 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
1035 memset(&result, 0, sizeof(result));
1037 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1038 result.port_enabled = usbc_hprt.s.prtena;
1039 result.port_over_current = usbc_hprt.s.prtovrcurract;
1040 result.port_powered = usbc_hprt.s.prtpwr;
1041 result.port_speed = usbc_hprt.s.prtspd;
1042 result.connected = usbc_hprt.s.prtconnsts;
1043 result.connect_change = (result.connected != usb->port_status.connected);
1050 * Set the current state of the USB port. The status is used as
1051 * a reference for the "changed" bits returned by
1052 * cvmx_usb_get_status(). Other than serving as a reference, the
1053 * status passed to this function is not used. No fields can be
1054 * changed through this call.
1056 * @state: USB device state populated by
1057 * cvmx_usb_initialize().
1059 * Port status to set, most like returned by cvmx_usb_get_status()
1061 void cvmx_usb_set_status(struct cvmx_usb_state *state, struct cvmx_usb_port_status port_status)
1063 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
1064 usb->port_status = port_status;
1070 * Convert a USB transaction into a handle
1072 * @usb: USB device state populated by
1073 * cvmx_usb_initialize().
1075 * Transaction to get handle for
1079 static inline int __cvmx_usb_get_submit_handle(struct cvmx_usb_internal_state *usb,
1080 struct cvmx_usb_transaction *transaction)
1082 return ((unsigned long)transaction - (unsigned long)usb->transaction) /
1083 sizeof(*transaction);
1088 * Convert a USB pipe into a handle
1090 * @usb: USB device state populated by
1091 * cvmx_usb_initialize().
1092 * @pipe: Pipe to get handle for
1096 static inline int __cvmx_usb_get_pipe_handle(struct cvmx_usb_internal_state *usb,
1097 struct cvmx_usb_pipe *pipe)
1099 return ((unsigned long)pipe - (unsigned long)usb->pipe) / sizeof(*pipe);
1104 * Open a virtual pipe between the host and a USB device. A pipe
1105 * must be opened before data can be transferred between a device
1108 * @state: USB device state populated by
1109 * cvmx_usb_initialize().
1111 * USB device address to open the pipe to
1114 * USB endpoint number to open the pipe to
1117 * The speed of the device the pipe is going
1118 * to. This must match the device's speed,
1119 * which may be different than the port speed.
1120 * @max_packet: The maximum packet length the device can
1121 * transmit/receive (low speed=0-8, full
1122 * speed=0-1023, high speed=0-1024). This value
1123 * comes from the standard endpoint descriptor
1124 * field wMaxPacketSize bits <10:0>.
1126 * The type of transfer this pipe is for.
1128 * The direction the pipe is in. This is not
1129 * used for control pipes.
1130 * @interval: For ISOCHRONOUS and INTERRUPT transfers,
1131 * this is how often the transfer is scheduled
1132 * for. All other transfers should specify
1133 * zero. The units are in frames (8000/sec at
1134 * high speed, 1000/sec for full speed).
1136 * For high speed devices, this is the maximum
1137 * allowed number of packet per microframe.
1138 * Specify zero for non high speed devices. This
1139 * value comes from the standard endpoint descriptor
1140 * field wMaxPacketSize bits <12:11>.
1142 * Hub device address this device is connected
1143 * to. Devices connected directly to Octeon
1144 * use zero. This is only used when the device
1145 * is full/low speed behind a high speed hub.
1146 * The address will be of the high speed hub,
1147 * not and full speed hubs after it.
1148 * @hub_port: Which port on the hub the device is
1149 * connected. Use zero for devices connected
1150 * directly to Octeon. Like hub_device_addr,
1151 * this is only used for full/low speed
1152 * devices behind a high speed hub.
1154 * Returns: A non negative value is a pipe handle. Negative
1155 * values are error codes.
1157 int cvmx_usb_open_pipe(struct cvmx_usb_state *state,
1158 int device_addr, int endpoint_num,
1159 enum cvmx_usb_speed device_speed, int max_packet,
1160 enum cvmx_usb_transfer transfer_type,
1161 enum cvmx_usb_direction transfer_dir, int interval,
1162 int multi_count, int hub_device_addr, int hub_port)
1164 struct cvmx_usb_pipe *pipe;
1165 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
1167 if (unlikely((device_addr < 0) || (device_addr > MAX_USB_ADDRESS)))
1169 if (unlikely((endpoint_num < 0) || (endpoint_num > MAX_USB_ENDPOINT)))
1171 if (unlikely(device_speed > CVMX_USB_SPEED_LOW))
1173 if (unlikely((max_packet <= 0) || (max_packet > 1024)))
1175 if (unlikely(transfer_type > CVMX_USB_TRANSFER_INTERRUPT))
1177 if (unlikely((transfer_dir != CVMX_USB_DIRECTION_OUT) &&
1178 (transfer_dir != CVMX_USB_DIRECTION_IN)))
1180 if (unlikely(interval < 0))
1182 if (unlikely((transfer_type == CVMX_USB_TRANSFER_CONTROL) && interval))
1184 if (unlikely(multi_count < 0))
1186 if (unlikely((device_speed != CVMX_USB_SPEED_HIGH) &&
1187 (multi_count != 0)))
1189 if (unlikely((hub_device_addr < 0) || (hub_device_addr > MAX_USB_ADDRESS)))
1191 if (unlikely((hub_port < 0) || (hub_port > MAX_USB_HUB_PORT)))
1194 /* Find a free pipe */
1195 pipe = usb->free_pipes.head;
1198 __cvmx_usb_remove_pipe(&usb->free_pipes, pipe);
1199 pipe->flags = __CVMX_USB_PIPE_FLAGS_OPEN;
1200 if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1201 (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1202 (transfer_type == CVMX_USB_TRANSFER_BULK))
1203 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
1204 pipe->device_addr = device_addr;
1205 pipe->endpoint_num = endpoint_num;
1206 pipe->device_speed = device_speed;
1207 pipe->max_packet = max_packet;
1208 pipe->transfer_type = transfer_type;
1209 pipe->transfer_dir = transfer_dir;
1211 * All pipes use interval to rate limit NAK processing. Force an
1212 * interval if one wasn't supplied
1216 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1217 pipe->interval = interval*8;
1218 /* Force start splits to be schedule on uFrame 0 */
1219 pipe->next_tx_frame = ((usb->frame_number+7)&~7) + pipe->interval;
1221 pipe->interval = interval;
1222 pipe->next_tx_frame = usb->frame_number + pipe->interval;
1224 pipe->multi_count = multi_count;
1225 pipe->hub_device_addr = hub_device_addr;
1226 pipe->hub_port = hub_port;
1227 pipe->pid_toggle = 0;
1228 pipe->split_sc_frame = -1;
1229 __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
1232 * We don't need to tell the hardware about this pipe yet since
1233 * it doesn't have any submitted requests
1236 return __cvmx_usb_get_pipe_handle(usb, pipe);
1241 * Poll the RX FIFOs and remove data as needed. This function is only used
1242 * in non DMA mode. It is very important that this function be called quickly
1243 * enough to prevent FIFO overflow.
1245 * @usb: USB device state populated by
1246 * cvmx_usb_initialize().
1248 static void __cvmx_usb_poll_rx_fifo(struct cvmx_usb_internal_state *usb)
1250 union cvmx_usbcx_grxstsph rx_status;
1256 rx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GRXSTSPH(usb->index));
1257 /* Only read data if IN data is there */
1258 if (rx_status.s.pktsts != 2)
1260 /* Check if no data is available */
1261 if (!rx_status.s.bcnt)
1264 channel = rx_status.s.chnum;
1265 bytes = rx_status.s.bcnt;
1269 /* Get where the DMA engine would have written this data */
1270 address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8);
1271 ptr = cvmx_phys_to_ptr(address);
1272 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, address + bytes);
1274 /* Loop writing the FIFO data for this packet into memory */
1276 *ptr++ = __cvmx_usb_read_csr32(usb, USB_FIFO_ADDRESS(channel, usb->index));
1286 * Fill the TX hardware fifo with data out of the software
1289 * @usb: USB device state populated by
1290 * cvmx_usb_initialize().
1291 * @fifo: Software fifo to use
1292 * @available: Amount of space in the hardware fifo
1294 * Returns: Non zero if the hardware fifo was too small and needs
1295 * to be serviced again.
1297 static int __cvmx_usb_fill_tx_hw(struct cvmx_usb_internal_state *usb, struct cvmx_usb_tx_fifo *fifo, int available)
1300 * We're done either when there isn't anymore space or the software FIFO
1303 while (available && (fifo->head != fifo->tail)) {
1305 const uint32_t *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1306 uint64_t csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel, usb->index) ^ 4;
1307 int words = available;
1309 /* Limit the amount of data to waht the SW fifo has */
1310 if (fifo->entry[i].size <= available) {
1311 words = fifo->entry[i].size;
1313 if (fifo->tail > MAX_CHANNELS)
1317 /* Update the next locations and counts */
1319 fifo->entry[i].address += words * 4;
1320 fifo->entry[i].size -= words;
1323 * Write the HW fifo data. The read every three writes is due
1324 * to an errata on CN3XXX chips
1327 cvmx_write64_uint32(csr_address, *ptr++);
1328 cvmx_write64_uint32(csr_address, *ptr++);
1329 cvmx_write64_uint32(csr_address, *ptr++);
1330 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1333 cvmx_write64_uint32(csr_address, *ptr++);
1335 cvmx_write64_uint32(csr_address, *ptr++);
1337 cvmx_write64_uint32(csr_address, *ptr++);
1339 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1341 return fifo->head != fifo->tail;
1346 * Check the hardware FIFOs and fill them as needed
1348 * @usb: USB device state populated by
1349 * cvmx_usb_initialize().
1351 static void __cvmx_usb_poll_tx_fifo(struct cvmx_usb_internal_state *usb)
1353 if (usb->periodic.head != usb->periodic.tail) {
1354 union cvmx_usbcx_hptxsts tx_status;
1355 tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXSTS(usb->index));
1356 if (__cvmx_usb_fill_tx_hw(usb, &usb->periodic, tx_status.s.ptxfspcavail))
1357 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, ptxfempmsk, 1);
1359 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, ptxfempmsk, 0);
1362 if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1363 union cvmx_usbcx_gnptxsts tx_status;
1364 tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXSTS(usb->index));
1365 if (__cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic, tx_status.s.nptxfspcavail))
1366 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, nptxfempmsk, 1);
1368 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, nptxfempmsk, 0);
1376 * Fill the TX FIFO with an outgoing packet
1378 * @usb: USB device state populated by
1379 * cvmx_usb_initialize().
1380 * @channel: Channel number to get packet from
1382 static void __cvmx_usb_fill_tx_fifo(struct cvmx_usb_internal_state *usb, int channel)
1384 union cvmx_usbcx_hccharx hcchar;
1385 union cvmx_usbcx_hcspltx usbc_hcsplt;
1386 union cvmx_usbcx_hctsizx usbc_hctsiz;
1387 struct cvmx_usb_tx_fifo *fifo;
1389 /* We only need to fill data on outbound channels */
1390 hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
1391 if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1394 /* OUT Splits only have data on the start and not the complete */
1395 usbc_hcsplt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index));
1396 if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1399 /* Find out how many bytes we need to fill and convert it into 32bit words */
1400 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1401 if (!usbc_hctsiz.s.xfersize)
1404 if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1405 (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1406 fifo = &usb->periodic;
1408 fifo = &usb->nonperiodic;
1410 fifo->entry[fifo->head].channel = channel;
1411 fifo->entry[fifo->head].address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8);
1412 fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize+3)>>2;
1414 if (fifo->head > MAX_CHANNELS)
1417 __cvmx_usb_poll_tx_fifo(usb);
1423 * Perform channel specific setup for Control transactions. All
1424 * the generic stuff will already have been done in
1425 * __cvmx_usb_start_channel()
1427 * @usb: USB device state populated by
1428 * cvmx_usb_initialize().
1429 * @channel: Channel to setup
1430 * @pipe: Pipe for control transaction
1432 static void __cvmx_usb_start_channel_control(struct cvmx_usb_internal_state *usb,
1434 struct cvmx_usb_pipe *pipe)
1436 struct cvmx_usb_transaction *transaction = pipe->head;
1437 union cvmx_usb_control_header *header =
1438 cvmx_phys_to_ptr(transaction->control_header);
1439 int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1440 int packets_to_transfer;
1441 union cvmx_usbcx_hctsizx usbc_hctsiz;
1443 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1445 switch (transaction->stage) {
1446 case CVMX_USB_STAGE_NON_CONTROL:
1447 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1448 cvmx_dprintf("%s: ERROR - Non control stage\n", __FUNCTION__);
1450 case CVMX_USB_STAGE_SETUP:
1451 usbc_hctsiz.s.pid = 3; /* Setup */
1452 bytes_to_transfer = sizeof(*header);
1453 /* All Control operations start with a setup going OUT */
1454 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir, CVMX_USB_DIRECTION_OUT);
1456 * Setup send the control header instead of the buffer data. The
1457 * buffer data will be used in the next stage
1459 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, transaction->control_header);
1461 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1462 usbc_hctsiz.s.pid = 3; /* Setup */
1463 bytes_to_transfer = 0;
1464 /* All Control operations start with a setup going OUT */
1465 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir, CVMX_USB_DIRECTION_OUT);
1466 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), union cvmx_usbcx_hcspltx, compsplt, 1);
1468 case CVMX_USB_STAGE_DATA:
1469 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1470 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1471 if (header->s.request_type & 0x80)
1472 bytes_to_transfer = 0;
1473 else if (bytes_to_transfer > pipe->max_packet)
1474 bytes_to_transfer = pipe->max_packet;
1476 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1477 union cvmx_usbcx_hccharx, epdir,
1478 ((header->s.request_type & 0x80) ?
1479 CVMX_USB_DIRECTION_IN :
1480 CVMX_USB_DIRECTION_OUT));
1482 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1483 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1484 if (!(header->s.request_type & 0x80))
1485 bytes_to_transfer = 0;
1486 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1487 union cvmx_usbcx_hccharx, epdir,
1488 ((header->s.request_type & 0x80) ?
1489 CVMX_USB_DIRECTION_IN :
1490 CVMX_USB_DIRECTION_OUT));
1491 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), union cvmx_usbcx_hcspltx, compsplt, 1);
1493 case CVMX_USB_STAGE_STATUS:
1494 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1495 bytes_to_transfer = 0;
1496 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir,
1497 ((header->s.request_type & 0x80) ?
1498 CVMX_USB_DIRECTION_OUT :
1499 CVMX_USB_DIRECTION_IN));
1501 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1502 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1503 bytes_to_transfer = 0;
1504 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir,
1505 ((header->s.request_type & 0x80) ?
1506 CVMX_USB_DIRECTION_OUT :
1507 CVMX_USB_DIRECTION_IN));
1508 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), union cvmx_usbcx_hcspltx, compsplt, 1);
1513 * Make sure the transfer never exceeds the byte limit of the hardware.
1514 * Further bytes will be sent as continued transactions
1516 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1517 /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1518 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1519 bytes_to_transfer *= pipe->max_packet;
1523 * Calculate the number of packets to transfer. If the length is zero
1524 * we still need to transfer one packet
1526 packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1527 if (packets_to_transfer == 0)
1528 packets_to_transfer = 1;
1529 else if ((packets_to_transfer > 1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1531 * Limit to one packet when not using DMA. Channels must be
1532 * restarted between every packet for IN transactions, so there
1533 * is no reason to do multiple packets in a row
1535 packets_to_transfer = 1;
1536 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1537 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1539 * Limit the number of packet and data transferred to what the
1540 * hardware can handle
1542 packets_to_transfer = MAX_TRANSFER_PACKETS;
1543 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1546 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1547 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1549 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1555 * Start a channel to perform the pipe's head transaction
1557 * @usb: USB device state populated by
1558 * cvmx_usb_initialize().
1559 * @channel: Channel to setup
1560 * @pipe: Pipe to start
1562 static void __cvmx_usb_start_channel(struct cvmx_usb_internal_state *usb,
1564 struct cvmx_usb_pipe *pipe)
1566 struct cvmx_usb_transaction *transaction = pipe->head;
1568 /* Make sure all writes to the DMA region get flushed */
1571 /* Attach the channel to the pipe */
1572 usb->pipe_for_channel[channel] = pipe;
1573 pipe->channel = channel;
1574 pipe->flags |= __CVMX_USB_PIPE_FLAGS_SCHEDULED;
1576 /* Mark this channel as in use */
1577 usb->idle_hardware_channels &= ~(1<<channel);
1579 /* Enable the channel interrupt bits */
1581 union cvmx_usbcx_hcintx usbc_hcint;
1582 union cvmx_usbcx_hcintmskx usbc_hcintmsk;
1583 union cvmx_usbcx_haintmsk usbc_haintmsk;
1585 /* Clear all channel status bits */
1586 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
1587 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index), usbc_hcint.u32);
1589 usbc_hcintmsk.u32 = 0;
1590 usbc_hcintmsk.s.chhltdmsk = 1;
1591 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1592 /* Channels need these extra interrupts when we aren't in DMA mode */
1593 usbc_hcintmsk.s.datatglerrmsk = 1;
1594 usbc_hcintmsk.s.frmovrunmsk = 1;
1595 usbc_hcintmsk.s.bblerrmsk = 1;
1596 usbc_hcintmsk.s.xacterrmsk = 1;
1597 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1598 /* Splits don't generate xfercompl, so we need ACK and NYET */
1599 usbc_hcintmsk.s.nyetmsk = 1;
1600 usbc_hcintmsk.s.ackmsk = 1;
1602 usbc_hcintmsk.s.nakmsk = 1;
1603 usbc_hcintmsk.s.stallmsk = 1;
1604 usbc_hcintmsk.s.xfercomplmsk = 1;
1606 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), usbc_hcintmsk.u32);
1608 /* Enable the channel interrupt to propagate */
1609 usbc_haintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index));
1610 usbc_haintmsk.s.haintmsk |= 1<<channel;
1611 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index), usbc_haintmsk.u32);
1614 /* Setup the locations the DMA engines use */
1616 uint64_t dma_address = transaction->buffer + transaction->actual_bytes;
1617 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1618 dma_address = transaction->buffer + transaction->iso_packets[0].offset + transaction->actual_bytes;
1619 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, dma_address);
1620 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, dma_address);
1623 /* Setup both the size of the transfer and the SPLIT characteristics */
1625 union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
1626 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
1627 int packets_to_transfer;
1628 int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1631 * ISOCHRONOUS transactions store each individual transfer size
1632 * in the packet structure, not the global buffer_length
1634 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1635 bytes_to_transfer = transaction->iso_packets[0].length - transaction->actual_bytes;
1638 * We need to do split transactions when we are talking to non
1639 * high speed devices that are behind a high speed hub
1641 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1643 * On the start split phase (stage is even) record the
1644 * frame number we will need to send the split complete.
1645 * We only store the lower two bits since the time ahead
1646 * can only be two frames
1648 if ((transaction->stage&1) == 0) {
1649 if (transaction->type == CVMX_USB_TRANSFER_BULK)
1650 pipe->split_sc_frame = (usb->frame_number + 1) & 0x7f;
1652 pipe->split_sc_frame = (usb->frame_number + 2) & 0x7f;
1654 pipe->split_sc_frame = -1;
1656 usbc_hcsplt.s.spltena = 1;
1657 usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1658 usbc_hcsplt.s.prtaddr = pipe->hub_port;
1659 usbc_hcsplt.s.compsplt = (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1662 * SPLIT transactions can only ever transmit one data
1663 * packet so limit the transfer size to the max packet
1666 if (bytes_to_transfer > pipe->max_packet)
1667 bytes_to_transfer = pipe->max_packet;
1670 * ISOCHRONOUS OUT splits are unique in that they limit
1671 * data transfers to 188 byte chunks representing the
1672 * begin/middle/end of the data or all
1674 if (!usbc_hcsplt.s.compsplt &&
1675 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1676 (pipe->transfer_type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1678 * Clear the split complete frame number as
1679 * there isn't going to be a split complete
1681 pipe->split_sc_frame = -1;
1683 * See if we've started this transfer and sent
1686 if (transaction->actual_bytes == 0) {
1688 * Nothing sent yet, this is either a
1689 * begin or the entire payload
1691 if (bytes_to_transfer <= 188)
1692 usbc_hcsplt.s.xactpos = 3; /* Entire payload in one go */
1694 usbc_hcsplt.s.xactpos = 2; /* First part of payload */
1697 * Continuing the previous data, we must
1698 * either be in the middle or at the end
1700 if (bytes_to_transfer <= 188)
1701 usbc_hcsplt.s.xactpos = 1; /* End of payload */
1703 usbc_hcsplt.s.xactpos = 0; /* Middle of payload */
1706 * Again, the transfer size is limited to 188
1709 if (bytes_to_transfer > 188)
1710 bytes_to_transfer = 188;
1715 * Make sure the transfer never exceeds the byte limit of the
1716 * hardware. Further bytes will be sent as continued
1719 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1721 * Round MAX_TRANSFER_BYTES to a multiple of out packet
1724 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1725 bytes_to_transfer *= pipe->max_packet;
1729 * Calculate the number of packets to transfer. If the length is
1730 * zero we still need to transfer one packet
1732 packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1733 if (packets_to_transfer == 0)
1734 packets_to_transfer = 1;
1735 else if ((packets_to_transfer > 1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1737 * Limit to one packet when not using DMA. Channels must
1738 * be restarted between every packet for IN
1739 * transactions, so there is no reason to do multiple
1742 packets_to_transfer = 1;
1743 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1744 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1746 * Limit the number of packet and data transferred to
1747 * what the hardware can handle
1749 packets_to_transfer = MAX_TRANSFER_PACKETS;
1750 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1753 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1754 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1756 /* Update the DATA0/DATA1 toggle */
1757 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1759 * High speed pipes may need a hardware ping before they start
1761 if (pipe->flags & __CVMX_USB_PIPE_FLAGS_NEED_PING)
1762 usbc_hctsiz.s.dopng = 1;
1764 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index), usbc_hcsplt.u32);
1765 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1768 /* Setup the Host Channel Characteristics Register */
1770 union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
1773 * Set the startframe odd/even properly. This is only used for
1776 usbc_hcchar.s.oddfrm = usb->frame_number&1;
1779 * Set the number of back to back packets allowed by this
1780 * endpoint. Split transactions interpret "ec" as the number of
1781 * immediate retries of failure. These retries happen too
1782 * quickly, so we disable these entirely for splits
1784 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1785 usbc_hcchar.s.ec = 1;
1786 else if (pipe->multi_count < 1)
1787 usbc_hcchar.s.ec = 1;
1788 else if (pipe->multi_count > 3)
1789 usbc_hcchar.s.ec = 3;
1791 usbc_hcchar.s.ec = pipe->multi_count;
1793 /* Set the rest of the endpoint specific settings */
1794 usbc_hcchar.s.devaddr = pipe->device_addr;
1795 usbc_hcchar.s.eptype = transaction->type;
1796 usbc_hcchar.s.lspddev = (pipe->device_speed == CVMX_USB_SPEED_LOW);
1797 usbc_hcchar.s.epdir = pipe->transfer_dir;
1798 usbc_hcchar.s.epnum = pipe->endpoint_num;
1799 usbc_hcchar.s.mps = pipe->max_packet;
1800 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
1803 /* Do transaction type specific fixups as needed */
1804 switch (transaction->type) {
1805 case CVMX_USB_TRANSFER_CONTROL:
1806 __cvmx_usb_start_channel_control(usb, channel, pipe);
1808 case CVMX_USB_TRANSFER_BULK:
1809 case CVMX_USB_TRANSFER_INTERRUPT:
1811 case CVMX_USB_TRANSFER_ISOCHRONOUS:
1812 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
1814 * ISO transactions require different PIDs depending on
1815 * direction and how many packets are needed
1817 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
1818 if (pipe->multi_count < 2) /* Need DATA0 */
1819 USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), union cvmx_usbcx_hctsizx, pid, 0);
1820 else /* Need MDATA */
1821 USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), union cvmx_usbcx_hctsizx, pid, 3);
1827 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index))};
1828 transaction->xfersize = usbc_hctsiz.s.xfersize;
1829 transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1831 /* Remeber when we start a split transaction */
1832 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1833 usb->active_split = transaction;
1834 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, chena, 1);
1835 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1836 __cvmx_usb_fill_tx_fifo(usb, channel);
1842 * Find a pipe that is ready to be scheduled to hardware.
1843 * @usb: USB device state populated by
1844 * cvmx_usb_initialize().
1845 * @list: Pipe list to search
1847 * Frame counter to use as a time reference.
1849 * Returns: Pipe or NULL if none are ready
1851 static struct cvmx_usb_pipe *__cvmx_usb_find_ready_pipe(struct cvmx_usb_internal_state *usb, struct cvmx_usb_pipe_list *list, uint64_t current_frame)
1853 struct cvmx_usb_pipe *pipe = list->head;
1855 if (!(pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED) && pipe->head &&
1856 (pipe->next_tx_frame <= current_frame) &&
1857 ((pipe->split_sc_frame == -1) || ((((int)current_frame - (int)pipe->split_sc_frame) & 0x7f) < 0x40)) &&
1858 (!usb->active_split || (usb->active_split == pipe->head))) {
1859 CVMX_PREFETCH(pipe, 128);
1860 CVMX_PREFETCH(pipe->head, 0);
1870 * Called whenever a pipe might need to be scheduled to the
1873 * @usb: USB device state populated by
1874 * cvmx_usb_initialize().
1875 * @is_sof: True if this schedule was called on a SOF interrupt.
1877 static void __cvmx_usb_schedule(struct cvmx_usb_internal_state *usb, int is_sof)
1880 struct cvmx_usb_pipe *pipe;
1882 enum cvmx_usb_transfer ttype;
1884 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1885 /* Without DMA we need to be careful to not schedule something at the end of a frame and cause an overrun */
1886 union cvmx_usbcx_hfnum hfnum = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index))};
1887 union cvmx_usbcx_hfir hfir = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFIR(usb->index))};
1888 if (hfnum.s.frrem < hfir.s.frint/4)
1892 while (usb->idle_hardware_channels) {
1893 /* Find an idle channel */
1894 channel = __fls(usb->idle_hardware_channels);
1895 if (unlikely(channel > 7))
1898 /* Find a pipe needing service */
1902 * Only process periodic pipes on SOF interrupts. This
1903 * way we are sure that the periodic data is sent in the
1904 * beginning of the frame
1906 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_ISOCHRONOUS, usb->frame_number);
1908 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_INTERRUPT, usb->frame_number);
1910 if (likely(!pipe)) {
1911 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_CONTROL, usb->frame_number);
1913 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_BULK, usb->frame_number);
1918 __cvmx_usb_start_channel(usb, channel, pipe);
1923 * Only enable SOF interrupts when we have transactions pending in the
1924 * future that might need to be scheduled
1927 for (ttype = CVMX_USB_TRANSFER_CONTROL; ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
1928 pipe = usb->active_pipes[ttype].head;
1930 if (pipe->next_tx_frame > usb->frame_number) {
1937 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, sofmsk, need_sof);
1943 * Call a user's callback for a specific reason.
1945 * @usb: USB device state populated by
1946 * cvmx_usb_initialize().
1947 * @pipe: Pipe the callback is for or NULL
1949 * Transaction the callback is for or NULL
1950 * @reason: Reason this callback is being called
1952 * Completion code for the transaction, if any
1954 static void __cvmx_usb_perform_callback(struct cvmx_usb_internal_state *usb,
1955 struct cvmx_usb_pipe *pipe,
1956 struct cvmx_usb_transaction *transaction,
1957 enum cvmx_usb_callback reason,
1958 enum cvmx_usb_complete complete_code)
1960 cvmx_usb_callback_func_t callback = usb->callback[reason];
1961 void *user_data = usb->callback_data[reason];
1962 int submit_handle = -1;
1963 int pipe_handle = -1;
1964 int bytes_transferred = 0;
1967 pipe_handle = __cvmx_usb_get_pipe_handle(usb, pipe);
1970 submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
1971 bytes_transferred = transaction->actual_bytes;
1972 /* Transactions are allowed to override the default callback */
1973 if ((reason == CVMX_USB_CALLBACK_TRANSFER_COMPLETE) && transaction->callback) {
1974 callback = transaction->callback;
1975 user_data = transaction->callback_data;
1982 callback((struct cvmx_usb_state *)usb, reason, complete_code, pipe_handle, submit_handle,
1983 bytes_transferred, user_data);
1988 * Signal the completion of a transaction and free it. The
1989 * transaction will be removed from the pipe transaction list.
1991 * @usb: USB device state populated by
1992 * cvmx_usb_initialize().
1993 * @pipe: Pipe the transaction is on
1995 * Transaction that completed
1999 static void __cvmx_usb_perform_complete(struct cvmx_usb_internal_state *usb,
2000 struct cvmx_usb_pipe *pipe,
2001 struct cvmx_usb_transaction *transaction,
2002 enum cvmx_usb_complete complete_code)
2004 /* If this was a split then clear our split in progress marker */
2005 if (usb->active_split == transaction)
2006 usb->active_split = NULL;
2009 * Isochronous transactions need extra processing as they might not be
2010 * done after a single data transfer
2012 if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
2013 /* Update the number of bytes transferred in this ISO packet */
2014 transaction->iso_packets[0].length = transaction->actual_bytes;
2015 transaction->iso_packets[0].status = complete_code;
2018 * If there are more ISOs pending and we succeeded, schedule the
2021 if ((transaction->iso_number_packets > 1) && (complete_code == CVMX_USB_COMPLETE_SUCCESS)) {
2022 transaction->actual_bytes = 0; /* No bytes transferred for this packet as of yet */
2023 transaction->iso_number_packets--; /* One less ISO waiting to transfer */
2024 transaction->iso_packets++; /* Increment to the next location in our packet array */
2025 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2030 /* Remove the transaction from the pipe list */
2031 if (transaction->next)
2032 transaction->next->prev = transaction->prev;
2034 pipe->tail = transaction->prev;
2035 if (transaction->prev)
2036 transaction->prev->next = transaction->next;
2038 pipe->head = transaction->next;
2040 __cvmx_usb_remove_pipe(usb->active_pipes + pipe->transfer_type, pipe);
2041 __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
2044 __cvmx_usb_perform_callback(usb, pipe, transaction,
2045 CVMX_USB_CALLBACK_TRANSFER_COMPLETE,
2047 __cvmx_usb_free_transaction(usb, transaction);
2054 * Submit a usb transaction to a pipe. Called for all types
2059 * Which pipe to submit to. Will be validated in this function.
2060 * @type: Transaction type
2061 * @buffer: User buffer for the transaction
2063 * User buffer's length in bytes
2065 * For control transactions, the 8 byte standard header
2067 * For ISO transactions, the start frame
2068 * @iso_number_packets:
2069 * For ISO, the number of packet in the transaction.
2071 * A description of each ISO packet
2072 * @callback: User callback to call when the transaction completes
2073 * @user_data: User's data for the callback
2075 * Returns: Submit handle or negative on failure. Matches the result
2076 * in the external API.
2078 static int __cvmx_usb_submit_transaction(struct cvmx_usb_internal_state *usb,
2080 enum cvmx_usb_transfer type,
2083 uint64_t control_header,
2084 int iso_start_frame,
2085 int iso_number_packets,
2086 struct cvmx_usb_iso_packet *iso_packets,
2087 cvmx_usb_callback_func_t callback,
2091 struct cvmx_usb_transaction *transaction;
2092 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2094 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2096 /* Fail if the pipe isn't open */
2097 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2099 if (unlikely(pipe->transfer_type != type))
2102 transaction = __cvmx_usb_alloc_transaction(usb);
2103 if (unlikely(!transaction))
2106 transaction->type = type;
2107 transaction->buffer = buffer;
2108 transaction->buffer_length = buffer_length;
2109 transaction->control_header = control_header;
2110 transaction->iso_start_frame = iso_start_frame; // FIXME: This is not used, implement it
2111 transaction->iso_number_packets = iso_number_packets;
2112 transaction->iso_packets = iso_packets;
2113 transaction->callback = callback;
2114 transaction->callback_data = user_data;
2115 if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2116 transaction->stage = CVMX_USB_STAGE_SETUP;
2118 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2120 transaction->next = NULL;
2122 transaction->prev = pipe->tail;
2123 transaction->prev->next = transaction;
2125 if (pipe->next_tx_frame < usb->frame_number)
2126 pipe->next_tx_frame = usb->frame_number + pipe->interval -
2127 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2128 transaction->prev = NULL;
2129 pipe->head = transaction;
2130 __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2131 __cvmx_usb_append_pipe(usb->active_pipes + pipe->transfer_type, pipe);
2133 pipe->tail = transaction;
2135 submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
2137 /* We may need to schedule the pipe if this was the head of the pipe */
2138 if (!transaction->prev)
2139 __cvmx_usb_schedule(usb, 0);
2141 return submit_handle;
2146 * Call to submit a USB Bulk transfer to a pipe.
2148 * @state: USB device state populated by
2149 * cvmx_usb_initialize().
2151 * Handle to the pipe for the transfer.
2152 * @buffer: Physical address of the data buffer in
2153 * memory. Note that this is NOT A POINTER, but
2154 * the full 64bit physical address of the
2155 * buffer. This may be zero if buffer_length is
2158 * Length of buffer in bytes.
2159 * @callback: Function to call when this transaction
2160 * completes. If the return value of this
2161 * function isn't an error, then this function
2162 * is guaranteed to be called when the
2163 * transaction completes. If this parameter is
2164 * NULL, then the generic callback registered
2165 * through cvmx_usb_register_callback is
2166 * called. If both are NULL, then there is no
2167 * way to know when a transaction completes.
2168 * @user_data: User supplied data returned when the
2169 * callback is called. This is only used if
2170 * callback in not NULL.
2172 * Returns: A submitted transaction handle or negative on
2173 * failure. Negative values are error codes.
2175 int cvmx_usb_submit_bulk(struct cvmx_usb_state *state, int pipe_handle,
2176 uint64_t buffer, int buffer_length,
2177 cvmx_usb_callback_func_t callback,
2181 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2183 /* Pipe handle checking is done later in a common place */
2184 if (unlikely(!buffer))
2186 if (unlikely(buffer_length < 0))
2189 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2190 CVMX_USB_TRANSFER_BULK,
2193 0, /* control_header */
2194 0, /* iso_start_frame */
2195 0, /* iso_number_packets */
2196 NULL, /* iso_packets */
2199 return submit_handle;
2204 * Call to submit a USB Interrupt transfer to a pipe.
2206 * @state: USB device state populated by
2207 * cvmx_usb_initialize().
2209 * Handle to the pipe for the transfer.
2210 * @buffer: Physical address of the data buffer in
2211 * memory. Note that this is NOT A POINTER, but
2212 * the full 64bit physical address of the
2213 * buffer. This may be zero if buffer_length is
2216 * Length of buffer in bytes.
2217 * @callback: Function to call when this transaction
2218 * completes. If the return value of this
2219 * function isn't an error, then this function
2220 * is guaranteed to be called when the
2221 * transaction completes. If this parameter is
2222 * NULL, then the generic callback registered
2223 * through cvmx_usb_register_callback is
2224 * called. If both are NULL, then there is no
2225 * way to know when a transaction completes.
2226 * @user_data: User supplied data returned when the
2227 * callback is called. This is only used if
2228 * callback in not NULL.
2230 * Returns: A submitted transaction handle or negative on
2231 * failure. Negative values are error codes.
2233 int cvmx_usb_submit_interrupt(struct cvmx_usb_state *state, int pipe_handle,
2234 uint64_t buffer, int buffer_length,
2235 cvmx_usb_callback_func_t callback,
2239 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2241 /* Pipe handle checking is done later in a common place */
2242 if (unlikely(!buffer))
2244 if (unlikely(buffer_length < 0))
2247 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2248 CVMX_USB_TRANSFER_INTERRUPT,
2251 0, /* control_header */
2252 0, /* iso_start_frame */
2253 0, /* iso_number_packets */
2254 NULL, /* iso_packets */
2257 return submit_handle;
2262 * Call to submit a USB Control transfer to a pipe.
2264 * @state: USB device state populated by
2265 * cvmx_usb_initialize().
2267 * Handle to the pipe for the transfer.
2269 * USB 8 byte control header physical address.
2270 * Note that this is NOT A POINTER, but the
2271 * full 64bit physical address of the buffer.
2272 * @buffer: Physical address of the data buffer in
2273 * memory. Note that this is NOT A POINTER, but
2274 * the full 64bit physical address of the
2275 * buffer. This may be zero if buffer_length is
2278 * Length of buffer in bytes.
2279 * @callback: Function to call when this transaction
2280 * completes. If the return value of this
2281 * function isn't an error, then this function
2282 * is guaranteed to be called when the
2283 * transaction completes. If this parameter is
2284 * NULL, then the generic callback registered
2285 * through cvmx_usb_register_callback is
2286 * called. If both are NULL, then there is no
2287 * way to know when a transaction completes.
2288 * @user_data: User supplied data returned when the
2289 * callback is called. This is only used if
2290 * callback in not NULL.
2292 * Returns: A submitted transaction handle or negative on
2293 * failure. Negative values are error codes.
2295 int cvmx_usb_submit_control(struct cvmx_usb_state *state, int pipe_handle,
2296 uint64_t control_header,
2297 uint64_t buffer, int buffer_length,
2298 cvmx_usb_callback_func_t callback,
2302 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2303 union cvmx_usb_control_header *header =
2304 cvmx_phys_to_ptr(control_header);
2306 /* Pipe handle checking is done later in a common place */
2307 if (unlikely(!control_header))
2309 /* Some drivers send a buffer with a zero length. God only knows why */
2310 if (unlikely(buffer && (buffer_length < 0)))
2312 if (unlikely(!buffer && (buffer_length != 0)))
2314 if ((header->s.request_type & 0x80) == 0)
2315 buffer_length = le16_to_cpu(header->s.length);
2317 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2318 CVMX_USB_TRANSFER_CONTROL,
2322 0, /* iso_start_frame */
2323 0, /* iso_number_packets */
2324 NULL, /* iso_packets */
2327 return submit_handle;
2332 * Call to submit a USB Isochronous transfer to a pipe.
2334 * @state: USB device state populated by
2335 * cvmx_usb_initialize().
2337 * Handle to the pipe for the transfer.
2339 * Number of frames into the future to schedule
2342 * Number of sequential packets to transfer.
2343 * "packets" is a pointer to an array of this
2344 * many packet structures.
2345 * @packets: Description of each transfer packet as
2346 * defined by struct cvmx_usb_iso_packet. The array
2347 * pointed to here must stay valid until the
2348 * complete callback is called.
2349 * @buffer: Physical address of the data buffer in
2350 * memory. Note that this is NOT A POINTER, but
2351 * the full 64bit physical address of the
2352 * buffer. This may be zero if buffer_length is
2355 * Length of buffer in bytes.
2356 * @callback: Function to call when this transaction
2357 * completes. If the return value of this
2358 * function isn't an error, then this function
2359 * is guaranteed to be called when the
2360 * transaction completes. If this parameter is
2361 * NULL, then the generic callback registered
2362 * through cvmx_usb_register_callback is
2363 * called. If both are NULL, then there is no
2364 * way to know when a transaction completes.
2365 * @user_data: User supplied data returned when the
2366 * callback is called. This is only used if
2367 * callback in not NULL.
2369 * Returns: A submitted transaction handle or negative on
2370 * failure. Negative values are error codes.
2372 int cvmx_usb_submit_isochronous(struct cvmx_usb_state *state, int pipe_handle,
2375 struct cvmx_usb_iso_packet packets[],
2376 uint64_t buffer, int buffer_length,
2377 cvmx_usb_callback_func_t callback,
2381 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2383 /* Pipe handle checking is done later in a common place */
2384 if (unlikely(start_frame < 0))
2386 if (unlikely(number_packets < 1))
2388 if (unlikely(!packets))
2390 if (unlikely(!buffer))
2392 if (unlikely(buffer_length < 0))
2395 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2396 CVMX_USB_TRANSFER_ISOCHRONOUS,
2399 0, /* control_header */
2405 return submit_handle;
2410 * Cancel one outstanding request in a pipe. Canceling a request
2411 * can fail if the transaction has already completed before cancel
2412 * is called. Even after a successful cancel call, it may take
2413 * a frame or two for the cvmx_usb_poll() function to call the
2414 * associated callback.
2416 * @state: USB device state populated by
2417 * cvmx_usb_initialize().
2419 * Pipe handle to cancel requests in.
2421 * Handle to transaction to cancel, returned by the submit function.
2423 * Returns: 0 or a negative error code.
2425 int cvmx_usb_cancel(struct cvmx_usb_state *state, int pipe_handle, int submit_handle)
2427 struct cvmx_usb_transaction *transaction;
2428 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2429 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2431 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2433 if (unlikely((submit_handle < 0) || (submit_handle >= MAX_TRANSACTIONS)))
2436 /* Fail if the pipe isn't open */
2437 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2440 transaction = usb->transaction + submit_handle;
2442 /* Fail if this transaction already completed */
2443 if (unlikely((transaction->flags & __CVMX_USB_TRANSACTION_FLAGS_IN_USE) == 0))
2447 * If the transaction is the HEAD of the queue and scheduled. We need to
2450 if ((pipe->head == transaction) &&
2451 (pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
2452 union cvmx_usbcx_hccharx usbc_hcchar;
2454 usb->pipe_for_channel[pipe->channel] = NULL;
2455 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2459 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
2460 /* If the channel isn't enabled then the transaction already completed */
2461 if (usbc_hcchar.s.chena) {
2462 usbc_hcchar.s.chdis = 1;
2463 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index), usbc_hcchar.u32);
2466 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_CANCEL);
2472 * Cancel all outstanding requests in a pipe. Logically all this
2473 * does is call cvmx_usb_cancel() in a loop.
2475 * @state: USB device state populated by
2476 * cvmx_usb_initialize().
2478 * Pipe handle to cancel requests in.
2480 * Returns: 0 or a negative error code.
2482 int cvmx_usb_cancel_all(struct cvmx_usb_state *state, int pipe_handle)
2484 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2485 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2487 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2490 /* Fail if the pipe isn't open */
2491 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2494 /* Simply loop through and attempt to cancel each transaction */
2495 while (pipe->head) {
2496 int result = cvmx_usb_cancel(state, pipe_handle,
2497 __cvmx_usb_get_submit_handle(usb, pipe->head));
2498 if (unlikely(result != 0))
2506 * Close a pipe created with cvmx_usb_open_pipe().
2508 * @state: USB device state populated by
2509 * cvmx_usb_initialize().
2511 * Pipe handle to close.
2513 * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
2514 * outstanding transfers.
2516 int cvmx_usb_close_pipe(struct cvmx_usb_state *state, int pipe_handle)
2518 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2519 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2521 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2524 /* Fail if the pipe isn't open */
2525 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2528 /* Fail if the pipe has pending transactions */
2529 if (unlikely(pipe->head))
2533 __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2534 __cvmx_usb_append_pipe(&usb->free_pipes, pipe);
2541 * Register a function to be called when various USB events occur.
2543 * @state: USB device state populated by
2544 * cvmx_usb_initialize().
2545 * @reason: Which event to register for.
2546 * @callback: Function to call when the event occurs.
2547 * @user_data: User data parameter to the function.
2549 * Returns: 0 or a negative error code.
2551 int cvmx_usb_register_callback(struct cvmx_usb_state *state,
2552 enum cvmx_usb_callback reason,
2553 cvmx_usb_callback_func_t callback,
2556 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2558 if (unlikely(reason >= __CVMX_USB_CALLBACK_END))
2560 if (unlikely(!callback))
2563 usb->callback[reason] = callback;
2564 usb->callback_data[reason] = user_data;
2571 * Get the current USB protocol level frame number. The frame
2572 * number is always in the range of 0-0x7ff.
2574 * @state: USB device state populated by
2575 * cvmx_usb_initialize().
2577 * Returns: USB frame number
2579 int cvmx_usb_get_frame_number(struct cvmx_usb_state *state)
2582 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
2583 union cvmx_usbcx_hfnum usbc_hfnum;
2585 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2586 frame_number = usbc_hfnum.s.frnum;
2588 return frame_number;
2593 * Poll a channel for status
2596 * @channel: Channel to poll
2598 * Returns: Zero on success
2600 static int __cvmx_usb_poll_channel(struct cvmx_usb_internal_state *usb, int channel)
2602 union cvmx_usbcx_hcintx usbc_hcint;
2603 union cvmx_usbcx_hctsizx usbc_hctsiz;
2604 union cvmx_usbcx_hccharx usbc_hcchar;
2605 struct cvmx_usb_pipe *pipe;
2606 struct cvmx_usb_transaction *transaction;
2607 int bytes_this_transfer;
2608 int bytes_in_last_packet;
2609 int packets_processed;
2610 int buffer_space_left;
2612 /* Read the interrupt status bits for the channel */
2613 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
2615 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2616 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2618 if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2620 * There seems to be a bug in CN31XX which can cause
2621 * interrupt IN transfers to get stuck until we do a
2622 * write of HCCHARX without changing things
2624 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2629 * In non DMA mode the channels don't halt themselves. We need
2630 * to manually disable channels that are left running
2632 if (!usbc_hcint.s.chhltd) {
2633 if (usbc_hcchar.s.chena) {
2634 union cvmx_usbcx_hcintmskx hcintmsk;
2635 /* Disable all interrupts except CHHLTD */
2637 hcintmsk.s.chhltdmsk = 1;
2638 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), hcintmsk.u32);
2639 usbc_hcchar.s.chdis = 1;
2640 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2642 } else if (usbc_hcint.s.xfercompl) {
2643 /* Successful IN/OUT with transfer complete. Channel halt isn't needed */
2645 cvmx_dprintf("USB%d: Channel %d interrupt without halt\n", usb->index, channel);
2651 * There is are no interrupts that we need to process when the
2652 * channel is still running
2654 if (!usbc_hcint.s.chhltd)
2658 /* Disable the channel interrupts now that it is done */
2659 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
2660 usb->idle_hardware_channels |= (1<<channel);
2662 /* Make sure this channel is tied to a valid pipe */
2663 pipe = usb->pipe_for_channel[channel];
2664 CVMX_PREFETCH(pipe, 0);
2665 CVMX_PREFETCH(pipe, 128);
2668 transaction = pipe->head;
2669 CVMX_PREFETCH(transaction, 0);
2672 * Disconnect this pipe from the HW channel. Later the schedule
2673 * function will figure out which pipe needs to go
2675 usb->pipe_for_channel[channel] = NULL;
2676 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2679 * Read the channel config info so we can figure out how much data
2682 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2683 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
2686 * Calculating the number of bytes successfully transferred is dependent
2687 * on the transfer direction
2689 packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2690 if (usbc_hcchar.s.epdir) {
2692 * IN transactions are easy. For every byte received the
2693 * hardware decrements xfersize. All we need to do is subtract
2694 * the current value of xfersize from its starting value and we
2695 * know how many bytes were written to the buffer
2697 bytes_this_transfer = transaction->xfersize - usbc_hctsiz.s.xfersize;
2700 * OUT transaction don't decrement xfersize. Instead pktcnt is
2701 * decremented on every successful packet send. The hardware
2702 * does this when it receives an ACK, or NYET. If it doesn't
2703 * receive one of these responses pktcnt doesn't change
2705 bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2707 * The last packet may not be a full transfer if we didn't have
2710 if (bytes_this_transfer > transaction->xfersize)
2711 bytes_this_transfer = transaction->xfersize;
2713 /* Figure out how many bytes were in the last packet of the transfer */
2714 if (packets_processed)
2715 bytes_in_last_packet = bytes_this_transfer - (packets_processed-1) * usbc_hcchar.s.mps;
2717 bytes_in_last_packet = bytes_this_transfer;
2720 * As a special case, setup transactions output the setup header, not
2721 * the user's data. For this reason we don't count setup data as bytes
2724 if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2725 (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2726 bytes_this_transfer = 0;
2729 * Add the bytes transferred to the running total. It is important that
2730 * bytes_this_transfer doesn't count any data that needs to be
2733 transaction->actual_bytes += bytes_this_transfer;
2734 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
2735 buffer_space_left = transaction->iso_packets[0].length - transaction->actual_bytes;
2737 buffer_space_left = transaction->buffer_length - transaction->actual_bytes;
2740 * We need to remember the PID toggle state for the next transaction.
2741 * The hardware already updated it for the next transaction
2743 pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2746 * For high speed bulk out, assume the next transaction will need to do
2747 * a ping before proceeding. If this isn't true the ACK processing below
2748 * will clear this flag
2750 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2751 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2752 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2753 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
2755 if (usbc_hcint.s.stall) {
2757 * STALL as a response means this transaction cannot be
2758 * completed because the device can't process transactions. Tell
2759 * the user. Any data that was transferred will be counted on
2760 * the actual bytes transferred
2762 pipe->pid_toggle = 0;
2763 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_STALL);
2764 } else if (usbc_hcint.s.xacterr) {
2766 * We know at least one packet worked if we get a ACK or NAK.
2767 * Reset the retry counter
2769 if (usbc_hcint.s.nak || usbc_hcint.s.ack)
2770 transaction->retries = 0;
2771 transaction->retries++;
2772 if (transaction->retries > MAX_RETRIES) {
2774 * XactErr as a response means the device signaled
2775 * something wrong with the transfer. For example, PID
2776 * toggle errors cause these
2778 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_XACTERR);
2781 * If this was a split then clear our split in progress
2784 if (usb->active_split == transaction)
2785 usb->active_split = NULL;
2787 * Rewind to the beginning of the transaction by anding
2788 * off the split complete bit
2790 transaction->stage &= ~1;
2791 pipe->split_sc_frame = -1;
2792 pipe->next_tx_frame += pipe->interval;
2793 if (pipe->next_tx_frame < usb->frame_number)
2794 pipe->next_tx_frame = usb->frame_number + pipe->interval -
2795 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2797 } else if (usbc_hcint.s.bblerr) {
2798 /* Babble Error (BblErr) */
2799 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_BABBLEERR);
2800 } else if (usbc_hcint.s.datatglerr) {
2801 /* We'll retry the exact same transaction again */
2802 transaction->retries++;
2803 } else if (usbc_hcint.s.nyet) {
2805 * NYET as a response is only allowed in three cases: as a
2806 * response to a ping, as a response to a split transaction, and
2807 * as a response to a bulk out. The ping case is handled by
2808 * hardware, so we only have splits and bulk out
2810 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
2811 transaction->retries = 0;
2813 * If there is more data to go then we need to try
2814 * again. Otherwise this transaction is complete
2816 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet))
2817 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2820 * Split transactions retry the split complete 4 times
2821 * then rewind to the start split and do the entire
2822 * transactions again
2824 transaction->retries++;
2825 if ((transaction->retries & 0x3) == 0) {
2827 * Rewind to the beginning of the transaction by
2828 * anding off the split complete bit
2830 transaction->stage &= ~1;
2831 pipe->split_sc_frame = -1;
2834 } else if (usbc_hcint.s.ack) {
2835 transaction->retries = 0;
2837 * The ACK bit can only be checked after the other error bits.
2838 * This is because a multi packet transfer may succeed in a
2839 * number of packets and then get a different response on the
2840 * last packet. In this case both ACK and the last response bit
2841 * will be set. If none of the other response bits is set, then
2842 * the last packet must have been an ACK
2844 * Since we got an ACK, we know we don't need to do a ping on
2847 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_NEED_PING;
2849 switch (transaction->type) {
2850 case CVMX_USB_TRANSFER_CONTROL:
2851 switch (transaction->stage) {
2852 case CVMX_USB_STAGE_NON_CONTROL:
2853 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2854 /* This should be impossible */
2855 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
2857 case CVMX_USB_STAGE_SETUP:
2858 pipe->pid_toggle = 1;
2859 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2860 transaction->stage = CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
2862 union cvmx_usb_control_header *header =
2863 cvmx_phys_to_ptr(transaction->control_header);
2864 if (header->s.length)
2865 transaction->stage = CVMX_USB_STAGE_DATA;
2867 transaction->stage = CVMX_USB_STAGE_STATUS;
2870 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2872 union cvmx_usb_control_header *header =
2873 cvmx_phys_to_ptr(transaction->control_header);
2874 if (header->s.length)
2875 transaction->stage = CVMX_USB_STAGE_DATA;
2877 transaction->stage = CVMX_USB_STAGE_STATUS;
2880 case CVMX_USB_STAGE_DATA:
2881 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2882 transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
2884 * For setup OUT data that are splits,
2885 * the hardware doesn't appear to count
2886 * transferred data. Here we manually
2887 * update the data transferred
2889 if (!usbc_hcchar.s.epdir) {
2890 if (buffer_space_left < pipe->max_packet)
2891 transaction->actual_bytes += buffer_space_left;
2893 transaction->actual_bytes += pipe->max_packet;
2895 } else if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2896 pipe->pid_toggle = 1;
2897 transaction->stage = CVMX_USB_STAGE_STATUS;
2900 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
2901 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2902 pipe->pid_toggle = 1;
2903 transaction->stage = CVMX_USB_STAGE_STATUS;
2905 transaction->stage = CVMX_USB_STAGE_DATA;
2908 case CVMX_USB_STAGE_STATUS:
2909 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2910 transaction->stage = CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
2912 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2914 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
2915 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2919 case CVMX_USB_TRANSFER_BULK:
2920 case CVMX_USB_TRANSFER_INTERRUPT:
2922 * The only time a bulk transfer isn't complete when it
2923 * finishes with an ACK is during a split transaction.
2924 * For splits we need to continue the transfer if more
2927 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2928 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
2929 transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2931 if (buffer_space_left && (bytes_in_last_packet == pipe->max_packet))
2932 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2934 if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
2935 pipe->next_tx_frame += pipe->interval;
2936 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2940 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2941 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2942 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
2944 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
2945 if (!buffer_space_left || (bytes_in_last_packet < pipe->max_packet)) {
2946 if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
2947 pipe->next_tx_frame += pipe->interval;
2948 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2952 case CVMX_USB_TRANSFER_ISOCHRONOUS:
2953 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2955 * ISOCHRONOUS OUT splits don't require a
2956 * complete split stage. Instead they use a
2957 * sequence of begin OUT splits to transfer the
2958 * data 188 bytes at a time. Once the transfer
2959 * is complete, the pipe sleeps until the next
2962 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
2964 * If no space left or this wasn't a max
2965 * size packet then this transfer is
2966 * complete. Otherwise start it again to
2967 * send the next 188 bytes
2969 if (!buffer_space_left || (bytes_this_transfer < 188)) {
2970 pipe->next_tx_frame += pipe->interval;
2971 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2974 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
2976 * We are in the incoming data
2977 * phase. Keep getting data
2978 * until we run out of space or
2979 * get a small packet
2981 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2982 pipe->next_tx_frame += pipe->interval;
2983 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2986 transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2989 pipe->next_tx_frame += pipe->interval;
2990 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2994 } else if (usbc_hcint.s.nak) {
2995 /* If this was a split then clear our split in progress marker */
2996 if (usb->active_split == transaction)
2997 usb->active_split = NULL;
2999 * NAK as a response means the device couldn't accept the
3000 * transaction, but it should be retried in the future. Rewind
3001 * to the beginning of the transaction by anding off the split
3002 * complete bit. Retry in the next interval
3004 transaction->retries = 0;
3005 transaction->stage &= ~1;
3006 pipe->next_tx_frame += pipe->interval;
3007 if (pipe->next_tx_frame < usb->frame_number)
3008 pipe->next_tx_frame = usb->frame_number + pipe->interval -
3009 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
3011 struct cvmx_usb_port_status port;
3012 port = cvmx_usb_get_status((struct cvmx_usb_state *)usb);
3013 if (port.port_enabled) {
3014 /* We'll retry the exact same transaction again */
3015 transaction->retries++;
3018 * We get channel halted interrupts with no result bits
3019 * sets when the cable is unplugged
3021 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
3029 * Poll the USB block for status and call all needed callback
3030 * handlers. This function is meant to be called in the interrupt
3031 * handler for the USB controller. It can also be called
3032 * periodically in a loop for non-interrupt based operation.
3034 * @state: USB device state populated by
3035 * cvmx_usb_initialize().
3037 * Returns: 0 or a negative error code.
3039 int cvmx_usb_poll(struct cvmx_usb_state *state)
3041 union cvmx_usbcx_hfnum usbc_hfnum;
3042 union cvmx_usbcx_gintsts usbc_gintsts;
3043 struct cvmx_usb_internal_state *usb = (struct cvmx_usb_internal_state *)state;
3045 CVMX_PREFETCH(usb, 0);
3046 CVMX_PREFETCH(usb, 1*128);
3047 CVMX_PREFETCH(usb, 2*128);
3048 CVMX_PREFETCH(usb, 3*128);
3049 CVMX_PREFETCH(usb, 4*128);
3051 /* Update the frame counter */
3052 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
3053 if ((usb->frame_number&0x3fff) > usbc_hfnum.s.frnum)
3054 usb->frame_number += 0x4000;
3055 usb->frame_number &= ~0x3fffull;
3056 usb->frame_number |= usbc_hfnum.s.frnum;
3058 /* Read the pending interrupts */
3059 usbc_gintsts.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTSTS(usb->index));
3061 /* Clear the interrupts now that we know about them */
3062 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index), usbc_gintsts.u32);
3064 if (usbc_gintsts.s.rxflvl) {
3066 * RxFIFO Non-Empty (RxFLvl)
3067 * Indicates that there is at least one packet pending to be
3068 * read from the RxFIFO.
3070 * In DMA mode this is handled by hardware
3072 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3073 __cvmx_usb_poll_rx_fifo(usb);
3075 if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
3076 /* Fill the Tx FIFOs when not in DMA mode */
3077 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3078 __cvmx_usb_poll_tx_fifo(usb);
3080 if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
3081 union cvmx_usbcx_hprt usbc_hprt;
3083 * Disconnect Detected Interrupt (DisconnInt)
3084 * Asserted when a device disconnect is detected.
3086 * Host Port Interrupt (PrtInt)
3087 * The core sets this bit to indicate a change in port status of
3088 * one of the O2P USB core ports in Host mode. The application
3089 * must read the Host Port Control and Status (HPRT) register to
3090 * determine the exact event that caused this interrupt. The
3091 * application must clear the appropriate status bit in the Host
3092 * Port Control and Status register to clear this bit.
3094 * Call the user's port callback
3096 __cvmx_usb_perform_callback(usb, NULL, NULL,
3097 CVMX_USB_CALLBACK_PORT_CHANGED,
3098 CVMX_USB_COMPLETE_SUCCESS);
3099 /* Clear the port change bits */
3100 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
3101 usbc_hprt.s.prtena = 0;
3102 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index), usbc_hprt.u32);
3104 if (usbc_gintsts.s.hchint) {
3106 * Host Channels Interrupt (HChInt)
3107 * The core sets this bit to indicate that an interrupt is
3108 * pending on one of the channels of the core (in Host mode).
3109 * The application must read the Host All Channels Interrupt
3110 * (HAINT) register to determine the exact number of the channel
3111 * on which the interrupt occurred, and then read the
3112 * corresponding Host Channel-n Interrupt (HCINTn) register to
3113 * determine the exact cause of the interrupt. The application
3114 * must clear the appropriate status bit in the HCINTn register
3115 * to clear this bit.
3117 union cvmx_usbcx_haint usbc_haint;
3118 usbc_haint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINT(usb->index));
3119 while (usbc_haint.u32) {
3122 channel = __fls(usbc_haint.u32);
3123 __cvmx_usb_poll_channel(usb, channel);
3124 usbc_haint.u32 ^= 1<<channel;
3128 __cvmx_usb_schedule(usb, usbc_gintsts.s.sof);