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
235 * The low level hardware can transfer a maximum of this number of bytes in each
236 * transfer. The field is 19 bits wide
238 #define MAX_TRANSFER_BYTES ((1<<19)-1)
241 * The low level hardware can transfer a maximum of this number of packets in
242 * each transfer. The field is 10 bits wide
244 #define MAX_TRANSFER_PACKETS ((1<<10)-1)
247 * Logical transactions may take numerous low level
248 * transactions, especially when splits are concerned. This
249 * enum represents all of the possible stages a transaction can
250 * be in. Note that split completes are always even. This is so
251 * the NAK handler can backup to the previous low level
252 * transaction with a simple clearing of bit 0.
254 enum cvmx_usb_stage {
255 CVMX_USB_STAGE_NON_CONTROL,
256 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
257 CVMX_USB_STAGE_SETUP,
258 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
260 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
261 CVMX_USB_STAGE_STATUS,
262 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
266 * struct cvmx_usb_transaction - describes each pending USB transaction
267 * regardless of type. These are linked together
268 * to form a list of pending requests for a pipe.
270 * @node: List node for transactions in the pipe.
271 * @type: Type of transaction, duplicated of the pipe.
272 * @flags: State flags for this transaction.
273 * @buffer: User's physical buffer address to read/write.
274 * @buffer_length: Size of the user's buffer in bytes.
275 * @control_header: For control transactions, physical address of the 8
276 * byte standard header.
277 * @iso_start_frame: For ISO transactions, the starting frame number.
278 * @iso_number_packets: For ISO transactions, the number of packets in the
280 * @iso_packets: For ISO transactions, the sub packets in the request.
281 * @actual_bytes: Actual bytes transfer for this transaction.
282 * @stage: For control transactions, the current stage.
285 struct cvmx_usb_transaction {
286 struct list_head node;
287 enum cvmx_usb_transfer type;
290 uint64_t control_header;
292 int iso_number_packets;
293 struct cvmx_usb_iso_packet *iso_packets;
298 enum cvmx_usb_stage stage;
303 * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
304 * and some USB device. It contains a list of pending
305 * request to the device.
307 * @node: List node for pipe list
308 * @next: Pipe after this one in the list
309 * @transactions: List of pending transactions
310 * @interval: For periodic pipes, the interval between packets in
312 * @next_tx_frame: The next frame this pipe is allowed to transmit on
313 * @flags: State flags for this pipe
314 * @device_speed: Speed of device connected to this pipe
315 * @transfer_type: Type of transaction supported by this pipe
316 * @transfer_dir: IN or OUT. Ignored for Control
317 * @multi_count: Max packet in a row for the device
318 * @max_packet: The device's maximum packet size in bytes
319 * @device_addr: USB device address at other end of pipe
320 * @endpoint_num: USB endpoint number at other end of pipe
321 * @hub_device_addr: Hub address this device is connected to
322 * @hub_port: Hub port this device is connected to
323 * @pid_toggle: This toggles between 0/1 on every packet send to track
324 * the data pid needed
325 * @channel: Hardware DMA channel for this pipe
326 * @split_sc_frame: The low order bits of the frame number the split
327 * complete should be sent on
329 struct cvmx_usb_pipe {
330 struct list_head node;
331 struct list_head transactions;
333 uint64_t next_tx_frame;
334 enum cvmx_usb_pipe_flags flags;
335 enum cvmx_usb_speed device_speed;
336 enum cvmx_usb_transfer transfer_type;
337 enum cvmx_usb_direction transfer_dir;
341 uint8_t endpoint_num;
342 uint8_t hub_device_addr;
346 int8_t split_sc_frame;
349 struct cvmx_usb_tx_fifo {
354 } entry[MAX_CHANNELS+1];
360 * struct cvmx_usb_state - the state of the USB block
362 * init_flags: Flags passed to initialize.
363 * index: Which USB block this is for.
364 * idle_hardware_channels: Bit set for every idle hardware channel.
365 * usbcx_hprt: Stored port status so we don't need to read a CSR to
367 * pipe_for_channel: Map channels to pipes.
368 * pipe: Storage for pipes.
369 * indent: Used by debug output to indent functions.
370 * port_status: Last port status used for change notification.
371 * idle_pipes: List of open pipes that have no transactions.
372 * active_pipes: Active pipes indexed by transfer type.
373 * frame_number: Increments every SOF interrupt for time keeping.
374 * active_split: Points to the current active split, or NULL.
376 struct cvmx_usb_state {
379 int idle_hardware_channels;
380 union cvmx_usbcx_hprt usbcx_hprt;
381 struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
383 struct cvmx_usb_port_status port_status;
384 struct list_head idle_pipes;
385 struct list_head active_pipes[4];
386 uint64_t frame_number;
387 struct cvmx_usb_transaction *active_split;
388 struct cvmx_usb_tx_fifo periodic;
389 struct cvmx_usb_tx_fifo nonperiodic;
394 struct cvmx_usb_state usb;
397 /* This macro spins on a field waiting for it to reach a value */
398 #define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
401 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
402 octeon_get_clock_rate() / 1000000; \
405 c.u32 = __cvmx_usb_read_csr32(usb, address); \
406 if (c.s.field op (value)) { \
409 } else if (cvmx_get_cycle() > done) { \
419 * This macro logically sets a single field in a CSR. It does the sequence
420 * read, modify, and write
422 #define USB_SET_FIELD32(address, type, field, value) \
425 c.u32 = __cvmx_usb_read_csr32(usb, address); \
427 __cvmx_usb_write_csr32(usb, address, c.u32); \
430 /* Returns the IO address to push/pop stuff data from the FIFOs */
431 #define USB_FIFO_ADDRESS(channel, usb_index) \
432 (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
435 * struct octeon_temp_buffer - a bounce buffer for USB transfers
436 * @temp_buffer: the newly allocated temporary buffer (including meta-data)
437 * @orig_buffer: the original buffer passed by the USB stack
438 * @data: the newly allocated temporary buffer (excluding meta-data)
440 * Both the DMA engine and FIFO mode will always transfer full 32-bit words. If
441 * the buffer is too short, we need to allocate a temporary one, and this struct
444 struct octeon_temp_buffer {
450 static inline struct octeon_hcd *cvmx_usb_to_octeon(struct cvmx_usb_state *p)
452 return container_of(p, struct octeon_hcd, usb);
455 static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
457 return container_of((void *)p, struct usb_hcd, hcd_priv);
461 * octeon_alloc_temp_buffer - allocate a temporary buffer for USB transfer
464 * @mem_flags: Memory allocation flags.
466 * This function allocates a temporary bounce buffer whenever it's needed
467 * due to HW limitations.
469 static int octeon_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
471 struct octeon_temp_buffer *temp;
473 if (urb->num_sgs || urb->sg ||
474 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
475 !(urb->transfer_buffer_length % sizeof(u32)))
478 temp = kmalloc(ALIGN(urb->transfer_buffer_length, sizeof(u32)) +
479 sizeof(*temp), mem_flags);
483 temp->temp_buffer = temp;
484 temp->orig_buffer = urb->transfer_buffer;
485 if (usb_urb_dir_out(urb))
486 memcpy(temp->data, urb->transfer_buffer,
487 urb->transfer_buffer_length);
488 urb->transfer_buffer = temp->data;
489 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
495 * octeon_free_temp_buffer - free a temporary buffer used by USB transfers.
498 * Frees a buffer allocated by octeon_alloc_temp_buffer().
500 static void octeon_free_temp_buffer(struct urb *urb)
502 struct octeon_temp_buffer *temp;
504 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
507 temp = container_of(urb->transfer_buffer, struct octeon_temp_buffer,
509 if (usb_urb_dir_in(urb))
510 memcpy(temp->orig_buffer, urb->transfer_buffer,
512 urb->transfer_buffer = temp->orig_buffer;
513 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
514 kfree(temp->temp_buffer);
518 * octeon_map_urb_for_dma - Octeon-specific map_urb_for_dma().
519 * @hcd: USB HCD structure.
521 * @mem_flags: Memory allocation flags.
523 static int octeon_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
528 ret = octeon_alloc_temp_buffer(urb, mem_flags);
532 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
534 octeon_free_temp_buffer(urb);
540 * octeon_unmap_urb_for_dma - Octeon-specific unmap_urb_for_dma()
541 * @hcd: USB HCD structure.
544 static void octeon_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
546 usb_hcd_unmap_urb_for_dma(hcd, urb);
547 octeon_free_temp_buffer(urb);
551 * Read a USB 32bit CSR. It performs the necessary address swizzle
552 * for 32bit CSRs and logs the value in a readable format if
555 * @usb: USB block this access is for
556 * @address: 64bit address to read
558 * Returns: Result of the read
560 static inline uint32_t __cvmx_usb_read_csr32(struct cvmx_usb_state *usb,
563 uint32_t result = cvmx_read64_uint32(address ^ 4);
569 * Write a USB 32bit CSR. It performs the necessary address
570 * swizzle for 32bit CSRs and logs the value in a readable format
571 * if debugging is on.
573 * @usb: USB block this access is for
574 * @address: 64bit address to write
575 * @value: Value to write
577 static inline void __cvmx_usb_write_csr32(struct cvmx_usb_state *usb,
578 uint64_t address, uint32_t value)
580 cvmx_write64_uint32(address ^ 4, value);
581 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
585 * Return non zero if this pipe connects to a non HIGH speed
586 * device through a high speed hub.
588 * @usb: USB block this access is for
589 * @pipe: Pipe to check
591 * Returns: Non zero if we need to do split transactions
593 static inline int __cvmx_usb_pipe_needs_split(struct cvmx_usb_state *usb,
594 struct cvmx_usb_pipe *pipe)
596 return pipe->device_speed != CVMX_USB_SPEED_HIGH &&
597 usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH;
602 * Trivial utility function to return the correct PID for a pipe
604 * @pipe: pipe to check
606 * Returns: PID for pipe
608 static inline int __cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
610 if (pipe->pid_toggle)
611 return 2; /* Data1 */
612 return 0; /* Data0 */
616 * Initialize a USB port for use. This must be called before any
617 * other access to the Octeon USB port is made. The port starts
618 * off in the disabled state.
620 * @usb: Pointer to an empty struct cvmx_usb_state
621 * that will be populated by the initialize call.
622 * This structure is then passed to all other USB
625 * Which Octeon USB port to initialize.
627 * Returns: 0 or a negative error code.
629 static int cvmx_usb_initialize(struct cvmx_usb_state *usb,
631 enum cvmx_usb_initialize_flags flags)
633 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
634 union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
637 memset(usb, 0, sizeof(*usb));
638 usb->init_flags = flags;
640 /* Initialize the USB state structure */
641 usb->index = usb_port_number;
642 INIT_LIST_HEAD(&usb->idle_pipes);
643 for (i = 0; i < ARRAY_SIZE(usb->active_pipes); i++)
644 INIT_LIST_HEAD(&usb->active_pipes[i]);
647 * Power On Reset and PHY Initialization
649 * 1. Wait for DCOK to assert (nothing to do)
651 * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
652 * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
654 usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
655 usbn_clk_ctl.s.por = 1;
656 usbn_clk_ctl.s.hrst = 0;
657 usbn_clk_ctl.s.prst = 0;
658 usbn_clk_ctl.s.hclk_rst = 0;
659 usbn_clk_ctl.s.enable = 0;
661 * 2b. Select the USB reference clock/crystal parameters by writing
662 * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
664 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
666 * The USB port uses 12/24/48MHz 2.5V board clock
667 * source at USB_XO. USB_XI should be tied to GND.
668 * Most Octeon evaluation boards require this setting
670 if (OCTEON_IS_MODEL(OCTEON_CN3XXX) ||
671 OCTEON_IS_MODEL(OCTEON_CN56XX) ||
672 OCTEON_IS_MODEL(OCTEON_CN50XX))
673 /* From CN56XX,CN50XX,CN31XX,CN30XX manuals */
674 usbn_clk_ctl.s.p_rtype = 2; /* p_rclk=1 & p_xenbn=0 */
676 /* From CN52XX manual */
677 usbn_clk_ctl.s.p_rtype = 1;
679 switch (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
680 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
681 usbn_clk_ctl.s.p_c_sel = 0;
683 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
684 usbn_clk_ctl.s.p_c_sel = 1;
686 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
687 usbn_clk_ctl.s.p_c_sel = 2;
692 * The USB port uses a 12MHz crystal as clock source
693 * at USB_XO and USB_XI
695 if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
696 /* From CN31XX,CN30XX manual */
697 usbn_clk_ctl.s.p_rtype = 3; /* p_rclk=1 & p_xenbn=1 */
699 /* From CN56XX,CN52XX,CN50XX manuals. */
700 usbn_clk_ctl.s.p_rtype = 0;
702 usbn_clk_ctl.s.p_c_sel = 0;
705 * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
706 * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
707 * such that USB is as close as possible to 125Mhz
710 int divisor = DIV_ROUND_UP(octeon_get_clock_rate(), 125000000);
711 /* Lower than 4 doesn't seem to work properly */
714 usbn_clk_ctl.s.divide = divisor;
715 usbn_clk_ctl.s.divide2 = 0;
717 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
718 /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
719 usbn_clk_ctl.s.hclk_rst = 1;
720 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
721 /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
724 * 3. Program the power-on reset field in the USBN clock-control
726 * USBN_CLK_CTL[POR] = 0
728 usbn_clk_ctl.s.por = 0;
729 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
730 /* 4. Wait 1 ms for PHY clock to start */
733 * 5. Program the Reset input from automatic test equipment field in the
734 * USBP control and status register:
735 * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
737 usbn_usbp_ctl_status.u64 =
738 cvmx_read64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index));
739 usbn_usbp_ctl_status.s.ate_reset = 1;
740 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
741 usbn_usbp_ctl_status.u64);
742 /* 6. Wait 10 cycles */
745 * 7. Clear ATE_RESET field in the USBN clock-control register:
746 * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
748 usbn_usbp_ctl_status.s.ate_reset = 0;
749 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
750 usbn_usbp_ctl_status.u64);
752 * 8. Program the PHY reset field in the USBN clock-control register:
753 * USBN_CLK_CTL[PRST] = 1
755 usbn_clk_ctl.s.prst = 1;
756 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
758 * 9. Program the USBP control and status register to select host or
759 * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
762 usbn_usbp_ctl_status.s.hst_mode = 0;
763 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
764 usbn_usbp_ctl_status.u64);
768 * 11. Program the hreset_n field in the USBN clock-control register:
769 * USBN_CLK_CTL[HRST] = 1
771 usbn_clk_ctl.s.hrst = 1;
772 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
773 /* 12. Proceed to USB core initialization */
774 usbn_clk_ctl.s.enable = 1;
775 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
779 * USB Core Initialization
781 * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
782 * determine USB core configuration parameters.
786 * 2. Program the following fields in the global AHB configuration
787 * register (USBC_GAHBCFG)
788 * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
789 * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
790 * Nonperiodic TxFIFO empty level (slave mode only),
791 * USBC_GAHBCFG[NPTXFEMPLVL]
792 * Periodic TxFIFO empty level (slave mode only),
793 * USBC_GAHBCFG[PTXFEMPLVL]
794 * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
797 union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
798 /* Due to an errata, CN31XX doesn't support DMA */
799 if (OCTEON_IS_MODEL(OCTEON_CN31XX))
800 usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
801 usbcx_gahbcfg.u32 = 0;
802 usbcx_gahbcfg.s.dmaen = !(usb->init_flags &
803 CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
804 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
805 /* Only use one channel with non DMA */
806 usb->idle_hardware_channels = 0x1;
807 else if (OCTEON_IS_MODEL(OCTEON_CN5XXX))
808 /* CN5XXX have an errata with channel 3 */
809 usb->idle_hardware_channels = 0xf7;
811 usb->idle_hardware_channels = 0xff;
812 usbcx_gahbcfg.s.hbstlen = 0;
813 usbcx_gahbcfg.s.nptxfemplvl = 1;
814 usbcx_gahbcfg.s.ptxfemplvl = 1;
815 usbcx_gahbcfg.s.glblintrmsk = 1;
816 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
820 * 3. Program the following fields in USBC_GUSBCFG register.
821 * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
822 * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
823 * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
824 * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
827 union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
829 usbcx_gusbcfg.u32 = __cvmx_usb_read_csr32(usb,
830 CVMX_USBCX_GUSBCFG(usb->index));
831 usbcx_gusbcfg.s.toutcal = 0;
832 usbcx_gusbcfg.s.ddrsel = 0;
833 usbcx_gusbcfg.s.usbtrdtim = 0x5;
834 usbcx_gusbcfg.s.phylpwrclksel = 0;
835 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
839 * 4. The software must unmask the following bits in the USBC_GINTMSK
841 * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
842 * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
845 union cvmx_usbcx_gintmsk usbcx_gintmsk;
848 usbcx_gintmsk.u32 = __cvmx_usb_read_csr32(usb,
849 CVMX_USBCX_GINTMSK(usb->index));
850 usbcx_gintmsk.s.otgintmsk = 1;
851 usbcx_gintmsk.s.modemismsk = 1;
852 usbcx_gintmsk.s.hchintmsk = 1;
853 usbcx_gintmsk.s.sofmsk = 0;
854 /* We need RX FIFO interrupts if we don't have DMA */
855 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
856 usbcx_gintmsk.s.rxflvlmsk = 1;
857 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
861 * Disable all channel interrupts. We'll enable them per channel
864 for (channel = 0; channel < 8; channel++)
865 __cvmx_usb_write_csr32(usb,
866 CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
871 * Host Port Initialization
873 * 1. Program the host-port interrupt-mask field to unmask,
874 * USBC_GINTMSK[PRTINT] = 1
876 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
877 union cvmx_usbcx_gintmsk, prtintmsk, 1);
878 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
879 union cvmx_usbcx_gintmsk, disconnintmsk, 1);
881 * 2. Program the USBC_HCFG register to select full-speed host
882 * or high-speed host.
885 union cvmx_usbcx_hcfg usbcx_hcfg;
887 usbcx_hcfg.u32 = __cvmx_usb_read_csr32(usb,
888 CVMX_USBCX_HCFG(usb->index));
889 usbcx_hcfg.s.fslssupp = 0;
890 usbcx_hcfg.s.fslspclksel = 0;
891 __cvmx_usb_write_csr32(usb,
892 CVMX_USBCX_HCFG(usb->index),
896 * 3. Program the port power bit to drive VBUS on the USB,
897 * USBC_HPRT[PRTPWR] = 1
899 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index),
900 union cvmx_usbcx_hprt, prtpwr, 1);
903 * Steps 4-15 from the manual are done later in the port enable
912 * Shutdown a USB port after a call to cvmx_usb_initialize().
913 * The port should be disabled with all pipes closed when this
914 * function is called.
916 * @usb: USB device state populated by cvmx_usb_initialize().
918 * Returns: 0 or a negative error code.
920 static int cvmx_usb_shutdown(struct cvmx_usb_state *usb)
922 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
924 /* Make sure all pipes are closed */
925 if (!list_empty(&usb->idle_pipes) ||
926 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS]) ||
927 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT]) ||
928 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_CONTROL]) ||
929 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_BULK]))
932 /* Disable the clocks and put them in power on reset */
933 usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
934 usbn_clk_ctl.s.enable = 1;
935 usbn_clk_ctl.s.por = 1;
936 usbn_clk_ctl.s.hclk_rst = 1;
937 usbn_clk_ctl.s.prst = 0;
938 usbn_clk_ctl.s.hrst = 0;
939 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
945 * Enable a USB port. After this call succeeds, the USB port is
946 * online and servicing requests.
948 * @usb: USB device state populated by cvmx_usb_initialize().
950 * Returns: 0 or a negative error code.
952 static int cvmx_usb_enable(struct cvmx_usb_state *usb)
954 union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
956 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb,
957 CVMX_USBCX_HPRT(usb->index));
960 * If the port is already enabled the just return. We don't need to do
963 if (usb->usbcx_hprt.s.prtena)
966 /* If there is nothing plugged into the port then fail immediately */
967 if (!usb->usbcx_hprt.s.prtconnsts)
970 /* Program the port reset bit to start the reset process */
971 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
975 * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
976 * process to complete.
980 /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
981 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
984 /* Wait for the USBC_HPRT[PRTENA]. */
985 if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb->index),
986 union cvmx_usbcx_hprt, prtena, ==, 1, 100000))
990 * Read the port speed field to get the enumerated speed,
993 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb,
994 CVMX_USBCX_HPRT(usb->index));
995 usbcx_ghwcfg3.u32 = __cvmx_usb_read_csr32(usb,
996 CVMX_USBCX_GHWCFG3(usb->index));
999 * 13. Program the USBC_GRXFSIZ register to select the size of the
1000 * receive FIFO (25%).
1002 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index),
1003 union cvmx_usbcx_grxfsiz, rxfdep,
1004 usbcx_ghwcfg3.s.dfifodepth / 4);
1006 * 14. Program the USBC_GNPTXFSIZ register to select the size and the
1007 * start address of the non- periodic transmit FIFO for nonperiodic
1008 * transactions (50%).
1011 union cvmx_usbcx_gnptxfsiz siz;
1013 siz.u32 = __cvmx_usb_read_csr32(usb,
1014 CVMX_USBCX_GNPTXFSIZ(usb->index));
1015 siz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
1016 siz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
1017 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index),
1021 * 15. Program the USBC_HPTXFSIZ register to select the size and start
1022 * address of the periodic transmit FIFO for periodic transactions
1026 union cvmx_usbcx_hptxfsiz siz;
1028 siz.u32 = __cvmx_usb_read_csr32(usb,
1029 CVMX_USBCX_HPTXFSIZ(usb->index));
1030 siz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
1031 siz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
1032 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index),
1035 /* Flush all FIFOs */
1036 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1037 union cvmx_usbcx_grstctl, txfnum, 0x10);
1038 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1039 union cvmx_usbcx_grstctl, txfflsh, 1);
1040 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1041 union cvmx_usbcx_grstctl,
1042 txfflsh, ==, 0, 100);
1043 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1044 union cvmx_usbcx_grstctl, rxfflsh, 1);
1045 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1046 union cvmx_usbcx_grstctl,
1047 rxfflsh, ==, 0, 100);
1054 * Disable a USB port. After this call the USB port will not
1055 * generate data transfers and will not generate events.
1056 * Transactions in process will fail and call their
1057 * associated callbacks.
1059 * @usb: USB device state populated by cvmx_usb_initialize().
1061 * Returns: 0 or a negative error code.
1063 static int cvmx_usb_disable(struct cvmx_usb_state *usb)
1065 /* Disable the port */
1066 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
1073 * Get the current state of the USB port. Use this call to
1074 * determine if the usb port has anything connected, is enabled,
1075 * or has some sort of error condition. The return value of this
1076 * call has "changed" bits to signal of the value of some fields
1077 * have changed between calls.
1079 * @usb: USB device state populated by cvmx_usb_initialize().
1081 * Returns: Port status information
1083 static struct cvmx_usb_port_status cvmx_usb_get_status(
1084 struct cvmx_usb_state *usb)
1086 union cvmx_usbcx_hprt usbc_hprt;
1087 struct cvmx_usb_port_status result;
1089 memset(&result, 0, sizeof(result));
1091 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb,
1092 CVMX_USBCX_HPRT(usb->index));
1093 result.port_enabled = usbc_hprt.s.prtena;
1094 result.port_over_current = usbc_hprt.s.prtovrcurract;
1095 result.port_powered = usbc_hprt.s.prtpwr;
1096 result.port_speed = usbc_hprt.s.prtspd;
1097 result.connected = usbc_hprt.s.prtconnsts;
1098 result.connect_change =
1099 (result.connected != usb->port_status.connected);
1105 * Open a virtual pipe between the host and a USB device. A pipe
1106 * must be opened before data can be transferred between a device
1109 * @usb: USB device state populated by cvmx_usb_initialize().
1111 * USB device address to open the pipe to
1114 * USB endpoint number to open the pipe to
1117 * The speed of the device the pipe is going
1118 * to. This must match the device's speed,
1119 * which may be different than the port speed.
1120 * @max_packet: The maximum packet length the device can
1121 * transmit/receive (low speed=0-8, full
1122 * speed=0-1023, high speed=0-1024). This value
1123 * comes from the standard endpoint descriptor
1124 * field wMaxPacketSize bits <10:0>.
1126 * The type of transfer this pipe is for.
1128 * The direction the pipe is in. This is not
1129 * used for control pipes.
1130 * @interval: For ISOCHRONOUS and INTERRUPT transfers,
1131 * this is how often the transfer is scheduled
1132 * for. All other transfers should specify
1133 * zero. The units are in frames (8000/sec at
1134 * high speed, 1000/sec for full speed).
1136 * For high speed devices, this is the maximum
1137 * allowed number of packet per microframe.
1138 * Specify zero for non high speed devices. This
1139 * value comes from the standard endpoint descriptor
1140 * field wMaxPacketSize bits <12:11>.
1142 * Hub device address this device is connected
1143 * to. Devices connected directly to Octeon
1144 * use zero. This is only used when the device
1145 * is full/low speed behind a high speed hub.
1146 * The address will be of the high speed hub,
1147 * not and full speed hubs after it.
1148 * @hub_port: Which port on the hub the device is
1149 * connected. Use zero for devices connected
1150 * directly to Octeon. Like hub_device_addr,
1151 * this is only used for full/low speed
1152 * devices behind a high speed hub.
1154 * Returns: A non-NULL value is a pipe. NULL means an error.
1156 static struct cvmx_usb_pipe *cvmx_usb_open_pipe(struct cvmx_usb_state *usb,
1162 enum cvmx_usb_transfer
1164 enum cvmx_usb_direction
1166 int interval, int multi_count,
1167 int hub_device_addr,
1170 struct cvmx_usb_pipe *pipe;
1172 pipe = kzalloc(sizeof(*pipe), GFP_ATOMIC);
1175 if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1176 (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1177 (transfer_type == CVMX_USB_TRANSFER_BULK))
1178 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
1179 pipe->device_addr = device_addr;
1180 pipe->endpoint_num = endpoint_num;
1181 pipe->device_speed = device_speed;
1182 pipe->max_packet = max_packet;
1183 pipe->transfer_type = transfer_type;
1184 pipe->transfer_dir = transfer_dir;
1185 INIT_LIST_HEAD(&pipe->transactions);
1188 * All pipes use interval to rate limit NAK processing. Force an
1189 * interval if one wasn't supplied
1193 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1194 pipe->interval = interval*8;
1195 /* Force start splits to be schedule on uFrame 0 */
1196 pipe->next_tx_frame = ((usb->frame_number+7)&~7) +
1199 pipe->interval = interval;
1200 pipe->next_tx_frame = usb->frame_number + pipe->interval;
1202 pipe->multi_count = multi_count;
1203 pipe->hub_device_addr = hub_device_addr;
1204 pipe->hub_port = hub_port;
1205 pipe->pid_toggle = 0;
1206 pipe->split_sc_frame = -1;
1207 list_add_tail(&pipe->node, &usb->idle_pipes);
1210 * We don't need to tell the hardware about this pipe yet since
1211 * it doesn't have any submitted requests
1219 * Poll the RX FIFOs and remove data as needed. This function is only used
1220 * in non DMA mode. It is very important that this function be called quickly
1221 * enough to prevent FIFO overflow.
1223 * @usb: USB device state populated by cvmx_usb_initialize().
1225 static void __cvmx_usb_poll_rx_fifo(struct cvmx_usb_state *usb)
1227 union cvmx_usbcx_grxstsph rx_status;
1233 rx_status.u32 = __cvmx_usb_read_csr32(usb,
1234 CVMX_USBCX_GRXSTSPH(usb->index));
1235 /* Only read data if IN data is there */
1236 if (rx_status.s.pktsts != 2)
1238 /* Check if no data is available */
1239 if (!rx_status.s.bcnt)
1242 channel = rx_status.s.chnum;
1243 bytes = rx_status.s.bcnt;
1247 /* Get where the DMA engine would have written this data */
1248 address = cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) +
1251 ptr = cvmx_phys_to_ptr(address);
1252 cvmx_write64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel * 8,
1255 /* Loop writing the FIFO data for this packet into memory */
1257 *ptr++ = __cvmx_usb_read_csr32(usb,
1258 USB_FIFO_ADDRESS(channel, usb->index));
1266 * Fill the TX hardware fifo with data out of the software
1269 * @usb: USB device state populated by cvmx_usb_initialize().
1270 * @fifo: Software fifo to use
1271 * @available: Amount of space in the hardware fifo
1273 * Returns: Non zero if the hardware fifo was too small and needs
1274 * to be serviced again.
1276 static int __cvmx_usb_fill_tx_hw(struct cvmx_usb_state *usb,
1277 struct cvmx_usb_tx_fifo *fifo, int available)
1280 * We're done either when there isn't anymore space or the software FIFO
1283 while (available && (fifo->head != fifo->tail)) {
1285 const uint32_t *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1286 uint64_t csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel,
1288 int words = available;
1290 /* Limit the amount of data to waht the SW fifo has */
1291 if (fifo->entry[i].size <= available) {
1292 words = fifo->entry[i].size;
1294 if (fifo->tail > MAX_CHANNELS)
1298 /* Update the next locations and counts */
1300 fifo->entry[i].address += words * 4;
1301 fifo->entry[i].size -= words;
1304 * Write the HW fifo data. The read every three writes is due
1305 * to an errata on CN3XXX chips
1308 cvmx_write64_uint32(csr_address, *ptr++);
1309 cvmx_write64_uint32(csr_address, *ptr++);
1310 cvmx_write64_uint32(csr_address, *ptr++);
1312 CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1315 cvmx_write64_uint32(csr_address, *ptr++);
1317 cvmx_write64_uint32(csr_address, *ptr++);
1319 cvmx_write64_uint32(csr_address, *ptr++);
1321 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1323 return fifo->head != fifo->tail;
1328 * Check the hardware FIFOs and fill them as needed
1330 * @usb: USB device state populated by cvmx_usb_initialize().
1332 static void __cvmx_usb_poll_tx_fifo(struct cvmx_usb_state *usb)
1334 if (usb->periodic.head != usb->periodic.tail) {
1335 union cvmx_usbcx_hptxsts tx_status;
1337 tx_status.u32 = __cvmx_usb_read_csr32(usb,
1338 CVMX_USBCX_HPTXSTS(usb->index));
1339 if (__cvmx_usb_fill_tx_hw(usb, &usb->periodic,
1340 tx_status.s.ptxfspcavail))
1341 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1342 union cvmx_usbcx_gintmsk,
1345 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1346 union cvmx_usbcx_gintmsk,
1350 if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1351 union cvmx_usbcx_gnptxsts tx_status;
1353 tx_status.u32 = __cvmx_usb_read_csr32(usb,
1354 CVMX_USBCX_GNPTXSTS(usb->index));
1355 if (__cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic,
1356 tx_status.s.nptxfspcavail))
1357 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1358 union cvmx_usbcx_gintmsk,
1361 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1362 union cvmx_usbcx_gintmsk,
1369 * Fill the TX FIFO with an outgoing packet
1371 * @usb: USB device state populated by cvmx_usb_initialize().
1372 * @channel: Channel number to get packet from
1374 static void __cvmx_usb_fill_tx_fifo(struct cvmx_usb_state *usb, int channel)
1376 union cvmx_usbcx_hccharx hcchar;
1377 union cvmx_usbcx_hcspltx usbc_hcsplt;
1378 union cvmx_usbcx_hctsizx usbc_hctsiz;
1379 struct cvmx_usb_tx_fifo *fifo;
1381 /* We only need to fill data on outbound channels */
1382 hcchar.u32 = __cvmx_usb_read_csr32(usb,
1383 CVMX_USBCX_HCCHARX(channel, usb->index));
1384 if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1387 /* OUT Splits only have data on the start and not the complete */
1388 usbc_hcsplt.u32 = __cvmx_usb_read_csr32(usb,
1389 CVMX_USBCX_HCSPLTX(channel, usb->index));
1390 if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1394 * Find out how many bytes we need to fill and convert it into 32bit
1397 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
1398 CVMX_USBCX_HCTSIZX(channel, usb->index));
1399 if (!usbc_hctsiz.s.xfersize)
1402 if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1403 (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1404 fifo = &usb->periodic;
1406 fifo = &usb->nonperiodic;
1408 fifo->entry[fifo->head].channel = channel;
1409 fifo->entry[fifo->head].address =
1410 cvmx_read64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1412 fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize+3)>>2;
1414 if (fifo->head > MAX_CHANNELS)
1417 __cvmx_usb_poll_tx_fifo(usb);
1421 * Perform channel specific setup for Control transactions. All
1422 * the generic stuff will already have been done in
1423 * __cvmx_usb_start_channel()
1425 * @usb: USB device state populated by cvmx_usb_initialize().
1426 * @channel: Channel to setup
1427 * @pipe: Pipe for control transaction
1429 static void __cvmx_usb_start_channel_control(struct cvmx_usb_state *usb,
1431 struct cvmx_usb_pipe *pipe)
1433 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
1434 struct usb_hcd *hcd = octeon_to_hcd(priv);
1435 struct device *dev = hcd->self.controller;
1436 struct cvmx_usb_transaction *transaction =
1437 list_first_entry(&pipe->transactions, typeof(*transaction),
1439 struct usb_ctrlrequest *header =
1440 cvmx_phys_to_ptr(transaction->control_header);
1441 int bytes_to_transfer = transaction->buffer_length -
1442 transaction->actual_bytes;
1443 int packets_to_transfer;
1444 union cvmx_usbcx_hctsizx usbc_hctsiz;
1446 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
1447 CVMX_USBCX_HCTSIZX(channel, usb->index));
1449 switch (transaction->stage) {
1450 case CVMX_USB_STAGE_NON_CONTROL:
1451 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1452 dev_err(dev, "%s: ERROR - Non control stage\n", __func__);
1454 case CVMX_USB_STAGE_SETUP:
1455 usbc_hctsiz.s.pid = 3; /* Setup */
1456 bytes_to_transfer = sizeof(*header);
1457 /* All Control operations start with a setup going OUT */
1458 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1459 union cvmx_usbcx_hccharx, epdir,
1460 CVMX_USB_DIRECTION_OUT);
1462 * Setup send the control header instead of the buffer data. The
1463 * buffer data will be used in the next stage
1465 cvmx_write64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1467 transaction->control_header);
1469 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1470 usbc_hctsiz.s.pid = 3; /* Setup */
1471 bytes_to_transfer = 0;
1472 /* All Control operations start with a setup going OUT */
1473 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1474 union cvmx_usbcx_hccharx, epdir,
1475 CVMX_USB_DIRECTION_OUT);
1477 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1478 union cvmx_usbcx_hcspltx, compsplt, 1);
1480 case CVMX_USB_STAGE_DATA:
1481 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1482 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1483 if (header->bRequestType & USB_DIR_IN)
1484 bytes_to_transfer = 0;
1485 else if (bytes_to_transfer > pipe->max_packet)
1486 bytes_to_transfer = pipe->max_packet;
1488 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1489 union cvmx_usbcx_hccharx, epdir,
1490 ((header->bRequestType & USB_DIR_IN) ?
1491 CVMX_USB_DIRECTION_IN :
1492 CVMX_USB_DIRECTION_OUT));
1494 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1495 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1496 if (!(header->bRequestType & USB_DIR_IN))
1497 bytes_to_transfer = 0;
1498 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1499 union cvmx_usbcx_hccharx, epdir,
1500 ((header->bRequestType & USB_DIR_IN) ?
1501 CVMX_USB_DIRECTION_IN :
1502 CVMX_USB_DIRECTION_OUT));
1503 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1504 union cvmx_usbcx_hcspltx, compsplt, 1);
1506 case CVMX_USB_STAGE_STATUS:
1507 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1508 bytes_to_transfer = 0;
1509 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1510 union cvmx_usbcx_hccharx, epdir,
1511 ((header->bRequestType & USB_DIR_IN) ?
1512 CVMX_USB_DIRECTION_OUT :
1513 CVMX_USB_DIRECTION_IN));
1515 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1516 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1517 bytes_to_transfer = 0;
1518 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1519 union cvmx_usbcx_hccharx, epdir,
1520 ((header->bRequestType & USB_DIR_IN) ?
1521 CVMX_USB_DIRECTION_OUT :
1522 CVMX_USB_DIRECTION_IN));
1523 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1524 union cvmx_usbcx_hcspltx, compsplt, 1);
1529 * Make sure the transfer never exceeds the byte limit of the hardware.
1530 * Further bytes will be sent as continued transactions
1532 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1533 /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1534 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1535 bytes_to_transfer *= pipe->max_packet;
1539 * Calculate the number of packets to transfer. If the length is zero
1540 * we still need to transfer one packet
1542 packets_to_transfer = DIV_ROUND_UP(bytes_to_transfer,
1544 if (packets_to_transfer == 0)
1545 packets_to_transfer = 1;
1546 else if ((packets_to_transfer > 1) &&
1547 (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1549 * Limit to one packet when not using DMA. Channels must be
1550 * restarted between every packet for IN transactions, so there
1551 * is no reason to do multiple packets in a row
1553 packets_to_transfer = 1;
1554 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1555 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1557 * Limit the number of packet and data transferred to what the
1558 * hardware can handle
1560 packets_to_transfer = MAX_TRANSFER_PACKETS;
1561 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1564 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1565 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1567 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index),
1573 * Start a channel to perform the pipe's head transaction
1575 * @usb: USB device state populated by cvmx_usb_initialize().
1576 * @channel: Channel to setup
1577 * @pipe: Pipe to start
1579 static void __cvmx_usb_start_channel(struct cvmx_usb_state *usb,
1581 struct cvmx_usb_pipe *pipe)
1583 struct cvmx_usb_transaction *transaction =
1584 list_first_entry(&pipe->transactions, typeof(*transaction),
1587 /* Make sure all writes to the DMA region get flushed */
1590 /* Attach the channel to the pipe */
1591 usb->pipe_for_channel[channel] = pipe;
1592 pipe->channel = channel;
1593 pipe->flags |= __CVMX_USB_PIPE_FLAGS_SCHEDULED;
1595 /* Mark this channel as in use */
1596 usb->idle_hardware_channels &= ~(1<<channel);
1598 /* Enable the channel interrupt bits */
1600 union cvmx_usbcx_hcintx usbc_hcint;
1601 union cvmx_usbcx_hcintmskx usbc_hcintmsk;
1602 union cvmx_usbcx_haintmsk usbc_haintmsk;
1604 /* Clear all channel status bits */
1605 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb,
1606 CVMX_USBCX_HCINTX(channel, usb->index));
1608 __cvmx_usb_write_csr32(usb,
1609 CVMX_USBCX_HCINTX(channel, usb->index),
1612 usbc_hcintmsk.u32 = 0;
1613 usbc_hcintmsk.s.chhltdmsk = 1;
1614 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1616 * Channels need these extra interrupts when we aren't
1619 usbc_hcintmsk.s.datatglerrmsk = 1;
1620 usbc_hcintmsk.s.frmovrunmsk = 1;
1621 usbc_hcintmsk.s.bblerrmsk = 1;
1622 usbc_hcintmsk.s.xacterrmsk = 1;
1623 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1625 * Splits don't generate xfercompl, so we need
1628 usbc_hcintmsk.s.nyetmsk = 1;
1629 usbc_hcintmsk.s.ackmsk = 1;
1631 usbc_hcintmsk.s.nakmsk = 1;
1632 usbc_hcintmsk.s.stallmsk = 1;
1633 usbc_hcintmsk.s.xfercomplmsk = 1;
1635 __cvmx_usb_write_csr32(usb,
1636 CVMX_USBCX_HCINTMSKX(channel, usb->index),
1639 /* Enable the channel interrupt to propagate */
1640 usbc_haintmsk.u32 = __cvmx_usb_read_csr32(usb,
1641 CVMX_USBCX_HAINTMSK(usb->index));
1642 usbc_haintmsk.s.haintmsk |= 1<<channel;
1643 __cvmx_usb_write_csr32(usb,
1644 CVMX_USBCX_HAINTMSK(usb->index),
1648 /* Setup the locations the DMA engines use */
1650 uint64_t dma_address = transaction->buffer +
1651 transaction->actual_bytes;
1653 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1654 dma_address = transaction->buffer +
1655 transaction->iso_packets[0].offset +
1656 transaction->actual_bytes;
1658 cvmx_write64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1661 cvmx_write64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) +
1666 /* Setup both the size of the transfer and the SPLIT characteristics */
1668 union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
1669 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
1670 int packets_to_transfer;
1671 int bytes_to_transfer = transaction->buffer_length -
1672 transaction->actual_bytes;
1675 * ISOCHRONOUS transactions store each individual transfer size
1676 * in the packet structure, not the global buffer_length
1678 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1680 transaction->iso_packets[0].length -
1681 transaction->actual_bytes;
1684 * We need to do split transactions when we are talking to non
1685 * high speed devices that are behind a high speed hub
1687 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1689 * On the start split phase (stage is even) record the
1690 * frame number we will need to send the split complete.
1691 * We only store the lower two bits since the time ahead
1692 * can only be two frames
1694 if ((transaction->stage&1) == 0) {
1695 if (transaction->type == CVMX_USB_TRANSFER_BULK)
1696 pipe->split_sc_frame =
1697 (usb->frame_number + 1) & 0x7f;
1699 pipe->split_sc_frame =
1700 (usb->frame_number + 2) & 0x7f;
1702 pipe->split_sc_frame = -1;
1704 usbc_hcsplt.s.spltena = 1;
1705 usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1706 usbc_hcsplt.s.prtaddr = pipe->hub_port;
1707 usbc_hcsplt.s.compsplt = (transaction->stage ==
1708 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1711 * SPLIT transactions can only ever transmit one data
1712 * packet so limit the transfer size to the max packet
1715 if (bytes_to_transfer > pipe->max_packet)
1716 bytes_to_transfer = pipe->max_packet;
1719 * ISOCHRONOUS OUT splits are unique in that they limit
1720 * data transfers to 188 byte chunks representing the
1721 * begin/middle/end of the data or all
1723 if (!usbc_hcsplt.s.compsplt &&
1724 (pipe->transfer_dir ==
1725 CVMX_USB_DIRECTION_OUT) &&
1726 (pipe->transfer_type ==
1727 CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1729 * Clear the split complete frame number as
1730 * there isn't going to be a split complete
1732 pipe->split_sc_frame = -1;
1734 * See if we've started this transfer and sent
1737 if (transaction->actual_bytes == 0) {
1739 * Nothing sent yet, this is either a
1740 * begin or the entire payload
1742 if (bytes_to_transfer <= 188)
1743 /* Entire payload in one go */
1744 usbc_hcsplt.s.xactpos = 3;
1746 /* First part of payload */
1747 usbc_hcsplt.s.xactpos = 2;
1750 * Continuing the previous data, we must
1751 * either be in the middle or at the end
1753 if (bytes_to_transfer <= 188)
1754 /* End of payload */
1755 usbc_hcsplt.s.xactpos = 1;
1757 /* Middle of payload */
1758 usbc_hcsplt.s.xactpos = 0;
1761 * Again, the transfer size is limited to 188
1764 if (bytes_to_transfer > 188)
1765 bytes_to_transfer = 188;
1770 * Make sure the transfer never exceeds the byte limit of the
1771 * hardware. Further bytes will be sent as continued
1774 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1776 * Round MAX_TRANSFER_BYTES to a multiple of out packet
1779 bytes_to_transfer = MAX_TRANSFER_BYTES /
1781 bytes_to_transfer *= pipe->max_packet;
1785 * Calculate the number of packets to transfer. If the length is
1786 * zero we still need to transfer one packet
1788 packets_to_transfer =
1789 DIV_ROUND_UP(bytes_to_transfer, pipe->max_packet);
1790 if (packets_to_transfer == 0)
1791 packets_to_transfer = 1;
1792 else if ((packets_to_transfer > 1) &&
1794 CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1796 * Limit to one packet when not using DMA. Channels must
1797 * be restarted between every packet for IN
1798 * transactions, so there is no reason to do multiple
1801 packets_to_transfer = 1;
1802 bytes_to_transfer = packets_to_transfer *
1804 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1806 * Limit the number of packet and data transferred to
1807 * what the hardware can handle
1809 packets_to_transfer = MAX_TRANSFER_PACKETS;
1810 bytes_to_transfer = packets_to_transfer *
1814 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1815 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1817 /* Update the DATA0/DATA1 toggle */
1818 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1820 * High speed pipes may need a hardware ping before they start
1822 if (pipe->flags & __CVMX_USB_PIPE_FLAGS_NEED_PING)
1823 usbc_hctsiz.s.dopng = 1;
1825 __cvmx_usb_write_csr32(usb,
1826 CVMX_USBCX_HCSPLTX(channel, usb->index),
1828 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel,
1829 usb->index), usbc_hctsiz.u32);
1832 /* Setup the Host Channel Characteristics Register */
1834 union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
1837 * Set the startframe odd/even properly. This is only used for
1840 usbc_hcchar.s.oddfrm = usb->frame_number&1;
1843 * Set the number of back to back packets allowed by this
1844 * endpoint. Split transactions interpret "ec" as the number of
1845 * immediate retries of failure. These retries happen too
1846 * quickly, so we disable these entirely for splits
1848 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1849 usbc_hcchar.s.ec = 1;
1850 else if (pipe->multi_count < 1)
1851 usbc_hcchar.s.ec = 1;
1852 else if (pipe->multi_count > 3)
1853 usbc_hcchar.s.ec = 3;
1855 usbc_hcchar.s.ec = pipe->multi_count;
1857 /* Set the rest of the endpoint specific settings */
1858 usbc_hcchar.s.devaddr = pipe->device_addr;
1859 usbc_hcchar.s.eptype = transaction->type;
1860 usbc_hcchar.s.lspddev =
1861 (pipe->device_speed == CVMX_USB_SPEED_LOW);
1862 usbc_hcchar.s.epdir = pipe->transfer_dir;
1863 usbc_hcchar.s.epnum = pipe->endpoint_num;
1864 usbc_hcchar.s.mps = pipe->max_packet;
1865 __cvmx_usb_write_csr32(usb,
1866 CVMX_USBCX_HCCHARX(channel, usb->index),
1870 /* Do transaction type specific fixups as needed */
1871 switch (transaction->type) {
1872 case CVMX_USB_TRANSFER_CONTROL:
1873 __cvmx_usb_start_channel_control(usb, channel, pipe);
1875 case CVMX_USB_TRANSFER_BULK:
1876 case CVMX_USB_TRANSFER_INTERRUPT:
1878 case CVMX_USB_TRANSFER_ISOCHRONOUS:
1879 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
1881 * ISO transactions require different PIDs depending on
1882 * direction and how many packets are needed
1884 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
1885 if (pipe->multi_count < 2) /* Need DATA0 */
1887 CVMX_USBCX_HCTSIZX(channel,
1889 union cvmx_usbcx_hctsizx,
1891 else /* Need MDATA */
1893 CVMX_USBCX_HCTSIZX(channel,
1895 union cvmx_usbcx_hctsizx,
1902 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 =
1903 __cvmx_usb_read_csr32(usb,
1904 CVMX_USBCX_HCTSIZX(channel, usb->index))};
1905 transaction->xfersize = usbc_hctsiz.s.xfersize;
1906 transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1908 /* Remeber when we start a split transaction */
1909 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1910 usb->active_split = transaction;
1911 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1912 union cvmx_usbcx_hccharx, chena, 1);
1913 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1914 __cvmx_usb_fill_tx_fifo(usb, channel);
1919 * Find a pipe that is ready to be scheduled to hardware.
1920 * @usb: USB device state populated by cvmx_usb_initialize().
1921 * @list: Pipe list to search
1923 * Frame counter to use as a time reference.
1925 * Returns: Pipe or NULL if none are ready
1927 static struct cvmx_usb_pipe *__cvmx_usb_find_ready_pipe(
1928 struct cvmx_usb_state *usb,
1929 struct list_head *list,
1930 uint64_t current_frame)
1932 struct cvmx_usb_pipe *pipe;
1934 list_for_each_entry(pipe, list, node) {
1935 struct cvmx_usb_transaction *t =
1936 list_first_entry(&pipe->transactions, typeof(*t),
1938 if (!(pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED) && t &&
1939 (pipe->next_tx_frame <= current_frame) &&
1940 ((pipe->split_sc_frame == -1) ||
1941 ((((int)current_frame - (int)pipe->split_sc_frame)
1943 (!usb->active_split || (usb->active_split == t))) {
1953 * Called whenever a pipe might need to be scheduled to the
1956 * @usb: USB device state populated by cvmx_usb_initialize().
1957 * @is_sof: True if this schedule was called on a SOF interrupt.
1959 static void __cvmx_usb_schedule(struct cvmx_usb_state *usb, int is_sof)
1962 struct cvmx_usb_pipe *pipe;
1964 enum cvmx_usb_transfer ttype;
1966 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1968 * Without DMA we need to be careful to not schedule something
1969 * at the end of a frame and cause an overrun.
1971 union cvmx_usbcx_hfnum hfnum = {
1972 .u32 = __cvmx_usb_read_csr32(usb,
1973 CVMX_USBCX_HFNUM(usb->index))
1976 union cvmx_usbcx_hfir hfir = {
1977 .u32 = __cvmx_usb_read_csr32(usb,
1978 CVMX_USBCX_HFIR(usb->index))
1981 if (hfnum.s.frrem < hfir.s.frint/4)
1985 while (usb->idle_hardware_channels) {
1986 /* Find an idle channel */
1987 channel = __fls(usb->idle_hardware_channels);
1988 if (unlikely(channel > 7))
1991 /* Find a pipe needing service */
1995 * Only process periodic pipes on SOF interrupts. This
1996 * way we are sure that the periodic data is sent in the
1997 * beginning of the frame
1999 pipe = __cvmx_usb_find_ready_pipe(usb,
2001 CVMX_USB_TRANSFER_ISOCHRONOUS,
2004 pipe = __cvmx_usb_find_ready_pipe(usb,
2006 CVMX_USB_TRANSFER_INTERRUPT,
2009 if (likely(!pipe)) {
2010 pipe = __cvmx_usb_find_ready_pipe(usb,
2012 CVMX_USB_TRANSFER_CONTROL,
2015 pipe = __cvmx_usb_find_ready_pipe(usb,
2017 CVMX_USB_TRANSFER_BULK,
2023 __cvmx_usb_start_channel(usb, channel, pipe);
2028 * Only enable SOF interrupts when we have transactions pending in the
2029 * future that might need to be scheduled
2032 for (ttype = CVMX_USB_TRANSFER_CONTROL;
2033 ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
2034 list_for_each_entry(pipe, &usb->active_pipes[ttype], node) {
2035 if (pipe->next_tx_frame > usb->frame_number) {
2041 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
2042 union cvmx_usbcx_gintmsk, sofmsk, need_sof);
2045 static void octeon_usb_urb_complete_callback(struct cvmx_usb_state *usb,
2046 enum cvmx_usb_complete status,
2047 struct cvmx_usb_pipe *pipe,
2048 struct cvmx_usb_transaction
2050 int bytes_transferred,
2053 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
2054 struct usb_hcd *hcd = octeon_to_hcd(priv);
2055 struct device *dev = hcd->self.controller;
2057 if (likely(status == CVMX_USB_COMPLETE_SUCCESS))
2058 urb->actual_length = bytes_transferred;
2060 urb->actual_length = 0;
2064 /* For Isochronous transactions we need to update the URB packet status
2065 list from data in our private copy */
2066 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2069 * The pointer to the private list is stored in the setup_packet
2072 struct cvmx_usb_iso_packet *iso_packet =
2073 (struct cvmx_usb_iso_packet *) urb->setup_packet;
2074 /* Recalculate the transfer size by adding up each packet */
2075 urb->actual_length = 0;
2076 for (i = 0; i < urb->number_of_packets; i++) {
2077 if (iso_packet[i].status ==
2078 CVMX_USB_COMPLETE_SUCCESS) {
2079 urb->iso_frame_desc[i].status = 0;
2080 urb->iso_frame_desc[i].actual_length =
2081 iso_packet[i].length;
2082 urb->actual_length +=
2083 urb->iso_frame_desc[i].actual_length;
2085 dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%p transaction=%p size=%d\n",
2086 i, urb->number_of_packets,
2087 iso_packet[i].status, pipe,
2088 transaction, iso_packet[i].length);
2089 urb->iso_frame_desc[i].status = -EREMOTEIO;
2092 /* Free the private list now that we don't need it anymore */
2094 urb->setup_packet = NULL;
2098 case CVMX_USB_COMPLETE_SUCCESS:
2101 case CVMX_USB_COMPLETE_CANCEL:
2102 if (urb->status == 0)
2103 urb->status = -ENOENT;
2105 case CVMX_USB_COMPLETE_STALL:
2106 dev_dbg(dev, "status=stall pipe=%p transaction=%p size=%d\n",
2107 pipe, transaction, bytes_transferred);
2108 urb->status = -EPIPE;
2110 case CVMX_USB_COMPLETE_BABBLEERR:
2111 dev_dbg(dev, "status=babble pipe=%p transaction=%p size=%d\n",
2112 pipe, transaction, bytes_transferred);
2113 urb->status = -EPIPE;
2115 case CVMX_USB_COMPLETE_SHORT:
2116 dev_dbg(dev, "status=short pipe=%p transaction=%p size=%d\n",
2117 pipe, transaction, bytes_transferred);
2118 urb->status = -EREMOTEIO;
2120 case CVMX_USB_COMPLETE_ERROR:
2121 case CVMX_USB_COMPLETE_XACTERR:
2122 case CVMX_USB_COMPLETE_DATATGLERR:
2123 case CVMX_USB_COMPLETE_FRAMEERR:
2124 dev_dbg(dev, "status=%d pipe=%p transaction=%p size=%d\n",
2125 status, pipe, transaction, bytes_transferred);
2126 urb->status = -EPROTO;
2129 usb_hcd_unlink_urb_from_ep(octeon_to_hcd(priv), urb);
2130 spin_unlock(&priv->lock);
2131 usb_hcd_giveback_urb(octeon_to_hcd(priv), urb, urb->status);
2132 spin_lock(&priv->lock);
2136 * Signal the completion of a transaction and free it. The
2137 * transaction will be removed from the pipe transaction list.
2139 * @usb: USB device state populated by cvmx_usb_initialize().
2140 * @pipe: Pipe the transaction is on
2142 * Transaction that completed
2146 static void __cvmx_usb_perform_complete(
2147 struct cvmx_usb_state *usb,
2148 struct cvmx_usb_pipe *pipe,
2149 struct cvmx_usb_transaction *transaction,
2150 enum cvmx_usb_complete complete_code)
2152 /* If this was a split then clear our split in progress marker */
2153 if (usb->active_split == transaction)
2154 usb->active_split = NULL;
2157 * Isochronous transactions need extra processing as they might not be
2158 * done after a single data transfer
2160 if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
2161 /* Update the number of bytes transferred in this ISO packet */
2162 transaction->iso_packets[0].length = transaction->actual_bytes;
2163 transaction->iso_packets[0].status = complete_code;
2166 * If there are more ISOs pending and we succeeded, schedule the
2169 if ((transaction->iso_number_packets > 1) &&
2170 (complete_code == CVMX_USB_COMPLETE_SUCCESS)) {
2171 /* No bytes transferred for this packet as of yet */
2172 transaction->actual_bytes = 0;
2173 /* One less ISO waiting to transfer */
2174 transaction->iso_number_packets--;
2175 /* Increment to the next location in our packet array */
2176 transaction->iso_packets++;
2177 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2182 /* Remove the transaction from the pipe list */
2183 list_del(&transaction->node);
2184 if (list_empty(&pipe->transactions))
2185 list_move_tail(&pipe->node, &usb->idle_pipes);
2186 octeon_usb_urb_complete_callback(usb, complete_code, pipe,
2188 transaction->actual_bytes,
2195 * Submit a usb transaction to a pipe. Called for all types
2199 * @pipe: Which pipe to submit to.
2200 * @type: Transaction type
2201 * @buffer: User buffer for the transaction
2203 * User buffer's length in bytes
2205 * For control transactions, the 8 byte standard header
2207 * For ISO transactions, the start frame
2208 * @iso_number_packets:
2209 * For ISO, the number of packet in the transaction.
2211 * A description of each ISO packet
2212 * @urb: URB for the callback
2214 * Returns: Transaction or NULL on failure.
2216 static struct cvmx_usb_transaction *__cvmx_usb_submit_transaction(
2217 struct cvmx_usb_state *usb,
2218 struct cvmx_usb_pipe *pipe,
2219 enum cvmx_usb_transfer type,
2222 uint64_t control_header,
2223 int iso_start_frame,
2224 int iso_number_packets,
2225 struct cvmx_usb_iso_packet *iso_packets,
2228 struct cvmx_usb_transaction *transaction;
2230 if (unlikely(pipe->transfer_type != type))
2233 transaction = kzalloc(sizeof(*transaction), GFP_ATOMIC);
2234 if (unlikely(!transaction))
2237 transaction->type = type;
2238 transaction->buffer = buffer;
2239 transaction->buffer_length = buffer_length;
2240 transaction->control_header = control_header;
2241 /* FIXME: This is not used, implement it. */
2242 transaction->iso_start_frame = iso_start_frame;
2243 transaction->iso_number_packets = iso_number_packets;
2244 transaction->iso_packets = iso_packets;
2245 transaction->urb = urb;
2246 if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2247 transaction->stage = CVMX_USB_STAGE_SETUP;
2249 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2251 if (!list_empty(&pipe->transactions)) {
2252 list_add_tail(&transaction->node, &pipe->transactions);
2254 list_add_tail(&transaction->node, &pipe->transactions);
2255 list_move_tail(&pipe->node,
2256 &usb->active_pipes[pipe->transfer_type]);
2259 * We may need to schedule the pipe if this was the head of the
2262 __cvmx_usb_schedule(usb, 0);
2270 * Call to submit a USB Bulk transfer to a pipe.
2272 * @usb: USB device state populated by cvmx_usb_initialize().
2273 * @pipe: Handle to the pipe for the transfer.
2276 * Returns: A submitted transaction or NULL on failure.
2278 static struct cvmx_usb_transaction *cvmx_usb_submit_bulk(
2279 struct cvmx_usb_state *usb,
2280 struct cvmx_usb_pipe *pipe,
2283 return __cvmx_usb_submit_transaction(usb, pipe, CVMX_USB_TRANSFER_BULK,
2285 urb->transfer_buffer_length,
2286 0, /* control_header */
2287 0, /* iso_start_frame */
2288 0, /* iso_number_packets */
2289 NULL, /* iso_packets */
2295 * Call to submit a USB Interrupt transfer to a pipe.
2297 * @usb: USB device state populated by cvmx_usb_initialize().
2298 * @pipe: Handle to the pipe for the transfer.
2299 * @urb: URB returned when the callback is called.
2301 * Returns: A submitted transaction or NULL on failure.
2303 static struct cvmx_usb_transaction *cvmx_usb_submit_interrupt(
2304 struct cvmx_usb_state *usb,
2305 struct cvmx_usb_pipe *pipe,
2308 return __cvmx_usb_submit_transaction(usb, pipe,
2309 CVMX_USB_TRANSFER_INTERRUPT,
2311 urb->transfer_buffer_length,
2312 0, /* control_header */
2313 0, /* iso_start_frame */
2314 0, /* iso_number_packets */
2315 NULL, /* iso_packets */
2321 * Call to submit a USB Control transfer to a pipe.
2323 * @usb: USB device state populated by cvmx_usb_initialize().
2324 * @pipe: Handle to the pipe for the transfer.
2327 * Returns: A submitted transaction or NULL on failure.
2329 static struct cvmx_usb_transaction *cvmx_usb_submit_control(
2330 struct cvmx_usb_state *usb,
2331 struct cvmx_usb_pipe *pipe,
2334 int buffer_length = urb->transfer_buffer_length;
2335 uint64_t control_header = urb->setup_dma;
2336 struct usb_ctrlrequest *header = cvmx_phys_to_ptr(control_header);
2338 if ((header->bRequestType & USB_DIR_IN) == 0)
2339 buffer_length = le16_to_cpu(header->wLength);
2341 return __cvmx_usb_submit_transaction(usb, pipe,
2342 CVMX_USB_TRANSFER_CONTROL,
2343 urb->transfer_dma, buffer_length,
2345 0, /* iso_start_frame */
2346 0, /* iso_number_packets */
2347 NULL, /* iso_packets */
2353 * Call to submit a USB Isochronous transfer to a pipe.
2355 * @usb: USB device state populated by cvmx_usb_initialize().
2356 * @pipe: Handle to the pipe for the transfer.
2357 * @urb: URB returned when the callback is called.
2359 * Returns: A submitted transaction or NULL on failure.
2361 static struct cvmx_usb_transaction *cvmx_usb_submit_isochronous(
2362 struct cvmx_usb_state *usb,
2363 struct cvmx_usb_pipe *pipe,
2366 struct cvmx_usb_iso_packet *packets;
2368 packets = (struct cvmx_usb_iso_packet *) urb->setup_packet;
2369 return __cvmx_usb_submit_transaction(usb, pipe,
2370 CVMX_USB_TRANSFER_ISOCHRONOUS,
2372 urb->transfer_buffer_length,
2373 0, /* control_header */
2375 urb->number_of_packets,
2381 * Cancel one outstanding request in a pipe. Canceling a request
2382 * can fail if the transaction has already completed before cancel
2383 * is called. Even after a successful cancel call, it may take
2384 * a frame or two for the cvmx_usb_poll() function to call the
2385 * associated callback.
2387 * @usb: USB device state populated by cvmx_usb_initialize().
2388 * @pipe: Pipe to cancel requests in.
2389 * @transaction: Transaction to cancel, returned by the submit function.
2391 * Returns: 0 or a negative error code.
2393 static int cvmx_usb_cancel(struct cvmx_usb_state *usb,
2394 struct cvmx_usb_pipe *pipe,
2395 struct cvmx_usb_transaction *transaction)
2398 * If the transaction is the HEAD of the queue and scheduled. We need to
2401 if (list_first_entry(&pipe->transactions, typeof(*transaction), node) ==
2402 transaction && (pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
2403 union cvmx_usbcx_hccharx usbc_hcchar;
2405 usb->pipe_for_channel[pipe->channel] = NULL;
2406 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2410 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
2411 CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
2413 * If the channel isn't enabled then the transaction already
2416 if (usbc_hcchar.s.chena) {
2417 usbc_hcchar.s.chdis = 1;
2418 __cvmx_usb_write_csr32(usb,
2419 CVMX_USBCX_HCCHARX(pipe->channel,
2424 __cvmx_usb_perform_complete(usb, pipe, transaction,
2425 CVMX_USB_COMPLETE_CANCEL);
2431 * Cancel all outstanding requests in a pipe. Logically all this
2432 * does is call cvmx_usb_cancel() in a loop.
2434 * @usb: USB device state populated by cvmx_usb_initialize().
2435 * @pipe: Pipe to cancel requests in.
2437 * Returns: 0 or a negative error code.
2439 static int cvmx_usb_cancel_all(struct cvmx_usb_state *usb,
2440 struct cvmx_usb_pipe *pipe)
2442 struct cvmx_usb_transaction *transaction, *next;
2444 /* Simply loop through and attempt to cancel each transaction */
2445 list_for_each_entry_safe(transaction, next, &pipe->transactions, node) {
2446 int result = cvmx_usb_cancel(usb, pipe, transaction);
2448 if (unlikely(result != 0))
2456 * Close a pipe created with cvmx_usb_open_pipe().
2458 * @usb: USB device state populated by cvmx_usb_initialize().
2459 * @pipe: Pipe to close.
2461 * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
2462 * outstanding transfers.
2464 static int cvmx_usb_close_pipe(struct cvmx_usb_state *usb,
2465 struct cvmx_usb_pipe *pipe)
2467 /* Fail if the pipe has pending transactions */
2468 if (!list_empty(&pipe->transactions))
2471 list_del(&pipe->node);
2478 * Get the current USB protocol level frame number. The frame
2479 * number is always in the range of 0-0x7ff.
2481 * @usb: USB device state populated by cvmx_usb_initialize().
2483 * Returns: USB frame number
2485 static int cvmx_usb_get_frame_number(struct cvmx_usb_state *usb)
2488 union cvmx_usbcx_hfnum usbc_hfnum;
2490 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb,
2491 CVMX_USBCX_HFNUM(usb->index));
2492 frame_number = usbc_hfnum.s.frnum;
2494 return frame_number;
2499 * Poll a channel for status
2502 * @channel: Channel to poll
2504 * Returns: Zero on success
2506 static int __cvmx_usb_poll_channel(struct cvmx_usb_state *usb, int channel)
2508 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
2509 struct usb_hcd *hcd = octeon_to_hcd(priv);
2510 struct device *dev = hcd->self.controller;
2511 union cvmx_usbcx_hcintx usbc_hcint;
2512 union cvmx_usbcx_hctsizx usbc_hctsiz;
2513 union cvmx_usbcx_hccharx usbc_hcchar;
2514 struct cvmx_usb_pipe *pipe;
2515 struct cvmx_usb_transaction *transaction;
2516 int bytes_this_transfer;
2517 int bytes_in_last_packet;
2518 int packets_processed;
2519 int buffer_space_left;
2521 /* Read the interrupt status bits for the channel */
2522 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb,
2523 CVMX_USBCX_HCINTX(channel, usb->index));
2525 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2526 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
2527 CVMX_USBCX_HCCHARX(channel, usb->index));
2529 if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2531 * There seems to be a bug in CN31XX which can cause
2532 * interrupt IN transfers to get stuck until we do a
2533 * write of HCCHARX without changing things
2535 __cvmx_usb_write_csr32(usb,
2536 CVMX_USBCX_HCCHARX(channel,
2543 * In non DMA mode the channels don't halt themselves. We need
2544 * to manually disable channels that are left running
2546 if (!usbc_hcint.s.chhltd) {
2547 if (usbc_hcchar.s.chena) {
2548 union cvmx_usbcx_hcintmskx hcintmsk;
2549 /* Disable all interrupts except CHHLTD */
2551 hcintmsk.s.chhltdmsk = 1;
2552 __cvmx_usb_write_csr32(usb,
2553 CVMX_USBCX_HCINTMSKX(channel,
2556 usbc_hcchar.s.chdis = 1;
2557 __cvmx_usb_write_csr32(usb,
2558 CVMX_USBCX_HCCHARX(channel,
2562 } else if (usbc_hcint.s.xfercompl) {
2564 * Successful IN/OUT with transfer complete.
2565 * Channel halt isn't needed.
2568 dev_err(dev, "USB%d: Channel %d interrupt without halt\n",
2569 usb->index, channel);
2575 * There is are no interrupts that we need to process when the
2576 * channel is still running
2578 if (!usbc_hcint.s.chhltd)
2582 /* Disable the channel interrupts now that it is done */
2583 __cvmx_usb_write_csr32(usb,
2584 CVMX_USBCX_HCINTMSKX(channel, usb->index),
2586 usb->idle_hardware_channels |= (1<<channel);
2588 /* Make sure this channel is tied to a valid pipe */
2589 pipe = usb->pipe_for_channel[channel];
2593 transaction = list_first_entry(&pipe->transactions,
2594 typeof(*transaction),
2596 prefetch(transaction);
2599 * Disconnect this pipe from the HW channel. Later the schedule
2600 * function will figure out which pipe needs to go
2602 usb->pipe_for_channel[channel] = NULL;
2603 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2606 * Read the channel config info so we can figure out how much data
2609 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
2610 CVMX_USBCX_HCCHARX(channel, usb->index));
2611 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
2612 CVMX_USBCX_HCTSIZX(channel, usb->index));
2615 * Calculating the number of bytes successfully transferred is dependent
2616 * on the transfer direction
2618 packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2619 if (usbc_hcchar.s.epdir) {
2621 * IN transactions are easy. For every byte received the
2622 * hardware decrements xfersize. All we need to do is subtract
2623 * the current value of xfersize from its starting value and we
2624 * know how many bytes were written to the buffer
2626 bytes_this_transfer = transaction->xfersize -
2627 usbc_hctsiz.s.xfersize;
2630 * OUT transaction don't decrement xfersize. Instead pktcnt is
2631 * decremented on every successful packet send. The hardware
2632 * does this when it receives an ACK, or NYET. If it doesn't
2633 * receive one of these responses pktcnt doesn't change
2635 bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2637 * The last packet may not be a full transfer if we didn't have
2640 if (bytes_this_transfer > transaction->xfersize)
2641 bytes_this_transfer = transaction->xfersize;
2643 /* Figure out how many bytes were in the last packet of the transfer */
2644 if (packets_processed)
2645 bytes_in_last_packet = bytes_this_transfer -
2646 (packets_processed - 1) * usbc_hcchar.s.mps;
2648 bytes_in_last_packet = bytes_this_transfer;
2651 * As a special case, setup transactions output the setup header, not
2652 * the user's data. For this reason we don't count setup data as bytes
2655 if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2656 (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2657 bytes_this_transfer = 0;
2660 * Add the bytes transferred to the running total. It is important that
2661 * bytes_this_transfer doesn't count any data that needs to be
2664 transaction->actual_bytes += bytes_this_transfer;
2665 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
2666 buffer_space_left = transaction->iso_packets[0].length -
2667 transaction->actual_bytes;
2669 buffer_space_left = transaction->buffer_length -
2670 transaction->actual_bytes;
2673 * We need to remember the PID toggle state for the next transaction.
2674 * The hardware already updated it for the next transaction
2676 pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2679 * For high speed bulk out, assume the next transaction will need to do
2680 * a ping before proceeding. If this isn't true the ACK processing below
2681 * will clear this flag
2683 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2684 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2685 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2686 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
2688 if (usbc_hcint.s.stall) {
2690 * STALL as a response means this transaction cannot be
2691 * completed because the device can't process transactions. Tell
2692 * the user. Any data that was transferred will be counted on
2693 * the actual bytes transferred
2695 pipe->pid_toggle = 0;
2696 __cvmx_usb_perform_complete(usb, pipe, transaction,
2697 CVMX_USB_COMPLETE_STALL);
2698 } else if (usbc_hcint.s.xacterr) {
2700 * We know at least one packet worked if we get a ACK or NAK.
2701 * Reset the retry counter
2703 if (usbc_hcint.s.nak || usbc_hcint.s.ack)
2704 transaction->retries = 0;
2705 transaction->retries++;
2706 if (transaction->retries > MAX_RETRIES) {
2708 * XactErr as a response means the device signaled
2709 * something wrong with the transfer. For example, PID
2710 * toggle errors cause these
2712 __cvmx_usb_perform_complete(usb, pipe, transaction,
2713 CVMX_USB_COMPLETE_XACTERR);
2716 * If this was a split then clear our split in progress
2719 if (usb->active_split == transaction)
2720 usb->active_split = NULL;
2722 * Rewind to the beginning of the transaction by anding
2723 * off the split complete bit
2725 transaction->stage &= ~1;
2726 pipe->split_sc_frame = -1;
2727 pipe->next_tx_frame += pipe->interval;
2728 if (pipe->next_tx_frame < usb->frame_number)
2729 pipe->next_tx_frame =
2730 usb->frame_number + pipe->interval -
2731 (usb->frame_number -
2732 pipe->next_tx_frame) % pipe->interval;
2734 } else if (usbc_hcint.s.bblerr) {
2735 /* Babble Error (BblErr) */
2736 __cvmx_usb_perform_complete(usb, pipe, transaction,
2737 CVMX_USB_COMPLETE_BABBLEERR);
2738 } else if (usbc_hcint.s.datatglerr) {
2739 /* Data toggle error */
2740 __cvmx_usb_perform_complete(usb, pipe, transaction,
2741 CVMX_USB_COMPLETE_DATATGLERR);
2742 } else if (usbc_hcint.s.nyet) {
2744 * NYET as a response is only allowed in three cases: as a
2745 * response to a ping, as a response to a split transaction, and
2746 * as a response to a bulk out. The ping case is handled by
2747 * hardware, so we only have splits and bulk out
2749 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
2750 transaction->retries = 0;
2752 * If there is more data to go then we need to try
2753 * again. Otherwise this transaction is complete
2755 if ((buffer_space_left == 0) ||
2756 (bytes_in_last_packet < pipe->max_packet))
2757 __cvmx_usb_perform_complete(usb, pipe,
2759 CVMX_USB_COMPLETE_SUCCESS);
2762 * Split transactions retry the split complete 4 times
2763 * then rewind to the start split and do the entire
2764 * transactions again
2766 transaction->retries++;
2767 if ((transaction->retries & 0x3) == 0) {
2769 * Rewind to the beginning of the transaction by
2770 * anding off the split complete bit
2772 transaction->stage &= ~1;
2773 pipe->split_sc_frame = -1;
2776 } else if (usbc_hcint.s.ack) {
2777 transaction->retries = 0;
2779 * The ACK bit can only be checked after the other error bits.
2780 * This is because a multi packet transfer may succeed in a
2781 * number of packets and then get a different response on the
2782 * last packet. In this case both ACK and the last response bit
2783 * will be set. If none of the other response bits is set, then
2784 * the last packet must have been an ACK
2786 * Since we got an ACK, we know we don't need to do a ping on
2789 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_NEED_PING;
2791 switch (transaction->type) {
2792 case CVMX_USB_TRANSFER_CONTROL:
2793 switch (transaction->stage) {
2794 case CVMX_USB_STAGE_NON_CONTROL:
2795 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2796 /* This should be impossible */
2797 __cvmx_usb_perform_complete(usb, pipe,
2798 transaction, CVMX_USB_COMPLETE_ERROR);
2800 case CVMX_USB_STAGE_SETUP:
2801 pipe->pid_toggle = 1;
2802 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2803 transaction->stage =
2804 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
2806 struct usb_ctrlrequest *header =
2807 cvmx_phys_to_ptr(transaction->control_header);
2808 if (header->wLength)
2809 transaction->stage =
2810 CVMX_USB_STAGE_DATA;
2812 transaction->stage =
2813 CVMX_USB_STAGE_STATUS;
2816 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2818 struct usb_ctrlrequest *header =
2819 cvmx_phys_to_ptr(transaction->control_header);
2820 if (header->wLength)
2821 transaction->stage =
2822 CVMX_USB_STAGE_DATA;
2824 transaction->stage =
2825 CVMX_USB_STAGE_STATUS;
2828 case CVMX_USB_STAGE_DATA:
2829 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2830 transaction->stage =
2831 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
2833 * For setup OUT data that are splits,
2834 * the hardware doesn't appear to count
2835 * transferred data. Here we manually
2836 * update the data transferred
2838 if (!usbc_hcchar.s.epdir) {
2839 if (buffer_space_left < pipe->max_packet)
2840 transaction->actual_bytes +=
2843 transaction->actual_bytes +=
2846 } else if ((buffer_space_left == 0) ||
2847 (bytes_in_last_packet <
2848 pipe->max_packet)) {
2849 pipe->pid_toggle = 1;
2850 transaction->stage =
2851 CVMX_USB_STAGE_STATUS;
2854 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
2855 if ((buffer_space_left == 0) ||
2856 (bytes_in_last_packet <
2857 pipe->max_packet)) {
2858 pipe->pid_toggle = 1;
2859 transaction->stage =
2860 CVMX_USB_STAGE_STATUS;
2862 transaction->stage =
2863 CVMX_USB_STAGE_DATA;
2866 case CVMX_USB_STAGE_STATUS:
2867 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2868 transaction->stage =
2869 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
2871 __cvmx_usb_perform_complete(usb, pipe,
2873 CVMX_USB_COMPLETE_SUCCESS);
2875 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
2876 __cvmx_usb_perform_complete(usb, pipe,
2878 CVMX_USB_COMPLETE_SUCCESS);
2882 case CVMX_USB_TRANSFER_BULK:
2883 case CVMX_USB_TRANSFER_INTERRUPT:
2885 * The only time a bulk transfer isn't complete when it
2886 * finishes with an ACK is during a split transaction.
2887 * For splits we need to continue the transfer if more
2890 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2891 if (transaction->stage ==
2892 CVMX_USB_STAGE_NON_CONTROL)
2893 transaction->stage =
2894 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2896 if (buffer_space_left &&
2897 (bytes_in_last_packet ==
2899 transaction->stage =
2900 CVMX_USB_STAGE_NON_CONTROL;
2902 if (transaction->type ==
2903 CVMX_USB_TRANSFER_INTERRUPT)
2904 pipe->next_tx_frame +=
2906 __cvmx_usb_perform_complete(
2910 CVMX_USB_COMPLETE_SUCCESS);
2914 if ((pipe->device_speed ==
2915 CVMX_USB_SPEED_HIGH) &&
2916 (pipe->transfer_type ==
2917 CVMX_USB_TRANSFER_BULK) &&
2918 (pipe->transfer_dir ==
2919 CVMX_USB_DIRECTION_OUT) &&
2922 __CVMX_USB_PIPE_FLAGS_NEED_PING;
2923 if (!buffer_space_left ||
2924 (bytes_in_last_packet <
2925 pipe->max_packet)) {
2926 if (transaction->type ==
2927 CVMX_USB_TRANSFER_INTERRUPT)
2928 pipe->next_tx_frame +=
2930 __cvmx_usb_perform_complete(usb,
2933 CVMX_USB_COMPLETE_SUCCESS);
2937 case CVMX_USB_TRANSFER_ISOCHRONOUS:
2938 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2940 * ISOCHRONOUS OUT splits don't require a
2941 * complete split stage. Instead they use a
2942 * sequence of begin OUT splits to transfer the
2943 * data 188 bytes at a time. Once the transfer
2944 * is complete, the pipe sleeps until the next
2947 if (pipe->transfer_dir ==
2948 CVMX_USB_DIRECTION_OUT) {
2950 * If no space left or this wasn't a max
2951 * size packet then this transfer is
2952 * complete. Otherwise start it again to
2953 * send the next 188 bytes
2955 if (!buffer_space_left ||
2956 (bytes_this_transfer < 188)) {
2957 pipe->next_tx_frame +=
2959 __cvmx_usb_perform_complete(
2963 CVMX_USB_COMPLETE_SUCCESS);
2966 if (transaction->stage ==
2967 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
2969 * We are in the incoming data
2970 * phase. Keep getting data
2971 * until we run out of space or
2972 * get a small packet
2974 if ((buffer_space_left == 0) ||
2975 (bytes_in_last_packet <
2976 pipe->max_packet)) {
2977 pipe->next_tx_frame +=
2979 __cvmx_usb_perform_complete(
2983 CVMX_USB_COMPLETE_SUCCESS);
2986 transaction->stage =
2987 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2990 pipe->next_tx_frame += pipe->interval;
2991 __cvmx_usb_perform_complete(usb,
2994 CVMX_USB_COMPLETE_SUCCESS);
2998 } else if (usbc_hcint.s.nak) {
3000 * If this was a split then clear our split in progress marker.
3002 if (usb->active_split == transaction)
3003 usb->active_split = NULL;
3005 * NAK as a response means the device couldn't accept the
3006 * transaction, but it should be retried in the future. Rewind
3007 * to the beginning of the transaction by anding off the split
3008 * complete bit. Retry in the next interval
3010 transaction->retries = 0;
3011 transaction->stage &= ~1;
3012 pipe->next_tx_frame += pipe->interval;
3013 if (pipe->next_tx_frame < usb->frame_number)
3014 pipe->next_tx_frame = usb->frame_number +
3016 (usb->frame_number - pipe->next_tx_frame) %
3019 struct cvmx_usb_port_status port;
3021 port = cvmx_usb_get_status(usb);
3022 if (port.port_enabled) {
3023 /* We'll retry the exact same transaction again */
3024 transaction->retries++;
3027 * We get channel halted interrupts with no result bits
3028 * sets when the cable is unplugged
3030 __cvmx_usb_perform_complete(usb, pipe, transaction,
3031 CVMX_USB_COMPLETE_ERROR);
3037 static void octeon_usb_port_callback(struct cvmx_usb_state *usb)
3039 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
3041 spin_unlock(&priv->lock);
3042 usb_hcd_poll_rh_status(octeon_to_hcd(priv));
3043 spin_lock(&priv->lock);
3047 * Poll the USB block for status and call all needed callback
3048 * handlers. This function is meant to be called in the interrupt
3049 * handler for the USB controller. It can also be called
3050 * periodically in a loop for non-interrupt based operation.
3052 * @usb: USB device state populated by cvmx_usb_initialize().
3054 * Returns: 0 or a negative error code.
3056 static int cvmx_usb_poll(struct cvmx_usb_state *usb)
3058 union cvmx_usbcx_hfnum usbc_hfnum;
3059 union cvmx_usbcx_gintsts usbc_gintsts;
3061 prefetch_range(usb, sizeof(*usb));
3063 /* Update the frame counter */
3064 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb,
3065 CVMX_USBCX_HFNUM(usb->index));
3066 if ((usb->frame_number&0x3fff) > usbc_hfnum.s.frnum)
3067 usb->frame_number += 0x4000;
3068 usb->frame_number &= ~0x3fffull;
3069 usb->frame_number |= usbc_hfnum.s.frnum;
3071 /* Read the pending interrupts */
3072 usbc_gintsts.u32 = __cvmx_usb_read_csr32(usb,
3073 CVMX_USBCX_GINTSTS(usb->index));
3075 /* Clear the interrupts now that we know about them */
3076 __cvmx_usb_write_csr32(usb,
3077 CVMX_USBCX_GINTSTS(usb->index),
3080 if (usbc_gintsts.s.rxflvl) {
3082 * RxFIFO Non-Empty (RxFLvl)
3083 * Indicates that there is at least one packet pending to be
3084 * read from the RxFIFO.
3086 * In DMA mode this is handled by hardware
3088 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3089 __cvmx_usb_poll_rx_fifo(usb);
3091 if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
3092 /* Fill the Tx FIFOs when not in DMA mode */
3093 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3094 __cvmx_usb_poll_tx_fifo(usb);
3096 if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
3097 union cvmx_usbcx_hprt usbc_hprt;
3099 * Disconnect Detected Interrupt (DisconnInt)
3100 * Asserted when a device disconnect is detected.
3102 * Host Port Interrupt (PrtInt)
3103 * The core sets this bit to indicate a change in port status of
3104 * one of the O2P USB core ports in Host mode. The application
3105 * must read the Host Port Control and Status (HPRT) register to
3106 * determine the exact event that caused this interrupt. The
3107 * application must clear the appropriate status bit in the Host
3108 * Port Control and Status register to clear this bit.
3110 * Call the user's port callback
3112 octeon_usb_port_callback(usb);
3113 /* Clear the port change bits */
3114 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb,
3115 CVMX_USBCX_HPRT(usb->index));
3116 usbc_hprt.s.prtena = 0;
3117 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index),
3120 if (usbc_gintsts.s.hchint) {
3122 * Host Channels Interrupt (HChInt)
3123 * The core sets this bit to indicate that an interrupt is
3124 * pending on one of the channels of the core (in Host mode).
3125 * The application must read the Host All Channels Interrupt
3126 * (HAINT) register to determine the exact number of the channel
3127 * on which the interrupt occurred, and then read the
3128 * corresponding Host Channel-n Interrupt (HCINTn) register to
3129 * determine the exact cause of the interrupt. The application
3130 * must clear the appropriate status bit in the HCINTn register
3131 * to clear this bit.
3133 union cvmx_usbcx_haint usbc_haint;
3135 usbc_haint.u32 = __cvmx_usb_read_csr32(usb,
3136 CVMX_USBCX_HAINT(usb->index));
3137 while (usbc_haint.u32) {
3140 channel = __fls(usbc_haint.u32);
3141 __cvmx_usb_poll_channel(usb, channel);
3142 usbc_haint.u32 ^= 1<<channel;
3146 __cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
3151 /* convert between an HCD pointer and the corresponding struct octeon_hcd */
3152 static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
3154 return (struct octeon_hcd *)(hcd->hcd_priv);
3157 static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
3159 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3160 unsigned long flags;
3162 spin_lock_irqsave(&priv->lock, flags);
3163 cvmx_usb_poll(&priv->usb);
3164 spin_unlock_irqrestore(&priv->lock, flags);
3168 static int octeon_usb_start(struct usb_hcd *hcd)
3170 hcd->state = HC_STATE_RUNNING;
3174 static void octeon_usb_stop(struct usb_hcd *hcd)
3176 hcd->state = HC_STATE_HALT;
3179 static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
3181 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3183 return cvmx_usb_get_frame_number(&priv->usb);
3186 static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
3190 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3191 struct device *dev = hcd->self.controller;
3192 struct cvmx_usb_transaction *transaction = NULL;
3193 struct cvmx_usb_pipe *pipe;
3194 unsigned long flags;
3195 struct cvmx_usb_iso_packet *iso_packet;
3196 struct usb_host_endpoint *ep = urb->ep;
3200 spin_lock_irqsave(&priv->lock, flags);
3202 rc = usb_hcd_link_urb_to_ep(hcd, urb);
3204 spin_unlock_irqrestore(&priv->lock, flags);
3209 enum cvmx_usb_transfer transfer_type;
3210 enum cvmx_usb_speed speed;
3211 int split_device = 0;
3214 switch (usb_pipetype(urb->pipe)) {
3215 case PIPE_ISOCHRONOUS:
3216 transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
3218 case PIPE_INTERRUPT:
3219 transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
3222 transfer_type = CVMX_USB_TRANSFER_CONTROL;
3225 transfer_type = CVMX_USB_TRANSFER_BULK;
3228 switch (urb->dev->speed) {
3230 speed = CVMX_USB_SPEED_LOW;
3232 case USB_SPEED_FULL:
3233 speed = CVMX_USB_SPEED_FULL;
3236 speed = CVMX_USB_SPEED_HIGH;
3240 * For slow devices on high speed ports we need to find the hub
3241 * that does the speed translation so we know where to send the
3242 * split transactions.
3244 if (speed != CVMX_USB_SPEED_HIGH) {
3246 * Start at this device and work our way up the usb
3249 struct usb_device *dev = urb->dev;
3251 while (dev->parent) {
3253 * If our parent is high speed then he'll
3254 * receive the splits.
3256 if (dev->parent->speed == USB_SPEED_HIGH) {
3257 split_device = dev->parent->devnum;
3258 split_port = dev->portnum;
3262 * Move up the tree one level. If we make it all
3263 * the way up the tree, then the port must not
3264 * be in high speed mode and we don't need a
3270 pipe = cvmx_usb_open_pipe(&priv->usb, usb_pipedevice(urb->pipe),
3271 usb_pipeendpoint(urb->pipe), speed,
3272 le16_to_cpu(ep->desc.wMaxPacketSize)
3275 usb_pipein(urb->pipe) ?
3276 CVMX_USB_DIRECTION_IN :
3277 CVMX_USB_DIRECTION_OUT,
3279 (le16_to_cpu(ep->desc.wMaxPacketSize)
3281 split_device, split_port);
3283 usb_hcd_unlink_urb_from_ep(hcd, urb);
3284 spin_unlock_irqrestore(&priv->lock, flags);
3285 dev_dbg(dev, "Failed to create pipe\n");
3293 switch (usb_pipetype(urb->pipe)) {
3294 case PIPE_ISOCHRONOUS:
3295 dev_dbg(dev, "Submit isochronous to %d.%d\n",
3296 usb_pipedevice(urb->pipe),
3297 usb_pipeendpoint(urb->pipe));
3299 * Allocate a structure to use for our private list of
3300 * isochronous packets.
3302 iso_packet = kmalloc(urb->number_of_packets *
3303 sizeof(struct cvmx_usb_iso_packet),
3307 /* Fill the list with the data from the URB */
3308 for (i = 0; i < urb->number_of_packets; i++) {
3309 iso_packet[i].offset =
3310 urb->iso_frame_desc[i].offset;
3311 iso_packet[i].length =
3312 urb->iso_frame_desc[i].length;
3313 iso_packet[i].status =
3314 CVMX_USB_COMPLETE_ERROR;
3317 * Store a pointer to the list in the URB setup_packet
3318 * field. We know this currently isn't being used and
3319 * this saves us a bunch of logic.
3321 urb->setup_packet = (char *)iso_packet;
3322 transaction = cvmx_usb_submit_isochronous(&priv->usb,
3325 * If submit failed we need to free our private packet
3329 urb->setup_packet = NULL;
3334 case PIPE_INTERRUPT:
3335 dev_dbg(dev, "Submit interrupt to %d.%d\n",
3336 usb_pipedevice(urb->pipe),
3337 usb_pipeendpoint(urb->pipe));
3338 transaction = cvmx_usb_submit_interrupt(&priv->usb, pipe, urb);
3341 dev_dbg(dev, "Submit control to %d.%d\n",
3342 usb_pipedevice(urb->pipe),
3343 usb_pipeendpoint(urb->pipe));
3344 transaction = cvmx_usb_submit_control(&priv->usb, pipe, urb);
3347 dev_dbg(dev, "Submit bulk to %d.%d\n",
3348 usb_pipedevice(urb->pipe),
3349 usb_pipeendpoint(urb->pipe));
3350 transaction = cvmx_usb_submit_bulk(&priv->usb, pipe, urb);
3354 usb_hcd_unlink_urb_from_ep(hcd, urb);
3355 spin_unlock_irqrestore(&priv->lock, flags);
3356 dev_dbg(dev, "Failed to submit\n");
3359 urb->hcpriv = transaction;
3360 spin_unlock_irqrestore(&priv->lock, flags);
3364 static int octeon_usb_urb_dequeue(struct usb_hcd *hcd,
3368 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3369 unsigned long flags;
3375 spin_lock_irqsave(&priv->lock, flags);
3377 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
3381 urb->status = status;
3382 cvmx_usb_cancel(&priv->usb, urb->ep->hcpriv, urb->hcpriv);
3385 spin_unlock_irqrestore(&priv->lock, flags);
3390 static void octeon_usb_endpoint_disable(struct usb_hcd *hcd,
3391 struct usb_host_endpoint *ep)
3393 struct device *dev = hcd->self.controller;
3396 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3397 struct cvmx_usb_pipe *pipe = ep->hcpriv;
3398 unsigned long flags;
3400 spin_lock_irqsave(&priv->lock, flags);
3401 cvmx_usb_cancel_all(&priv->usb, pipe);
3402 if (cvmx_usb_close_pipe(&priv->usb, pipe))
3403 dev_dbg(dev, "Closing pipe %p failed\n", pipe);
3404 spin_unlock_irqrestore(&priv->lock, flags);
3409 static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
3411 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3412 struct cvmx_usb_port_status port_status;
3413 unsigned long flags;
3415 spin_lock_irqsave(&priv->lock, flags);
3416 port_status = cvmx_usb_get_status(&priv->usb);
3417 spin_unlock_irqrestore(&priv->lock, flags);
3419 buf[0] = port_status.connect_change << 1;
3424 static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3425 u16 wIndex, char *buf, u16 wLength)
3427 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3428 struct device *dev = hcd->self.controller;
3429 struct cvmx_usb_port_status usb_port_status;
3431 struct usb_hub_descriptor *desc;
3432 unsigned long flags;
3435 case ClearHubFeature:
3436 dev_dbg(dev, "ClearHubFeature\n");
3438 case C_HUB_LOCAL_POWER:
3439 case C_HUB_OVER_CURRENT:
3440 /* Nothing required here */
3446 case ClearPortFeature:
3447 dev_dbg(dev, "ClearPortFeature\n");
3449 dev_dbg(dev, " INVALID\n");
3454 case USB_PORT_FEAT_ENABLE:
3455 dev_dbg(dev, " ENABLE\n");
3456 spin_lock_irqsave(&priv->lock, flags);
3457 cvmx_usb_disable(&priv->usb);
3458 spin_unlock_irqrestore(&priv->lock, flags);
3460 case USB_PORT_FEAT_SUSPEND:
3461 dev_dbg(dev, " SUSPEND\n");
3462 /* Not supported on Octeon */
3464 case USB_PORT_FEAT_POWER:
3465 dev_dbg(dev, " POWER\n");
3466 /* Not supported on Octeon */
3468 case USB_PORT_FEAT_INDICATOR:
3469 dev_dbg(dev, " INDICATOR\n");
3470 /* Port inidicator not supported */
3472 case USB_PORT_FEAT_C_CONNECTION:
3473 dev_dbg(dev, " C_CONNECTION\n");
3474 /* Clears drivers internal connect status change flag */
3475 spin_lock_irqsave(&priv->lock, flags);
3476 priv->usb.port_status =
3477 cvmx_usb_get_status(&priv->usb);
3478 spin_unlock_irqrestore(&priv->lock, flags);
3480 case USB_PORT_FEAT_C_RESET:
3481 dev_dbg(dev, " C_RESET\n");
3483 * Clears the driver's internal Port Reset Change flag.
3485 spin_lock_irqsave(&priv->lock, flags);
3486 priv->usb.port_status =
3487 cvmx_usb_get_status(&priv->usb);
3488 spin_unlock_irqrestore(&priv->lock, flags);
3490 case USB_PORT_FEAT_C_ENABLE:
3491 dev_dbg(dev, " C_ENABLE\n");
3493 * Clears the driver's internal Port Enable/Disable
3496 spin_lock_irqsave(&priv->lock, flags);
3497 priv->usb.port_status =
3498 cvmx_usb_get_status(&priv->usb);
3499 spin_unlock_irqrestore(&priv->lock, flags);
3501 case USB_PORT_FEAT_C_SUSPEND:
3502 dev_dbg(dev, " C_SUSPEND\n");
3504 * Clears the driver's internal Port Suspend Change
3505 * flag, which is set when resume signaling on the host
3509 case USB_PORT_FEAT_C_OVER_CURRENT:
3510 dev_dbg(dev, " C_OVER_CURRENT\n");
3511 /* Clears the driver's overcurrent Change flag */
3512 spin_lock_irqsave(&priv->lock, flags);
3513 priv->usb.port_status =
3514 cvmx_usb_get_status(&priv->usb);
3515 spin_unlock_irqrestore(&priv->lock, flags);
3518 dev_dbg(dev, " UNKNOWN\n");
3522 case GetHubDescriptor:
3523 dev_dbg(dev, "GetHubDescriptor\n");
3524 desc = (struct usb_hub_descriptor *)buf;
3525 desc->bDescLength = 9;
3526 desc->bDescriptorType = 0x29;
3527 desc->bNbrPorts = 1;
3528 desc->wHubCharacteristics = cpu_to_le16(0x08);
3529 desc->bPwrOn2PwrGood = 1;
3530 desc->bHubContrCurrent = 0;
3531 desc->u.hs.DeviceRemovable[0] = 0;
3532 desc->u.hs.DeviceRemovable[1] = 0xff;
3535 dev_dbg(dev, "GetHubStatus\n");
3536 *(__le32 *) buf = 0;
3539 dev_dbg(dev, "GetPortStatus\n");
3541 dev_dbg(dev, " INVALID\n");
3545 spin_lock_irqsave(&priv->lock, flags);
3546 usb_port_status = cvmx_usb_get_status(&priv->usb);
3547 spin_unlock_irqrestore(&priv->lock, flags);
3550 if (usb_port_status.connect_change) {
3551 port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
3552 dev_dbg(dev, " C_CONNECTION\n");
3555 if (usb_port_status.port_enabled) {
3556 port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
3557 dev_dbg(dev, " C_ENABLE\n");
3560 if (usb_port_status.connected) {
3561 port_status |= (1 << USB_PORT_FEAT_CONNECTION);
3562 dev_dbg(dev, " CONNECTION\n");
3565 if (usb_port_status.port_enabled) {
3566 port_status |= (1 << USB_PORT_FEAT_ENABLE);
3567 dev_dbg(dev, " ENABLE\n");
3570 if (usb_port_status.port_over_current) {
3571 port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
3572 dev_dbg(dev, " OVER_CURRENT\n");
3575 if (usb_port_status.port_powered) {
3576 port_status |= (1 << USB_PORT_FEAT_POWER);
3577 dev_dbg(dev, " POWER\n");
3580 if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
3581 port_status |= USB_PORT_STAT_HIGH_SPEED;
3582 dev_dbg(dev, " HIGHSPEED\n");
3583 } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
3584 port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
3585 dev_dbg(dev, " LOWSPEED\n");
3588 *((__le32 *) buf) = cpu_to_le32(port_status);
3591 dev_dbg(dev, "SetHubFeature\n");
3592 /* No HUB features supported */
3594 case SetPortFeature:
3595 dev_dbg(dev, "SetPortFeature\n");
3597 dev_dbg(dev, " INVALID\n");
3602 case USB_PORT_FEAT_SUSPEND:
3603 dev_dbg(dev, " SUSPEND\n");
3605 case USB_PORT_FEAT_POWER:
3606 dev_dbg(dev, " POWER\n");
3608 case USB_PORT_FEAT_RESET:
3609 dev_dbg(dev, " RESET\n");
3610 spin_lock_irqsave(&priv->lock, flags);
3611 cvmx_usb_disable(&priv->usb);
3612 if (cvmx_usb_enable(&priv->usb))
3613 dev_dbg(dev, "Failed to enable the port\n");
3614 spin_unlock_irqrestore(&priv->lock, flags);
3616 case USB_PORT_FEAT_INDICATOR:
3617 dev_dbg(dev, " INDICATOR\n");
3621 dev_dbg(dev, " UNKNOWN\n");
3626 dev_dbg(dev, "Unknown root hub request\n");
3632 static const struct hc_driver octeon_hc_driver = {
3633 .description = "Octeon USB",
3634 .product_desc = "Octeon Host Controller",
3635 .hcd_priv_size = sizeof(struct octeon_hcd),
3636 .irq = octeon_usb_irq,
3637 .flags = HCD_MEMORY | HCD_USB2,
3638 .start = octeon_usb_start,
3639 .stop = octeon_usb_stop,
3640 .urb_enqueue = octeon_usb_urb_enqueue,
3641 .urb_dequeue = octeon_usb_urb_dequeue,
3642 .endpoint_disable = octeon_usb_endpoint_disable,
3643 .get_frame_number = octeon_usb_get_frame_number,
3644 .hub_status_data = octeon_usb_hub_status_data,
3645 .hub_control = octeon_usb_hub_control,
3646 .map_urb_for_dma = octeon_map_urb_for_dma,
3647 .unmap_urb_for_dma = octeon_unmap_urb_for_dma,
3650 static int octeon_usb_probe(struct platform_device *pdev)
3653 int initialize_flags;
3655 struct resource *res_mem;
3656 struct device_node *usbn_node;
3657 int irq = platform_get_irq(pdev, 0);
3658 struct device *dev = &pdev->dev;
3659 struct octeon_hcd *priv;
3660 struct usb_hcd *hcd;
3661 unsigned long flags;
3662 u32 clock_rate = 48000000;
3663 bool is_crystal_clock = false;
3664 const char *clock_type;
3667 if (dev->of_node == NULL) {
3668 dev_err(dev, "Error: empty of_node\n");
3671 usbn_node = dev->of_node->parent;
3673 i = of_property_read_u32(usbn_node,
3674 "refclk-frequency", &clock_rate);
3676 dev_err(dev, "No USBN \"refclk-frequency\"\n");
3679 switch (clock_rate) {
3681 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
3684 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
3687 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
3690 dev_err(dev, "Illebal USBN \"refclk-frequency\" %u\n",
3696 i = of_property_read_string(usbn_node,
3697 "refclk-type", &clock_type);
3699 if (!i && strcmp("crystal", clock_type) == 0)
3700 is_crystal_clock = true;
3702 if (is_crystal_clock)
3703 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
3705 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
3707 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3708 if (res_mem == NULL) {
3709 dev_err(dev, "found no memory resource\n");
3712 usb_num = (res_mem->start >> 44) & 1;
3715 /* Defective device tree, but we know how to fix it. */
3716 irq_hw_number_t hwirq = usb_num ? (1 << 6) + 17 : 56;
3718 irq = irq_create_mapping(NULL, hwirq);
3722 * Set the DMA mask to 64bits so we get buffers already translated for
3725 dev->coherent_dma_mask = ~0;
3726 dev->dma_mask = &dev->coherent_dma_mask;
3729 * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
3730 * IOB priority registers. Under heavy network load USB
3731 * hardware can be starved by the IOB causing a crash. Give
3732 * it a priority boost if it has been waiting more than 400
3733 * cycles to avoid this situation.
3735 * Testing indicates that a cnt_val of 8192 is not sufficient,
3736 * but no failures are seen with 4096. We choose a value of
3737 * 400 to give a safety factor of 10.
3739 if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
3740 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
3743 pri_cnt.s.cnt_enb = 1;
3744 pri_cnt.s.cnt_val = 400;
3745 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
3748 hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
3750 dev_dbg(dev, "Failed to allocate memory for HCD\n");
3753 hcd->uses_new_polling = 1;
3754 priv = (struct octeon_hcd *)hcd->hcd_priv;
3756 spin_lock_init(&priv->lock);
3758 status = cvmx_usb_initialize(&priv->usb, usb_num, initialize_flags);
3760 dev_dbg(dev, "USB initialization failed with %d\n", status);
3765 /* This delay is needed for CN3010, but I don't know why... */
3768 spin_lock_irqsave(&priv->lock, flags);
3769 cvmx_usb_poll(&priv->usb);
3770 spin_unlock_irqrestore(&priv->lock, flags);
3772 status = usb_add_hcd(hcd, irq, 0);
3774 dev_dbg(dev, "USB add HCD failed with %d\n", status);
3778 device_wakeup_enable(hcd->self.controller);
3780 dev_info(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
3785 static int octeon_usb_remove(struct platform_device *pdev)
3788 struct device *dev = &pdev->dev;
3789 struct usb_hcd *hcd = dev_get_drvdata(dev);
3790 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3791 unsigned long flags;
3793 usb_remove_hcd(hcd);
3794 spin_lock_irqsave(&priv->lock, flags);
3795 status = cvmx_usb_shutdown(&priv->usb);
3796 spin_unlock_irqrestore(&priv->lock, flags);
3798 dev_dbg(dev, "USB shutdown failed with %d\n", status);
3805 static const struct of_device_id octeon_usb_match[] = {
3807 .compatible = "cavium,octeon-5750-usbc",
3812 static struct platform_driver octeon_usb_driver = {
3814 .name = "OcteonUSB",
3815 .of_match_table = octeon_usb_match,
3817 .probe = octeon_usb_probe,
3818 .remove = octeon_usb_remove,
3821 static int __init octeon_usb_driver_init(void)
3826 return platform_driver_register(&octeon_usb_driver);
3828 module_init(octeon_usb_driver_init);
3830 static void __exit octeon_usb_driver_exit(void)
3835 platform_driver_unregister(&octeon_usb_driver);
3837 module_exit(octeon_usb_driver_exit);
3839 MODULE_LICENSE("GPL");
3840 MODULE_AUTHOR("Cavium, Inc. <support@cavium.com>");
3841 MODULE_DESCRIPTION("Cavium Inc. OCTEON USB Host driver.");