2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2008 Cavium Networks
8 * Some parts of the code were originally released under BSD license:
10 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * * Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
25 * * Neither the name of Cavium Networks nor the names of
26 * its contributors may be used to endorse or promote products
27 * derived from this software without specific prior written
30 * This Software, including technical data, may be subject to U.S. export
31 * control laws, including the U.S. Export Administration Act and its associated
32 * regulations, and may be subject to export or import regulations in other
35 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
36 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
37 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
38 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION
39 * OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
40 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
41 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
42 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
43 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
44 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/pci.h>
50 #include <linux/prefetch.h>
51 #include <linux/interrupt.h>
52 #include <linux/platform_device.h>
53 #include <linux/usb.h>
55 #include <linux/time.h>
56 #include <linux/delay.h>
58 #include <asm/octeon/cvmx.h>
59 #include <asm/octeon/cvmx-iob-defs.h>
61 #include <linux/usb/hcd.h>
63 #include <linux/err.h>
65 #include <asm/octeon/octeon.h>
66 #include <asm/octeon/cvmx-helper.h>
67 #include <asm/octeon/cvmx-sysinfo.h>
68 #include <asm/octeon/cvmx-helper-board.h>
70 #include "octeon-hcd.h"
73 * enum cvmx_usb_speed - the possible USB device speeds
75 * @CVMX_USB_SPEED_HIGH: Device is operation at 480Mbps
76 * @CVMX_USB_SPEED_FULL: Device is operation at 12Mbps
77 * @CVMX_USB_SPEED_LOW: Device is operation at 1.5Mbps
80 CVMX_USB_SPEED_HIGH
= 0,
81 CVMX_USB_SPEED_FULL
= 1,
82 CVMX_USB_SPEED_LOW
= 2,
86 * enum cvmx_usb_transfer - the possible USB transfer types
88 * @CVMX_USB_TRANSFER_CONTROL: USB transfer type control for hub and status
90 * @CVMX_USB_TRANSFER_ISOCHRONOUS: USB transfer type isochronous for low
91 * priority periodic transfers
92 * @CVMX_USB_TRANSFER_BULK: USB transfer type bulk for large low priority
94 * @CVMX_USB_TRANSFER_INTERRUPT: USB transfer type interrupt for high priority
97 enum cvmx_usb_transfer
{
98 CVMX_USB_TRANSFER_CONTROL
= 0,
99 CVMX_USB_TRANSFER_ISOCHRONOUS
= 1,
100 CVMX_USB_TRANSFER_BULK
= 2,
101 CVMX_USB_TRANSFER_INTERRUPT
= 3,
105 * enum cvmx_usb_direction - the transfer directions
107 * @CVMX_USB_DIRECTION_OUT: Data is transferring from Octeon to the device/host
108 * @CVMX_USB_DIRECTION_IN: Data is transferring from the device/host to Octeon
110 enum cvmx_usb_direction
{
111 CVMX_USB_DIRECTION_OUT
,
112 CVMX_USB_DIRECTION_IN
,
116 * enum cvmx_usb_complete - possible callback function status codes
118 * @CVMX_USB_COMPLETE_SUCCESS: The transaction / operation finished without
120 * @CVMX_USB_COMPLETE_SHORT: FIXME: This is currently not implemented
121 * @CVMX_USB_COMPLETE_CANCEL: The transaction was canceled while in flight
122 * by a user call to cvmx_usb_cancel
123 * @CVMX_USB_COMPLETE_ERROR: The transaction aborted with an unexpected
125 * @CVMX_USB_COMPLETE_STALL: The transaction received a USB STALL response
127 * @CVMX_USB_COMPLETE_XACTERR: The transaction failed with an error from the
128 * device even after a number of retries
129 * @CVMX_USB_COMPLETE_DATATGLERR: The transaction failed with a data toggle
130 * error even after a number of retries
131 * @CVMX_USB_COMPLETE_BABBLEERR: The transaction failed with a babble error
132 * @CVMX_USB_COMPLETE_FRAMEERR: The transaction failed with a frame error
133 * even after a number of retries
135 enum cvmx_usb_complete
{
136 CVMX_USB_COMPLETE_SUCCESS
,
137 CVMX_USB_COMPLETE_SHORT
,
138 CVMX_USB_COMPLETE_CANCEL
,
139 CVMX_USB_COMPLETE_ERROR
,
140 CVMX_USB_COMPLETE_STALL
,
141 CVMX_USB_COMPLETE_XACTERR
,
142 CVMX_USB_COMPLETE_DATATGLERR
,
143 CVMX_USB_COMPLETE_BABBLEERR
,
144 CVMX_USB_COMPLETE_FRAMEERR
,
148 * struct cvmx_usb_port_status - the USB port status information
150 * @port_enabled: 1 = Usb port is enabled, 0 = disabled
151 * @port_over_current: 1 = Over current detected, 0 = Over current not
152 * detected. Octeon doesn't support over current detection.
153 * @port_powered: 1 = Port power is being supplied to the device, 0 =
154 * power is off. Octeon doesn't support turning port power
156 * @port_speed: Current port speed.
157 * @connected: 1 = A device is connected to the port, 0 = No device is
159 * @connect_change: 1 = Device connected state changed since the last set
162 struct cvmx_usb_port_status
{
163 uint32_t reserved
: 25;
164 uint32_t port_enabled
: 1;
165 uint32_t port_over_current
: 1;
166 uint32_t port_powered
: 1;
167 enum cvmx_usb_speed port_speed
: 2;
168 uint32_t connected
: 1;
169 uint32_t connect_change
: 1;
173 * struct cvmx_usb_iso_packet - descriptor for Isochronous packets
175 * @offset: This is the offset in bytes into the main buffer where this data
177 * @length: This is the length in bytes of the data.
178 * @status: This is the status of this individual packet transfer.
180 struct cvmx_usb_iso_packet
{
183 enum cvmx_usb_complete status
;
187 * enum cvmx_usb_initialize_flags - flags used by the initialization function
189 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI: The USB port uses a 12MHz crystal
190 * as clock source at USB_XO and
192 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND: The USB port uses 12/24/48MHz 2.5V
193 * board clock source at USB_XO.
194 * USB_XI should be tied to GND.
195 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK: Mask for clock speed field
196 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ: Speed of reference clock or
198 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ: Speed of reference clock
199 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ: Speed of reference clock
200 * @CVMX_USB_INITIALIZE_FLAGS_NO_DMA: Disable DMA and used polled IO for
201 * data transfer use for the USB
203 enum cvmx_usb_initialize_flags
{
204 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI
= 1 << 0,
205 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND
= 1 << 1,
206 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK
= 3 << 3,
207 CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ
= 1 << 3,
208 CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ
= 2 << 3,
209 CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ
= 3 << 3,
210 /* Bits 3-4 used to encode the clock frequency */
211 CVMX_USB_INITIALIZE_FLAGS_NO_DMA
= 1 << 5,
215 * enum cvmx_usb_pipe_flags - internal flags for a pipe.
217 * @__CVMX_USB_PIPE_FLAGS_SCHEDULED: Used internally to determine if a pipe is
218 * actively using hardware. Do not use.
219 * @__CVMX_USB_PIPE_FLAGS_NEED_PING: Used internally to determine if a high
220 * speed pipe is in the ping state. Do not
223 enum cvmx_usb_pipe_flags
{
224 __CVMX_USB_PIPE_FLAGS_SCHEDULED
= 1 << 17,
225 __CVMX_USB_PIPE_FLAGS_NEED_PING
= 1 << 18,
228 /* Maximum number of times to retry failed transactions */
229 #define MAX_RETRIES 3
231 /* Maximum number of hardware channels supported by the USB block */
232 #define MAX_CHANNELS 8
234 /* The highest valid USB device address */
235 #define MAX_USB_ADDRESS 127
237 /* The highest valid USB endpoint number */
238 #define MAX_USB_ENDPOINT 15
240 /* The highest valid port number on a hub */
241 #define MAX_USB_HUB_PORT 15
244 * The low level hardware can transfer a maximum of this number of bytes in each
245 * transfer. The field is 19 bits wide
247 #define MAX_TRANSFER_BYTES ((1<<19)-1)
250 * The low level hardware can transfer a maximum of this number of packets in
251 * each transfer. The field is 10 bits wide
253 #define MAX_TRANSFER_PACKETS ((1<<10)-1)
256 * Logical transactions may take numerous low level
257 * transactions, especially when splits are concerned. This
258 * enum represents all of the possible stages a transaction can
259 * be in. Note that split completes are always even. This is so
260 * the NAK handler can backup to the previous low level
261 * transaction with a simple clearing of bit 0.
263 enum cvmx_usb_stage
{
264 CVMX_USB_STAGE_NON_CONTROL
,
265 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE
,
266 CVMX_USB_STAGE_SETUP
,
267 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE
,
269 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE
,
270 CVMX_USB_STAGE_STATUS
,
271 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE
,
275 * struct cvmx_usb_transaction - describes each pending USB transaction
276 * regardless of type. These are linked together
277 * to form a list of pending requests for a pipe.
279 * @node: List node for transactions in the pipe.
280 * @type: Type of transaction, duplicated of the pipe.
281 * @flags: State flags for this transaction.
282 * @buffer: User's physical buffer address to read/write.
283 * @buffer_length: Size of the user's buffer in bytes.
284 * @control_header: For control transactions, physical address of the 8
285 * byte standard header.
286 * @iso_start_frame: For ISO transactions, the starting frame number.
287 * @iso_number_packets: For ISO transactions, the number of packets in the
289 * @iso_packets: For ISO transactions, the sub packets in the request.
290 * @actual_bytes: Actual bytes transfer for this transaction.
291 * @stage: For control transactions, the current stage.
294 struct cvmx_usb_transaction
{
295 struct list_head node
;
296 enum cvmx_usb_transfer type
;
299 uint64_t control_header
;
301 int iso_number_packets
;
302 struct cvmx_usb_iso_packet
*iso_packets
;
307 enum cvmx_usb_stage stage
;
312 * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
313 * and some USB device. It contains a list of pending
314 * request to the device.
316 * @node: List node for pipe list
317 * @next: Pipe after this one in the list
318 * @transactions: List of pending transactions
319 * @interval: For periodic pipes, the interval between packets in
321 * @next_tx_frame: The next frame this pipe is allowed to transmit on
322 * @flags: State flags for this pipe
323 * @device_speed: Speed of device connected to this pipe
324 * @transfer_type: Type of transaction supported by this pipe
325 * @transfer_dir: IN or OUT. Ignored for Control
326 * @multi_count: Max packet in a row for the device
327 * @max_packet: The device's maximum packet size in bytes
328 * @device_addr: USB device address at other end of pipe
329 * @endpoint_num: USB endpoint number at other end of pipe
330 * @hub_device_addr: Hub address this device is connected to
331 * @hub_port: Hub port this device is connected to
332 * @pid_toggle: This toggles between 0/1 on every packet send to track
333 * the data pid needed
334 * @channel: Hardware DMA channel for this pipe
335 * @split_sc_frame: The low order bits of the frame number the split
336 * complete should be sent on
338 struct cvmx_usb_pipe
{
339 struct list_head node
;
340 struct list_head transactions
;
342 uint64_t next_tx_frame
;
343 enum cvmx_usb_pipe_flags flags
;
344 enum cvmx_usb_speed device_speed
;
345 enum cvmx_usb_transfer transfer_type
;
346 enum cvmx_usb_direction transfer_dir
;
350 uint8_t endpoint_num
;
351 uint8_t hub_device_addr
;
355 int8_t split_sc_frame
;
358 struct cvmx_usb_tx_fifo
{
363 } entry
[MAX_CHANNELS
+1];
369 * struct cvmx_usb_state - the state of the USB block
371 * init_flags: Flags passed to initialize.
372 * index: Which USB block this is for.
373 * idle_hardware_channels: Bit set for every idle hardware channel.
374 * usbcx_hprt: Stored port status so we don't need to read a CSR to
376 * pipe_for_channel: Map channels to pipes.
377 * pipe: Storage for pipes.
378 * indent: Used by debug output to indent functions.
379 * port_status: Last port status used for change notification.
380 * idle_pipes: List of open pipes that have no transactions.
381 * active_pipes: Active pipes indexed by transfer type.
382 * frame_number: Increments every SOF interrupt for time keeping.
383 * active_split: Points to the current active split, or NULL.
385 struct cvmx_usb_state
{
388 int idle_hardware_channels
;
389 union cvmx_usbcx_hprt usbcx_hprt
;
390 struct cvmx_usb_pipe
*pipe_for_channel
[MAX_CHANNELS
];
392 struct cvmx_usb_port_status port_status
;
393 struct list_head idle_pipes
;
394 struct list_head active_pipes
[4];
395 uint64_t frame_number
;
396 struct cvmx_usb_transaction
*active_split
;
397 struct cvmx_usb_tx_fifo periodic
;
398 struct cvmx_usb_tx_fifo nonperiodic
;
403 struct cvmx_usb_state usb
;
406 /* This macro spins on a field waiting for it to reach a value */
407 #define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
410 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
411 octeon_get_clock_rate() / 1000000; \
414 c.u32 = __cvmx_usb_read_csr32(usb, address); \
415 if (c.s.field op (value)) { \
418 } else if (cvmx_get_cycle() > done) { \
428 * This macro logically sets a single field in a CSR. It does the sequence
429 * read, modify, and write
431 #define USB_SET_FIELD32(address, type, field, value) \
434 c.u32 = __cvmx_usb_read_csr32(usb, address); \
436 __cvmx_usb_write_csr32(usb, address, c.u32); \
439 /* Returns the IO address to push/pop stuff data from the FIFOs */
440 #define USB_FIFO_ADDRESS(channel, usb_index) \
441 (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
444 * struct octeon_temp_buffer - a bounce buffer for USB transfers
445 * @temp_buffer: the newly allocated temporary buffer (including meta-data)
446 * @orig_buffer: the original buffer passed by the USB stack
447 * @data: the newly allocated temporary buffer (excluding meta-data)
449 * Both the DMA engine and FIFO mode will always transfer full 32-bit words. If
450 * the buffer is too short, we need to allocate a temporary one, and this struct
453 struct octeon_temp_buffer
{
460 * octeon_alloc_temp_buffer - allocate a temporary buffer for USB transfer
463 * @mem_flags: Memory allocation flags.
465 * This function allocates a temporary bounce buffer whenever it's needed
466 * due to HW limitations.
468 static int octeon_alloc_temp_buffer(struct urb
*urb
, gfp_t mem_flags
)
470 struct octeon_temp_buffer
*temp
;
472 if (urb
->num_sgs
|| urb
->sg
||
473 (urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
) ||
474 !(urb
->transfer_buffer_length
% sizeof(u32
)))
477 temp
= kmalloc(ALIGN(urb
->transfer_buffer_length
, sizeof(u32
)) +
478 sizeof(*temp
), mem_flags
);
482 temp
->temp_buffer
= temp
;
483 temp
->orig_buffer
= urb
->transfer_buffer
;
484 if (usb_urb_dir_out(urb
))
485 memcpy(temp
->data
, urb
->transfer_buffer
,
486 urb
->transfer_buffer_length
);
487 urb
->transfer_buffer
= temp
->data
;
488 urb
->transfer_flags
|= URB_ALIGNED_TEMP_BUFFER
;
494 * octeon_free_temp_buffer - free a temporary buffer used by USB transfers.
497 * Frees a buffer allocated by octeon_alloc_temp_buffer().
499 static void octeon_free_temp_buffer(struct urb
*urb
)
501 struct octeon_temp_buffer
*temp
;
503 if (!(urb
->transfer_flags
& URB_ALIGNED_TEMP_BUFFER
))
506 temp
= container_of(urb
->transfer_buffer
, struct octeon_temp_buffer
,
508 if (usb_urb_dir_in(urb
))
509 memcpy(temp
->orig_buffer
, urb
->transfer_buffer
,
511 urb
->transfer_buffer
= temp
->orig_buffer
;
512 urb
->transfer_flags
&= ~URB_ALIGNED_TEMP_BUFFER
;
513 kfree(temp
->temp_buffer
);
517 * octeon_map_urb_for_dma - Octeon-specific map_urb_for_dma().
518 * @hcd: USB HCD structure.
520 * @mem_flags: Memory allocation flags.
522 static int octeon_map_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
,
527 ret
= octeon_alloc_temp_buffer(urb
, mem_flags
);
531 ret
= usb_hcd_map_urb_for_dma(hcd
, urb
, mem_flags
);
533 octeon_free_temp_buffer(urb
);
539 * octeon_unmap_urb_for_dma - Octeon-specific unmap_urb_for_dma()
540 * @hcd: USB HCD structure.
543 static void octeon_unmap_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
)
545 usb_hcd_unmap_urb_for_dma(hcd
, urb
);
546 octeon_free_temp_buffer(urb
);
550 * Read a USB 32bit CSR. It performs the necessary address swizzle
551 * for 32bit CSRs and logs the value in a readable format if
554 * @usb: USB block this access is for
555 * @address: 64bit address to read
557 * Returns: Result of the read
559 static inline uint32_t __cvmx_usb_read_csr32(struct cvmx_usb_state
*usb
,
562 uint32_t result
= cvmx_read64_uint32(address
^ 4);
568 * Write a USB 32bit CSR. It performs the necessary address
569 * swizzle for 32bit CSRs and logs the value in a readable format
570 * if debugging is on.
572 * @usb: USB block this access is for
573 * @address: 64bit address to write
574 * @value: Value to write
576 static inline void __cvmx_usb_write_csr32(struct cvmx_usb_state
*usb
,
577 uint64_t address
, uint32_t value
)
579 cvmx_write64_uint32(address
^ 4, value
);
580 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb
->index
));
585 * Read a USB 64bit CSR. It logs the value in a readable format if
588 * @usb: USB block this access is for
589 * @address: 64bit address to read
591 * Returns: Result of the read
593 static inline uint64_t __cvmx_usb_read_csr64(struct cvmx_usb_state
*usb
,
596 uint64_t result
= cvmx_read64_uint64(address
);
602 * Write a USB 64bit CSR. It logs the value in a readable format
603 * if debugging is on.
605 * @usb: USB block this access is for
606 * @address: 64bit address to write
607 * @value: Value to write
609 static inline void __cvmx_usb_write_csr64(struct cvmx_usb_state
*usb
,
610 uint64_t address
, uint64_t value
)
612 cvmx_write64_uint64(address
, value
);
616 * Return non zero if this pipe connects to a non HIGH speed
617 * device through a high speed hub.
619 * @usb: USB block this access is for
620 * @pipe: Pipe to check
622 * Returns: Non zero if we need to do split transactions
624 static inline int __cvmx_usb_pipe_needs_split(struct cvmx_usb_state
*usb
,
625 struct cvmx_usb_pipe
*pipe
)
627 return pipe
->device_speed
!= CVMX_USB_SPEED_HIGH
&&
628 usb
->usbcx_hprt
.s
.prtspd
== CVMX_USB_SPEED_HIGH
;
633 * Trivial utility function to return the correct PID for a pipe
635 * @pipe: pipe to check
637 * Returns: PID for pipe
639 static inline int __cvmx_usb_get_data_pid(struct cvmx_usb_pipe
*pipe
)
641 if (pipe
->pid_toggle
)
642 return 2; /* Data1 */
644 return 0; /* Data0 */
648 * Initialize a USB port for use. This must be called before any
649 * other access to the Octeon USB port is made. The port starts
650 * off in the disabled state.
652 * @usb: Pointer to an empty struct cvmx_usb_state
653 * that will be populated by the initialize call.
654 * This structure is then passed to all other USB
657 * Which Octeon USB port to initialize.
659 * Returns: 0 or a negative error code.
661 static int cvmx_usb_initialize(struct cvmx_usb_state
*usb
,
663 enum cvmx_usb_initialize_flags flags
)
665 union cvmx_usbnx_clk_ctl usbn_clk_ctl
;
666 union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status
;
669 /* At first allow 0-1 for the usb port number */
670 if ((usb_port_number
< 0) || (usb_port_number
> 1))
673 memset(usb
, 0, sizeof(*usb
));
674 usb
->init_flags
= flags
;
676 /* Initialize the USB state structure */
677 usb
->index
= usb_port_number
;
678 INIT_LIST_HEAD(&usb
->idle_pipes
);
679 for (i
= 0; i
< ARRAY_SIZE(usb
->active_pipes
); i
++)
680 INIT_LIST_HEAD(&usb
->active_pipes
[i
]);
683 * Power On Reset and PHY Initialization
685 * 1. Wait for DCOK to assert (nothing to do)
687 * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
688 * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
691 __cvmx_usb_read_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
));
692 usbn_clk_ctl
.s
.por
= 1;
693 usbn_clk_ctl
.s
.hrst
= 0;
694 usbn_clk_ctl
.s
.prst
= 0;
695 usbn_clk_ctl
.s
.hclk_rst
= 0;
696 usbn_clk_ctl
.s
.enable
= 0;
698 * 2b. Select the USB reference clock/crystal parameters by writing
699 * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
701 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND
) {
703 * The USB port uses 12/24/48MHz 2.5V board clock
704 * source at USB_XO. USB_XI should be tied to GND.
705 * Most Octeon evaluation boards require this setting
707 if (OCTEON_IS_MODEL(OCTEON_CN3XXX
) ||
708 OCTEON_IS_MODEL(OCTEON_CN56XX
) ||
709 OCTEON_IS_MODEL(OCTEON_CN50XX
))
710 /* From CN56XX,CN50XX,CN31XX,CN30XX manuals */
711 usbn_clk_ctl
.s
.p_rtype
= 2; /* p_rclk=1 & p_xenbn=0 */
713 /* From CN52XX manual */
714 usbn_clk_ctl
.s
.p_rtype
= 1;
716 switch (flags
& CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK
) {
717 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ
:
718 usbn_clk_ctl
.s
.p_c_sel
= 0;
720 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ
:
721 usbn_clk_ctl
.s
.p_c_sel
= 1;
723 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ
:
724 usbn_clk_ctl
.s
.p_c_sel
= 2;
729 * The USB port uses a 12MHz crystal as clock source
730 * at USB_XO and USB_XI
732 if (OCTEON_IS_MODEL(OCTEON_CN3XXX
))
733 /* From CN31XX,CN30XX manual */
734 usbn_clk_ctl
.s
.p_rtype
= 3; /* p_rclk=1 & p_xenbn=1 */
736 /* From CN56XX,CN52XX,CN50XX manuals. */
737 usbn_clk_ctl
.s
.p_rtype
= 0;
739 usbn_clk_ctl
.s
.p_c_sel
= 0;
742 * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
743 * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
744 * such that USB is as close as possible to 125Mhz
747 int divisor
= (octeon_get_clock_rate()+125000000-1)/125000000;
748 /* Lower than 4 doesn't seem to work properly */
751 usbn_clk_ctl
.s
.divide
= divisor
;
752 usbn_clk_ctl
.s
.divide2
= 0;
754 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
),
756 /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
757 usbn_clk_ctl
.s
.hclk_rst
= 1;
758 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
),
760 /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
763 * 3. Program the power-on reset field in the USBN clock-control
765 * USBN_CLK_CTL[POR] = 0
767 usbn_clk_ctl
.s
.por
= 0;
768 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
),
770 /* 4. Wait 1 ms for PHY clock to start */
773 * 5. Program the Reset input from automatic test equipment field in the
774 * USBP control and status register:
775 * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
777 usbn_usbp_ctl_status
.u64
= __cvmx_usb_read_csr64(usb
,
778 CVMX_USBNX_USBP_CTL_STATUS(usb
->index
));
779 usbn_usbp_ctl_status
.s
.ate_reset
= 1;
780 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_USBP_CTL_STATUS(usb
->index
),
781 usbn_usbp_ctl_status
.u64
);
782 /* 6. Wait 10 cycles */
785 * 7. Clear ATE_RESET field in the USBN clock-control register:
786 * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
788 usbn_usbp_ctl_status
.s
.ate_reset
= 0;
789 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_USBP_CTL_STATUS(usb
->index
),
790 usbn_usbp_ctl_status
.u64
);
792 * 8. Program the PHY reset field in the USBN clock-control register:
793 * USBN_CLK_CTL[PRST] = 1
795 usbn_clk_ctl
.s
.prst
= 1;
796 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
),
799 * 9. Program the USBP control and status register to select host or
800 * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
803 usbn_usbp_ctl_status
.s
.hst_mode
= 0;
804 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_USBP_CTL_STATUS(usb
->index
),
805 usbn_usbp_ctl_status
.u64
);
809 * 11. Program the hreset_n field in the USBN clock-control register:
810 * USBN_CLK_CTL[HRST] = 1
812 usbn_clk_ctl
.s
.hrst
= 1;
813 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
),
815 /* 12. Proceed to USB core initialization */
816 usbn_clk_ctl
.s
.enable
= 1;
817 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
),
822 * USB Core Initialization
824 * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
825 * determine USB core configuration parameters.
829 * 2. Program the following fields in the global AHB configuration
830 * register (USBC_GAHBCFG)
831 * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
832 * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
833 * Nonperiodic TxFIFO empty level (slave mode only),
834 * USBC_GAHBCFG[NPTXFEMPLVL]
835 * Periodic TxFIFO empty level (slave mode only),
836 * USBC_GAHBCFG[PTXFEMPLVL]
837 * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
840 union cvmx_usbcx_gahbcfg usbcx_gahbcfg
;
841 /* Due to an errata, CN31XX doesn't support DMA */
842 if (OCTEON_IS_MODEL(OCTEON_CN31XX
))
843 usb
->init_flags
|= CVMX_USB_INITIALIZE_FLAGS_NO_DMA
;
844 usbcx_gahbcfg
.u32
= 0;
845 usbcx_gahbcfg
.s
.dmaen
= !(usb
->init_flags
&
846 CVMX_USB_INITIALIZE_FLAGS_NO_DMA
);
847 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
)
848 /* Only use one channel with non DMA */
849 usb
->idle_hardware_channels
= 0x1;
850 else if (OCTEON_IS_MODEL(OCTEON_CN5XXX
))
851 /* CN5XXX have an errata with channel 3 */
852 usb
->idle_hardware_channels
= 0xf7;
854 usb
->idle_hardware_channels
= 0xff;
855 usbcx_gahbcfg
.s
.hbstlen
= 0;
856 usbcx_gahbcfg
.s
.nptxfemplvl
= 1;
857 usbcx_gahbcfg
.s
.ptxfemplvl
= 1;
858 usbcx_gahbcfg
.s
.glblintrmsk
= 1;
859 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_GAHBCFG(usb
->index
),
863 * 3. Program the following fields in USBC_GUSBCFG register.
864 * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
865 * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
866 * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
867 * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
870 union cvmx_usbcx_gusbcfg usbcx_gusbcfg
;
872 usbcx_gusbcfg
.u32
= __cvmx_usb_read_csr32(usb
,
873 CVMX_USBCX_GUSBCFG(usb
->index
));
874 usbcx_gusbcfg
.s
.toutcal
= 0;
875 usbcx_gusbcfg
.s
.ddrsel
= 0;
876 usbcx_gusbcfg
.s
.usbtrdtim
= 0x5;
877 usbcx_gusbcfg
.s
.phylpwrclksel
= 0;
878 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_GUSBCFG(usb
->index
),
882 * 4. The software must unmask the following bits in the USBC_GINTMSK
884 * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
885 * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
888 union cvmx_usbcx_gintmsk usbcx_gintmsk
;
891 usbcx_gintmsk
.u32
= __cvmx_usb_read_csr32(usb
,
892 CVMX_USBCX_GINTMSK(usb
->index
));
893 usbcx_gintmsk
.s
.otgintmsk
= 1;
894 usbcx_gintmsk
.s
.modemismsk
= 1;
895 usbcx_gintmsk
.s
.hchintmsk
= 1;
896 usbcx_gintmsk
.s
.sofmsk
= 0;
897 /* We need RX FIFO interrupts if we don't have DMA */
898 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
)
899 usbcx_gintmsk
.s
.rxflvlmsk
= 1;
900 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_GINTMSK(usb
->index
),
904 * Disable all channel interrupts. We'll enable them per channel
907 for (channel
= 0; channel
< 8; channel
++)
908 __cvmx_usb_write_csr32(usb
,
909 CVMX_USBCX_HCINTMSKX(channel
, usb
->index
), 0);
914 * Host Port Initialization
916 * 1. Program the host-port interrupt-mask field to unmask,
917 * USBC_GINTMSK[PRTINT] = 1
919 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb
->index
),
920 union cvmx_usbcx_gintmsk
, prtintmsk
, 1);
921 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb
->index
),
922 union cvmx_usbcx_gintmsk
, disconnintmsk
, 1);
924 * 2. Program the USBC_HCFG register to select full-speed host
925 * or high-speed host.
928 union cvmx_usbcx_hcfg usbcx_hcfg
;
930 usbcx_hcfg
.u32
= __cvmx_usb_read_csr32(usb
,
931 CVMX_USBCX_HCFG(usb
->index
));
932 usbcx_hcfg
.s
.fslssupp
= 0;
933 usbcx_hcfg
.s
.fslspclksel
= 0;
934 __cvmx_usb_write_csr32(usb
,
935 CVMX_USBCX_HCFG(usb
->index
),
939 * 3. Program the port power bit to drive VBUS on the USB,
940 * USBC_HPRT[PRTPWR] = 1
942 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb
->index
),
943 union cvmx_usbcx_hprt
, prtpwr
, 1);
946 * Steps 4-15 from the manual are done later in the port enable
955 * Shutdown a USB port after a call to cvmx_usb_initialize().
956 * The port should be disabled with all pipes closed when this
957 * function is called.
959 * @usb: USB device state populated by cvmx_usb_initialize().
961 * Returns: 0 or a negative error code.
963 static int cvmx_usb_shutdown(struct cvmx_usb_state
*usb
)
965 union cvmx_usbnx_clk_ctl usbn_clk_ctl
;
967 /* Make sure all pipes are closed */
968 if (!list_empty(&usb
->idle_pipes
) ||
969 !list_empty(&usb
->active_pipes
[CVMX_USB_TRANSFER_ISOCHRONOUS
]) ||
970 !list_empty(&usb
->active_pipes
[CVMX_USB_TRANSFER_INTERRUPT
]) ||
971 !list_empty(&usb
->active_pipes
[CVMX_USB_TRANSFER_CONTROL
]) ||
972 !list_empty(&usb
->active_pipes
[CVMX_USB_TRANSFER_BULK
]))
975 /* Disable the clocks and put them in power on reset */
976 usbn_clk_ctl
.u64
= __cvmx_usb_read_csr64(usb
,
977 CVMX_USBNX_CLK_CTL(usb
->index
));
978 usbn_clk_ctl
.s
.enable
= 1;
979 usbn_clk_ctl
.s
.por
= 1;
980 usbn_clk_ctl
.s
.hclk_rst
= 1;
981 usbn_clk_ctl
.s
.prst
= 0;
982 usbn_clk_ctl
.s
.hrst
= 0;
983 __cvmx_usb_write_csr64(usb
, CVMX_USBNX_CLK_CTL(usb
->index
),
990 * Enable a USB port. After this call succeeds, the USB port is
991 * online and servicing requests.
993 * @usb: USB device state populated by cvmx_usb_initialize().
995 * Returns: 0 or a negative error code.
997 static int cvmx_usb_enable(struct cvmx_usb_state
*usb
)
999 union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3
;
1001 usb
->usbcx_hprt
.u32
= __cvmx_usb_read_csr32(usb
,
1002 CVMX_USBCX_HPRT(usb
->index
));
1005 * If the port is already enabled the just return. We don't need to do
1008 if (usb
->usbcx_hprt
.s
.prtena
)
1011 /* If there is nothing plugged into the port then fail immediately */
1012 if (!usb
->usbcx_hprt
.s
.prtconnsts
)
1015 /* Program the port reset bit to start the reset process */
1016 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb
->index
), union cvmx_usbcx_hprt
,
1020 * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
1021 * process to complete.
1025 /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
1026 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb
->index
), union cvmx_usbcx_hprt
,
1029 /* Wait for the USBC_HPRT[PRTENA]. */
1030 if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb
->index
),
1031 union cvmx_usbcx_hprt
, prtena
, ==, 1, 100000))
1035 * Read the port speed field to get the enumerated speed,
1036 * USBC_HPRT[PRTSPD].
1038 usb
->usbcx_hprt
.u32
= __cvmx_usb_read_csr32(usb
,
1039 CVMX_USBCX_HPRT(usb
->index
));
1040 usbcx_ghwcfg3
.u32
= __cvmx_usb_read_csr32(usb
,
1041 CVMX_USBCX_GHWCFG3(usb
->index
));
1044 * 13. Program the USBC_GRXFSIZ register to select the size of the
1045 * receive FIFO (25%).
1047 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb
->index
),
1048 union cvmx_usbcx_grxfsiz
, rxfdep
,
1049 usbcx_ghwcfg3
.s
.dfifodepth
/ 4);
1051 * 14. Program the USBC_GNPTXFSIZ register to select the size and the
1052 * start address of the non- periodic transmit FIFO for nonperiodic
1053 * transactions (50%).
1056 union cvmx_usbcx_gnptxfsiz siz
;
1058 siz
.u32
= __cvmx_usb_read_csr32(usb
,
1059 CVMX_USBCX_GNPTXFSIZ(usb
->index
));
1060 siz
.s
.nptxfdep
= usbcx_ghwcfg3
.s
.dfifodepth
/ 2;
1061 siz
.s
.nptxfstaddr
= usbcx_ghwcfg3
.s
.dfifodepth
/ 4;
1062 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_GNPTXFSIZ(usb
->index
),
1066 * 15. Program the USBC_HPTXFSIZ register to select the size and start
1067 * address of the periodic transmit FIFO for periodic transactions
1071 union cvmx_usbcx_hptxfsiz siz
;
1073 siz
.u32
= __cvmx_usb_read_csr32(usb
,
1074 CVMX_USBCX_HPTXFSIZ(usb
->index
));
1075 siz
.s
.ptxfsize
= usbcx_ghwcfg3
.s
.dfifodepth
/ 4;
1076 siz
.s
.ptxfstaddr
= 3 * usbcx_ghwcfg3
.s
.dfifodepth
/ 4;
1077 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_HPTXFSIZ(usb
->index
),
1080 /* Flush all FIFOs */
1081 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb
->index
),
1082 union cvmx_usbcx_grstctl
, txfnum
, 0x10);
1083 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb
->index
),
1084 union cvmx_usbcx_grstctl
, txfflsh
, 1);
1085 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb
->index
),
1086 union cvmx_usbcx_grstctl
,
1087 txfflsh
, ==, 0, 100);
1088 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb
->index
),
1089 union cvmx_usbcx_grstctl
, rxfflsh
, 1);
1090 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb
->index
),
1091 union cvmx_usbcx_grstctl
,
1092 rxfflsh
, ==, 0, 100);
1099 * Disable a USB port. After this call the USB port will not
1100 * generate data transfers and will not generate events.
1101 * Transactions in process will fail and call their
1102 * associated callbacks.
1104 * @usb: USB device state populated by cvmx_usb_initialize().
1106 * Returns: 0 or a negative error code.
1108 static int cvmx_usb_disable(struct cvmx_usb_state
*usb
)
1110 /* Disable the port */
1111 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb
->index
), union cvmx_usbcx_hprt
,
1118 * Get the current state of the USB port. Use this call to
1119 * determine if the usb port has anything connected, is enabled,
1120 * or has some sort of error condition. The return value of this
1121 * call has "changed" bits to signal of the value of some fields
1122 * have changed between calls.
1124 * @usb: USB device state populated by cvmx_usb_initialize().
1126 * Returns: Port status information
1128 static struct cvmx_usb_port_status
cvmx_usb_get_status(
1129 struct cvmx_usb_state
*usb
)
1131 union cvmx_usbcx_hprt usbc_hprt
;
1132 struct cvmx_usb_port_status result
;
1134 memset(&result
, 0, sizeof(result
));
1136 usbc_hprt
.u32
= __cvmx_usb_read_csr32(usb
,
1137 CVMX_USBCX_HPRT(usb
->index
));
1138 result
.port_enabled
= usbc_hprt
.s
.prtena
;
1139 result
.port_over_current
= usbc_hprt
.s
.prtovrcurract
;
1140 result
.port_powered
= usbc_hprt
.s
.prtpwr
;
1141 result
.port_speed
= usbc_hprt
.s
.prtspd
;
1142 result
.connected
= usbc_hprt
.s
.prtconnsts
;
1143 result
.connect_change
=
1144 (result
.connected
!= usb
->port_status
.connected
);
1150 * Open a virtual pipe between the host and a USB device. A pipe
1151 * must be opened before data can be transferred between a device
1154 * @usb: USB device state populated by cvmx_usb_initialize().
1156 * USB device address to open the pipe to
1159 * USB endpoint number to open the pipe to
1162 * The speed of the device the pipe is going
1163 * to. This must match the device's speed,
1164 * which may be different than the port speed.
1165 * @max_packet: The maximum packet length the device can
1166 * transmit/receive (low speed=0-8, full
1167 * speed=0-1023, high speed=0-1024). This value
1168 * comes from the standard endpoint descriptor
1169 * field wMaxPacketSize bits <10:0>.
1171 * The type of transfer this pipe is for.
1173 * The direction the pipe is in. This is not
1174 * used for control pipes.
1175 * @interval: For ISOCHRONOUS and INTERRUPT transfers,
1176 * this is how often the transfer is scheduled
1177 * for. All other transfers should specify
1178 * zero. The units are in frames (8000/sec at
1179 * high speed, 1000/sec for full speed).
1181 * For high speed devices, this is the maximum
1182 * allowed number of packet per microframe.
1183 * Specify zero for non high speed devices. This
1184 * value comes from the standard endpoint descriptor
1185 * field wMaxPacketSize bits <12:11>.
1187 * Hub device address this device is connected
1188 * to. Devices connected directly to Octeon
1189 * use zero. This is only used when the device
1190 * is full/low speed behind a high speed hub.
1191 * The address will be of the high speed hub,
1192 * not and full speed hubs after it.
1193 * @hub_port: Which port on the hub the device is
1194 * connected. Use zero for devices connected
1195 * directly to Octeon. Like hub_device_addr,
1196 * this is only used for full/low speed
1197 * devices behind a high speed hub.
1199 * Returns: A non-NULL value is a pipe. NULL means an error.
1201 static struct cvmx_usb_pipe
*cvmx_usb_open_pipe(struct cvmx_usb_state
*usb
,
1207 enum cvmx_usb_transfer
1209 enum cvmx_usb_direction
1211 int interval
, int multi_count
,
1212 int hub_device_addr
,
1215 struct cvmx_usb_pipe
*pipe
;
1217 if (unlikely((device_addr
< 0) || (device_addr
> MAX_USB_ADDRESS
)))
1219 if (unlikely((endpoint_num
< 0) || (endpoint_num
> MAX_USB_ENDPOINT
)))
1221 if (unlikely(device_speed
> CVMX_USB_SPEED_LOW
))
1223 if (unlikely((max_packet
<= 0) || (max_packet
> 1024)))
1225 if (unlikely(transfer_type
> CVMX_USB_TRANSFER_INTERRUPT
))
1227 if (unlikely((transfer_dir
!= CVMX_USB_DIRECTION_OUT
) &&
1228 (transfer_dir
!= CVMX_USB_DIRECTION_IN
)))
1230 if (unlikely(interval
< 0))
1232 if (unlikely((transfer_type
== CVMX_USB_TRANSFER_CONTROL
) && interval
))
1234 if (unlikely(multi_count
< 0))
1236 if (unlikely((device_speed
!= CVMX_USB_SPEED_HIGH
) &&
1237 (multi_count
!= 0)))
1239 if (unlikely((hub_device_addr
< 0) ||
1240 (hub_device_addr
> MAX_USB_ADDRESS
)))
1242 if (unlikely((hub_port
< 0) || (hub_port
> MAX_USB_HUB_PORT
)))
1245 pipe
= kzalloc(sizeof(*pipe
), GFP_ATOMIC
);
1248 if ((device_speed
== CVMX_USB_SPEED_HIGH
) &&
1249 (transfer_dir
== CVMX_USB_DIRECTION_OUT
) &&
1250 (transfer_type
== CVMX_USB_TRANSFER_BULK
))
1251 pipe
->flags
|= __CVMX_USB_PIPE_FLAGS_NEED_PING
;
1252 pipe
->device_addr
= device_addr
;
1253 pipe
->endpoint_num
= endpoint_num
;
1254 pipe
->device_speed
= device_speed
;
1255 pipe
->max_packet
= max_packet
;
1256 pipe
->transfer_type
= transfer_type
;
1257 pipe
->transfer_dir
= transfer_dir
;
1258 INIT_LIST_HEAD(&pipe
->transactions
);
1261 * All pipes use interval to rate limit NAK processing. Force an
1262 * interval if one wasn't supplied
1266 if (__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
1267 pipe
->interval
= interval
*8;
1268 /* Force start splits to be schedule on uFrame 0 */
1269 pipe
->next_tx_frame
= ((usb
->frame_number
+7)&~7) +
1272 pipe
->interval
= interval
;
1273 pipe
->next_tx_frame
= usb
->frame_number
+ pipe
->interval
;
1275 pipe
->multi_count
= multi_count
;
1276 pipe
->hub_device_addr
= hub_device_addr
;
1277 pipe
->hub_port
= hub_port
;
1278 pipe
->pid_toggle
= 0;
1279 pipe
->split_sc_frame
= -1;
1280 list_add_tail(&pipe
->node
, &usb
->idle_pipes
);
1283 * We don't need to tell the hardware about this pipe yet since
1284 * it doesn't have any submitted requests
1292 * Poll the RX FIFOs and remove data as needed. This function is only used
1293 * in non DMA mode. It is very important that this function be called quickly
1294 * enough to prevent FIFO overflow.
1296 * @usb: USB device state populated by cvmx_usb_initialize().
1298 static void __cvmx_usb_poll_rx_fifo(struct cvmx_usb_state
*usb
)
1300 union cvmx_usbcx_grxstsph rx_status
;
1306 rx_status
.u32
= __cvmx_usb_read_csr32(usb
,
1307 CVMX_USBCX_GRXSTSPH(usb
->index
));
1308 /* Only read data if IN data is there */
1309 if (rx_status
.s
.pktsts
!= 2)
1311 /* Check if no data is available */
1312 if (!rx_status
.s
.bcnt
)
1315 channel
= rx_status
.s
.chnum
;
1316 bytes
= rx_status
.s
.bcnt
;
1320 /* Get where the DMA engine would have written this data */
1321 address
= __cvmx_usb_read_csr64(usb
,
1322 CVMX_USBNX_DMA0_INB_CHN0(usb
->index
) + channel
*8);
1324 ptr
= cvmx_phys_to_ptr(address
);
1325 __cvmx_usb_write_csr64(usb
,
1326 CVMX_USBNX_DMA0_INB_CHN0(usb
->index
) + channel
*8,
1329 /* Loop writing the FIFO data for this packet into memory */
1331 *ptr
++ = __cvmx_usb_read_csr32(usb
, USB_FIFO_ADDRESS(channel
, usb
->index
));
1339 * Fill the TX hardware fifo with data out of the software
1342 * @usb: USB device state populated by cvmx_usb_initialize().
1343 * @fifo: Software fifo to use
1344 * @available: Amount of space in the hardware fifo
1346 * Returns: Non zero if the hardware fifo was too small and needs
1347 * to be serviced again.
1349 static int __cvmx_usb_fill_tx_hw(struct cvmx_usb_state
*usb
,
1350 struct cvmx_usb_tx_fifo
*fifo
, int available
)
1353 * We're done either when there isn't anymore space or the software FIFO
1356 while (available
&& (fifo
->head
!= fifo
->tail
)) {
1358 const uint32_t *ptr
= cvmx_phys_to_ptr(fifo
->entry
[i
].address
);
1359 uint64_t csr_address
= USB_FIFO_ADDRESS(fifo
->entry
[i
].channel
,
1361 int words
= available
;
1363 /* Limit the amount of data to waht the SW fifo has */
1364 if (fifo
->entry
[i
].size
<= available
) {
1365 words
= fifo
->entry
[i
].size
;
1367 if (fifo
->tail
> MAX_CHANNELS
)
1371 /* Update the next locations and counts */
1373 fifo
->entry
[i
].address
+= words
* 4;
1374 fifo
->entry
[i
].size
-= words
;
1377 * Write the HW fifo data. The read every three writes is due
1378 * to an errata on CN3XXX chips
1381 cvmx_write64_uint32(csr_address
, *ptr
++);
1382 cvmx_write64_uint32(csr_address
, *ptr
++);
1383 cvmx_write64_uint32(csr_address
, *ptr
++);
1385 CVMX_USBNX_DMA0_INB_CHN0(usb
->index
));
1388 cvmx_write64_uint32(csr_address
, *ptr
++);
1390 cvmx_write64_uint32(csr_address
, *ptr
++);
1392 cvmx_write64_uint32(csr_address
, *ptr
++);
1394 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb
->index
));
1396 return fifo
->head
!= fifo
->tail
;
1401 * Check the hardware FIFOs and fill them as needed
1403 * @usb: USB device state populated by cvmx_usb_initialize().
1405 static void __cvmx_usb_poll_tx_fifo(struct cvmx_usb_state
*usb
)
1407 if (usb
->periodic
.head
!= usb
->periodic
.tail
) {
1408 union cvmx_usbcx_hptxsts tx_status
;
1410 tx_status
.u32
= __cvmx_usb_read_csr32(usb
,
1411 CVMX_USBCX_HPTXSTS(usb
->index
));
1412 if (__cvmx_usb_fill_tx_hw(usb
, &usb
->periodic
,
1413 tx_status
.s
.ptxfspcavail
))
1414 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb
->index
),
1415 union cvmx_usbcx_gintmsk
,
1418 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb
->index
),
1419 union cvmx_usbcx_gintmsk
,
1423 if (usb
->nonperiodic
.head
!= usb
->nonperiodic
.tail
) {
1424 union cvmx_usbcx_gnptxsts tx_status
;
1426 tx_status
.u32
= __cvmx_usb_read_csr32(usb
,
1427 CVMX_USBCX_GNPTXSTS(usb
->index
));
1428 if (__cvmx_usb_fill_tx_hw(usb
, &usb
->nonperiodic
,
1429 tx_status
.s
.nptxfspcavail
))
1430 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb
->index
),
1431 union cvmx_usbcx_gintmsk
,
1434 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb
->index
),
1435 union cvmx_usbcx_gintmsk
,
1442 * Fill the TX FIFO with an outgoing packet
1444 * @usb: USB device state populated by cvmx_usb_initialize().
1445 * @channel: Channel number to get packet from
1447 static void __cvmx_usb_fill_tx_fifo(struct cvmx_usb_state
*usb
, int channel
)
1449 union cvmx_usbcx_hccharx hcchar
;
1450 union cvmx_usbcx_hcspltx usbc_hcsplt
;
1451 union cvmx_usbcx_hctsizx usbc_hctsiz
;
1452 struct cvmx_usb_tx_fifo
*fifo
;
1454 /* We only need to fill data on outbound channels */
1455 hcchar
.u32
= __cvmx_usb_read_csr32(usb
,
1456 CVMX_USBCX_HCCHARX(channel
, usb
->index
));
1457 if (hcchar
.s
.epdir
!= CVMX_USB_DIRECTION_OUT
)
1460 /* OUT Splits only have data on the start and not the complete */
1461 usbc_hcsplt
.u32
= __cvmx_usb_read_csr32(usb
,
1462 CVMX_USBCX_HCSPLTX(channel
, usb
->index
));
1463 if (usbc_hcsplt
.s
.spltena
&& usbc_hcsplt
.s
.compsplt
)
1467 * Find out how many bytes we need to fill and convert it into 32bit
1470 usbc_hctsiz
.u32
= __cvmx_usb_read_csr32(usb
,
1471 CVMX_USBCX_HCTSIZX(channel
, usb
->index
));
1472 if (!usbc_hctsiz
.s
.xfersize
)
1475 if ((hcchar
.s
.eptype
== CVMX_USB_TRANSFER_INTERRUPT
) ||
1476 (hcchar
.s
.eptype
== CVMX_USB_TRANSFER_ISOCHRONOUS
))
1477 fifo
= &usb
->periodic
;
1479 fifo
= &usb
->nonperiodic
;
1481 fifo
->entry
[fifo
->head
].channel
= channel
;
1482 fifo
->entry
[fifo
->head
].address
= __cvmx_usb_read_csr64(usb
, CVMX_USBNX_DMA0_OUTB_CHN0(usb
->index
) + channel
*8);
1483 fifo
->entry
[fifo
->head
].size
= (usbc_hctsiz
.s
.xfersize
+3)>>2;
1485 if (fifo
->head
> MAX_CHANNELS
)
1488 __cvmx_usb_poll_tx_fifo(usb
);
1492 * Perform channel specific setup for Control transactions. All
1493 * the generic stuff will already have been done in
1494 * __cvmx_usb_start_channel()
1496 * @usb: USB device state populated by cvmx_usb_initialize().
1497 * @channel: Channel to setup
1498 * @pipe: Pipe for control transaction
1500 static void __cvmx_usb_start_channel_control(struct cvmx_usb_state
*usb
,
1502 struct cvmx_usb_pipe
*pipe
)
1504 struct cvmx_usb_transaction
*transaction
=
1505 list_first_entry(&pipe
->transactions
, typeof(*transaction
),
1507 struct usb_ctrlrequest
*header
=
1508 cvmx_phys_to_ptr(transaction
->control_header
);
1509 int bytes_to_transfer
= transaction
->buffer_length
-
1510 transaction
->actual_bytes
;
1511 int packets_to_transfer
;
1512 union cvmx_usbcx_hctsizx usbc_hctsiz
;
1514 usbc_hctsiz
.u32
= __cvmx_usb_read_csr32(usb
,
1515 CVMX_USBCX_HCTSIZX(channel
, usb
->index
));
1517 switch (transaction
->stage
) {
1518 case CVMX_USB_STAGE_NON_CONTROL
:
1519 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE
:
1520 cvmx_dprintf("%s: ERROR - Non control stage\n", __func__
);
1522 case CVMX_USB_STAGE_SETUP
:
1523 usbc_hctsiz
.s
.pid
= 3; /* Setup */
1524 bytes_to_transfer
= sizeof(*header
);
1525 /* All Control operations start with a setup going OUT */
1526 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1527 union cvmx_usbcx_hccharx
, epdir
,
1528 CVMX_USB_DIRECTION_OUT
);
1530 * Setup send the control header instead of the buffer data. The
1531 * buffer data will be used in the next stage
1533 __cvmx_usb_write_csr64(usb
,
1534 CVMX_USBNX_DMA0_OUTB_CHN0(usb
->index
) + channel
*8,
1535 transaction
->control_header
);
1537 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE
:
1538 usbc_hctsiz
.s
.pid
= 3; /* Setup */
1539 bytes_to_transfer
= 0;
1540 /* All Control operations start with a setup going OUT */
1541 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1542 union cvmx_usbcx_hccharx
, epdir
,
1543 CVMX_USB_DIRECTION_OUT
);
1545 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel
, usb
->index
),
1546 union cvmx_usbcx_hcspltx
, compsplt
, 1);
1548 case CVMX_USB_STAGE_DATA
:
1549 usbc_hctsiz
.s
.pid
= __cvmx_usb_get_data_pid(pipe
);
1550 if (__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
1551 if (header
->bRequestType
& USB_DIR_IN
)
1552 bytes_to_transfer
= 0;
1553 else if (bytes_to_transfer
> pipe
->max_packet
)
1554 bytes_to_transfer
= pipe
->max_packet
;
1556 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1557 union cvmx_usbcx_hccharx
, epdir
,
1558 ((header
->bRequestType
& USB_DIR_IN
) ?
1559 CVMX_USB_DIRECTION_IN
:
1560 CVMX_USB_DIRECTION_OUT
));
1562 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE
:
1563 usbc_hctsiz
.s
.pid
= __cvmx_usb_get_data_pid(pipe
);
1564 if (!(header
->bRequestType
& USB_DIR_IN
))
1565 bytes_to_transfer
= 0;
1566 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1567 union cvmx_usbcx_hccharx
, epdir
,
1568 ((header
->bRequestType
& USB_DIR_IN
) ?
1569 CVMX_USB_DIRECTION_IN
:
1570 CVMX_USB_DIRECTION_OUT
));
1571 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel
, usb
->index
),
1572 union cvmx_usbcx_hcspltx
, compsplt
, 1);
1574 case CVMX_USB_STAGE_STATUS
:
1575 usbc_hctsiz
.s
.pid
= __cvmx_usb_get_data_pid(pipe
);
1576 bytes_to_transfer
= 0;
1577 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1578 union cvmx_usbcx_hccharx
, epdir
,
1579 ((header
->bRequestType
& USB_DIR_IN
) ?
1580 CVMX_USB_DIRECTION_OUT
:
1581 CVMX_USB_DIRECTION_IN
));
1583 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE
:
1584 usbc_hctsiz
.s
.pid
= __cvmx_usb_get_data_pid(pipe
);
1585 bytes_to_transfer
= 0;
1586 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1587 union cvmx_usbcx_hccharx
, epdir
,
1588 ((header
->bRequestType
& USB_DIR_IN
) ?
1589 CVMX_USB_DIRECTION_OUT
:
1590 CVMX_USB_DIRECTION_IN
));
1591 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel
, usb
->index
),
1592 union cvmx_usbcx_hcspltx
, compsplt
, 1);
1597 * Make sure the transfer never exceeds the byte limit of the hardware.
1598 * Further bytes will be sent as continued transactions
1600 if (bytes_to_transfer
> MAX_TRANSFER_BYTES
) {
1601 /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1602 bytes_to_transfer
= MAX_TRANSFER_BYTES
/ pipe
->max_packet
;
1603 bytes_to_transfer
*= pipe
->max_packet
;
1607 * Calculate the number of packets to transfer. If the length is zero
1608 * we still need to transfer one packet
1610 packets_to_transfer
= (bytes_to_transfer
+ pipe
->max_packet
- 1) /
1612 if (packets_to_transfer
== 0)
1613 packets_to_transfer
= 1;
1614 else if ((packets_to_transfer
> 1) &&
1615 (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
)) {
1617 * Limit to one packet when not using DMA. Channels must be
1618 * restarted between every packet for IN transactions, so there
1619 * is no reason to do multiple packets in a row
1621 packets_to_transfer
= 1;
1622 bytes_to_transfer
= packets_to_transfer
* pipe
->max_packet
;
1623 } else if (packets_to_transfer
> MAX_TRANSFER_PACKETS
) {
1625 * Limit the number of packet and data transferred to what the
1626 * hardware can handle
1628 packets_to_transfer
= MAX_TRANSFER_PACKETS
;
1629 bytes_to_transfer
= packets_to_transfer
* pipe
->max_packet
;
1632 usbc_hctsiz
.s
.xfersize
= bytes_to_transfer
;
1633 usbc_hctsiz
.s
.pktcnt
= packets_to_transfer
;
1635 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_HCTSIZX(channel
, usb
->index
),
1641 * Start a channel to perform the pipe's head transaction
1643 * @usb: USB device state populated by cvmx_usb_initialize().
1644 * @channel: Channel to setup
1645 * @pipe: Pipe to start
1647 static void __cvmx_usb_start_channel(struct cvmx_usb_state
*usb
,
1649 struct cvmx_usb_pipe
*pipe
)
1651 struct cvmx_usb_transaction
*transaction
=
1652 list_first_entry(&pipe
->transactions
, typeof(*transaction
),
1655 /* Make sure all writes to the DMA region get flushed */
1658 /* Attach the channel to the pipe */
1659 usb
->pipe_for_channel
[channel
] = pipe
;
1660 pipe
->channel
= channel
;
1661 pipe
->flags
|= __CVMX_USB_PIPE_FLAGS_SCHEDULED
;
1663 /* Mark this channel as in use */
1664 usb
->idle_hardware_channels
&= ~(1<<channel
);
1666 /* Enable the channel interrupt bits */
1668 union cvmx_usbcx_hcintx usbc_hcint
;
1669 union cvmx_usbcx_hcintmskx usbc_hcintmsk
;
1670 union cvmx_usbcx_haintmsk usbc_haintmsk
;
1672 /* Clear all channel status bits */
1673 usbc_hcint
.u32
= __cvmx_usb_read_csr32(usb
,
1674 CVMX_USBCX_HCINTX(channel
, usb
->index
));
1676 __cvmx_usb_write_csr32(usb
,
1677 CVMX_USBCX_HCINTX(channel
, usb
->index
),
1680 usbc_hcintmsk
.u32
= 0;
1681 usbc_hcintmsk
.s
.chhltdmsk
= 1;
1682 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
) {
1684 * Channels need these extra interrupts when we aren't
1687 usbc_hcintmsk
.s
.datatglerrmsk
= 1;
1688 usbc_hcintmsk
.s
.frmovrunmsk
= 1;
1689 usbc_hcintmsk
.s
.bblerrmsk
= 1;
1690 usbc_hcintmsk
.s
.xacterrmsk
= 1;
1691 if (__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
1693 * Splits don't generate xfercompl, so we need
1696 usbc_hcintmsk
.s
.nyetmsk
= 1;
1697 usbc_hcintmsk
.s
.ackmsk
= 1;
1699 usbc_hcintmsk
.s
.nakmsk
= 1;
1700 usbc_hcintmsk
.s
.stallmsk
= 1;
1701 usbc_hcintmsk
.s
.xfercomplmsk
= 1;
1703 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_HCINTMSKX(channel
, usb
->index
), usbc_hcintmsk
.u32
);
1705 /* Enable the channel interrupt to propagate */
1706 usbc_haintmsk
.u32
= __cvmx_usb_read_csr32(usb
,
1707 CVMX_USBCX_HAINTMSK(usb
->index
));
1708 usbc_haintmsk
.s
.haintmsk
|= 1<<channel
;
1709 __cvmx_usb_write_csr32(usb
,
1710 CVMX_USBCX_HAINTMSK(usb
->index
),
1714 /* Setup the locations the DMA engines use */
1716 uint64_t dma_address
= transaction
->buffer
+
1717 transaction
->actual_bytes
;
1719 if (transaction
->type
== CVMX_USB_TRANSFER_ISOCHRONOUS
)
1720 dma_address
= transaction
->buffer
+
1721 transaction
->iso_packets
[0].offset
+
1722 transaction
->actual_bytes
;
1724 __cvmx_usb_write_csr64(usb
,
1725 CVMX_USBNX_DMA0_OUTB_CHN0(usb
->index
) + channel
*8,
1728 __cvmx_usb_write_csr64(usb
,
1729 CVMX_USBNX_DMA0_INB_CHN0(usb
->index
) + channel
*8,
1733 /* Setup both the size of the transfer and the SPLIT characteristics */
1735 union cvmx_usbcx_hcspltx usbc_hcsplt
= {.u32
= 0};
1736 union cvmx_usbcx_hctsizx usbc_hctsiz
= {.u32
= 0};
1737 int packets_to_transfer
;
1738 int bytes_to_transfer
= transaction
->buffer_length
-
1739 transaction
->actual_bytes
;
1742 * ISOCHRONOUS transactions store each individual transfer size
1743 * in the packet structure, not the global buffer_length
1745 if (transaction
->type
== CVMX_USB_TRANSFER_ISOCHRONOUS
)
1747 transaction
->iso_packets
[0].length
-
1748 transaction
->actual_bytes
;
1751 * We need to do split transactions when we are talking to non
1752 * high speed devices that are behind a high speed hub
1754 if (__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
1756 * On the start split phase (stage is even) record the
1757 * frame number we will need to send the split complete.
1758 * We only store the lower two bits since the time ahead
1759 * can only be two frames
1761 if ((transaction
->stage
&1) == 0) {
1762 if (transaction
->type
== CVMX_USB_TRANSFER_BULK
)
1763 pipe
->split_sc_frame
=
1764 (usb
->frame_number
+ 1) & 0x7f;
1766 pipe
->split_sc_frame
=
1767 (usb
->frame_number
+ 2) & 0x7f;
1769 pipe
->split_sc_frame
= -1;
1771 usbc_hcsplt
.s
.spltena
= 1;
1772 usbc_hcsplt
.s
.hubaddr
= pipe
->hub_device_addr
;
1773 usbc_hcsplt
.s
.prtaddr
= pipe
->hub_port
;
1774 usbc_hcsplt
.s
.compsplt
= (transaction
->stage
==
1775 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE
);
1778 * SPLIT transactions can only ever transmit one data
1779 * packet so limit the transfer size to the max packet
1782 if (bytes_to_transfer
> pipe
->max_packet
)
1783 bytes_to_transfer
= pipe
->max_packet
;
1786 * ISOCHRONOUS OUT splits are unique in that they limit
1787 * data transfers to 188 byte chunks representing the
1788 * begin/middle/end of the data or all
1790 if (!usbc_hcsplt
.s
.compsplt
&&
1791 (pipe
->transfer_dir
==
1792 CVMX_USB_DIRECTION_OUT
) &&
1793 (pipe
->transfer_type
==
1794 CVMX_USB_TRANSFER_ISOCHRONOUS
)) {
1796 * Clear the split complete frame number as
1797 * there isn't going to be a split complete
1799 pipe
->split_sc_frame
= -1;
1801 * See if we've started this transfer and sent
1804 if (transaction
->actual_bytes
== 0) {
1806 * Nothing sent yet, this is either a
1807 * begin or the entire payload
1809 if (bytes_to_transfer
<= 188)
1810 /* Entire payload in one go */
1811 usbc_hcsplt
.s
.xactpos
= 3;
1813 /* First part of payload */
1814 usbc_hcsplt
.s
.xactpos
= 2;
1817 * Continuing the previous data, we must
1818 * either be in the middle or at the end
1820 if (bytes_to_transfer
<= 188)
1821 /* End of payload */
1822 usbc_hcsplt
.s
.xactpos
= 1;
1824 /* Middle of payload */
1825 usbc_hcsplt
.s
.xactpos
= 0;
1828 * Again, the transfer size is limited to 188
1831 if (bytes_to_transfer
> 188)
1832 bytes_to_transfer
= 188;
1837 * Make sure the transfer never exceeds the byte limit of the
1838 * hardware. Further bytes will be sent as continued
1841 if (bytes_to_transfer
> MAX_TRANSFER_BYTES
) {
1843 * Round MAX_TRANSFER_BYTES to a multiple of out packet
1846 bytes_to_transfer
= MAX_TRANSFER_BYTES
/
1848 bytes_to_transfer
*= pipe
->max_packet
;
1852 * Calculate the number of packets to transfer. If the length is
1853 * zero we still need to transfer one packet
1855 packets_to_transfer
=
1856 (bytes_to_transfer
+ pipe
->max_packet
- 1) /
1858 if (packets_to_transfer
== 0)
1859 packets_to_transfer
= 1;
1860 else if ((packets_to_transfer
> 1) &&
1862 CVMX_USB_INITIALIZE_FLAGS_NO_DMA
)) {
1864 * Limit to one packet when not using DMA. Channels must
1865 * be restarted between every packet for IN
1866 * transactions, so there is no reason to do multiple
1869 packets_to_transfer
= 1;
1870 bytes_to_transfer
= packets_to_transfer
*
1872 } else if (packets_to_transfer
> MAX_TRANSFER_PACKETS
) {
1874 * Limit the number of packet and data transferred to
1875 * what the hardware can handle
1877 packets_to_transfer
= MAX_TRANSFER_PACKETS
;
1878 bytes_to_transfer
= packets_to_transfer
*
1882 usbc_hctsiz
.s
.xfersize
= bytes_to_transfer
;
1883 usbc_hctsiz
.s
.pktcnt
= packets_to_transfer
;
1885 /* Update the DATA0/DATA1 toggle */
1886 usbc_hctsiz
.s
.pid
= __cvmx_usb_get_data_pid(pipe
);
1888 * High speed pipes may need a hardware ping before they start
1890 if (pipe
->flags
& __CVMX_USB_PIPE_FLAGS_NEED_PING
)
1891 usbc_hctsiz
.s
.dopng
= 1;
1893 __cvmx_usb_write_csr32(usb
,
1894 CVMX_USBCX_HCSPLTX(channel
, usb
->index
),
1896 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_HCTSIZX(channel
,
1897 usb
->index
), usbc_hctsiz
.u32
);
1900 /* Setup the Host Channel Characteristics Register */
1902 union cvmx_usbcx_hccharx usbc_hcchar
= {.u32
= 0};
1905 * Set the startframe odd/even properly. This is only used for
1908 usbc_hcchar
.s
.oddfrm
= usb
->frame_number
&1;
1911 * Set the number of back to back packets allowed by this
1912 * endpoint. Split transactions interpret "ec" as the number of
1913 * immediate retries of failure. These retries happen too
1914 * quickly, so we disable these entirely for splits
1916 if (__cvmx_usb_pipe_needs_split(usb
, pipe
))
1917 usbc_hcchar
.s
.ec
= 1;
1918 else if (pipe
->multi_count
< 1)
1919 usbc_hcchar
.s
.ec
= 1;
1920 else if (pipe
->multi_count
> 3)
1921 usbc_hcchar
.s
.ec
= 3;
1923 usbc_hcchar
.s
.ec
= pipe
->multi_count
;
1925 /* Set the rest of the endpoint specific settings */
1926 usbc_hcchar
.s
.devaddr
= pipe
->device_addr
;
1927 usbc_hcchar
.s
.eptype
= transaction
->type
;
1928 usbc_hcchar
.s
.lspddev
=
1929 (pipe
->device_speed
== CVMX_USB_SPEED_LOW
);
1930 usbc_hcchar
.s
.epdir
= pipe
->transfer_dir
;
1931 usbc_hcchar
.s
.epnum
= pipe
->endpoint_num
;
1932 usbc_hcchar
.s
.mps
= pipe
->max_packet
;
1933 __cvmx_usb_write_csr32(usb
,
1934 CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1938 /* Do transaction type specific fixups as needed */
1939 switch (transaction
->type
) {
1940 case CVMX_USB_TRANSFER_CONTROL
:
1941 __cvmx_usb_start_channel_control(usb
, channel
, pipe
);
1943 case CVMX_USB_TRANSFER_BULK
:
1944 case CVMX_USB_TRANSFER_INTERRUPT
:
1946 case CVMX_USB_TRANSFER_ISOCHRONOUS
:
1947 if (!__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
1949 * ISO transactions require different PIDs depending on
1950 * direction and how many packets are needed
1952 if (pipe
->transfer_dir
== CVMX_USB_DIRECTION_OUT
) {
1953 if (pipe
->multi_count
< 2) /* Need DATA0 */
1955 CVMX_USBCX_HCTSIZX(channel
,
1957 union cvmx_usbcx_hctsizx
,
1959 else /* Need MDATA */
1961 CVMX_USBCX_HCTSIZX(channel
,
1963 union cvmx_usbcx_hctsizx
,
1970 union cvmx_usbcx_hctsizx usbc_hctsiz
= {.u32
=
1971 __cvmx_usb_read_csr32(usb
,
1972 CVMX_USBCX_HCTSIZX(channel
, usb
->index
))};
1973 transaction
->xfersize
= usbc_hctsiz
.s
.xfersize
;
1974 transaction
->pktcnt
= usbc_hctsiz
.s
.pktcnt
;
1976 /* Remeber when we start a split transaction */
1977 if (__cvmx_usb_pipe_needs_split(usb
, pipe
))
1978 usb
->active_split
= transaction
;
1979 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel
, usb
->index
),
1980 union cvmx_usbcx_hccharx
, chena
, 1);
1981 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
)
1982 __cvmx_usb_fill_tx_fifo(usb
, channel
);
1987 * Find a pipe that is ready to be scheduled to hardware.
1988 * @usb: USB device state populated by cvmx_usb_initialize().
1989 * @list: Pipe list to search
1991 * Frame counter to use as a time reference.
1993 * Returns: Pipe or NULL if none are ready
1995 static struct cvmx_usb_pipe
*__cvmx_usb_find_ready_pipe(
1996 struct cvmx_usb_state
*usb
,
1997 struct list_head
*list
,
1998 uint64_t current_frame
)
2000 struct cvmx_usb_pipe
*pipe
;
2002 list_for_each_entry(pipe
, list
, node
) {
2003 struct cvmx_usb_transaction
*t
=
2004 list_first_entry(&pipe
->transactions
, typeof(*t
),
2006 if (!(pipe
->flags
& __CVMX_USB_PIPE_FLAGS_SCHEDULED
) && t
&&
2007 (pipe
->next_tx_frame
<= current_frame
) &&
2008 ((pipe
->split_sc_frame
== -1) ||
2009 ((((int)current_frame
- (int)pipe
->split_sc_frame
)
2011 (!usb
->active_split
|| (usb
->active_split
== t
))) {
2021 * Called whenever a pipe might need to be scheduled to the
2024 * @usb: USB device state populated by cvmx_usb_initialize().
2025 * @is_sof: True if this schedule was called on a SOF interrupt.
2027 static void __cvmx_usb_schedule(struct cvmx_usb_state
*usb
, int is_sof
)
2030 struct cvmx_usb_pipe
*pipe
;
2032 enum cvmx_usb_transfer ttype
;
2034 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
) {
2036 * Without DMA we need to be careful to not schedule something
2037 * at the end of a frame and cause an overrun.
2039 union cvmx_usbcx_hfnum hfnum
= {
2040 .u32
= __cvmx_usb_read_csr32(usb
,
2041 CVMX_USBCX_HFNUM(usb
->index
))
2044 union cvmx_usbcx_hfir hfir
= {
2045 .u32
= __cvmx_usb_read_csr32(usb
,
2046 CVMX_USBCX_HFIR(usb
->index
))
2049 if (hfnum
.s
.frrem
< hfir
.s
.frint
/4)
2053 while (usb
->idle_hardware_channels
) {
2054 /* Find an idle channel */
2055 channel
= __fls(usb
->idle_hardware_channels
);
2056 if (unlikely(channel
> 7))
2059 /* Find a pipe needing service */
2063 * Only process periodic pipes on SOF interrupts. This
2064 * way we are sure that the periodic data is sent in the
2065 * beginning of the frame
2067 pipe
= __cvmx_usb_find_ready_pipe(usb
,
2069 CVMX_USB_TRANSFER_ISOCHRONOUS
,
2072 pipe
= __cvmx_usb_find_ready_pipe(usb
,
2074 CVMX_USB_TRANSFER_INTERRUPT
,
2077 if (likely(!pipe
)) {
2078 pipe
= __cvmx_usb_find_ready_pipe(usb
,
2080 CVMX_USB_TRANSFER_CONTROL
,
2083 pipe
= __cvmx_usb_find_ready_pipe(usb
,
2085 CVMX_USB_TRANSFER_BULK
,
2091 __cvmx_usb_start_channel(usb
, channel
, pipe
);
2096 * Only enable SOF interrupts when we have transactions pending in the
2097 * future that might need to be scheduled
2100 for (ttype
= CVMX_USB_TRANSFER_CONTROL
;
2101 ttype
<= CVMX_USB_TRANSFER_INTERRUPT
; ttype
++) {
2102 list_for_each_entry(pipe
, &usb
->active_pipes
[ttype
], node
) {
2103 if (pipe
->next_tx_frame
> usb
->frame_number
) {
2109 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb
->index
),
2110 union cvmx_usbcx_gintmsk
, sofmsk
, need_sof
);
2113 static inline struct octeon_hcd
*cvmx_usb_to_octeon(struct cvmx_usb_state
*p
)
2115 return container_of(p
, struct octeon_hcd
, usb
);
2118 static inline struct usb_hcd
*octeon_to_hcd(struct octeon_hcd
*p
)
2120 return container_of((void *)p
, struct usb_hcd
, hcd_priv
);
2123 static void octeon_usb_urb_complete_callback(struct cvmx_usb_state
*usb
,
2124 enum cvmx_usb_complete status
,
2125 struct cvmx_usb_pipe
*pipe
,
2126 struct cvmx_usb_transaction
2128 int bytes_transferred
,
2131 struct octeon_hcd
*priv
= cvmx_usb_to_octeon(usb
);
2132 struct usb_hcd
*hcd
= octeon_to_hcd(priv
);
2133 struct device
*dev
= hcd
->self
.controller
;
2135 if (likely(status
== CVMX_USB_COMPLETE_SUCCESS
))
2136 urb
->actual_length
= bytes_transferred
;
2138 urb
->actual_length
= 0;
2142 /* For Isochronous transactions we need to update the URB packet status
2143 list from data in our private copy */
2144 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
2147 * The pointer to the private list is stored in the setup_packet
2150 struct cvmx_usb_iso_packet
*iso_packet
=
2151 (struct cvmx_usb_iso_packet
*) urb
->setup_packet
;
2152 /* Recalculate the transfer size by adding up each packet */
2153 urb
->actual_length
= 0;
2154 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
2155 if (iso_packet
[i
].status
==
2156 CVMX_USB_COMPLETE_SUCCESS
) {
2157 urb
->iso_frame_desc
[i
].status
= 0;
2158 urb
->iso_frame_desc
[i
].actual_length
=
2159 iso_packet
[i
].length
;
2160 urb
->actual_length
+=
2161 urb
->iso_frame_desc
[i
].actual_length
;
2163 dev_dbg(dev
, "ISOCHRONOUS packet=%d of %d status=%d pipe=%p transaction=%p size=%d\n",
2164 i
, urb
->number_of_packets
,
2165 iso_packet
[i
].status
, pipe
,
2166 transaction
, iso_packet
[i
].length
);
2167 urb
->iso_frame_desc
[i
].status
= -EREMOTEIO
;
2170 /* Free the private list now that we don't need it anymore */
2172 urb
->setup_packet
= NULL
;
2176 case CVMX_USB_COMPLETE_SUCCESS
:
2179 case CVMX_USB_COMPLETE_CANCEL
:
2180 if (urb
->status
== 0)
2181 urb
->status
= -ENOENT
;
2183 case CVMX_USB_COMPLETE_STALL
:
2184 dev_dbg(dev
, "status=stall pipe=%p transaction=%p size=%d\n",
2185 pipe
, transaction
, bytes_transferred
);
2186 urb
->status
= -EPIPE
;
2188 case CVMX_USB_COMPLETE_BABBLEERR
:
2189 dev_dbg(dev
, "status=babble pipe=%p transaction=%p size=%d\n",
2190 pipe
, transaction
, bytes_transferred
);
2191 urb
->status
= -EPIPE
;
2193 case CVMX_USB_COMPLETE_SHORT
:
2194 dev_dbg(dev
, "status=short pipe=%p transaction=%p size=%d\n",
2195 pipe
, transaction
, bytes_transferred
);
2196 urb
->status
= -EREMOTEIO
;
2198 case CVMX_USB_COMPLETE_ERROR
:
2199 case CVMX_USB_COMPLETE_XACTERR
:
2200 case CVMX_USB_COMPLETE_DATATGLERR
:
2201 case CVMX_USB_COMPLETE_FRAMEERR
:
2202 dev_dbg(dev
, "status=%d pipe=%p transaction=%p size=%d\n",
2203 status
, pipe
, transaction
, bytes_transferred
);
2204 urb
->status
= -EPROTO
;
2207 usb_hcd_unlink_urb_from_ep(octeon_to_hcd(priv
), urb
);
2208 spin_unlock(&priv
->lock
);
2209 usb_hcd_giveback_urb(octeon_to_hcd(priv
), urb
, urb
->status
);
2210 spin_lock(&priv
->lock
);
2214 * Signal the completion of a transaction and free it. The
2215 * transaction will be removed from the pipe transaction list.
2217 * @usb: USB device state populated by cvmx_usb_initialize().
2218 * @pipe: Pipe the transaction is on
2220 * Transaction that completed
2224 static void __cvmx_usb_perform_complete(
2225 struct cvmx_usb_state
*usb
,
2226 struct cvmx_usb_pipe
*pipe
,
2227 struct cvmx_usb_transaction
*transaction
,
2228 enum cvmx_usb_complete complete_code
)
2230 /* If this was a split then clear our split in progress marker */
2231 if (usb
->active_split
== transaction
)
2232 usb
->active_split
= NULL
;
2235 * Isochronous transactions need extra processing as they might not be
2236 * done after a single data transfer
2238 if (unlikely(transaction
->type
== CVMX_USB_TRANSFER_ISOCHRONOUS
)) {
2239 /* Update the number of bytes transferred in this ISO packet */
2240 transaction
->iso_packets
[0].length
= transaction
->actual_bytes
;
2241 transaction
->iso_packets
[0].status
= complete_code
;
2244 * If there are more ISOs pending and we succeeded, schedule the
2247 if ((transaction
->iso_number_packets
> 1) &&
2248 (complete_code
== CVMX_USB_COMPLETE_SUCCESS
)) {
2249 /* No bytes transferred for this packet as of yet */
2250 transaction
->actual_bytes
= 0;
2251 /* One less ISO waiting to transfer */
2252 transaction
->iso_number_packets
--;
2253 /* Increment to the next location in our packet array */
2254 transaction
->iso_packets
++;
2255 transaction
->stage
= CVMX_USB_STAGE_NON_CONTROL
;
2260 /* Remove the transaction from the pipe list */
2261 list_del(&transaction
->node
);
2262 if (list_empty(&pipe
->transactions
))
2263 list_move_tail(&pipe
->node
, &usb
->idle_pipes
);
2264 octeon_usb_urb_complete_callback(usb
, complete_code
, pipe
,
2266 transaction
->actual_bytes
,
2273 * Submit a usb transaction to a pipe. Called for all types
2277 * @pipe: Which pipe to submit to.
2278 * @type: Transaction type
2279 * @buffer: User buffer for the transaction
2281 * User buffer's length in bytes
2283 * For control transactions, the 8 byte standard header
2285 * For ISO transactions, the start frame
2286 * @iso_number_packets:
2287 * For ISO, the number of packet in the transaction.
2289 * A description of each ISO packet
2290 * @urb: URB for the callback
2292 * Returns: Transaction or NULL on failure.
2294 static struct cvmx_usb_transaction
*__cvmx_usb_submit_transaction(
2295 struct cvmx_usb_state
*usb
,
2296 struct cvmx_usb_pipe
*pipe
,
2297 enum cvmx_usb_transfer type
,
2300 uint64_t control_header
,
2301 int iso_start_frame
,
2302 int iso_number_packets
,
2303 struct cvmx_usb_iso_packet
*iso_packets
,
2306 struct cvmx_usb_transaction
*transaction
;
2308 if (unlikely(pipe
->transfer_type
!= type
))
2311 transaction
= kzalloc(sizeof(*transaction
), GFP_ATOMIC
);
2312 if (unlikely(!transaction
))
2315 transaction
->type
= type
;
2316 transaction
->buffer
= buffer
;
2317 transaction
->buffer_length
= buffer_length
;
2318 transaction
->control_header
= control_header
;
2319 /* FIXME: This is not used, implement it. */
2320 transaction
->iso_start_frame
= iso_start_frame
;
2321 transaction
->iso_number_packets
= iso_number_packets
;
2322 transaction
->iso_packets
= iso_packets
;
2323 transaction
->urb
= urb
;
2324 if (transaction
->type
== CVMX_USB_TRANSFER_CONTROL
)
2325 transaction
->stage
= CVMX_USB_STAGE_SETUP
;
2327 transaction
->stage
= CVMX_USB_STAGE_NON_CONTROL
;
2329 if (!list_empty(&pipe
->transactions
)) {
2330 list_add_tail(&transaction
->node
, &pipe
->transactions
);
2332 list_add_tail(&transaction
->node
, &pipe
->transactions
);
2333 list_move_tail(&pipe
->node
,
2334 &usb
->active_pipes
[pipe
->transfer_type
]);
2337 * We may need to schedule the pipe if this was the head of the
2340 __cvmx_usb_schedule(usb
, 0);
2348 * Call to submit a USB Bulk transfer to a pipe.
2350 * @usb: USB device state populated by cvmx_usb_initialize().
2351 * @pipe: Handle to the pipe for the transfer.
2354 * Returns: A submitted transaction or NULL on failure.
2356 static struct cvmx_usb_transaction
*cvmx_usb_submit_bulk(
2357 struct cvmx_usb_state
*usb
,
2358 struct cvmx_usb_pipe
*pipe
,
2361 return __cvmx_usb_submit_transaction(usb
, pipe
, CVMX_USB_TRANSFER_BULK
,
2363 urb
->transfer_buffer_length
,
2364 0, /* control_header */
2365 0, /* iso_start_frame */
2366 0, /* iso_number_packets */
2367 NULL
, /* iso_packets */
2373 * Call to submit a USB Interrupt transfer to a pipe.
2375 * @usb: USB device state populated by cvmx_usb_initialize().
2376 * @pipe: Handle to the pipe for the transfer.
2377 * @urb: URB returned when the callback is called.
2379 * Returns: A submitted transaction or NULL on failure.
2381 static struct cvmx_usb_transaction
*cvmx_usb_submit_interrupt(
2382 struct cvmx_usb_state
*usb
,
2383 struct cvmx_usb_pipe
*pipe
,
2386 return __cvmx_usb_submit_transaction(usb
, pipe
,
2387 CVMX_USB_TRANSFER_INTERRUPT
,
2389 urb
->transfer_buffer_length
,
2390 0, /* control_header */
2391 0, /* iso_start_frame */
2392 0, /* iso_number_packets */
2393 NULL
, /* iso_packets */
2399 * Call to submit a USB Control transfer to a pipe.
2401 * @usb: USB device state populated by cvmx_usb_initialize().
2402 * @pipe: Handle to the pipe for the transfer.
2405 * Returns: A submitted transaction or NULL on failure.
2407 static struct cvmx_usb_transaction
*cvmx_usb_submit_control(
2408 struct cvmx_usb_state
*usb
,
2409 struct cvmx_usb_pipe
*pipe
,
2412 int buffer_length
= urb
->transfer_buffer_length
;
2413 uint64_t control_header
= urb
->setup_dma
;
2414 struct usb_ctrlrequest
*header
= cvmx_phys_to_ptr(control_header
);
2416 if ((header
->bRequestType
& USB_DIR_IN
) == 0)
2417 buffer_length
= le16_to_cpu(header
->wLength
);
2419 return __cvmx_usb_submit_transaction(usb
, pipe
,
2420 CVMX_USB_TRANSFER_CONTROL
,
2421 urb
->transfer_dma
, buffer_length
,
2423 0, /* iso_start_frame */
2424 0, /* iso_number_packets */
2425 NULL
, /* iso_packets */
2431 * Call to submit a USB Isochronous transfer to a pipe.
2433 * @usb: USB device state populated by cvmx_usb_initialize().
2434 * @pipe: Handle to the pipe for the transfer.
2435 * @urb: URB returned when the callback is called.
2437 * Returns: A submitted transaction or NULL on failure.
2439 static struct cvmx_usb_transaction
*cvmx_usb_submit_isochronous(
2440 struct cvmx_usb_state
*usb
,
2441 struct cvmx_usb_pipe
*pipe
,
2444 struct cvmx_usb_iso_packet
*packets
;
2446 packets
= (struct cvmx_usb_iso_packet
*) urb
->setup_packet
;
2447 return __cvmx_usb_submit_transaction(usb
, pipe
,
2448 CVMX_USB_TRANSFER_ISOCHRONOUS
,
2450 urb
->transfer_buffer_length
,
2451 0, /* control_header */
2453 urb
->number_of_packets
,
2459 * Cancel one outstanding request in a pipe. Canceling a request
2460 * can fail if the transaction has already completed before cancel
2461 * is called. Even after a successful cancel call, it may take
2462 * a frame or two for the cvmx_usb_poll() function to call the
2463 * associated callback.
2465 * @usb: USB device state populated by cvmx_usb_initialize().
2466 * @pipe: Pipe to cancel requests in.
2467 * @transaction: Transaction to cancel, returned by the submit function.
2469 * Returns: 0 or a negative error code.
2471 static int cvmx_usb_cancel(struct cvmx_usb_state
*usb
,
2472 struct cvmx_usb_pipe
*pipe
,
2473 struct cvmx_usb_transaction
*transaction
)
2476 * If the transaction is the HEAD of the queue and scheduled. We need to
2479 if (list_first_entry(&pipe
->transactions
, typeof(*transaction
), node
) ==
2480 transaction
&& (pipe
->flags
& __CVMX_USB_PIPE_FLAGS_SCHEDULED
)) {
2481 union cvmx_usbcx_hccharx usbc_hcchar
;
2483 usb
->pipe_for_channel
[pipe
->channel
] = NULL
;
2484 pipe
->flags
&= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED
;
2488 usbc_hcchar
.u32
= __cvmx_usb_read_csr32(usb
,
2489 CVMX_USBCX_HCCHARX(pipe
->channel
, usb
->index
));
2491 * If the channel isn't enabled then the transaction already
2494 if (usbc_hcchar
.s
.chena
) {
2495 usbc_hcchar
.s
.chdis
= 1;
2496 __cvmx_usb_write_csr32(usb
,
2497 CVMX_USBCX_HCCHARX(pipe
->channel
,
2502 __cvmx_usb_perform_complete(usb
, pipe
, transaction
,
2503 CVMX_USB_COMPLETE_CANCEL
);
2509 * Cancel all outstanding requests in a pipe. Logically all this
2510 * does is call cvmx_usb_cancel() in a loop.
2512 * @usb: USB device state populated by cvmx_usb_initialize().
2513 * @pipe: Pipe to cancel requests in.
2515 * Returns: 0 or a negative error code.
2517 static int cvmx_usb_cancel_all(struct cvmx_usb_state
*usb
,
2518 struct cvmx_usb_pipe
*pipe
)
2520 struct cvmx_usb_transaction
*transaction
, *next
;
2522 /* Simply loop through and attempt to cancel each transaction */
2523 list_for_each_entry_safe(transaction
, next
, &pipe
->transactions
, node
) {
2524 int result
= cvmx_usb_cancel(usb
, pipe
, transaction
);
2526 if (unlikely(result
!= 0))
2534 * Close a pipe created with cvmx_usb_open_pipe().
2536 * @usb: USB device state populated by cvmx_usb_initialize().
2537 * @pipe: Pipe to close.
2539 * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
2540 * outstanding transfers.
2542 static int cvmx_usb_close_pipe(struct cvmx_usb_state
*usb
,
2543 struct cvmx_usb_pipe
*pipe
)
2545 /* Fail if the pipe has pending transactions */
2546 if (!list_empty(&pipe
->transactions
))
2549 list_del(&pipe
->node
);
2556 * Get the current USB protocol level frame number. The frame
2557 * number is always in the range of 0-0x7ff.
2559 * @usb: USB device state populated by cvmx_usb_initialize().
2561 * Returns: USB frame number
2563 static int cvmx_usb_get_frame_number(struct cvmx_usb_state
*usb
)
2566 union cvmx_usbcx_hfnum usbc_hfnum
;
2568 usbc_hfnum
.u32
= __cvmx_usb_read_csr32(usb
,
2569 CVMX_USBCX_HFNUM(usb
->index
));
2570 frame_number
= usbc_hfnum
.s
.frnum
;
2572 return frame_number
;
2577 * Poll a channel for status
2580 * @channel: Channel to poll
2582 * Returns: Zero on success
2584 static int __cvmx_usb_poll_channel(struct cvmx_usb_state
*usb
, int channel
)
2586 union cvmx_usbcx_hcintx usbc_hcint
;
2587 union cvmx_usbcx_hctsizx usbc_hctsiz
;
2588 union cvmx_usbcx_hccharx usbc_hcchar
;
2589 struct cvmx_usb_pipe
*pipe
;
2590 struct cvmx_usb_transaction
*transaction
;
2591 int bytes_this_transfer
;
2592 int bytes_in_last_packet
;
2593 int packets_processed
;
2594 int buffer_space_left
;
2596 /* Read the interrupt status bits for the channel */
2597 usbc_hcint
.u32
= __cvmx_usb_read_csr32(usb
,
2598 CVMX_USBCX_HCINTX(channel
, usb
->index
));
2600 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
) {
2601 usbc_hcchar
.u32
= __cvmx_usb_read_csr32(usb
,
2602 CVMX_USBCX_HCCHARX(channel
, usb
->index
));
2604 if (usbc_hcchar
.s
.chena
&& usbc_hcchar
.s
.chdis
) {
2606 * There seems to be a bug in CN31XX which can cause
2607 * interrupt IN transfers to get stuck until we do a
2608 * write of HCCHARX without changing things
2610 __cvmx_usb_write_csr32(usb
,
2611 CVMX_USBCX_HCCHARX(channel
,
2618 * In non DMA mode the channels don't halt themselves. We need
2619 * to manually disable channels that are left running
2621 if (!usbc_hcint
.s
.chhltd
) {
2622 if (usbc_hcchar
.s
.chena
) {
2623 union cvmx_usbcx_hcintmskx hcintmsk
;
2624 /* Disable all interrupts except CHHLTD */
2626 hcintmsk
.s
.chhltdmsk
= 1;
2627 __cvmx_usb_write_csr32(usb
,
2628 CVMX_USBCX_HCINTMSKX(channel
,
2631 usbc_hcchar
.s
.chdis
= 1;
2632 __cvmx_usb_write_csr32(usb
,
2633 CVMX_USBCX_HCCHARX(channel
,
2637 } else if (usbc_hcint
.s
.xfercompl
) {
2639 * Successful IN/OUT with transfer complete.
2640 * Channel halt isn't needed.
2643 cvmx_dprintf("USB%d: Channel %d interrupt without halt\n",
2644 usb
->index
, channel
);
2650 * There is are no interrupts that we need to process when the
2651 * channel is still running
2653 if (!usbc_hcint
.s
.chhltd
)
2657 /* Disable the channel interrupts now that it is done */
2658 __cvmx_usb_write_csr32(usb
,
2659 CVMX_USBCX_HCINTMSKX(channel
, usb
->index
),
2661 usb
->idle_hardware_channels
|= (1<<channel
);
2663 /* Make sure this channel is tied to a valid pipe */
2664 pipe
= usb
->pipe_for_channel
[channel
];
2668 transaction
= list_first_entry(&pipe
->transactions
,
2669 typeof(*transaction
),
2671 prefetch(transaction
);
2674 * Disconnect this pipe from the HW channel. Later the schedule
2675 * function will figure out which pipe needs to go
2677 usb
->pipe_for_channel
[channel
] = NULL
;
2678 pipe
->flags
&= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED
;
2681 * Read the channel config info so we can figure out how much data
2684 usbc_hcchar
.u32
= __cvmx_usb_read_csr32(usb
,
2685 CVMX_USBCX_HCCHARX(channel
, usb
->index
));
2686 usbc_hctsiz
.u32
= __cvmx_usb_read_csr32(usb
,
2687 CVMX_USBCX_HCTSIZX(channel
, usb
->index
));
2690 * Calculating the number of bytes successfully transferred is dependent
2691 * on the transfer direction
2693 packets_processed
= transaction
->pktcnt
- usbc_hctsiz
.s
.pktcnt
;
2694 if (usbc_hcchar
.s
.epdir
) {
2696 * IN transactions are easy. For every byte received the
2697 * hardware decrements xfersize. All we need to do is subtract
2698 * the current value of xfersize from its starting value and we
2699 * know how many bytes were written to the buffer
2701 bytes_this_transfer
= transaction
->xfersize
-
2702 usbc_hctsiz
.s
.xfersize
;
2705 * OUT transaction don't decrement xfersize. Instead pktcnt is
2706 * decremented on every successful packet send. The hardware
2707 * does this when it receives an ACK, or NYET. If it doesn't
2708 * receive one of these responses pktcnt doesn't change
2710 bytes_this_transfer
= packets_processed
* usbc_hcchar
.s
.mps
;
2712 * The last packet may not be a full transfer if we didn't have
2715 if (bytes_this_transfer
> transaction
->xfersize
)
2716 bytes_this_transfer
= transaction
->xfersize
;
2718 /* Figure out how many bytes were in the last packet of the transfer */
2719 if (packets_processed
)
2720 bytes_in_last_packet
= bytes_this_transfer
-
2721 (packets_processed
- 1) * usbc_hcchar
.s
.mps
;
2723 bytes_in_last_packet
= bytes_this_transfer
;
2726 * As a special case, setup transactions output the setup header, not
2727 * the user's data. For this reason we don't count setup data as bytes
2730 if ((transaction
->stage
== CVMX_USB_STAGE_SETUP
) ||
2731 (transaction
->stage
== CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE
))
2732 bytes_this_transfer
= 0;
2735 * Add the bytes transferred to the running total. It is important that
2736 * bytes_this_transfer doesn't count any data that needs to be
2739 transaction
->actual_bytes
+= bytes_this_transfer
;
2740 if (transaction
->type
== CVMX_USB_TRANSFER_ISOCHRONOUS
)
2741 buffer_space_left
= transaction
->iso_packets
[0].length
-
2742 transaction
->actual_bytes
;
2744 buffer_space_left
= transaction
->buffer_length
-
2745 transaction
->actual_bytes
;
2748 * We need to remember the PID toggle state for the next transaction.
2749 * The hardware already updated it for the next transaction
2751 pipe
->pid_toggle
= !(usbc_hctsiz
.s
.pid
== 0);
2754 * For high speed bulk out, assume the next transaction will need to do
2755 * a ping before proceeding. If this isn't true the ACK processing below
2756 * will clear this flag
2758 if ((pipe
->device_speed
== CVMX_USB_SPEED_HIGH
) &&
2759 (pipe
->transfer_type
== CVMX_USB_TRANSFER_BULK
) &&
2760 (pipe
->transfer_dir
== CVMX_USB_DIRECTION_OUT
))
2761 pipe
->flags
|= __CVMX_USB_PIPE_FLAGS_NEED_PING
;
2763 if (usbc_hcint
.s
.stall
) {
2765 * STALL as a response means this transaction cannot be
2766 * completed because the device can't process transactions. Tell
2767 * the user. Any data that was transferred will be counted on
2768 * the actual bytes transferred
2770 pipe
->pid_toggle
= 0;
2771 __cvmx_usb_perform_complete(usb
, pipe
, transaction
,
2772 CVMX_USB_COMPLETE_STALL
);
2773 } else if (usbc_hcint
.s
.xacterr
) {
2775 * We know at least one packet worked if we get a ACK or NAK.
2776 * Reset the retry counter
2778 if (usbc_hcint
.s
.nak
|| usbc_hcint
.s
.ack
)
2779 transaction
->retries
= 0;
2780 transaction
->retries
++;
2781 if (transaction
->retries
> MAX_RETRIES
) {
2783 * XactErr as a response means the device signaled
2784 * something wrong with the transfer. For example, PID
2785 * toggle errors cause these
2787 __cvmx_usb_perform_complete(usb
, pipe
, transaction
,
2788 CVMX_USB_COMPLETE_XACTERR
);
2791 * If this was a split then clear our split in progress
2794 if (usb
->active_split
== transaction
)
2795 usb
->active_split
= NULL
;
2797 * Rewind to the beginning of the transaction by anding
2798 * off the split complete bit
2800 transaction
->stage
&= ~1;
2801 pipe
->split_sc_frame
= -1;
2802 pipe
->next_tx_frame
+= pipe
->interval
;
2803 if (pipe
->next_tx_frame
< usb
->frame_number
)
2804 pipe
->next_tx_frame
=
2805 usb
->frame_number
+ pipe
->interval
-
2806 (usb
->frame_number
-
2807 pipe
->next_tx_frame
) % pipe
->interval
;
2809 } else if (usbc_hcint
.s
.bblerr
) {
2810 /* Babble Error (BblErr) */
2811 __cvmx_usb_perform_complete(usb
, pipe
, transaction
,
2812 CVMX_USB_COMPLETE_BABBLEERR
);
2813 } else if (usbc_hcint
.s
.datatglerr
) {
2814 /* Data toggle error */
2815 __cvmx_usb_perform_complete(usb
, pipe
, transaction
,
2816 CVMX_USB_COMPLETE_DATATGLERR
);
2817 } else if (usbc_hcint
.s
.nyet
) {
2819 * NYET as a response is only allowed in three cases: as a
2820 * response to a ping, as a response to a split transaction, and
2821 * as a response to a bulk out. The ping case is handled by
2822 * hardware, so we only have splits and bulk out
2824 if (!__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
2825 transaction
->retries
= 0;
2827 * If there is more data to go then we need to try
2828 * again. Otherwise this transaction is complete
2830 if ((buffer_space_left
== 0) ||
2831 (bytes_in_last_packet
< pipe
->max_packet
))
2832 __cvmx_usb_perform_complete(usb
, pipe
,
2834 CVMX_USB_COMPLETE_SUCCESS
);
2837 * Split transactions retry the split complete 4 times
2838 * then rewind to the start split and do the entire
2839 * transactions again
2841 transaction
->retries
++;
2842 if ((transaction
->retries
& 0x3) == 0) {
2844 * Rewind to the beginning of the transaction by
2845 * anding off the split complete bit
2847 transaction
->stage
&= ~1;
2848 pipe
->split_sc_frame
= -1;
2851 } else if (usbc_hcint
.s
.ack
) {
2852 transaction
->retries
= 0;
2854 * The ACK bit can only be checked after the other error bits.
2855 * This is because a multi packet transfer may succeed in a
2856 * number of packets and then get a different response on the
2857 * last packet. In this case both ACK and the last response bit
2858 * will be set. If none of the other response bits is set, then
2859 * the last packet must have been an ACK
2861 * Since we got an ACK, we know we don't need to do a ping on
2864 pipe
->flags
&= ~__CVMX_USB_PIPE_FLAGS_NEED_PING
;
2866 switch (transaction
->type
) {
2867 case CVMX_USB_TRANSFER_CONTROL
:
2868 switch (transaction
->stage
) {
2869 case CVMX_USB_STAGE_NON_CONTROL
:
2870 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE
:
2871 /* This should be impossible */
2872 __cvmx_usb_perform_complete(usb
, pipe
,
2873 transaction
, CVMX_USB_COMPLETE_ERROR
);
2875 case CVMX_USB_STAGE_SETUP
:
2876 pipe
->pid_toggle
= 1;
2877 if (__cvmx_usb_pipe_needs_split(usb
, pipe
))
2878 transaction
->stage
=
2879 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE
;
2881 struct usb_ctrlrequest
*header
=
2882 cvmx_phys_to_ptr(transaction
->control_header
);
2883 if (header
->wLength
)
2884 transaction
->stage
= CVMX_USB_STAGE_DATA
;
2886 transaction
->stage
= CVMX_USB_STAGE_STATUS
;
2889 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE
:
2891 struct usb_ctrlrequest
*header
=
2892 cvmx_phys_to_ptr(transaction
->control_header
);
2893 if (header
->wLength
)
2894 transaction
->stage
= CVMX_USB_STAGE_DATA
;
2896 transaction
->stage
= CVMX_USB_STAGE_STATUS
;
2899 case CVMX_USB_STAGE_DATA
:
2900 if (__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
2901 transaction
->stage
=
2902 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE
;
2904 * For setup OUT data that are splits,
2905 * the hardware doesn't appear to count
2906 * transferred data. Here we manually
2907 * update the data transferred
2909 if (!usbc_hcchar
.s
.epdir
) {
2910 if (buffer_space_left
< pipe
->max_packet
)
2911 transaction
->actual_bytes
+=
2914 transaction
->actual_bytes
+=
2917 } else if ((buffer_space_left
== 0) ||
2918 (bytes_in_last_packet
<
2919 pipe
->max_packet
)) {
2920 pipe
->pid_toggle
= 1;
2921 transaction
->stage
=
2922 CVMX_USB_STAGE_STATUS
;
2925 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE
:
2926 if ((buffer_space_left
== 0) ||
2927 (bytes_in_last_packet
<
2928 pipe
->max_packet
)) {
2929 pipe
->pid_toggle
= 1;
2930 transaction
->stage
=
2931 CVMX_USB_STAGE_STATUS
;
2933 transaction
->stage
=
2934 CVMX_USB_STAGE_DATA
;
2937 case CVMX_USB_STAGE_STATUS
:
2938 if (__cvmx_usb_pipe_needs_split(usb
, pipe
))
2939 transaction
->stage
=
2940 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE
;
2942 __cvmx_usb_perform_complete(usb
, pipe
,
2944 CVMX_USB_COMPLETE_SUCCESS
);
2946 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE
:
2947 __cvmx_usb_perform_complete(usb
, pipe
,
2949 CVMX_USB_COMPLETE_SUCCESS
);
2953 case CVMX_USB_TRANSFER_BULK
:
2954 case CVMX_USB_TRANSFER_INTERRUPT
:
2956 * The only time a bulk transfer isn't complete when it
2957 * finishes with an ACK is during a split transaction.
2958 * For splits we need to continue the transfer if more
2961 if (__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
2962 if (transaction
->stage
==
2963 CVMX_USB_STAGE_NON_CONTROL
)
2964 transaction
->stage
=
2965 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE
;
2967 if (buffer_space_left
&&
2968 (bytes_in_last_packet
==
2970 transaction
->stage
=
2971 CVMX_USB_STAGE_NON_CONTROL
;
2973 if (transaction
->type
==
2974 CVMX_USB_TRANSFER_INTERRUPT
)
2975 pipe
->next_tx_frame
+=
2977 __cvmx_usb_perform_complete(
2981 CVMX_USB_COMPLETE_SUCCESS
);
2985 if ((pipe
->device_speed
==
2986 CVMX_USB_SPEED_HIGH
) &&
2987 (pipe
->transfer_type
==
2988 CVMX_USB_TRANSFER_BULK
) &&
2989 (pipe
->transfer_dir
==
2990 CVMX_USB_DIRECTION_OUT
) &&
2993 __CVMX_USB_PIPE_FLAGS_NEED_PING
;
2994 if (!buffer_space_left
||
2995 (bytes_in_last_packet
<
2996 pipe
->max_packet
)) {
2997 if (transaction
->type
==
2998 CVMX_USB_TRANSFER_INTERRUPT
)
2999 pipe
->next_tx_frame
+=
3001 __cvmx_usb_perform_complete(usb
,
3004 CVMX_USB_COMPLETE_SUCCESS
);
3008 case CVMX_USB_TRANSFER_ISOCHRONOUS
:
3009 if (__cvmx_usb_pipe_needs_split(usb
, pipe
)) {
3011 * ISOCHRONOUS OUT splits don't require a
3012 * complete split stage. Instead they use a
3013 * sequence of begin OUT splits to transfer the
3014 * data 188 bytes at a time. Once the transfer
3015 * is complete, the pipe sleeps until the next
3018 if (pipe
->transfer_dir
== CVMX_USB_DIRECTION_OUT
) {
3020 * If no space left or this wasn't a max
3021 * size packet then this transfer is
3022 * complete. Otherwise start it again to
3023 * send the next 188 bytes
3025 if (!buffer_space_left
||
3026 (bytes_this_transfer
< 188)) {
3027 pipe
->next_tx_frame
+=
3029 __cvmx_usb_perform_complete(
3033 CVMX_USB_COMPLETE_SUCCESS
);
3036 if (transaction
->stage
==
3037 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE
) {
3039 * We are in the incoming data
3040 * phase. Keep getting data
3041 * until we run out of space or
3042 * get a small packet
3044 if ((buffer_space_left
== 0) ||
3045 (bytes_in_last_packet
<
3046 pipe
->max_packet
)) {
3047 pipe
->next_tx_frame
+=
3049 __cvmx_usb_perform_complete(
3053 CVMX_USB_COMPLETE_SUCCESS
);
3056 transaction
->stage
=
3057 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE
;
3060 pipe
->next_tx_frame
+= pipe
->interval
;
3061 __cvmx_usb_perform_complete(usb
,
3064 CVMX_USB_COMPLETE_SUCCESS
);
3068 } else if (usbc_hcint
.s
.nak
) {
3070 * If this was a split then clear our split in progress marker.
3072 if (usb
->active_split
== transaction
)
3073 usb
->active_split
= NULL
;
3075 * NAK as a response means the device couldn't accept the
3076 * transaction, but it should be retried in the future. Rewind
3077 * to the beginning of the transaction by anding off the split
3078 * complete bit. Retry in the next interval
3080 transaction
->retries
= 0;
3081 transaction
->stage
&= ~1;
3082 pipe
->next_tx_frame
+= pipe
->interval
;
3083 if (pipe
->next_tx_frame
< usb
->frame_number
)
3084 pipe
->next_tx_frame
= usb
->frame_number
+
3086 (usb
->frame_number
- pipe
->next_tx_frame
) %
3089 struct cvmx_usb_port_status port
;
3091 port
= cvmx_usb_get_status(usb
);
3092 if (port
.port_enabled
) {
3093 /* We'll retry the exact same transaction again */
3094 transaction
->retries
++;
3097 * We get channel halted interrupts with no result bits
3098 * sets when the cable is unplugged
3100 __cvmx_usb_perform_complete(usb
, pipe
, transaction
,
3101 CVMX_USB_COMPLETE_ERROR
);
3107 static void octeon_usb_port_callback(struct cvmx_usb_state
*usb
)
3109 struct octeon_hcd
*priv
= cvmx_usb_to_octeon(usb
);
3111 spin_unlock(&priv
->lock
);
3112 usb_hcd_poll_rh_status(octeon_to_hcd(priv
));
3113 spin_lock(&priv
->lock
);
3117 * Poll the USB block for status and call all needed callback
3118 * handlers. This function is meant to be called in the interrupt
3119 * handler for the USB controller. It can also be called
3120 * periodically in a loop for non-interrupt based operation.
3122 * @usb: USB device state populated by cvmx_usb_initialize().
3124 * Returns: 0 or a negative error code.
3126 static int cvmx_usb_poll(struct cvmx_usb_state
*usb
)
3128 union cvmx_usbcx_hfnum usbc_hfnum
;
3129 union cvmx_usbcx_gintsts usbc_gintsts
;
3131 prefetch_range(usb
, sizeof(*usb
));
3133 /* Update the frame counter */
3134 usbc_hfnum
.u32
= __cvmx_usb_read_csr32(usb
,
3135 CVMX_USBCX_HFNUM(usb
->index
));
3136 if ((usb
->frame_number
&0x3fff) > usbc_hfnum
.s
.frnum
)
3137 usb
->frame_number
+= 0x4000;
3138 usb
->frame_number
&= ~0x3fffull
;
3139 usb
->frame_number
|= usbc_hfnum
.s
.frnum
;
3141 /* Read the pending interrupts */
3142 usbc_gintsts
.u32
= __cvmx_usb_read_csr32(usb
,
3143 CVMX_USBCX_GINTSTS(usb
->index
));
3145 /* Clear the interrupts now that we know about them */
3146 __cvmx_usb_write_csr32(usb
,
3147 CVMX_USBCX_GINTSTS(usb
->index
),
3150 if (usbc_gintsts
.s
.rxflvl
) {
3152 * RxFIFO Non-Empty (RxFLvl)
3153 * Indicates that there is at least one packet pending to be
3154 * read from the RxFIFO.
3156 * In DMA mode this is handled by hardware
3158 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
)
3159 __cvmx_usb_poll_rx_fifo(usb
);
3161 if (usbc_gintsts
.s
.ptxfemp
|| usbc_gintsts
.s
.nptxfemp
) {
3162 /* Fill the Tx FIFOs when not in DMA mode */
3163 if (usb
->init_flags
& CVMX_USB_INITIALIZE_FLAGS_NO_DMA
)
3164 __cvmx_usb_poll_tx_fifo(usb
);
3166 if (usbc_gintsts
.s
.disconnint
|| usbc_gintsts
.s
.prtint
) {
3167 union cvmx_usbcx_hprt usbc_hprt
;
3169 * Disconnect Detected Interrupt (DisconnInt)
3170 * Asserted when a device disconnect is detected.
3172 * Host Port Interrupt (PrtInt)
3173 * The core sets this bit to indicate a change in port status of
3174 * one of the O2P USB core ports in Host mode. The application
3175 * must read the Host Port Control and Status (HPRT) register to
3176 * determine the exact event that caused this interrupt. The
3177 * application must clear the appropriate status bit in the Host
3178 * Port Control and Status register to clear this bit.
3180 * Call the user's port callback
3182 octeon_usb_port_callback(usb
);
3183 /* Clear the port change bits */
3184 usbc_hprt
.u32
= __cvmx_usb_read_csr32(usb
,
3185 CVMX_USBCX_HPRT(usb
->index
));
3186 usbc_hprt
.s
.prtena
= 0;
3187 __cvmx_usb_write_csr32(usb
, CVMX_USBCX_HPRT(usb
->index
),
3190 if (usbc_gintsts
.s
.hchint
) {
3192 * Host Channels Interrupt (HChInt)
3193 * The core sets this bit to indicate that an interrupt is
3194 * pending on one of the channels of the core (in Host mode).
3195 * The application must read the Host All Channels Interrupt
3196 * (HAINT) register to determine the exact number of the channel
3197 * on which the interrupt occurred, and then read the
3198 * corresponding Host Channel-n Interrupt (HCINTn) register to
3199 * determine the exact cause of the interrupt. The application
3200 * must clear the appropriate status bit in the HCINTn register
3201 * to clear this bit.
3203 union cvmx_usbcx_haint usbc_haint
;
3205 usbc_haint
.u32
= __cvmx_usb_read_csr32(usb
,
3206 CVMX_USBCX_HAINT(usb
->index
));
3207 while (usbc_haint
.u32
) {
3210 channel
= __fls(usbc_haint
.u32
);
3211 __cvmx_usb_poll_channel(usb
, channel
);
3212 usbc_haint
.u32
^= 1<<channel
;
3216 __cvmx_usb_schedule(usb
, usbc_gintsts
.s
.sof
);
3221 /* convert between an HCD pointer and the corresponding struct octeon_hcd */
3222 static inline struct octeon_hcd
*hcd_to_octeon(struct usb_hcd
*hcd
)
3224 return (struct octeon_hcd
*)(hcd
->hcd_priv
);
3227 static irqreturn_t
octeon_usb_irq(struct usb_hcd
*hcd
)
3229 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3230 unsigned long flags
;
3232 spin_lock_irqsave(&priv
->lock
, flags
);
3233 cvmx_usb_poll(&priv
->usb
);
3234 spin_unlock_irqrestore(&priv
->lock
, flags
);
3238 static int octeon_usb_start(struct usb_hcd
*hcd
)
3240 hcd
->state
= HC_STATE_RUNNING
;
3244 static void octeon_usb_stop(struct usb_hcd
*hcd
)
3246 hcd
->state
= HC_STATE_HALT
;
3249 static int octeon_usb_get_frame_number(struct usb_hcd
*hcd
)
3251 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3253 return cvmx_usb_get_frame_number(&priv
->usb
);
3256 static int octeon_usb_urb_enqueue(struct usb_hcd
*hcd
,
3260 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3261 struct device
*dev
= hcd
->self
.controller
;
3262 struct cvmx_usb_transaction
*transaction
= NULL
;
3263 struct cvmx_usb_pipe
*pipe
;
3264 unsigned long flags
;
3265 struct cvmx_usb_iso_packet
*iso_packet
;
3266 struct usb_host_endpoint
*ep
= urb
->ep
;
3270 spin_lock_irqsave(&priv
->lock
, flags
);
3272 rc
= usb_hcd_link_urb_to_ep(hcd
, urb
);
3274 spin_unlock_irqrestore(&priv
->lock
, flags
);
3279 enum cvmx_usb_transfer transfer_type
;
3280 enum cvmx_usb_speed speed
;
3281 int split_device
= 0;
3284 switch (usb_pipetype(urb
->pipe
)) {
3285 case PIPE_ISOCHRONOUS
:
3286 transfer_type
= CVMX_USB_TRANSFER_ISOCHRONOUS
;
3288 case PIPE_INTERRUPT
:
3289 transfer_type
= CVMX_USB_TRANSFER_INTERRUPT
;
3292 transfer_type
= CVMX_USB_TRANSFER_CONTROL
;
3295 transfer_type
= CVMX_USB_TRANSFER_BULK
;
3298 switch (urb
->dev
->speed
) {
3300 speed
= CVMX_USB_SPEED_LOW
;
3302 case USB_SPEED_FULL
:
3303 speed
= CVMX_USB_SPEED_FULL
;
3306 speed
= CVMX_USB_SPEED_HIGH
;
3310 * For slow devices on high speed ports we need to find the hub
3311 * that does the speed translation so we know where to send the
3312 * split transactions.
3314 if (speed
!= CVMX_USB_SPEED_HIGH
) {
3316 * Start at this device and work our way up the usb
3319 struct usb_device
*dev
= urb
->dev
;
3321 while (dev
->parent
) {
3323 * If our parent is high speed then he'll
3324 * receive the splits.
3326 if (dev
->parent
->speed
== USB_SPEED_HIGH
) {
3327 split_device
= dev
->parent
->devnum
;
3328 split_port
= dev
->portnum
;
3332 * Move up the tree one level. If we make it all
3333 * the way up the tree, then the port must not
3334 * be in high speed mode and we don't need a
3340 pipe
= cvmx_usb_open_pipe(&priv
->usb
, usb_pipedevice(urb
->pipe
),
3341 usb_pipeendpoint(urb
->pipe
), speed
,
3342 le16_to_cpu(ep
->desc
.wMaxPacketSize
)
3345 usb_pipein(urb
->pipe
) ?
3346 CVMX_USB_DIRECTION_IN
:
3347 CVMX_USB_DIRECTION_OUT
,
3349 (le16_to_cpu(ep
->desc
.wMaxPacketSize
)
3351 split_device
, split_port
);
3353 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
3354 spin_unlock_irqrestore(&priv
->lock
, flags
);
3355 dev_dbg(dev
, "Failed to create pipe\n");
3363 switch (usb_pipetype(urb
->pipe
)) {
3364 case PIPE_ISOCHRONOUS
:
3365 dev_dbg(dev
, "Submit isochronous to %d.%d\n",
3366 usb_pipedevice(urb
->pipe
),
3367 usb_pipeendpoint(urb
->pipe
));
3369 * Allocate a structure to use for our private list of
3370 * isochronous packets.
3372 iso_packet
= kmalloc(urb
->number_of_packets
*
3373 sizeof(struct cvmx_usb_iso_packet
),
3377 /* Fill the list with the data from the URB */
3378 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
3379 iso_packet
[i
].offset
=
3380 urb
->iso_frame_desc
[i
].offset
;
3381 iso_packet
[i
].length
=
3382 urb
->iso_frame_desc
[i
].length
;
3383 iso_packet
[i
].status
=
3384 CVMX_USB_COMPLETE_ERROR
;
3387 * Store a pointer to the list in the URB setup_packet
3388 * field. We know this currently isn't being used and
3389 * this saves us a bunch of logic.
3391 urb
->setup_packet
= (char *)iso_packet
;
3392 transaction
= cvmx_usb_submit_isochronous(&priv
->usb
,
3395 * If submit failed we need to free our private packet
3399 urb
->setup_packet
= NULL
;
3404 case PIPE_INTERRUPT
:
3405 dev_dbg(dev
, "Submit interrupt to %d.%d\n",
3406 usb_pipedevice(urb
->pipe
),
3407 usb_pipeendpoint(urb
->pipe
));
3408 transaction
= cvmx_usb_submit_interrupt(&priv
->usb
, pipe
, urb
);
3411 dev_dbg(dev
, "Submit control to %d.%d\n",
3412 usb_pipedevice(urb
->pipe
),
3413 usb_pipeendpoint(urb
->pipe
));
3414 transaction
= cvmx_usb_submit_control(&priv
->usb
, pipe
, urb
);
3417 dev_dbg(dev
, "Submit bulk to %d.%d\n",
3418 usb_pipedevice(urb
->pipe
),
3419 usb_pipeendpoint(urb
->pipe
));
3420 transaction
= cvmx_usb_submit_bulk(&priv
->usb
, pipe
, urb
);
3424 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
3425 spin_unlock_irqrestore(&priv
->lock
, flags
);
3426 dev_dbg(dev
, "Failed to submit\n");
3429 urb
->hcpriv
= transaction
;
3430 spin_unlock_irqrestore(&priv
->lock
, flags
);
3434 static int octeon_usb_urb_dequeue(struct usb_hcd
*hcd
,
3438 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3439 unsigned long flags
;
3445 spin_lock_irqsave(&priv
->lock
, flags
);
3447 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
3451 urb
->status
= status
;
3452 cvmx_usb_cancel(&priv
->usb
, urb
->ep
->hcpriv
, urb
->hcpriv
);
3455 spin_unlock_irqrestore(&priv
->lock
, flags
);
3460 static void octeon_usb_endpoint_disable(struct usb_hcd
*hcd
,
3461 struct usb_host_endpoint
*ep
)
3463 struct device
*dev
= hcd
->self
.controller
;
3466 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3467 struct cvmx_usb_pipe
*pipe
= ep
->hcpriv
;
3468 unsigned long flags
;
3470 spin_lock_irqsave(&priv
->lock
, flags
);
3471 cvmx_usb_cancel_all(&priv
->usb
, pipe
);
3472 if (cvmx_usb_close_pipe(&priv
->usb
, pipe
))
3473 dev_dbg(dev
, "Closing pipe %p failed\n", pipe
);
3474 spin_unlock_irqrestore(&priv
->lock
, flags
);
3479 static int octeon_usb_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
3481 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3482 struct cvmx_usb_port_status port_status
;
3483 unsigned long flags
;
3485 spin_lock_irqsave(&priv
->lock
, flags
);
3486 port_status
= cvmx_usb_get_status(&priv
->usb
);
3487 spin_unlock_irqrestore(&priv
->lock
, flags
);
3489 buf
[0] = port_status
.connect_change
<< 1;
3494 static int octeon_usb_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
3495 u16 wIndex
, char *buf
, u16 wLength
)
3497 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3498 struct device
*dev
= hcd
->self
.controller
;
3499 struct cvmx_usb_port_status usb_port_status
;
3501 struct usb_hub_descriptor
*desc
;
3502 unsigned long flags
;
3505 case ClearHubFeature
:
3506 dev_dbg(dev
, "ClearHubFeature\n");
3508 case C_HUB_LOCAL_POWER
:
3509 case C_HUB_OVER_CURRENT
:
3510 /* Nothing required here */
3516 case ClearPortFeature
:
3517 dev_dbg(dev
, "ClearPortFeature\n");
3519 dev_dbg(dev
, " INVALID\n");
3524 case USB_PORT_FEAT_ENABLE
:
3525 dev_dbg(dev
, " ENABLE\n");
3526 spin_lock_irqsave(&priv
->lock
, flags
);
3527 cvmx_usb_disable(&priv
->usb
);
3528 spin_unlock_irqrestore(&priv
->lock
, flags
);
3530 case USB_PORT_FEAT_SUSPEND
:
3531 dev_dbg(dev
, " SUSPEND\n");
3532 /* Not supported on Octeon */
3534 case USB_PORT_FEAT_POWER
:
3535 dev_dbg(dev
, " POWER\n");
3536 /* Not supported on Octeon */
3538 case USB_PORT_FEAT_INDICATOR
:
3539 dev_dbg(dev
, " INDICATOR\n");
3540 /* Port inidicator not supported */
3542 case USB_PORT_FEAT_C_CONNECTION
:
3543 dev_dbg(dev
, " C_CONNECTION\n");
3544 /* Clears drivers internal connect status change flag */
3545 spin_lock_irqsave(&priv
->lock
, flags
);
3546 priv
->usb
.port_status
=
3547 cvmx_usb_get_status(&priv
->usb
);
3548 spin_unlock_irqrestore(&priv
->lock
, flags
);
3550 case USB_PORT_FEAT_C_RESET
:
3551 dev_dbg(dev
, " C_RESET\n");
3553 * Clears the driver's internal Port Reset Change flag.
3555 spin_lock_irqsave(&priv
->lock
, flags
);
3556 priv
->usb
.port_status
=
3557 cvmx_usb_get_status(&priv
->usb
);
3558 spin_unlock_irqrestore(&priv
->lock
, flags
);
3560 case USB_PORT_FEAT_C_ENABLE
:
3561 dev_dbg(dev
, " C_ENABLE\n");
3563 * Clears the driver's internal Port Enable/Disable
3566 spin_lock_irqsave(&priv
->lock
, flags
);
3567 priv
->usb
.port_status
=
3568 cvmx_usb_get_status(&priv
->usb
);
3569 spin_unlock_irqrestore(&priv
->lock
, flags
);
3571 case USB_PORT_FEAT_C_SUSPEND
:
3572 dev_dbg(dev
, " C_SUSPEND\n");
3574 * Clears the driver's internal Port Suspend Change
3575 * flag, which is set when resume signaling on the host
3579 case USB_PORT_FEAT_C_OVER_CURRENT
:
3580 dev_dbg(dev
, " C_OVER_CURRENT\n");
3581 /* Clears the driver's overcurrent Change flag */
3582 spin_lock_irqsave(&priv
->lock
, flags
);
3583 priv
->usb
.port_status
=
3584 cvmx_usb_get_status(&priv
->usb
);
3585 spin_unlock_irqrestore(&priv
->lock
, flags
);
3588 dev_dbg(dev
, " UNKNOWN\n");
3592 case GetHubDescriptor
:
3593 dev_dbg(dev
, "GetHubDescriptor\n");
3594 desc
= (struct usb_hub_descriptor
*)buf
;
3595 desc
->bDescLength
= 9;
3596 desc
->bDescriptorType
= 0x29;
3597 desc
->bNbrPorts
= 1;
3598 desc
->wHubCharacteristics
= cpu_to_le16(0x08);
3599 desc
->bPwrOn2PwrGood
= 1;
3600 desc
->bHubContrCurrent
= 0;
3601 desc
->u
.hs
.DeviceRemovable
[0] = 0;
3602 desc
->u
.hs
.DeviceRemovable
[1] = 0xff;
3605 dev_dbg(dev
, "GetHubStatus\n");
3606 *(__le32
*) buf
= 0;
3609 dev_dbg(dev
, "GetPortStatus\n");
3611 dev_dbg(dev
, " INVALID\n");
3615 spin_lock_irqsave(&priv
->lock
, flags
);
3616 usb_port_status
= cvmx_usb_get_status(&priv
->usb
);
3617 spin_unlock_irqrestore(&priv
->lock
, flags
);
3620 if (usb_port_status
.connect_change
) {
3621 port_status
|= (1 << USB_PORT_FEAT_C_CONNECTION
);
3622 dev_dbg(dev
, " C_CONNECTION\n");
3625 if (usb_port_status
.port_enabled
) {
3626 port_status
|= (1 << USB_PORT_FEAT_C_ENABLE
);
3627 dev_dbg(dev
, " C_ENABLE\n");
3630 if (usb_port_status
.connected
) {
3631 port_status
|= (1 << USB_PORT_FEAT_CONNECTION
);
3632 dev_dbg(dev
, " CONNECTION\n");
3635 if (usb_port_status
.port_enabled
) {
3636 port_status
|= (1 << USB_PORT_FEAT_ENABLE
);
3637 dev_dbg(dev
, " ENABLE\n");
3640 if (usb_port_status
.port_over_current
) {
3641 port_status
|= (1 << USB_PORT_FEAT_OVER_CURRENT
);
3642 dev_dbg(dev
, " OVER_CURRENT\n");
3645 if (usb_port_status
.port_powered
) {
3646 port_status
|= (1 << USB_PORT_FEAT_POWER
);
3647 dev_dbg(dev
, " POWER\n");
3650 if (usb_port_status
.port_speed
== CVMX_USB_SPEED_HIGH
) {
3651 port_status
|= USB_PORT_STAT_HIGH_SPEED
;
3652 dev_dbg(dev
, " HIGHSPEED\n");
3653 } else if (usb_port_status
.port_speed
== CVMX_USB_SPEED_LOW
) {
3654 port_status
|= (1 << USB_PORT_FEAT_LOWSPEED
);
3655 dev_dbg(dev
, " LOWSPEED\n");
3658 *((__le32
*) buf
) = cpu_to_le32(port_status
);
3661 dev_dbg(dev
, "SetHubFeature\n");
3662 /* No HUB features supported */
3664 case SetPortFeature
:
3665 dev_dbg(dev
, "SetPortFeature\n");
3667 dev_dbg(dev
, " INVALID\n");
3672 case USB_PORT_FEAT_SUSPEND
:
3673 dev_dbg(dev
, " SUSPEND\n");
3675 case USB_PORT_FEAT_POWER
:
3676 dev_dbg(dev
, " POWER\n");
3678 case USB_PORT_FEAT_RESET
:
3679 dev_dbg(dev
, " RESET\n");
3680 spin_lock_irqsave(&priv
->lock
, flags
);
3681 cvmx_usb_disable(&priv
->usb
);
3682 if (cvmx_usb_enable(&priv
->usb
))
3683 dev_dbg(dev
, "Failed to enable the port\n");
3684 spin_unlock_irqrestore(&priv
->lock
, flags
);
3686 case USB_PORT_FEAT_INDICATOR
:
3687 dev_dbg(dev
, " INDICATOR\n");
3691 dev_dbg(dev
, " UNKNOWN\n");
3696 dev_dbg(dev
, "Unknown root hub request\n");
3702 static const struct hc_driver octeon_hc_driver
= {
3703 .description
= "Octeon USB",
3704 .product_desc
= "Octeon Host Controller",
3705 .hcd_priv_size
= sizeof(struct octeon_hcd
),
3706 .irq
= octeon_usb_irq
,
3707 .flags
= HCD_MEMORY
| HCD_USB2
,
3708 .start
= octeon_usb_start
,
3709 .stop
= octeon_usb_stop
,
3710 .urb_enqueue
= octeon_usb_urb_enqueue
,
3711 .urb_dequeue
= octeon_usb_urb_dequeue
,
3712 .endpoint_disable
= octeon_usb_endpoint_disable
,
3713 .get_frame_number
= octeon_usb_get_frame_number
,
3714 .hub_status_data
= octeon_usb_hub_status_data
,
3715 .hub_control
= octeon_usb_hub_control
,
3716 .map_urb_for_dma
= octeon_map_urb_for_dma
,
3717 .unmap_urb_for_dma
= octeon_unmap_urb_for_dma
,
3720 static int octeon_usb_probe(struct platform_device
*pdev
)
3723 int initialize_flags
;
3725 struct resource
*res_mem
;
3726 struct device_node
*usbn_node
;
3727 int irq
= platform_get_irq(pdev
, 0);
3728 struct device
*dev
= &pdev
->dev
;
3729 struct octeon_hcd
*priv
;
3730 struct usb_hcd
*hcd
;
3731 unsigned long flags
;
3732 u32 clock_rate
= 48000000;
3733 bool is_crystal_clock
= false;
3734 const char *clock_type
;
3737 if (dev
->of_node
== NULL
) {
3738 dev_err(dev
, "Error: empty of_node\n");
3741 usbn_node
= dev
->of_node
->parent
;
3743 i
= of_property_read_u32(usbn_node
,
3744 "refclk-frequency", &clock_rate
);
3746 dev_err(dev
, "No USBN \"refclk-frequency\"\n");
3749 switch (clock_rate
) {
3751 initialize_flags
= CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ
;
3754 initialize_flags
= CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ
;
3757 initialize_flags
= CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ
;
3760 dev_err(dev
, "Illebal USBN \"refclk-frequency\" %u\n",
3766 i
= of_property_read_string(usbn_node
,
3767 "refclk-type", &clock_type
);
3769 if (!i
&& strcmp("crystal", clock_type
) == 0)
3770 is_crystal_clock
= true;
3772 if (is_crystal_clock
)
3773 initialize_flags
|= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI
;
3775 initialize_flags
|= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND
;
3777 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3778 if (res_mem
== NULL
) {
3779 dev_err(dev
, "found no memory resource\n");
3782 usb_num
= (res_mem
->start
>> 44) & 1;
3785 /* Defective device tree, but we know how to fix it. */
3786 irq_hw_number_t hwirq
= usb_num
? (1 << 6) + 17 : 56;
3788 irq
= irq_create_mapping(NULL
, hwirq
);
3792 * Set the DMA mask to 64bits so we get buffers already translated for
3795 dev
->coherent_dma_mask
= ~0;
3796 dev
->dma_mask
= &dev
->coherent_dma_mask
;
3799 * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
3800 * IOB priority registers. Under heavy network load USB
3801 * hardware can be starved by the IOB causing a crash. Give
3802 * it a priority boost if it has been waiting more than 400
3803 * cycles to avoid this situation.
3805 * Testing indicates that a cnt_val of 8192 is not sufficient,
3806 * but no failures are seen with 4096. We choose a value of
3807 * 400 to give a safety factor of 10.
3809 if (OCTEON_IS_MODEL(OCTEON_CN52XX
) || OCTEON_IS_MODEL(OCTEON_CN56XX
)) {
3810 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt
;
3813 pri_cnt
.s
.cnt_enb
= 1;
3814 pri_cnt
.s
.cnt_val
= 400;
3815 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT
, pri_cnt
.u64
);
3818 hcd
= usb_create_hcd(&octeon_hc_driver
, dev
, dev_name(dev
));
3820 dev_dbg(dev
, "Failed to allocate memory for HCD\n");
3823 hcd
->uses_new_polling
= 1;
3824 priv
= (struct octeon_hcd
*)hcd
->hcd_priv
;
3826 spin_lock_init(&priv
->lock
);
3828 status
= cvmx_usb_initialize(&priv
->usb
, usb_num
, initialize_flags
);
3830 dev_dbg(dev
, "USB initialization failed with %d\n", status
);
3835 /* This delay is needed for CN3010, but I don't know why... */
3838 spin_lock_irqsave(&priv
->lock
, flags
);
3839 cvmx_usb_poll(&priv
->usb
);
3840 spin_unlock_irqrestore(&priv
->lock
, flags
);
3842 status
= usb_add_hcd(hcd
, irq
, 0);
3844 dev_dbg(dev
, "USB add HCD failed with %d\n", status
);
3848 device_wakeup_enable(hcd
->self
.controller
);
3850 dev_info(dev
, "Registered HCD for port %d on irq %d\n", usb_num
, irq
);
3855 static int octeon_usb_remove(struct platform_device
*pdev
)
3858 struct device
*dev
= &pdev
->dev
;
3859 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
3860 struct octeon_hcd
*priv
= hcd_to_octeon(hcd
);
3861 unsigned long flags
;
3863 usb_remove_hcd(hcd
);
3864 spin_lock_irqsave(&priv
->lock
, flags
);
3865 status
= cvmx_usb_shutdown(&priv
->usb
);
3866 spin_unlock_irqrestore(&priv
->lock
, flags
);
3868 dev_dbg(dev
, "USB shutdown failed with %d\n", status
);
3875 static struct of_device_id octeon_usb_match
[] = {
3877 .compatible
= "cavium,octeon-5750-usbc",
3882 static struct platform_driver octeon_usb_driver
= {
3884 .name
= "OcteonUSB",
3885 .owner
= THIS_MODULE
,
3886 .of_match_table
= octeon_usb_match
,
3888 .probe
= octeon_usb_probe
,
3889 .remove
= octeon_usb_remove
,
3892 static int __init
octeon_usb_driver_init(void)
3897 return platform_driver_register(&octeon_usb_driver
);
3899 module_init(octeon_usb_driver_init
);
3901 static void __exit
octeon_usb_driver_exit(void)
3906 platform_driver_unregister(&octeon_usb_driver
);
3908 module_exit(octeon_usb_driver_exit
);
3910 MODULE_LICENSE("GPL");
3911 MODULE_AUTHOR("Cavium, Inc. <support@cavium.com>");
3912 MODULE_DESCRIPTION("Cavium Inc. OCTEON USB Host driver.");