xref: /linux/drivers/usb/host/octeon-hcd.c (revision 68ee2f70d0257aa327ebf8406cd28475574585f2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file is subject to the terms and conditions of the GNU General Public
4  * License.  See the file "COPYING" in the main directory of this archive
5  * for more details.
6  *
7  * Copyright (C) 2008 Cavium Networks
8  *
9  * Some parts of the code were originally released under BSD license:
10  *
11  * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
12  * reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met:
17  *
18  *   * Redistributions of source code must retain the above copyright
19  *     notice, this list of conditions and the following disclaimer.
20  *
21  *   * Redistributions in binary form must reproduce the above
22  *     copyright notice, this list of conditions and the following
23  *     disclaimer in the documentation and/or other materials provided
24  *     with the distribution.
25  *
26  *   * Neither the name of Cavium Networks nor the names of
27  *     its contributors may be used to endorse or promote products
28  *     derived from this software without specific prior written
29  *     permission.
30  *
31  * This Software, including technical data, may be subject to U.S. export
32  * control laws, including the U.S. Export Administration Act and its associated
33  * regulations, and may be subject to export or import regulations in other
34  * countries.
35  *
36  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
37  * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
38  * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
39  * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION
40  * OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
41  * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
42  * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
43  * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
44  * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
45  * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
46  */
47 
48 #include <linux/usb.h>
49 #include <linux/slab.h>
50 #include <linux/module.h>
51 #include <linux/usb/hcd.h>
52 #include <linux/prefetch.h>
53 #include <linux/irqdomain.h>
54 #include <linux/dma-mapping.h>
55 #include <linux/platform_device.h>
56 #include <linux/of.h>
57 
58 #include <asm/octeon/octeon.h>
59 
60 #include "octeon-hcd.h"
61 
62 /**
63  * enum cvmx_usb_speed - the possible USB device speeds
64  *
65  * @CVMX_USB_SPEED_HIGH: Device is operation at 480Mbps
66  * @CVMX_USB_SPEED_FULL: Device is operation at 12Mbps
67  * @CVMX_USB_SPEED_LOW:  Device is operation at 1.5Mbps
68  */
69 enum cvmx_usb_speed {
70 	CVMX_USB_SPEED_HIGH = 0,
71 	CVMX_USB_SPEED_FULL = 1,
72 	CVMX_USB_SPEED_LOW = 2,
73 };
74 
75 /**
76  * enum cvmx_usb_transfer - the possible USB transfer types
77  *
78  * @CVMX_USB_TRANSFER_CONTROL:	   USB transfer type control for hub and status
79  *				   transfers
80  * @CVMX_USB_TRANSFER_ISOCHRONOUS: USB transfer type isochronous for low
81  *				   priority periodic transfers
82  * @CVMX_USB_TRANSFER_BULK:	   USB transfer type bulk for large low priority
83  *				   transfers
84  * @CVMX_USB_TRANSFER_INTERRUPT:   USB transfer type interrupt for high priority
85  *				   periodic transfers
86  */
87 enum cvmx_usb_transfer {
88 	CVMX_USB_TRANSFER_CONTROL = 0,
89 	CVMX_USB_TRANSFER_ISOCHRONOUS = 1,
90 	CVMX_USB_TRANSFER_BULK = 2,
91 	CVMX_USB_TRANSFER_INTERRUPT = 3,
92 };
93 
94 /**
95  * enum cvmx_usb_direction - the transfer directions
96  *
97  * @CVMX_USB_DIRECTION_OUT: Data is transferring from Octeon to the device/host
98  * @CVMX_USB_DIRECTION_IN:  Data is transferring from the device/host to Octeon
99  */
100 enum cvmx_usb_direction {
101 	CVMX_USB_DIRECTION_OUT,
102 	CVMX_USB_DIRECTION_IN,
103 };
104 
105 /**
106  * enum cvmx_usb_status - possible callback function status codes
107  *
108  * @CVMX_USB_STATUS_OK:		  The transaction / operation finished without
109  *				  any errors
110  * @CVMX_USB_STATUS_SHORT:	  FIXME: This is currently not implemented
111  * @CVMX_USB_STATUS_CANCEL:	  The transaction was canceled while in flight
112  *				  by a user call to cvmx_usb_cancel
113  * @CVMX_USB_STATUS_ERROR:	  The transaction aborted with an unexpected
114  *				  error status
115  * @CVMX_USB_STATUS_STALL:	  The transaction received a USB STALL response
116  *				  from the device
117  * @CVMX_USB_STATUS_XACTERR:	  The transaction failed with an error from the
118  *				  device even after a number of retries
119  * @CVMX_USB_STATUS_DATATGLERR:	  The transaction failed with a data toggle
120  *				  error even after a number of retries
121  * @CVMX_USB_STATUS_BABBLEERR:	  The transaction failed with a babble error
122  * @CVMX_USB_STATUS_FRAMEERR:	  The transaction failed with a frame error
123  *				  even after a number of retries
124  */
125 enum cvmx_usb_status {
126 	CVMX_USB_STATUS_OK,
127 	CVMX_USB_STATUS_SHORT,
128 	CVMX_USB_STATUS_CANCEL,
129 	CVMX_USB_STATUS_ERROR,
130 	CVMX_USB_STATUS_STALL,
131 	CVMX_USB_STATUS_XACTERR,
132 	CVMX_USB_STATUS_DATATGLERR,
133 	CVMX_USB_STATUS_BABBLEERR,
134 	CVMX_USB_STATUS_FRAMEERR,
135 };
136 
137 /**
138  * struct cvmx_usb_port_status - the USB port status information
139  *
140  * @port_enabled:	1 = Usb port is enabled, 0 = disabled
141  * @port_over_current:	1 = Over current detected, 0 = Over current not
142  *			detected. Octeon doesn't support over current detection.
143  * @port_powered:	1 = Port power is being supplied to the device, 0 =
144  *			power is off. Octeon doesn't support turning port power
145  *			off.
146  * @port_speed:		Current port speed.
147  * @connected:		1 = A device is connected to the port, 0 = No device is
148  *			connected.
149  * @connect_change:	1 = Device connected state changed since the last set
150  *			status call.
151  */
152 struct cvmx_usb_port_status {
153 	u32 reserved			: 25;
154 	u32 port_enabled		: 1;
155 	u32 port_over_current		: 1;
156 	u32 port_powered		: 1;
157 	enum cvmx_usb_speed port_speed	: 2;
158 	u32 connected			: 1;
159 	u32 connect_change		: 1;
160 };
161 
162 /**
163  * struct cvmx_usb_iso_packet - descriptor for Isochronous packets
164  *
165  * @offset:	This is the offset in bytes into the main buffer where this data
166  *		is stored.
167  * @length:	This is the length in bytes of the data.
168  * @status:	This is the status of this individual packet transfer.
169  */
170 struct cvmx_usb_iso_packet {
171 	int offset;
172 	int length;
173 	enum cvmx_usb_status status;
174 };
175 
176 /**
177  * enum cvmx_usb_initialize_flags - flags used by the initialization function
178  *
179  * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI:    The USB port uses a 12MHz crystal
180  *					      as clock source at USB_XO and
181  *					      USB_XI.
182  * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND:   The USB port uses 12/24/48MHz 2.5V
183  *					      board clock source at USB_XO.
184  *					      USB_XI should be tied to GND.
185  * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK: Mask for clock speed field
186  * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:    Speed of reference clock or
187  *					      crystal
188  * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:    Speed of reference clock
189  * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:    Speed of reference clock
190  * @CVMX_USB_INITIALIZE_FLAGS_NO_DMA:	      Disable DMA and used polled IO for
191  *					      data transfer use for the USB
192  */
193 enum cvmx_usb_initialize_flags {
194 	CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI		= 1 << 0,
195 	CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND		= 1 << 1,
196 	CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK	= 3 << 3,
197 	CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ		= 1 << 3,
198 	CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ		= 2 << 3,
199 	CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ		= 3 << 3,
200 	/* Bits 3-4 used to encode the clock frequency */
201 	CVMX_USB_INITIALIZE_FLAGS_NO_DMA		= 1 << 5,
202 };
203 
204 /**
205  * enum cvmx_usb_pipe_flags - internal flags for a pipe.
206  *
207  * @CVMX_USB_PIPE_FLAGS_SCHEDULED: Used internally to determine if a pipe is
208  *				   actively using hardware.
209  * @CVMX_USB_PIPE_FLAGS_NEED_PING: Used internally to determine if a high speed
210  *				   pipe is in the ping state.
211  */
212 enum cvmx_usb_pipe_flags {
213 	CVMX_USB_PIPE_FLAGS_SCHEDULED	= 1 << 17,
214 	CVMX_USB_PIPE_FLAGS_NEED_PING	= 1 << 18,
215 };
216 
217 /* Maximum number of times to retry failed transactions */
218 #define MAX_RETRIES		3
219 
220 /* Maximum number of hardware channels supported by the USB block */
221 #define MAX_CHANNELS		8
222 
223 /*
224  * The low level hardware can transfer a maximum of this number of bytes in each
225  * transfer. The field is 19 bits wide
226  */
227 #define MAX_TRANSFER_BYTES	((1 << 19) - 1)
228 
229 /*
230  * The low level hardware can transfer a maximum of this number of packets in
231  * each transfer. The field is 10 bits wide
232  */
233 #define MAX_TRANSFER_PACKETS	((1 << 10) - 1)
234 
235 /**
236  * Logical transactions may take numerous low level
237  * transactions, especially when splits are concerned. This
238  * enum represents all of the possible stages a transaction can
239  * be in. Note that split completes are always even. This is so
240  * the NAK handler can backup to the previous low level
241  * transaction with a simple clearing of bit 0.
242  */
243 enum cvmx_usb_stage {
244 	CVMX_USB_STAGE_NON_CONTROL,
245 	CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
246 	CVMX_USB_STAGE_SETUP,
247 	CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
248 	CVMX_USB_STAGE_DATA,
249 	CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
250 	CVMX_USB_STAGE_STATUS,
251 	CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
252 };
253 
254 /**
255  * struct cvmx_usb_transaction - describes each pending USB transaction
256  *				 regardless of type. These are linked together
257  *				 to form a list of pending requests for a pipe.
258  *
259  * @node:		List node for transactions in the pipe.
260  * @type:		Type of transaction, duplicated of the pipe.
261  * @flags:		State flags for this transaction.
262  * @buffer:		User's physical buffer address to read/write.
263  * @buffer_length:	Size of the user's buffer in bytes.
264  * @control_header:	For control transactions, physical address of the 8
265  *			byte standard header.
266  * @iso_start_frame:	For ISO transactions, the starting frame number.
267  * @iso_number_packets:	For ISO transactions, the number of packets in the
268  *			request.
269  * @iso_packets:	For ISO transactions, the sub packets in the request.
270  * @actual_bytes:	Actual bytes transfer for this transaction.
271  * @stage:		For control transactions, the current stage.
272  * @urb:		URB.
273  */
274 struct cvmx_usb_transaction {
275 	struct list_head node;
276 	enum cvmx_usb_transfer type;
277 	u64 buffer;
278 	int buffer_length;
279 	u64 control_header;
280 	int iso_start_frame;
281 	int iso_number_packets;
282 	struct cvmx_usb_iso_packet *iso_packets;
283 	int xfersize;
284 	int pktcnt;
285 	int retries;
286 	int actual_bytes;
287 	enum cvmx_usb_stage stage;
288 	struct urb *urb;
289 };
290 
291 /**
292  * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
293  *			  and some USB device. It contains a list of pending
294  *			  request to the device.
295  *
296  * @node:		List node for pipe list
297  * @next:		Pipe after this one in the list
298  * @transactions:	List of pending transactions
299  * @interval:		For periodic pipes, the interval between packets in
300  *			frames
301  * @next_tx_frame:	The next frame this pipe is allowed to transmit on
302  * @flags:		State flags for this pipe
303  * @device_speed:	Speed of device connected to this pipe
304  * @transfer_type:	Type of transaction supported by this pipe
305  * @transfer_dir:	IN or OUT. Ignored for Control
306  * @multi_count:	Max packet in a row for the device
307  * @max_packet:		The device's maximum packet size in bytes
308  * @device_addr:	USB device address at other end of pipe
309  * @endpoint_num:	USB endpoint number at other end of pipe
310  * @hub_device_addr:	Hub address this device is connected to
311  * @hub_port:		Hub port this device is connected to
312  * @pid_toggle:		This toggles between 0/1 on every packet send to track
313  *			the data pid needed
314  * @channel:		Hardware DMA channel for this pipe
315  * @split_sc_frame:	The low order bits of the frame number the split
316  *			complete should be sent on
317  */
318 struct cvmx_usb_pipe {
319 	struct list_head node;
320 	struct list_head transactions;
321 	u64 interval;
322 	u64 next_tx_frame;
323 	enum cvmx_usb_pipe_flags flags;
324 	enum cvmx_usb_speed device_speed;
325 	enum cvmx_usb_transfer transfer_type;
326 	enum cvmx_usb_direction transfer_dir;
327 	int multi_count;
328 	u16 max_packet;
329 	u8 device_addr;
330 	u8 endpoint_num;
331 	u8 hub_device_addr;
332 	u8 hub_port;
333 	u8 pid_toggle;
334 	u8 channel;
335 	s8 split_sc_frame;
336 };
337 
338 struct cvmx_usb_tx_fifo {
339 	struct {
340 		int channel;
341 		int size;
342 		u64 address;
343 	} entry[MAX_CHANNELS + 1];
344 	int head;
345 	int tail;
346 };
347 
348 /**
349  * struct octeon_hcd - the state of the USB block
350  *
351  * lock:		   Serialization lock.
352  * init_flags:		   Flags passed to initialize.
353  * index:		   Which USB block this is for.
354  * idle_hardware_channels: Bit set for every idle hardware channel.
355  * usbcx_hprt:		   Stored port status so we don't need to read a CSR to
356  *			   determine splits.
357  * pipe_for_channel:	   Map channels to pipes.
358  * pipe:		   Storage for pipes.
359  * indent:		   Used by debug output to indent functions.
360  * port_status:		   Last port status used for change notification.
361  * idle_pipes:		   List of open pipes that have no transactions.
362  * active_pipes:	   Active pipes indexed by transfer type.
363  * frame_number:	   Increments every SOF interrupt for time keeping.
364  * active_split:	   Points to the current active split, or NULL.
365  */
366 struct octeon_hcd {
367 	spinlock_t lock; /* serialization lock */
368 	int init_flags;
369 	int index;
370 	int idle_hardware_channels;
371 	union cvmx_usbcx_hprt usbcx_hprt;
372 	struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
373 	int indent;
374 	struct cvmx_usb_port_status port_status;
375 	struct list_head idle_pipes;
376 	struct list_head active_pipes[4];
377 	u64 frame_number;
378 	struct cvmx_usb_transaction *active_split;
379 	struct cvmx_usb_tx_fifo periodic;
380 	struct cvmx_usb_tx_fifo nonperiodic;
381 	struct hrtimer sof_timer;
382 };
383 
384 static int cvmx_usb_poll(struct octeon_hcd *usb);
385 
386 static enum hrtimer_restart octeon_usb_sof_timer(struct hrtimer *t)
387 {
388 	struct octeon_hcd *usb = container_of(t, struct octeon_hcd, sof_timer);
389 	unsigned long flags;
390 
391 	spin_lock_irqsave(&usb->lock, flags);
392 	cvmx_usb_poll(usb);
393 	spin_unlock_irqrestore(&usb->lock, flags);
394 	return HRTIMER_NORESTART;
395 }
396 
397 /*
398  * This macro logically sets a single field in a CSR. It does the sequence
399  * read, modify, and write
400  */
401 #define USB_SET_FIELD32(address, _union, field, value)		\
402 	do {							\
403 		union _union c;					\
404 								\
405 		c.u32 = cvmx_usb_read_csr32(usb, address);	\
406 		c.s.field = value;				\
407 		cvmx_usb_write_csr32(usb, address, c.u32);	\
408 	} while (0)
409 
410 /* Returns the IO address to push/pop stuff data from the FIFOs */
411 #define USB_FIFO_ADDRESS(channel, usb_index) \
412 	(CVMX_USBCX_GOTGCTL(usb_index) + ((channel) + 1) * 0x1000)
413 
414 /**
415  * struct octeon_temp_buffer - a bounce buffer for USB transfers
416  * @orig_buffer: the original buffer passed by the USB stack
417  * @data:	 the newly allocated temporary buffer (excluding meta-data)
418  *
419  * Both the DMA engine and FIFO mode will always transfer full 32-bit words. If
420  * the buffer is too short, we need to allocate a temporary one, and this struct
421  * represents it.
422  */
423 struct octeon_temp_buffer {
424 	void *orig_buffer;
425 	u8 data[];
426 };
427 
428 static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
429 {
430 	return container_of((void *)p, struct usb_hcd, hcd_priv);
431 }
432 
433 /**
434  * octeon_alloc_temp_buffer - allocate a temporary buffer for USB transfer
435  *                            (if needed)
436  * @urb:	URB.
437  * @mem_flags:	Memory allocation flags.
438  *
439  * This function allocates a temporary bounce buffer whenever it's needed
440  * due to HW limitations.
441  */
442 static int octeon_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
443 {
444 	struct octeon_temp_buffer *temp;
445 
446 	if (urb->num_sgs || urb->sg ||
447 	    (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
448 	    !(urb->transfer_buffer_length % sizeof(u32)))
449 		return 0;
450 
451 	temp = kmalloc(ALIGN(urb->transfer_buffer_length, sizeof(u32)) +
452 		       sizeof(*temp), mem_flags);
453 	if (!temp)
454 		return -ENOMEM;
455 
456 	temp->orig_buffer = urb->transfer_buffer;
457 	if (usb_urb_dir_out(urb))
458 		memcpy(temp->data, urb->transfer_buffer,
459 		       urb->transfer_buffer_length);
460 	urb->transfer_buffer = temp->data;
461 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
462 
463 	return 0;
464 }
465 
466 /**
467  * octeon_free_temp_buffer - free a temporary buffer used by USB transfers.
468  * @urb: URB.
469  *
470  * Frees a buffer allocated by octeon_alloc_temp_buffer().
471  */
472 static void octeon_free_temp_buffer(struct urb *urb)
473 {
474 	struct octeon_temp_buffer *temp;
475 	size_t length;
476 
477 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
478 		return;
479 
480 	temp = container_of(urb->transfer_buffer, struct octeon_temp_buffer,
481 			    data);
482 	if (usb_urb_dir_in(urb)) {
483 		if (usb_pipeisoc(urb->pipe))
484 			length = urb->transfer_buffer_length;
485 		else
486 			length = urb->actual_length;
487 
488 		memcpy(temp->orig_buffer, urb->transfer_buffer, length);
489 	}
490 	urb->transfer_buffer = temp->orig_buffer;
491 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
492 	kfree(temp);
493 }
494 
495 /**
496  * octeon_map_urb_for_dma - Octeon-specific map_urb_for_dma().
497  * @hcd:	USB HCD structure.
498  * @urb:	URB.
499  * @mem_flags:	Memory allocation flags.
500  */
501 static int octeon_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
502 				  gfp_t mem_flags)
503 {
504 	int ret;
505 
506 	ret = octeon_alloc_temp_buffer(urb, mem_flags);
507 	if (ret)
508 		return ret;
509 
510 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
511 	if (ret)
512 		octeon_free_temp_buffer(urb);
513 
514 	return ret;
515 }
516 
517 /**
518  * octeon_unmap_urb_for_dma - Octeon-specific unmap_urb_for_dma()
519  * @hcd:	USB HCD structure.
520  * @urb:	URB.
521  */
522 static void octeon_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
523 {
524 	usb_hcd_unmap_urb_for_dma(hcd, urb);
525 	octeon_free_temp_buffer(urb);
526 }
527 
528 /**
529  * Read a USB 32bit CSR. It performs the necessary address swizzle
530  * for 32bit CSRs and logs the value in a readable format if
531  * debugging is on.
532  *
533  * @usb:     USB block this access is for
534  * @address: 64bit address to read
535  *
536  * Returns: Result of the read
537  */
538 static inline u32 cvmx_usb_read_csr32(struct octeon_hcd *usb, u64 address)
539 {
540 	return cvmx_read64_uint32(address ^ 4);
541 }
542 
543 /**
544  * Write a USB 32bit CSR. It performs the necessary address
545  * swizzle for 32bit CSRs and logs the value in a readable format
546  * if debugging is on.
547  *
548  * @usb:     USB block this access is for
549  * @address: 64bit address to write
550  * @value:   Value to write
551  */
552 static inline void cvmx_usb_write_csr32(struct octeon_hcd *usb,
553 					u64 address, u32 value)
554 {
555 	cvmx_write64_uint32(address ^ 4, value);
556 	cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
557 }
558 
559 /**
560  * Return non zero if this pipe connects to a non HIGH speed
561  * device through a high speed hub.
562  *
563  * @usb:    USB block this access is for
564  * @pipe:   Pipe to check
565  *
566  * Returns: Non zero if we need to do split transactions
567  */
568 static inline int cvmx_usb_pipe_needs_split(struct octeon_hcd *usb,
569 					    struct cvmx_usb_pipe *pipe)
570 {
571 	return pipe->device_speed != CVMX_USB_SPEED_HIGH &&
572 	       usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH;
573 }
574 
575 /**
576  * Trivial utility function to return the correct PID for a pipe
577  *
578  * @pipe:   pipe to check
579  *
580  * Returns: PID for pipe
581  */
582 static inline int cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
583 {
584 	if (pipe->pid_toggle)
585 		return 2; /* Data1 */
586 	return 0; /* Data0 */
587 }
588 
589 /* Loops through register until txfflsh or rxfflsh become zero.*/
590 static int cvmx_wait_tx_rx(struct octeon_hcd *usb, int fflsh_type)
591 {
592 	int result;
593 	u64 address = CVMX_USBCX_GRSTCTL(usb->index);
594 	u64 done = cvmx_get_cycle() + 100 *
595 		   (u64)octeon_get_clock_rate() / 1000000;
596 	union cvmx_usbcx_grstctl c;
597 
598 	while (1) {
599 		c.u32 = cvmx_usb_read_csr32(usb, address);
600 		if (fflsh_type == 0 && c.s.txfflsh == 0) {
601 			result = 0;
602 			break;
603 		} else if (fflsh_type == 1 && c.s.rxfflsh == 0) {
604 			result = 0;
605 			break;
606 		} else if (cvmx_get_cycle() > done) {
607 			result = -1;
608 			break;
609 		}
610 
611 		__delay(100);
612 	}
613 	return result;
614 }
615 
616 static void cvmx_fifo_setup(struct octeon_hcd *usb)
617 {
618 	union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
619 	union cvmx_usbcx_gnptxfsiz npsiz;
620 	union cvmx_usbcx_hptxfsiz psiz;
621 
622 	usbcx_ghwcfg3.u32 = cvmx_usb_read_csr32(usb,
623 						CVMX_USBCX_GHWCFG3(usb->index));
624 
625 	/*
626 	 * Program the USBC_GRXFSIZ register to select the size of the receive
627 	 * FIFO (25%).
628 	 */
629 	USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), cvmx_usbcx_grxfsiz,
630 			rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
631 
632 	/*
633 	 * Program the USBC_GNPTXFSIZ register to select the size and the start
634 	 * address of the non-periodic transmit FIFO for nonperiodic
635 	 * transactions (50%).
636 	 */
637 	npsiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
638 	npsiz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
639 	npsiz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
640 	cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), npsiz.u32);
641 
642 	/*
643 	 * Program the USBC_HPTXFSIZ register to select the size and start
644 	 * address of the periodic transmit FIFO for periodic transactions
645 	 * (25%).
646 	 */
647 	psiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
648 	psiz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
649 	psiz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
650 	cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), psiz.u32);
651 
652 	/* Flush all FIFOs */
653 	USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
654 			cvmx_usbcx_grstctl, txfnum, 0x10);
655 	USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
656 			cvmx_usbcx_grstctl, txfflsh, 1);
657 	cvmx_wait_tx_rx(usb, 0);
658 	USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
659 			cvmx_usbcx_grstctl, rxfflsh, 1);
660 	cvmx_wait_tx_rx(usb, 1);
661 }
662 
663 /**
664  * Shutdown a USB port after a call to cvmx_usb_initialize().
665  * The port should be disabled with all pipes closed when this
666  * function is called.
667  *
668  * @usb: USB device state populated by cvmx_usb_initialize().
669  *
670  * Returns: 0 or a negative error code.
671  */
672 static int cvmx_usb_shutdown(struct octeon_hcd *usb)
673 {
674 	union cvmx_usbnx_clk_ctl usbn_clk_ctl;
675 
676 	/* Make sure all pipes are closed */
677 	if (!list_empty(&usb->idle_pipes) ||
678 	    !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS]) ||
679 	    !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT]) ||
680 	    !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_CONTROL]) ||
681 	    !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_BULK]))
682 		return -EBUSY;
683 
684 	/* Disable the clocks and put them in power on reset */
685 	usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
686 	usbn_clk_ctl.s.enable = 1;
687 	usbn_clk_ctl.s.por = 1;
688 	usbn_clk_ctl.s.hclk_rst = 1;
689 	usbn_clk_ctl.s.prst = 0;
690 	usbn_clk_ctl.s.hrst = 0;
691 	cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
692 	return 0;
693 }
694 
695 /**
696  * Initialize a USB port for use. This must be called before any
697  * other access to the Octeon USB port is made. The port starts
698  * off in the disabled state.
699  *
700  * @dev:	 Pointer to struct device for logging purposes.
701  * @usb:	 Pointer to struct octeon_hcd.
702  *
703  * Returns: 0 or a negative error code.
704  */
705 static int cvmx_usb_initialize(struct device *dev,
706 			       struct octeon_hcd *usb)
707 {
708 	int channel;
709 	int divisor;
710 	int retries = 0;
711 	int status;
712 	union cvmx_usbcx_hcfg usbcx_hcfg;
713 	union cvmx_usbnx_clk_ctl usbn_clk_ctl;
714 	union cvmx_usbcx_gintsts usbc_gintsts;
715 	union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
716 	union cvmx_usbcx_gintmsk usbcx_gintmsk;
717 	union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
718 	union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
719 
720 retry:
721 	/*
722 	 * Power On Reset and PHY Initialization
723 	 *
724 	 * 1. Wait for DCOK to assert (nothing to do)
725 	 *
726 	 * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
727 	 *     USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
728 	 */
729 	usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
730 	usbn_clk_ctl.s.por = 1;
731 	usbn_clk_ctl.s.hrst = 0;
732 	usbn_clk_ctl.s.prst = 0;
733 	usbn_clk_ctl.s.hclk_rst = 0;
734 	usbn_clk_ctl.s.enable = 0;
735 	/*
736 	 * 2b. Select the USB reference clock/crystal parameters by writing
737 	 *     appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
738 	 */
739 	if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
740 		/*
741 		 * The USB port uses 12/24/48MHz 2.5V board clock
742 		 * source at USB_XO. USB_XI should be tied to GND.
743 		 * Most Octeon evaluation boards require this setting
744 		 */
745 		if (OCTEON_IS_MODEL(OCTEON_CN3XXX) ||
746 		    OCTEON_IS_MODEL(OCTEON_CN56XX) ||
747 		    OCTEON_IS_MODEL(OCTEON_CN50XX))
748 			/* From CN56XX,CN50XX,CN31XX,CN30XX manuals */
749 			usbn_clk_ctl.s.p_rtype = 2; /* p_rclk=1 & p_xenbn=0 */
750 		else
751 			/* From CN52XX manual */
752 			usbn_clk_ctl.s.p_rtype = 1;
753 
754 		switch (usb->init_flags &
755 			CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
756 		case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
757 			usbn_clk_ctl.s.p_c_sel = 0;
758 			break;
759 		case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
760 			usbn_clk_ctl.s.p_c_sel = 1;
761 			break;
762 		case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
763 			usbn_clk_ctl.s.p_c_sel = 2;
764 			break;
765 		}
766 	} else {
767 		/*
768 		 * The USB port uses a 12MHz crystal as clock source
769 		 * at USB_XO and USB_XI
770 		 */
771 		if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
772 			/* From CN31XX,CN30XX manual */
773 			usbn_clk_ctl.s.p_rtype = 3; /* p_rclk=1 & p_xenbn=1 */
774 		else
775 			/* From CN56XX,CN52XX,CN50XX manuals. */
776 			usbn_clk_ctl.s.p_rtype = 0;
777 
778 		usbn_clk_ctl.s.p_c_sel = 0;
779 	}
780 	/*
781 	 * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
782 	 *     setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
783 	 *     such that USB is as close as possible to 125Mhz
784 	 */
785 	divisor = DIV_ROUND_UP(octeon_get_clock_rate(), 125000000);
786 	/* Lower than 4 doesn't seem to work properly */
787 	if (divisor < 4)
788 		divisor = 4;
789 	usbn_clk_ctl.s.divide = divisor;
790 	usbn_clk_ctl.s.divide2 = 0;
791 	cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
792 
793 	/* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
794 	usbn_clk_ctl.s.hclk_rst = 1;
795 	cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
796 	/* 2e.  Wait 64 core-clock cycles for HCLK to stabilize */
797 	__delay(64);
798 	/*
799 	 * 3. Program the power-on reset field in the USBN clock-control
800 	 *    register:
801 	 *    USBN_CLK_CTL[POR] = 0
802 	 */
803 	usbn_clk_ctl.s.por = 0;
804 	cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
805 	/* 4. Wait 1 ms for PHY clock to start */
806 	mdelay(1);
807 	/*
808 	 * 5. Program the Reset input from automatic test equipment field in the
809 	 *    USBP control and status register:
810 	 *    USBN_USBP_CTL_STATUS[ATE_RESET] = 1
811 	 */
812 	usbn_usbp_ctl_status.u64 =
813 		cvmx_read64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index));
814 	usbn_usbp_ctl_status.s.ate_reset = 1;
815 	cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
816 			    usbn_usbp_ctl_status.u64);
817 	/* 6. Wait 10 cycles */
818 	__delay(10);
819 	/*
820 	 * 7. Clear ATE_RESET field in the USBN clock-control register:
821 	 *    USBN_USBP_CTL_STATUS[ATE_RESET] = 0
822 	 */
823 	usbn_usbp_ctl_status.s.ate_reset = 0;
824 	cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
825 			    usbn_usbp_ctl_status.u64);
826 	/*
827 	 * 8. Program the PHY reset field in the USBN clock-control register:
828 	 *    USBN_CLK_CTL[PRST] = 1
829 	 */
830 	usbn_clk_ctl.s.prst = 1;
831 	cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
832 	/*
833 	 * 9. Program the USBP control and status register to select host or
834 	 *    device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
835 	 *    device
836 	 */
837 	usbn_usbp_ctl_status.s.hst_mode = 0;
838 	cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
839 			    usbn_usbp_ctl_status.u64);
840 	/* 10. Wait 1 us */
841 	udelay(1);
842 	/*
843 	 * 11. Program the hreset_n field in the USBN clock-control register:
844 	 *     USBN_CLK_CTL[HRST] = 1
845 	 */
846 	usbn_clk_ctl.s.hrst = 1;
847 	cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
848 	/* 12. Proceed to USB core initialization */
849 	usbn_clk_ctl.s.enable = 1;
850 	cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
851 	udelay(1);
852 
853 	/*
854 	 * USB Core Initialization
855 	 *
856 	 * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
857 	 *    determine USB core configuration parameters.
858 	 *
859 	 *    Nothing needed
860 	 *
861 	 * 2. Program the following fields in the global AHB configuration
862 	 *    register (USBC_GAHBCFG)
863 	 *    DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
864 	 *    Burst length, USBC_GAHBCFG[HBSTLEN] = 0
865 	 *    Nonperiodic TxFIFO empty level (slave mode only),
866 	 *    USBC_GAHBCFG[NPTXFEMPLVL]
867 	 *    Periodic TxFIFO empty level (slave mode only),
868 	 *    USBC_GAHBCFG[PTXFEMPLVL]
869 	 *    Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
870 	 */
871 	usbcx_gahbcfg.u32 = cvmx_usb_read_csr32(usb,
872 						CVMX_USBCX_GHWCFG3(usb->index));
873 	if (usbcx_gahbcfg.u32 == 0xffffffff || usbcx_gahbcfg.u32 == 0) {
874 		dev_err(dev, "USB core is not responding (GHWCFG3=0x%08x)\n",
875 			usbcx_gahbcfg.u32);
876 		status = -ENODEV;
877 		goto err_shutdown;
878 	}
879 
880 	usbcx_gahbcfg.u32 = 0;
881 	usbcx_gahbcfg.s.dmaen = !(usb->init_flags &
882 				  CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
883 	usbcx_gahbcfg.s.hbstlen = 0;
884 	usbcx_gahbcfg.s.nptxfemplvl = 1;
885 	usbcx_gahbcfg.s.ptxfemplvl = 1;
886 	usbcx_gahbcfg.s.glblintrmsk = 1;
887 	cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
888 			     usbcx_gahbcfg.u32);
889 
890 	/*
891 	 * 3. Program the following fields in USBC_GUSBCFG register.
892 	 *    HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
893 	 *    ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
894 	 *    USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
895 	 *    PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
896 	 */
897 	usbcx_gusbcfg.u32 = cvmx_usb_read_csr32(usb,
898 						CVMX_USBCX_GUSBCFG(usb->index));
899 	usbcx_gusbcfg.s.toutcal = 0;
900 	usbcx_gusbcfg.s.ddrsel = 0;
901 	usbcx_gusbcfg.s.usbtrdtim = 0x5;
902 	usbcx_gusbcfg.s.phylpwrclksel = 0;
903 	cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
904 			     usbcx_gusbcfg.u32);
905 
906 	/*
907 	 * 4. The software must unmask the following bits in the USBC_GINTMSK
908 	 *    register.
909 	 *    OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
910 	 *    Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
911 	 */
912 	usbcx_gintmsk.u32 = cvmx_usb_read_csr32(usb,
913 						CVMX_USBCX_GINTMSK(usb->index));
914 	usbcx_gintmsk.s.otgintmsk = 1;
915 	usbcx_gintmsk.s.modemismsk = 1;
916 	usbcx_gintmsk.s.hchintmsk = 1;
917 	usbcx_gintmsk.s.sofmsk = 0;
918 	/* We need RX FIFO interrupts if we don't have DMA */
919 	if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
920 		usbcx_gintmsk.s.rxflvlmsk = 1;
921 	cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
922 			     usbcx_gintmsk.u32);
923 
924 	/*
925 	 * Disable all channel interrupts. We'll enable them per channel later.
926 	 */
927 	for (channel = 0; channel < 8; channel++)
928 		cvmx_usb_write_csr32(usb,
929 				     CVMX_USBCX_HCINTMSKX(channel, usb->index),
930 				     0);
931 
932 	/*
933 	 * Host Port Initialization
934 	 *
935 	 * 1. Program the host-port interrupt-mask field to unmask,
936 	 *    USBC_GINTMSK[PRTINT] = 1
937 	 */
938 	USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
939 			cvmx_usbcx_gintmsk, prtintmsk, 1);
940 	USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
941 			cvmx_usbcx_gintmsk, disconnintmsk, 1);
942 
943 	/*
944 	 * 2. Program the USBC_HCFG register to select full-speed host
945 	 *    or high-speed host.
946 	 */
947 	usbcx_hcfg.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
948 	usbcx_hcfg.s.fslssupp = 0;
949 	usbcx_hcfg.s.fslspclksel = 0;
950 	cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
951 
952 	cvmx_fifo_setup(usb);
953 
954 	/*
955 	 * If the controller is getting port events right after the reset, it
956 	 * means the initialization failed. Try resetting the controller again
957 	 * in such case. This is seen to happen after cold boot on DSR-1000N.
958 	 */
959 	usbc_gintsts.u32 = cvmx_usb_read_csr32(usb,
960 					       CVMX_USBCX_GINTSTS(usb->index));
961 	cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index),
962 			     usbc_gintsts.u32);
963 	dev_dbg(dev, "gintsts after reset: 0x%x\n", (int)usbc_gintsts.u32);
964 	if (!usbc_gintsts.s.disconnint && !usbc_gintsts.s.prtint)
965 		return 0;
966 	if (retries++ >= 5) {
967 		status = -EAGAIN;
968 		goto err_shutdown;
969 	}
970 	dev_info(dev, "controller reset failed (gintsts=0x%x) - retrying\n",
971 		 (int)usbc_gintsts.u32);
972 	msleep(50);
973 	cvmx_usb_shutdown(usb);
974 	msleep(50);
975 	goto retry;
976 
977 err_shutdown:
978 	/*
979 	 * Steps 2b-2e above selected the reference clock, enabled
980 	 * USBN_CLK_CTL and took the PHY out of reset, and the caller only
981 	 * frees the HCD, so hand the block back before giving up. The pipe
982 	 * lists cvmx_usb_shutdown() checks are initialised by the caller
983 	 * before it gets here, so it cannot refuse with -EBUSY.
984 	 */
985 	cvmx_usb_shutdown(usb);
986 	return status;
987 }
988 
989 /**
990  * Reset a USB port. After this call succeeds, the USB port is
991  * online and servicing requests.
992  *
993  * @usb: USB device state populated by cvmx_usb_initialize().
994  */
995 static void cvmx_usb_reset_port(struct octeon_hcd *usb)
996 {
997 	usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
998 						  CVMX_USBCX_HPRT(usb->index));
999 
1000 	/* Program the port reset bit to start the reset process */
1001 	USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
1002 			prtrst, 1);
1003 
1004 	/*
1005 	 * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
1006 	 * process to complete.
1007 	 */
1008 	mdelay(50);
1009 
1010 	/* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
1011 	USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
1012 			prtrst, 0);
1013 
1014 	/*
1015 	 * Read the port speed field to get the enumerated speed,
1016 	 * USBC_HPRT[PRTSPD].
1017 	 */
1018 	usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
1019 						  CVMX_USBCX_HPRT(usb->index));
1020 }
1021 
1022 /**
1023  * Disable a USB port. After this call the USB port will not
1024  * generate data transfers and will not generate events.
1025  * Transactions in process will fail and call their
1026  * associated callbacks.
1027  *
1028  * @usb: USB device state populated by cvmx_usb_initialize().
1029  *
1030  * Returns: 0 or a negative error code.
1031  */
1032 static int cvmx_usb_disable(struct octeon_hcd *usb)
1033 {
1034 	/* Disable the port */
1035 	USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
1036 			prtena, 1);
1037 	return 0;
1038 }
1039 
1040 /**
1041  * Get the current state of the USB port. Use this call to
1042  * determine if the usb port has anything connected, is enabled,
1043  * or has some sort of error condition. The return value of this
1044  * call has "changed" bits to signal of the value of some fields
1045  * have changed between calls.
1046  *
1047  * @usb: USB device state populated by cvmx_usb_initialize().
1048  *
1049  * Returns: Port status information
1050  */
1051 static struct cvmx_usb_port_status cvmx_usb_get_status(struct octeon_hcd *usb)
1052 {
1053 	union cvmx_usbcx_hprt usbc_hprt;
1054 	struct cvmx_usb_port_status result;
1055 
1056 	memset(&result, 0, sizeof(result));
1057 
1058 	usbc_hprt.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1059 	result.port_enabled = usbc_hprt.s.prtena;
1060 	result.port_over_current = usbc_hprt.s.prtovrcurract;
1061 	result.port_powered = usbc_hprt.s.prtpwr;
1062 	result.port_speed = usbc_hprt.s.prtspd;
1063 	result.connected = usbc_hprt.s.prtconnsts;
1064 	result.connect_change =
1065 		result.connected != usb->port_status.connected;
1066 
1067 	return result;
1068 }
1069 
1070 /**
1071  * Open a virtual pipe between the host and a USB device. A pipe
1072  * must be opened before data can be transferred between a device
1073  * and Octeon.
1074  *
1075  * @usb:	     USB device state populated by cvmx_usb_initialize().
1076  * @device_addr:
1077  *		     USB device address to open the pipe to
1078  *		     (0-127).
1079  * @endpoint_num:
1080  *		     USB endpoint number to open the pipe to
1081  *		     (0-15).
1082  * @device_speed:
1083  *		     The speed of the device the pipe is going
1084  *		     to. This must match the device's speed,
1085  *		     which may be different than the port speed.
1086  * @max_packet:	     The maximum packet length the device can
1087  *		     transmit/receive (low speed=0-8, full
1088  *		     speed=0-1023, high speed=0-1024). This value
1089  *		     comes from the standard endpoint descriptor
1090  *		     field wMaxPacketSize bits <10:0>.
1091  * @transfer_type:
1092  *		     The type of transfer this pipe is for.
1093  * @transfer_dir:
1094  *		     The direction the pipe is in. This is not
1095  *		     used for control pipes.
1096  * @interval:	     For ISOCHRONOUS and INTERRUPT transfers,
1097  *		     this is how often the transfer is scheduled
1098  *		     for. All other transfers should specify
1099  *		     zero. The units are in frames (8000/sec at
1100  *		     high speed, 1000/sec for full speed).
1101  * @multi_count:
1102  *		     For high speed devices, this is the maximum
1103  *		     allowed number of packet per microframe.
1104  *		     Specify zero for non high speed devices. This
1105  *		     value comes from the standard endpoint descriptor
1106  *		     field wMaxPacketSize bits <12:11>.
1107  * @hub_device_addr:
1108  *		     Hub device address this device is connected
1109  *		     to. Devices connected directly to Octeon
1110  *		     use zero. This is only used when the device
1111  *		     is full/low speed behind a high speed hub.
1112  *		     The address will be of the high speed hub,
1113  *		     not and full speed hubs after it.
1114  * @hub_port:	     Which port on the hub the device is
1115  *		     connected. Use zero for devices connected
1116  *		     directly to Octeon. Like hub_device_addr,
1117  *		     this is only used for full/low speed
1118  *		     devices behind a high speed hub.
1119  *
1120  * Returns: A non-NULL value is a pipe. NULL means an error.
1121  */
1122 static struct cvmx_usb_pipe *cvmx_usb_open_pipe(struct octeon_hcd *usb,
1123 						int device_addr,
1124 						int endpoint_num,
1125 						enum cvmx_usb_speed
1126 							device_speed,
1127 						int max_packet,
1128 						enum cvmx_usb_transfer
1129 							transfer_type,
1130 						enum cvmx_usb_direction
1131 							transfer_dir,
1132 						int interval, int multi_count,
1133 						int hub_device_addr,
1134 						int hub_port)
1135 {
1136 	struct cvmx_usb_pipe *pipe;
1137 
1138 	pipe = kzalloc_obj(*pipe, GFP_ATOMIC);
1139 	if (!pipe)
1140 		return NULL;
1141 	if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1142 	    (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1143 	    (transfer_type == CVMX_USB_TRANSFER_BULK))
1144 		pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
1145 	pipe->device_addr = device_addr;
1146 	pipe->endpoint_num = endpoint_num;
1147 	pipe->device_speed = device_speed;
1148 	pipe->max_packet = max_packet;
1149 	pipe->transfer_type = transfer_type;
1150 	pipe->transfer_dir = transfer_dir;
1151 	INIT_LIST_HEAD(&pipe->transactions);
1152 
1153 	/*
1154 	 * All pipes use interval to rate limit NAK processing. Force an
1155 	 * interval if one wasn't supplied
1156 	 */
1157 	if (!interval)
1158 		interval = 1;
1159 	if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1160 		pipe->interval = interval * 8;
1161 		/* Force start splits to be schedule on uFrame 0 */
1162 		pipe->next_tx_frame = ((usb->frame_number + 7) & ~7) +
1163 					pipe->interval;
1164 	} else {
1165 		pipe->interval = interval;
1166 		pipe->next_tx_frame = usb->frame_number + pipe->interval;
1167 	}
1168 	pipe->multi_count = multi_count;
1169 	pipe->hub_device_addr = hub_device_addr;
1170 	pipe->hub_port = hub_port;
1171 	pipe->pid_toggle = 0;
1172 	pipe->split_sc_frame = -1;
1173 	list_add_tail(&pipe->node, &usb->idle_pipes);
1174 
1175 	/*
1176 	 * We don't need to tell the hardware about this pipe yet since
1177 	 * it doesn't have any submitted requests
1178 	 */
1179 
1180 	return pipe;
1181 }
1182 
1183 /**
1184  * Poll the RX FIFOs and remove data as needed. This function is only used
1185  * in non DMA mode. It is very important that this function be called quickly
1186  * enough to prevent FIFO overflow.
1187  *
1188  * @usb:	USB device state populated by cvmx_usb_initialize().
1189  */
1190 static void cvmx_usb_poll_rx_fifo(struct octeon_hcd *usb)
1191 {
1192 	union cvmx_usbcx_grxstsph rx_status;
1193 	int channel;
1194 	int bytes;
1195 	u64 address;
1196 	u32 *ptr;
1197 
1198 	rx_status.u32 = cvmx_usb_read_csr32(usb,
1199 					    CVMX_USBCX_GRXSTSPH(usb->index));
1200 	/* Only read data if IN data is there */
1201 	if (rx_status.s.pktsts != 2)
1202 		return;
1203 	/* Check if no data is available */
1204 	if (!rx_status.s.bcnt)
1205 		return;
1206 
1207 	channel = rx_status.s.chnum;
1208 	bytes = rx_status.s.bcnt;
1209 	if (!bytes)
1210 		return;
1211 
1212 	/* Get where the DMA engine would have written this data */
1213 	address = cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) +
1214 				     channel * 8);
1215 
1216 	ptr = cvmx_phys_to_ptr(address);
1217 	cvmx_write64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel * 8,
1218 			    address + bytes);
1219 
1220 	/* Loop writing the FIFO data for this packet into memory */
1221 	while (bytes > 0) {
1222 		*ptr++ = cvmx_usb_read_csr32(usb,
1223 					USB_FIFO_ADDRESS(channel, usb->index));
1224 		bytes -= 4;
1225 	}
1226 	CVMX_SYNCW;
1227 }
1228 
1229 /**
1230  * Fill the TX hardware fifo with data out of the software
1231  * fifos
1232  *
1233  * @usb:	    USB device state populated by cvmx_usb_initialize().
1234  * @fifo:	    Software fifo to use
1235  * @available:	    Amount of space in the hardware fifo
1236  *
1237  * Returns: Non zero if the hardware fifo was too small and needs
1238  *	    to be serviced again.
1239  */
1240 static int cvmx_usb_fill_tx_hw(struct octeon_hcd *usb,
1241 			       struct cvmx_usb_tx_fifo *fifo, int available)
1242 {
1243 	/*
1244 	 * We're done either when there isn't anymore space or the software FIFO
1245 	 * is empty
1246 	 */
1247 	while (available && (fifo->head != fifo->tail)) {
1248 		int i = fifo->tail;
1249 		const u32 *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1250 		u64 csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel,
1251 						   usb->index) ^ 4;
1252 		int words = available;
1253 
1254 		/* Limit the amount of data to what the SW fifo has */
1255 		if (fifo->entry[i].size <= available) {
1256 			words = fifo->entry[i].size;
1257 			fifo->tail++;
1258 			if (fifo->tail > MAX_CHANNELS)
1259 				fifo->tail = 0;
1260 		}
1261 
1262 		/* Update the next locations and counts */
1263 		available -= words;
1264 		fifo->entry[i].address += words * 4;
1265 		fifo->entry[i].size -= words;
1266 
1267 		/*
1268 		 * Write the HW fifo data. The read every three writes is due
1269 		 * to an errata on CN3XXX chips
1270 		 */
1271 		while (words > 3) {
1272 			cvmx_write64_uint32(csr_address, *ptr++);
1273 			cvmx_write64_uint32(csr_address, *ptr++);
1274 			cvmx_write64_uint32(csr_address, *ptr++);
1275 			cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1276 			words -= 3;
1277 		}
1278 		cvmx_write64_uint32(csr_address, *ptr++);
1279 		if (--words) {
1280 			cvmx_write64_uint32(csr_address, *ptr++);
1281 			if (--words)
1282 				cvmx_write64_uint32(csr_address, *ptr++);
1283 		}
1284 		cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1285 	}
1286 	return fifo->head != fifo->tail;
1287 }
1288 
1289 /**
1290  * Check the hardware FIFOs and fill them as needed
1291  *
1292  * @usb:	USB device state populated by cvmx_usb_initialize().
1293  */
1294 static void cvmx_usb_poll_tx_fifo(struct octeon_hcd *usb)
1295 {
1296 	if (usb->periodic.head != usb->periodic.tail) {
1297 		union cvmx_usbcx_hptxsts tx_status;
1298 
1299 		tx_status.u32 = cvmx_usb_read_csr32(usb,
1300 						    CVMX_USBCX_HPTXSTS(usb->index));
1301 		if (cvmx_usb_fill_tx_hw(usb, &usb->periodic,
1302 					tx_status.s.ptxfspcavail))
1303 			USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1304 					cvmx_usbcx_gintmsk, ptxfempmsk, 1);
1305 		else
1306 			USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1307 					cvmx_usbcx_gintmsk, ptxfempmsk, 0);
1308 	}
1309 
1310 	if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1311 		union cvmx_usbcx_gnptxsts tx_status;
1312 
1313 		tx_status.u32 = cvmx_usb_read_csr32(usb,
1314 						    CVMX_USBCX_GNPTXSTS(usb->index));
1315 		if (cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic,
1316 					tx_status.s.nptxfspcavail))
1317 			USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1318 					cvmx_usbcx_gintmsk, nptxfempmsk, 1);
1319 		else
1320 			USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1321 					cvmx_usbcx_gintmsk, nptxfempmsk, 0);
1322 	}
1323 }
1324 
1325 /**
1326  * Fill the TX FIFO with an outgoing packet
1327  *
1328  * @usb:	  USB device state populated by cvmx_usb_initialize().
1329  * @channel:	  Channel number to get packet from
1330  */
1331 static void cvmx_usb_fill_tx_fifo(struct octeon_hcd *usb, int channel)
1332 {
1333 	union cvmx_usbcx_hccharx hcchar;
1334 	union cvmx_usbcx_hcspltx usbc_hcsplt;
1335 	union cvmx_usbcx_hctsizx usbc_hctsiz;
1336 	struct cvmx_usb_tx_fifo *fifo;
1337 
1338 	/* We only need to fill data on outbound channels */
1339 	hcchar.u32 = cvmx_usb_read_csr32(usb,
1340 					 CVMX_USBCX_HCCHARX(channel, usb->index));
1341 	if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1342 		return;
1343 
1344 	/* OUT Splits only have data on the start and not the complete */
1345 	usbc_hcsplt.u32 = cvmx_usb_read_csr32(usb,
1346 					      CVMX_USBCX_HCSPLTX(channel, usb->index));
1347 	if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1348 		return;
1349 
1350 	/*
1351 	 * Find out how many bytes we need to fill and convert it into 32bit
1352 	 * words.
1353 	 */
1354 	usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
1355 					      CVMX_USBCX_HCTSIZX(channel, usb->index));
1356 	if (!usbc_hctsiz.s.xfersize)
1357 		return;
1358 
1359 	if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1360 	    (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1361 		fifo = &usb->periodic;
1362 	else
1363 		fifo = &usb->nonperiodic;
1364 
1365 	fifo->entry[fifo->head].channel = channel;
1366 	fifo->entry[fifo->head].address =
1367 		cvmx_read64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1368 				   channel * 8);
1369 	fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize + 3) >> 2;
1370 	fifo->head++;
1371 	if (fifo->head > MAX_CHANNELS)
1372 		fifo->head = 0;
1373 
1374 	cvmx_usb_poll_tx_fifo(usb);
1375 }
1376 
1377 /**
1378  * Perform channel specific setup for Control transactions. All
1379  * the generic stuff will already have been done in cvmx_usb_start_channel().
1380  *
1381  * @usb:	  USB device state populated by cvmx_usb_initialize().
1382  * @channel:	  Channel to setup
1383  * @pipe:	  Pipe for control transaction
1384  */
1385 static void cvmx_usb_start_channel_control(struct octeon_hcd *usb,
1386 					   int channel,
1387 					   struct cvmx_usb_pipe *pipe)
1388 {
1389 	struct usb_hcd *hcd = octeon_to_hcd(usb);
1390 	struct device *dev = hcd->self.controller;
1391 	struct cvmx_usb_transaction *transaction =
1392 		list_first_entry(&pipe->transactions, typeof(*transaction),
1393 				 node);
1394 	struct usb_ctrlrequest *header =
1395 		cvmx_phys_to_ptr(transaction->control_header);
1396 	int bytes_to_transfer = transaction->buffer_length -
1397 		transaction->actual_bytes;
1398 	int packets_to_transfer;
1399 	union cvmx_usbcx_hctsizx usbc_hctsiz;
1400 
1401 	usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
1402 					      CVMX_USBCX_HCTSIZX(channel, usb->index));
1403 
1404 	switch (transaction->stage) {
1405 	case CVMX_USB_STAGE_NON_CONTROL:
1406 	case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1407 		dev_err(dev, "%s: ERROR - Non control stage\n", __func__);
1408 		break;
1409 	case CVMX_USB_STAGE_SETUP:
1410 		usbc_hctsiz.s.pid = 3; /* Setup */
1411 		bytes_to_transfer = sizeof(*header);
1412 		/* All Control operations start with a setup going OUT */
1413 		USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1414 				cvmx_usbcx_hccharx, epdir,
1415 				CVMX_USB_DIRECTION_OUT);
1416 		/*
1417 		 * Setup send the control header instead of the buffer data. The
1418 		 * buffer data will be used in the next stage
1419 		 */
1420 		cvmx_write64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1421 					channel * 8,
1422 				    transaction->control_header);
1423 		break;
1424 	case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1425 		usbc_hctsiz.s.pid = 3; /* Setup */
1426 		bytes_to_transfer = 0;
1427 		/* All Control operations start with a setup going OUT */
1428 		USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1429 				cvmx_usbcx_hccharx, epdir,
1430 				CVMX_USB_DIRECTION_OUT);
1431 
1432 		USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1433 				cvmx_usbcx_hcspltx, compsplt, 1);
1434 		break;
1435 	case CVMX_USB_STAGE_DATA:
1436 		usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1437 		if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1438 			if (header->bRequestType & USB_DIR_IN)
1439 				bytes_to_transfer = 0;
1440 			else if (bytes_to_transfer > pipe->max_packet)
1441 				bytes_to_transfer = pipe->max_packet;
1442 		}
1443 		USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1444 				cvmx_usbcx_hccharx, epdir,
1445 				((header->bRequestType & USB_DIR_IN) ?
1446 					CVMX_USB_DIRECTION_IN :
1447 					CVMX_USB_DIRECTION_OUT));
1448 		break;
1449 	case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1450 		usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1451 		if (!(header->bRequestType & USB_DIR_IN))
1452 			bytes_to_transfer = 0;
1453 		USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1454 				cvmx_usbcx_hccharx, epdir,
1455 				((header->bRequestType & USB_DIR_IN) ?
1456 					CVMX_USB_DIRECTION_IN :
1457 					CVMX_USB_DIRECTION_OUT));
1458 		USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1459 				cvmx_usbcx_hcspltx, compsplt, 1);
1460 		break;
1461 	case CVMX_USB_STAGE_STATUS:
1462 		usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1463 		bytes_to_transfer = 0;
1464 		USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1465 				cvmx_usbcx_hccharx, epdir,
1466 				((header->bRequestType & USB_DIR_IN) ?
1467 					CVMX_USB_DIRECTION_OUT :
1468 					CVMX_USB_DIRECTION_IN));
1469 		break;
1470 	case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1471 		usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1472 		bytes_to_transfer = 0;
1473 		USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1474 				cvmx_usbcx_hccharx, epdir,
1475 				((header->bRequestType & USB_DIR_IN) ?
1476 					CVMX_USB_DIRECTION_OUT :
1477 					CVMX_USB_DIRECTION_IN));
1478 		USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1479 				cvmx_usbcx_hcspltx, compsplt, 1);
1480 		break;
1481 	}
1482 
1483 	/*
1484 	 * Make sure the transfer never exceeds the byte limit of the hardware.
1485 	 * Further bytes will be sent as continued transactions
1486 	 */
1487 	if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1488 		/* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1489 		bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1490 		bytes_to_transfer *= pipe->max_packet;
1491 	}
1492 
1493 	/*
1494 	 * Calculate the number of packets to transfer. If the length is zero
1495 	 * we still need to transfer one packet
1496 	 */
1497 	packets_to_transfer = DIV_ROUND_UP(bytes_to_transfer,
1498 					   pipe->max_packet);
1499 	if (packets_to_transfer == 0) {
1500 		packets_to_transfer = 1;
1501 	} else if ((packets_to_transfer > 1) &&
1502 			(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1503 		/*
1504 		 * Limit to one packet when not using DMA. Channels must be
1505 		 * restarted between every packet for IN transactions, so there
1506 		 * is no reason to do multiple packets in a row
1507 		 */
1508 		packets_to_transfer = 1;
1509 		bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1510 	} else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1511 		/*
1512 		 * Limit the number of packet and data transferred to what the
1513 		 * hardware can handle
1514 		 */
1515 		packets_to_transfer = MAX_TRANSFER_PACKETS;
1516 		bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1517 	}
1518 
1519 	usbc_hctsiz.s.xfersize = bytes_to_transfer;
1520 	usbc_hctsiz.s.pktcnt = packets_to_transfer;
1521 
1522 	cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index),
1523 			     usbc_hctsiz.u32);
1524 }
1525 
1526 /**
1527  * Start a channel to perform the pipe's head transaction
1528  *
1529  * @usb:	  USB device state populated by cvmx_usb_initialize().
1530  * @channel:	  Channel to setup
1531  * @pipe:	  Pipe to start
1532  */
1533 static void cvmx_usb_start_channel(struct octeon_hcd *usb, int channel,
1534 				   struct cvmx_usb_pipe *pipe)
1535 {
1536 	struct cvmx_usb_transaction *transaction =
1537 		list_first_entry(&pipe->transactions, typeof(*transaction),
1538 				 node);
1539 
1540 	/* Make sure all writes to the DMA region get flushed */
1541 	CVMX_SYNCW;
1542 
1543 	/* Attach the channel to the pipe */
1544 	usb->pipe_for_channel[channel] = pipe;
1545 	pipe->channel = channel;
1546 	pipe->flags |= CVMX_USB_PIPE_FLAGS_SCHEDULED;
1547 
1548 	/* Mark this channel as in use */
1549 	usb->idle_hardware_channels &= ~(1 << channel);
1550 
1551 	/* Enable the channel interrupt bits */
1552 	{
1553 		union cvmx_usbcx_hcintx usbc_hcint;
1554 		union cvmx_usbcx_hcintmskx usbc_hcintmsk;
1555 		union cvmx_usbcx_haintmsk usbc_haintmsk;
1556 
1557 		/* Clear all channel status bits */
1558 		usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
1559 						     CVMX_USBCX_HCINTX(channel, usb->index));
1560 
1561 		cvmx_usb_write_csr32(usb,
1562 				     CVMX_USBCX_HCINTX(channel, usb->index),
1563 				     usbc_hcint.u32);
1564 
1565 		usbc_hcintmsk.u32 = 0;
1566 		usbc_hcintmsk.s.chhltdmsk = 1;
1567 		if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1568 			/*
1569 			 * Channels need these extra interrupts when we aren't
1570 			 * in DMA mode.
1571 			 */
1572 			usbc_hcintmsk.s.datatglerrmsk = 1;
1573 			usbc_hcintmsk.s.frmovrunmsk = 1;
1574 			usbc_hcintmsk.s.bblerrmsk = 1;
1575 			usbc_hcintmsk.s.xacterrmsk = 1;
1576 			if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1577 				/*
1578 				 * Splits don't generate xfercompl, so we need
1579 				 * ACK and NYET.
1580 				 */
1581 				usbc_hcintmsk.s.nyetmsk = 1;
1582 				usbc_hcintmsk.s.ackmsk = 1;
1583 			}
1584 			usbc_hcintmsk.s.nakmsk = 1;
1585 			usbc_hcintmsk.s.stallmsk = 1;
1586 			usbc_hcintmsk.s.xfercomplmsk = 1;
1587 		}
1588 		cvmx_usb_write_csr32(usb,
1589 				     CVMX_USBCX_HCINTMSKX(channel, usb->index),
1590 				     usbc_hcintmsk.u32);
1591 
1592 		/* Enable the channel interrupt to propagate */
1593 		usbc_haintmsk.u32 = cvmx_usb_read_csr32(usb,
1594 							CVMX_USBCX_HAINTMSK(usb->index));
1595 		usbc_haintmsk.s.haintmsk |= 1 << channel;
1596 		cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index),
1597 				     usbc_haintmsk.u32);
1598 	}
1599 
1600 	/* Setup the location the DMA engine uses. */
1601 	{
1602 		u64 reg;
1603 		u64 dma_address = transaction->buffer +
1604 				  transaction->actual_bytes;
1605 
1606 		if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1607 			dma_address = transaction->buffer +
1608 					transaction->iso_packets[0].offset +
1609 					transaction->actual_bytes;
1610 
1611 		if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT)
1612 			reg = CVMX_USBNX_DMA0_OUTB_CHN0(usb->index);
1613 		else
1614 			reg = CVMX_USBNX_DMA0_INB_CHN0(usb->index);
1615 		cvmx_write64_uint64(reg + channel * 8, dma_address);
1616 	}
1617 
1618 	/* Setup both the size of the transfer and the SPLIT characteristics */
1619 	{
1620 		union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
1621 		union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
1622 		int packets_to_transfer;
1623 		int bytes_to_transfer = transaction->buffer_length -
1624 			transaction->actual_bytes;
1625 
1626 		/*
1627 		 * ISOCHRONOUS transactions store each individual transfer size
1628 		 * in the packet structure, not the global buffer_length
1629 		 */
1630 		if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1631 			bytes_to_transfer =
1632 				transaction->iso_packets[0].length -
1633 				transaction->actual_bytes;
1634 
1635 		/*
1636 		 * We need to do split transactions when we are talking to non
1637 		 * high speed devices that are behind a high speed hub
1638 		 */
1639 		if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1640 			/*
1641 			 * On the start split phase (stage is even) record the
1642 			 * frame number we will need to send the split complete.
1643 			 * We only store the lower two bits since the time ahead
1644 			 * can only be two frames
1645 			 */
1646 			if ((transaction->stage & 1) == 0) {
1647 				if (transaction->type == CVMX_USB_TRANSFER_BULK)
1648 					pipe->split_sc_frame =
1649 						(usb->frame_number + 1) & 0x7f;
1650 				else
1651 					pipe->split_sc_frame =
1652 						(usb->frame_number + 2) & 0x7f;
1653 			} else {
1654 				pipe->split_sc_frame = -1;
1655 			}
1656 
1657 			usbc_hcsplt.s.spltena = 1;
1658 			usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1659 			usbc_hcsplt.s.prtaddr = pipe->hub_port;
1660 			usbc_hcsplt.s.compsplt = (transaction->stage ==
1661 				CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1662 
1663 			/*
1664 			 * SPLIT transactions can only ever transmit one data
1665 			 * packet so limit the transfer size to the max packet
1666 			 * size
1667 			 */
1668 			if (bytes_to_transfer > pipe->max_packet)
1669 				bytes_to_transfer = pipe->max_packet;
1670 
1671 			/*
1672 			 * ISOCHRONOUS OUT splits are unique in that they limit
1673 			 * data transfers to 188 byte chunks representing the
1674 			 * begin/middle/end of the data or all
1675 			 */
1676 			if (!usbc_hcsplt.s.compsplt &&
1677 			    (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1678 			    (pipe->transfer_type ==
1679 			     CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1680 				/*
1681 				 * Clear the split complete frame number as
1682 				 * there isn't going to be a split complete
1683 				 */
1684 				pipe->split_sc_frame = -1;
1685 				/*
1686 				 * See if we've started this transfer and sent
1687 				 * data
1688 				 */
1689 				if (transaction->actual_bytes == 0) {
1690 					/*
1691 					 * Nothing sent yet, this is either a
1692 					 * begin or the entire payload
1693 					 */
1694 					if (bytes_to_transfer <= 188)
1695 						/* Entire payload in one go */
1696 						usbc_hcsplt.s.xactpos = 3;
1697 					else
1698 						/* First part of payload */
1699 						usbc_hcsplt.s.xactpos = 2;
1700 				} else {
1701 					/*
1702 					 * Continuing the previous data, we must
1703 					 * either be in the middle or at the end
1704 					 */
1705 					if (bytes_to_transfer <= 188)
1706 						/* End of payload */
1707 						usbc_hcsplt.s.xactpos = 1;
1708 					else
1709 						/* Middle of payload */
1710 						usbc_hcsplt.s.xactpos = 0;
1711 				}
1712 				/*
1713 				 * Again, the transfer size is limited to 188
1714 				 * bytes
1715 				 */
1716 				if (bytes_to_transfer > 188)
1717 					bytes_to_transfer = 188;
1718 			}
1719 		}
1720 
1721 		/*
1722 		 * Make sure the transfer never exceeds the byte limit of the
1723 		 * hardware. Further bytes will be sent as continued
1724 		 * transactions
1725 		 */
1726 		if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1727 			/*
1728 			 * Round MAX_TRANSFER_BYTES to a multiple of out packet
1729 			 * size
1730 			 */
1731 			bytes_to_transfer = MAX_TRANSFER_BYTES /
1732 				pipe->max_packet;
1733 			bytes_to_transfer *= pipe->max_packet;
1734 		}
1735 
1736 		/*
1737 		 * Calculate the number of packets to transfer. If the length is
1738 		 * zero we still need to transfer one packet
1739 		 */
1740 		packets_to_transfer =
1741 			DIV_ROUND_UP(bytes_to_transfer, pipe->max_packet);
1742 		if (packets_to_transfer == 0) {
1743 			packets_to_transfer = 1;
1744 		} else if ((packets_to_transfer > 1) &&
1745 			   (usb->init_flags &
1746 			    CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1747 			/*
1748 			 * Limit to one packet when not using DMA. Channels must
1749 			 * be restarted between every packet for IN
1750 			 * transactions, so there is no reason to do multiple
1751 			 * packets in a row
1752 			 */
1753 			packets_to_transfer = 1;
1754 			bytes_to_transfer = packets_to_transfer *
1755 				pipe->max_packet;
1756 		} else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1757 			/*
1758 			 * Limit the number of packet and data transferred to
1759 			 * what the hardware can handle
1760 			 */
1761 			packets_to_transfer = MAX_TRANSFER_PACKETS;
1762 			bytes_to_transfer = packets_to_transfer *
1763 				pipe->max_packet;
1764 		}
1765 
1766 		usbc_hctsiz.s.xfersize = bytes_to_transfer;
1767 		usbc_hctsiz.s.pktcnt = packets_to_transfer;
1768 
1769 		/* Update the DATA0/DATA1 toggle */
1770 		usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1771 		/*
1772 		 * High speed pipes may need a hardware ping before they start
1773 		 */
1774 		if (pipe->flags & CVMX_USB_PIPE_FLAGS_NEED_PING)
1775 			usbc_hctsiz.s.dopng = 1;
1776 
1777 		cvmx_usb_write_csr32(usb,
1778 				     CVMX_USBCX_HCSPLTX(channel, usb->index),
1779 				     usbc_hcsplt.u32);
1780 		cvmx_usb_write_csr32(usb,
1781 				     CVMX_USBCX_HCTSIZX(channel, usb->index),
1782 				     usbc_hctsiz.u32);
1783 	}
1784 
1785 	/* Setup the Host Channel Characteristics Register */
1786 	{
1787 		union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
1788 
1789 		/*
1790 		 * Set the startframe odd/even properly. This is only used for
1791 		 * periodic
1792 		 */
1793 		usbc_hcchar.s.oddfrm = usb->frame_number & 1;
1794 
1795 		/*
1796 		 * Set the number of back to back packets allowed by this
1797 		 * endpoint. Split transactions interpret "ec" as the number of
1798 		 * immediate retries of failure. These retries happen too
1799 		 * quickly, so we disable these entirely for splits
1800 		 */
1801 		if (cvmx_usb_pipe_needs_split(usb, pipe))
1802 			usbc_hcchar.s.ec = 1;
1803 		else if (pipe->multi_count < 1)
1804 			usbc_hcchar.s.ec = 1;
1805 		else if (pipe->multi_count > 3)
1806 			usbc_hcchar.s.ec = 3;
1807 		else
1808 			usbc_hcchar.s.ec = pipe->multi_count;
1809 
1810 		/* Set the rest of the endpoint specific settings */
1811 		usbc_hcchar.s.devaddr = pipe->device_addr;
1812 		usbc_hcchar.s.eptype = transaction->type;
1813 		usbc_hcchar.s.lspddev =
1814 			(pipe->device_speed == CVMX_USB_SPEED_LOW);
1815 		usbc_hcchar.s.epdir = pipe->transfer_dir;
1816 		usbc_hcchar.s.epnum = pipe->endpoint_num;
1817 		usbc_hcchar.s.mps = pipe->max_packet;
1818 		cvmx_usb_write_csr32(usb,
1819 				     CVMX_USBCX_HCCHARX(channel, usb->index),
1820 				     usbc_hcchar.u32);
1821 	}
1822 
1823 	/* Do transaction type specific fixups as needed */
1824 	switch (transaction->type) {
1825 	case CVMX_USB_TRANSFER_CONTROL:
1826 		cvmx_usb_start_channel_control(usb, channel, pipe);
1827 		break;
1828 	case CVMX_USB_TRANSFER_BULK:
1829 	case CVMX_USB_TRANSFER_INTERRUPT:
1830 		break;
1831 	case CVMX_USB_TRANSFER_ISOCHRONOUS:
1832 		if (!cvmx_usb_pipe_needs_split(usb, pipe)) {
1833 			/*
1834 			 * ISO transactions require different PIDs depending on
1835 			 * direction and how many packets are needed
1836 			 */
1837 			if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
1838 				if (pipe->multi_count < 2) /* Need DATA0 */
1839 					USB_SET_FIELD32(
1840 						CVMX_USBCX_HCTSIZX(channel,
1841 								   usb->index),
1842 						cvmx_usbcx_hctsizx, pid, 0);
1843 				else /* Need MDATA */
1844 					USB_SET_FIELD32(
1845 						CVMX_USBCX_HCTSIZX(channel,
1846 								   usb->index),
1847 						cvmx_usbcx_hctsizx, pid, 3);
1848 			}
1849 		}
1850 		break;
1851 	}
1852 	{
1853 		union cvmx_usbcx_hctsizx usbc_hctsiz = { .u32 =
1854 			cvmx_usb_read_csr32(usb,
1855 					    CVMX_USBCX_HCTSIZX(channel,
1856 							       usb->index))
1857 		};
1858 		transaction->xfersize = usbc_hctsiz.s.xfersize;
1859 		transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1860 	}
1861 	/* Remember when we start a split transaction */
1862 	if (cvmx_usb_pipe_needs_split(usb, pipe))
1863 		usb->active_split = transaction;
1864 	USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1865 			cvmx_usbcx_hccharx, chena, 1);
1866 	if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1867 		cvmx_usb_fill_tx_fifo(usb, channel);
1868 }
1869 
1870 /**
1871  * Find a pipe that is ready to be scheduled to hardware.
1872  * @usb:	 USB device state populated by cvmx_usb_initialize().
1873  * @xfer_type:	 Transfer type
1874  *
1875  * Returns: Pipe or NULL if none are ready
1876  */
1877 static struct cvmx_usb_pipe *cvmx_usb_find_ready_pipe(struct octeon_hcd *usb,
1878 						      enum cvmx_usb_transfer xfer_type)
1879 {
1880 	struct list_head *list = usb->active_pipes + xfer_type;
1881 	u64 current_frame = usb->frame_number;
1882 	struct cvmx_usb_pipe *pipe;
1883 
1884 	list_for_each_entry(pipe, list, node) {
1885 		struct cvmx_usb_transaction *t =
1886 			list_first_entry(&pipe->transactions, typeof(*t),
1887 					 node);
1888 		if (!(pipe->flags & CVMX_USB_PIPE_FLAGS_SCHEDULED) && t &&
1889 		    (pipe->next_tx_frame <= current_frame) &&
1890 		    ((pipe->split_sc_frame == -1) ||
1891 		     ((((int)current_frame - pipe->split_sc_frame) & 0x7f) <
1892 		      0x40)) &&
1893 		    (!usb->active_split || (usb->active_split == t))) {
1894 			prefetch(t);
1895 			return pipe;
1896 		}
1897 	}
1898 	return NULL;
1899 }
1900 
1901 static struct cvmx_usb_pipe *cvmx_usb_next_pipe(struct octeon_hcd *usb,
1902 						int is_sof)
1903 {
1904 	struct cvmx_usb_pipe *pipe;
1905 
1906 	/* Find a pipe needing service. */
1907 	if (is_sof) {
1908 		/*
1909 		 * Only process periodic pipes on SOF interrupts. This way we
1910 		 * are sure that the periodic data is sent in the beginning of
1911 		 * the frame.
1912 		 */
1913 		pipe = cvmx_usb_find_ready_pipe(usb,
1914 						CVMX_USB_TRANSFER_ISOCHRONOUS);
1915 		if (pipe)
1916 			return pipe;
1917 		pipe = cvmx_usb_find_ready_pipe(usb,
1918 						CVMX_USB_TRANSFER_INTERRUPT);
1919 		if (pipe)
1920 			return pipe;
1921 	}
1922 	pipe = cvmx_usb_find_ready_pipe(usb, CVMX_USB_TRANSFER_CONTROL);
1923 	if (pipe)
1924 		return pipe;
1925 	return cvmx_usb_find_ready_pipe(usb, CVMX_USB_TRANSFER_BULK);
1926 }
1927 
1928 /**
1929  * Called whenever a pipe might need to be scheduled to the
1930  * hardware.
1931  *
1932  * @usb:	 USB device state populated by cvmx_usb_initialize().
1933  * @is_sof:	 True if this schedule was called on a SOF interrupt.
1934  */
1935 static void cvmx_usb_schedule(struct octeon_hcd *usb, int is_sof)
1936 {
1937 	int channel;
1938 	struct cvmx_usb_pipe *pipe;
1939 	int need_sof;
1940 	u64 min_due;
1941 	bool due_now;
1942 	enum cvmx_usb_transfer ttype;
1943 
1944 	if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1945 		/*
1946 		 * Without DMA we need to be careful to not schedule something
1947 		 * at the end of a frame and cause an overrun.
1948 		 */
1949 		union cvmx_usbcx_hfnum hfnum = {
1950 			.u32 = cvmx_usb_read_csr32(usb,
1951 						CVMX_USBCX_HFNUM(usb->index))
1952 		};
1953 
1954 		union cvmx_usbcx_hfir hfir = {
1955 			.u32 = cvmx_usb_read_csr32(usb,
1956 						CVMX_USBCX_HFIR(usb->index))
1957 		};
1958 
1959 		if (hfnum.s.frrem < hfir.s.frint / 4)
1960 			goto done;
1961 	}
1962 
1963 	while (usb->idle_hardware_channels) {
1964 		/* Find an idle channel */
1965 		channel = __fls(usb->idle_hardware_channels);
1966 		if (unlikely(channel > 7))
1967 			break;
1968 
1969 		pipe = cvmx_usb_next_pipe(usb, is_sof);
1970 		if (!pipe)
1971 			break;
1972 
1973 		cvmx_usb_start_channel(usb, channel, pipe);
1974 	}
1975 
1976 done:
1977 	/*
1978 	 * Only enable SOF interrupts when we have transactions pending in the
1979 	 * future that might need to be scheduled
1980 	 */
1981 	need_sof = 0;
1982 	min_due = ~0ull;
1983 	due_now = false;
1984 	for (ttype = CVMX_USB_TRANSFER_CONTROL;
1985 	     ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
1986 		list_for_each_entry(pipe, &usb->active_pipes[ttype], node) {
1987 			/*
1988 			 * Do not sleep past a pipe that is only retried from a
1989 			 * SOF: a periodic one that is due but was not started
1990 			 * (no idle channel, closed split window, another pipe's
1991 			 * split active), or any pipe waiting for the frame its
1992 			 * split complete may go out in. Other control and bulk
1993 			 * pipes are left out: their next_tx_frame is not updated
1994 			 * on completion, so they always look due.
1995 			 */
1996 			if (pipe->split_sc_frame != -1 ||
1997 			    (pipe->next_tx_frame <= usb->frame_number &&
1998 			     (ttype == CVMX_USB_TRANSFER_ISOCHRONOUS ||
1999 			      ttype == CVMX_USB_TRANSFER_INTERRUPT)))
2000 				due_now = true;
2001 			if (pipe->next_tx_frame > usb->frame_number &&
2002 			    pipe->next_tx_frame < min_due)
2003 				min_due = pipe->next_tx_frame;
2004 		}
2005 	}
2006 	if (due_now) {
2007 		need_sof = 1;
2008 	} else if (min_due != ~0ull) {
2009 		u64 delta = min_due - usb->frame_number;
2010 
2011 		/*
2012 		 * frame_number is resynced from HFNUM on every poll, so a
2013 		 * deadline that is many frames away does not need an
2014 		 * interrupt on every SOF to count them down - sleep on the
2015 		 * timer instead and keep SOF interrupts for deadlines within
2016 		 * a few frames. Stay well below the 16383-frame wrap of
2017 		 * HFNUM. One (micro)frame is 125us in high-speed mode.
2018 		 */
2019 		if (delta <= 4 || delta > 8000)
2020 			need_sof = 1;
2021 		else
2022 			hrtimer_start(&usb->sof_timer,
2023 				      ns_to_ktime((delta - 2) * 125000),
2024 				      HRTIMER_MODE_REL);
2025 	}
2026 	USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
2027 			cvmx_usbcx_gintmsk, sofmsk, need_sof);
2028 }
2029 
2030 static void octeon_usb_urb_complete_callback(struct octeon_hcd *usb,
2031 					     enum cvmx_usb_status status,
2032 					     struct cvmx_usb_pipe *pipe,
2033 					     struct cvmx_usb_transaction
2034 						*transaction,
2035 					     int bytes_transferred,
2036 					     struct urb *urb)
2037 {
2038 	struct usb_hcd *hcd = octeon_to_hcd(usb);
2039 	struct device *dev = hcd->self.controller;
2040 
2041 	if (likely(status == CVMX_USB_STATUS_OK))
2042 		urb->actual_length = bytes_transferred;
2043 	else
2044 		urb->actual_length = 0;
2045 
2046 	urb->hcpriv = NULL;
2047 
2048 	/* For Isochronous transactions we need to update the URB packet status
2049 	 * list from data in our private copy
2050 	 */
2051 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2052 		int i;
2053 		/*
2054 		 * The pointer to the private list is stored in the setup_packet
2055 		 * field.
2056 		 */
2057 		struct cvmx_usb_iso_packet *iso_packet =
2058 			(struct cvmx_usb_iso_packet *)urb->setup_packet;
2059 		/* Recalculate the transfer size by adding up each packet */
2060 		urb->actual_length = 0;
2061 		for (i = 0; i < urb->number_of_packets; i++) {
2062 			if (iso_packet[i].status == CVMX_USB_STATUS_OK) {
2063 				urb->iso_frame_desc[i].status = 0;
2064 				urb->iso_frame_desc[i].actual_length =
2065 					iso_packet[i].length;
2066 				urb->actual_length +=
2067 					urb->iso_frame_desc[i].actual_length;
2068 			} else {
2069 				dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%p transaction=%p size=%d\n",
2070 					i, urb->number_of_packets,
2071 					iso_packet[i].status, pipe,
2072 					transaction, iso_packet[i].length);
2073 				urb->iso_frame_desc[i].status = -EREMOTEIO;
2074 			}
2075 		}
2076 		/* Free the private list now that we don't need it anymore */
2077 		kfree(iso_packet);
2078 		urb->setup_packet = NULL;
2079 	}
2080 
2081 	switch (status) {
2082 	case CVMX_USB_STATUS_OK:
2083 		urb->status = 0;
2084 		break;
2085 	case CVMX_USB_STATUS_CANCEL:
2086 		if (urb->status == 0)
2087 			urb->status = -ENOENT;
2088 		break;
2089 	case CVMX_USB_STATUS_STALL:
2090 		dev_dbg(dev, "status=stall pipe=%p transaction=%p size=%d\n",
2091 			pipe, transaction, bytes_transferred);
2092 		urb->status = -EPIPE;
2093 		break;
2094 	case CVMX_USB_STATUS_BABBLEERR:
2095 		dev_dbg(dev, "status=babble pipe=%p transaction=%p size=%d\n",
2096 			pipe, transaction, bytes_transferred);
2097 		urb->status = -EPIPE;
2098 		break;
2099 	case CVMX_USB_STATUS_SHORT:
2100 		dev_dbg(dev, "status=short pipe=%p transaction=%p size=%d\n",
2101 			pipe, transaction, bytes_transferred);
2102 		urb->status = -EREMOTEIO;
2103 		break;
2104 	case CVMX_USB_STATUS_ERROR:
2105 	case CVMX_USB_STATUS_XACTERR:
2106 	case CVMX_USB_STATUS_DATATGLERR:
2107 	case CVMX_USB_STATUS_FRAMEERR:
2108 		dev_dbg(dev, "status=%d pipe=%p transaction=%p size=%d\n",
2109 			status, pipe, transaction, bytes_transferred);
2110 		urb->status = -EPROTO;
2111 		break;
2112 	}
2113 	usb_hcd_unlink_urb_from_ep(octeon_to_hcd(usb), urb);
2114 	spin_unlock(&usb->lock);
2115 	usb_hcd_giveback_urb(octeon_to_hcd(usb), urb, urb->status);
2116 	spin_lock(&usb->lock);
2117 }
2118 
2119 /**
2120  * Signal the completion of a transaction and free it. The
2121  * transaction will be removed from the pipe transaction list.
2122  *
2123  * @usb:	 USB device state populated by cvmx_usb_initialize().
2124  * @pipe:	 Pipe the transaction is on
2125  * @transaction:
2126  *		 Transaction that completed
2127  * @complete_code:
2128  *		 Completion code
2129  */
2130 static void cvmx_usb_complete(struct octeon_hcd *usb,
2131 			      struct cvmx_usb_pipe *pipe,
2132 			      struct cvmx_usb_transaction *transaction,
2133 			      enum cvmx_usb_status complete_code)
2134 {
2135 	/* If this was a split then clear our split in progress marker */
2136 	if (usb->active_split == transaction)
2137 		usb->active_split = NULL;
2138 
2139 	/*
2140 	 * Isochronous transactions need extra processing as they might not be
2141 	 * done after a single data transfer
2142 	 */
2143 	if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
2144 		/* Update the number of bytes transferred in this ISO packet */
2145 		transaction->iso_packets[0].length = transaction->actual_bytes;
2146 		transaction->iso_packets[0].status = complete_code;
2147 
2148 		/*
2149 		 * If there are more ISOs pending and we succeeded, schedule the
2150 		 * next one
2151 		 */
2152 		if ((transaction->iso_number_packets > 1) &&
2153 		    (complete_code == CVMX_USB_STATUS_OK)) {
2154 			/* No bytes transferred for this packet as of yet */
2155 			transaction->actual_bytes = 0;
2156 			/* One less ISO waiting to transfer */
2157 			transaction->iso_number_packets--;
2158 			/* Increment to the next location in our packet array */
2159 			transaction->iso_packets++;
2160 			transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2161 			return;
2162 		}
2163 	}
2164 
2165 	/* Remove the transaction from the pipe list */
2166 	list_del(&transaction->node);
2167 	if (list_empty(&pipe->transactions))
2168 		list_move_tail(&pipe->node, &usb->idle_pipes);
2169 	octeon_usb_urb_complete_callback(usb, complete_code, pipe,
2170 					 transaction,
2171 					 transaction->actual_bytes,
2172 					 transaction->urb);
2173 	kfree(transaction);
2174 }
2175 
2176 /**
2177  * Submit a usb transaction to a pipe. Called for all types
2178  * of transactions.
2179  *
2180  * @usb:
2181  * @pipe:	    Which pipe to submit to.
2182  * @type:	    Transaction type
2183  * @buffer:	    User buffer for the transaction
2184  * @buffer_length:
2185  *		    User buffer's length in bytes
2186  * @control_header:
2187  *		    For control transactions, the 8 byte standard header
2188  * @iso_start_frame:
2189  *		    For ISO transactions, the start frame
2190  * @iso_number_packets:
2191  *		    For ISO, the number of packet in the transaction.
2192  * @iso_packets:
2193  *		    A description of each ISO packet
2194  * @urb:	    URB for the callback
2195  *
2196  * Returns: Transaction or NULL on failure.
2197  */
2198 static struct cvmx_usb_transaction *cvmx_usb_submit_transaction(
2199 				struct octeon_hcd *usb,
2200 				struct cvmx_usb_pipe *pipe,
2201 				enum cvmx_usb_transfer type,
2202 				u64 buffer,
2203 				int buffer_length,
2204 				u64 control_header,
2205 				int iso_start_frame,
2206 				int iso_number_packets,
2207 				struct cvmx_usb_iso_packet *iso_packets,
2208 				struct urb *urb)
2209 {
2210 	struct cvmx_usb_transaction *transaction;
2211 
2212 	if (unlikely(pipe->transfer_type != type))
2213 		return NULL;
2214 
2215 	transaction = kzalloc_obj(*transaction, GFP_ATOMIC);
2216 	if (unlikely(!transaction))
2217 		return NULL;
2218 
2219 	transaction->type = type;
2220 	transaction->buffer = buffer;
2221 	transaction->buffer_length = buffer_length;
2222 	transaction->control_header = control_header;
2223 	/* FIXME: This is not used, implement it. */
2224 	transaction->iso_start_frame = iso_start_frame;
2225 	transaction->iso_number_packets = iso_number_packets;
2226 	transaction->iso_packets = iso_packets;
2227 	transaction->urb = urb;
2228 	if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2229 		transaction->stage = CVMX_USB_STAGE_SETUP;
2230 	else
2231 		transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2232 
2233 	if (!list_empty(&pipe->transactions)) {
2234 		list_add_tail(&transaction->node, &pipe->transactions);
2235 	} else {
2236 		list_add_tail(&transaction->node, &pipe->transactions);
2237 		list_move_tail(&pipe->node,
2238 			       &usb->active_pipes[pipe->transfer_type]);
2239 
2240 		/*
2241 		 * We may need to schedule the pipe if this was the head of the
2242 		 * pipe.
2243 		 */
2244 		cvmx_usb_schedule(usb, 0);
2245 	}
2246 
2247 	return transaction;
2248 }
2249 
2250 /**
2251  * Call to submit a USB Bulk transfer to a pipe.
2252  *
2253  * @usb:	    USB device state populated by cvmx_usb_initialize().
2254  * @pipe:	    Handle to the pipe for the transfer.
2255  * @urb:	    URB.
2256  *
2257  * Returns: A submitted transaction or NULL on failure.
2258  */
2259 static struct cvmx_usb_transaction *cvmx_usb_submit_bulk(
2260 						struct octeon_hcd *usb,
2261 						struct cvmx_usb_pipe *pipe,
2262 						struct urb *urb)
2263 {
2264 	return cvmx_usb_submit_transaction(usb, pipe, CVMX_USB_TRANSFER_BULK,
2265 					   urb->transfer_dma,
2266 					   urb->transfer_buffer_length,
2267 					   0, /* control_header */
2268 					   0, /* iso_start_frame */
2269 					   0, /* iso_number_packets */
2270 					   NULL, /* iso_packets */
2271 					   urb);
2272 }
2273 
2274 /**
2275  * Call to submit a USB Interrupt transfer to a pipe.
2276  *
2277  * @usb:	    USB device state populated by cvmx_usb_initialize().
2278  * @pipe:	    Handle to the pipe for the transfer.
2279  * @urb:	    URB returned when the callback is called.
2280  *
2281  * Returns: A submitted transaction or NULL on failure.
2282  */
2283 static struct cvmx_usb_transaction *cvmx_usb_submit_interrupt(
2284 						struct octeon_hcd *usb,
2285 						struct cvmx_usb_pipe *pipe,
2286 						struct urb *urb)
2287 {
2288 	return cvmx_usb_submit_transaction(usb, pipe,
2289 					   CVMX_USB_TRANSFER_INTERRUPT,
2290 					   urb->transfer_dma,
2291 					   urb->transfer_buffer_length,
2292 					   0, /* control_header */
2293 					   0, /* iso_start_frame */
2294 					   0, /* iso_number_packets */
2295 					   NULL, /* iso_packets */
2296 					   urb);
2297 }
2298 
2299 /**
2300  * Call to submit a USB Control transfer to a pipe.
2301  *
2302  * @usb:	    USB device state populated by cvmx_usb_initialize().
2303  * @pipe:	    Handle to the pipe for the transfer.
2304  * @urb:	    URB.
2305  *
2306  * Returns: A submitted transaction or NULL on failure.
2307  */
2308 static struct cvmx_usb_transaction *cvmx_usb_submit_control(
2309 						struct octeon_hcd *usb,
2310 						struct cvmx_usb_pipe *pipe,
2311 						struct urb *urb)
2312 {
2313 	int buffer_length = urb->transfer_buffer_length;
2314 	u64 control_header = urb->setup_dma;
2315 	struct usb_ctrlrequest *header = cvmx_phys_to_ptr(control_header);
2316 
2317 	if ((header->bRequestType & USB_DIR_IN) == 0)
2318 		buffer_length = le16_to_cpu(header->wLength);
2319 
2320 	return cvmx_usb_submit_transaction(usb, pipe,
2321 					   CVMX_USB_TRANSFER_CONTROL,
2322 					   urb->transfer_dma, buffer_length,
2323 					   control_header,
2324 					   0, /* iso_start_frame */
2325 					   0, /* iso_number_packets */
2326 					   NULL, /* iso_packets */
2327 					   urb);
2328 }
2329 
2330 /**
2331  * Call to submit a USB Isochronous transfer to a pipe.
2332  *
2333  * @usb:	    USB device state populated by cvmx_usb_initialize().
2334  * @pipe:	    Handle to the pipe for the transfer.
2335  * @urb:	    URB returned when the callback is called.
2336  *
2337  * Returns: A submitted transaction or NULL on failure.
2338  */
2339 static struct cvmx_usb_transaction *cvmx_usb_submit_isochronous(
2340 						struct octeon_hcd *usb,
2341 						struct cvmx_usb_pipe *pipe,
2342 						struct urb *urb)
2343 {
2344 	struct cvmx_usb_iso_packet *packets;
2345 
2346 	packets = (struct cvmx_usb_iso_packet *)urb->setup_packet;
2347 	return cvmx_usb_submit_transaction(usb, pipe,
2348 					   CVMX_USB_TRANSFER_ISOCHRONOUS,
2349 					   urb->transfer_dma,
2350 					   urb->transfer_buffer_length,
2351 					   0, /* control_header */
2352 					   urb->start_frame,
2353 					   urb->number_of_packets,
2354 					   packets, urb);
2355 }
2356 
2357 /**
2358  * Cancel one outstanding request in a pipe. Canceling a request
2359  * can fail if the transaction has already completed before cancel
2360  * is called. Even after a successful cancel call, it may take
2361  * a frame or two for the cvmx_usb_poll() function to call the
2362  * associated callback.
2363  *
2364  * @usb:	 USB device state populated by cvmx_usb_initialize().
2365  * @pipe:	 Pipe to cancel requests in.
2366  * @transaction: Transaction to cancel, returned by the submit function.
2367  *
2368  * Returns: 0 or a negative error code.
2369  */
2370 static int cvmx_usb_cancel(struct octeon_hcd *usb,
2371 			   struct cvmx_usb_pipe *pipe,
2372 			   struct cvmx_usb_transaction *transaction)
2373 {
2374 	/*
2375 	 * If the transaction is the HEAD of the queue and scheduled. We need to
2376 	 * treat it special
2377 	 */
2378 	if (list_first_entry(&pipe->transactions, typeof(*transaction), node) ==
2379 	    transaction && (pipe->flags & CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
2380 		union cvmx_usbcx_hccharx usbc_hcchar;
2381 
2382 		usb->pipe_for_channel[pipe->channel] = NULL;
2383 		pipe->flags &= ~CVMX_USB_PIPE_FLAGS_SCHEDULED;
2384 
2385 		CVMX_SYNCW;
2386 
2387 		usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2388 						      CVMX_USBCX_HCCHARX(pipe->channel,
2389 									 usb->index));
2390 		/*
2391 		 * If the channel isn't enabled then the transaction already
2392 		 * completed.
2393 		 */
2394 		if (usbc_hcchar.s.chena) {
2395 			usbc_hcchar.s.chdis = 1;
2396 			cvmx_usb_write_csr32(usb,
2397 					     CVMX_USBCX_HCCHARX(pipe->channel,
2398 								usb->index),
2399 					     usbc_hcchar.u32);
2400 		}
2401 	}
2402 	cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_CANCEL);
2403 	return 0;
2404 }
2405 
2406 /**
2407  * Cancel all outstanding requests in a pipe. Logically all this
2408  * does is call cvmx_usb_cancel() in a loop.
2409  *
2410  * @usb:	 USB device state populated by cvmx_usb_initialize().
2411  * @pipe:	 Pipe to cancel requests in.
2412  *
2413  * Returns: 0 or a negative error code.
2414  */
2415 static int cvmx_usb_cancel_all(struct octeon_hcd *usb,
2416 			       struct cvmx_usb_pipe *pipe)
2417 {
2418 	struct cvmx_usb_transaction *transaction, *next;
2419 
2420 	/* Simply loop through and attempt to cancel each transaction */
2421 	list_for_each_entry_safe(transaction, next, &pipe->transactions, node) {
2422 		int result = cvmx_usb_cancel(usb, pipe, transaction);
2423 
2424 		if (unlikely(result != 0))
2425 			return result;
2426 	}
2427 	return 0;
2428 }
2429 
2430 /**
2431  * Close a pipe created with cvmx_usb_open_pipe().
2432  *
2433  * @usb:	 USB device state populated by cvmx_usb_initialize().
2434  * @pipe:	 Pipe to close.
2435  *
2436  * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
2437  *	    outstanding transfers.
2438  */
2439 static int cvmx_usb_close_pipe(struct octeon_hcd *usb,
2440 			       struct cvmx_usb_pipe *pipe)
2441 {
2442 	/* Fail if the pipe has pending transactions */
2443 	if (!list_empty(&pipe->transactions))
2444 		return -EBUSY;
2445 
2446 	list_del(&pipe->node);
2447 	kfree(pipe);
2448 
2449 	return 0;
2450 }
2451 
2452 /**
2453  * Get the current USB protocol level frame number. The frame
2454  * number is always in the range of 0-0x7ff.
2455  *
2456  * @usb: USB device state populated by cvmx_usb_initialize().
2457  *
2458  * Returns: USB frame number
2459  */
2460 static int cvmx_usb_get_frame_number(struct octeon_hcd *usb)
2461 {
2462 	union cvmx_usbcx_hfnum usbc_hfnum;
2463 
2464 	usbc_hfnum.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2465 
2466 	return usbc_hfnum.s.frnum;
2467 }
2468 
2469 static void cvmx_usb_transfer_control(struct octeon_hcd *usb,
2470 				      struct cvmx_usb_pipe *pipe,
2471 				      struct cvmx_usb_transaction *transaction,
2472 				      union cvmx_usbcx_hccharx usbc_hcchar,
2473 				      int buffer_space_left,
2474 				      int bytes_in_last_packet)
2475 {
2476 	switch (transaction->stage) {
2477 	case CVMX_USB_STAGE_NON_CONTROL:
2478 	case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2479 		/* This should be impossible */
2480 		cvmx_usb_complete(usb, pipe, transaction,
2481 				  CVMX_USB_STATUS_ERROR);
2482 		break;
2483 	case CVMX_USB_STAGE_SETUP:
2484 		pipe->pid_toggle = 1;
2485 		if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2486 			transaction->stage =
2487 				CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
2488 		} else {
2489 			struct usb_ctrlrequest *header =
2490 				cvmx_phys_to_ptr(transaction->control_header);
2491 			if (header->wLength)
2492 				transaction->stage = CVMX_USB_STAGE_DATA;
2493 			else
2494 				transaction->stage = CVMX_USB_STAGE_STATUS;
2495 		}
2496 		break;
2497 	case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2498 		{
2499 			struct usb_ctrlrequest *header =
2500 				cvmx_phys_to_ptr(transaction->control_header);
2501 			if (header->wLength)
2502 				transaction->stage = CVMX_USB_STAGE_DATA;
2503 			else
2504 				transaction->stage = CVMX_USB_STAGE_STATUS;
2505 		}
2506 		break;
2507 	case CVMX_USB_STAGE_DATA:
2508 		if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2509 			transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
2510 			/*
2511 			 * For setup OUT data that are splits,
2512 			 * the hardware doesn't appear to count
2513 			 * transferred data. Here we manually
2514 			 * update the data transferred
2515 			 */
2516 			if (!usbc_hcchar.s.epdir) {
2517 				if (buffer_space_left < pipe->max_packet)
2518 					transaction->actual_bytes +=
2519 						buffer_space_left;
2520 				else
2521 					transaction->actual_bytes +=
2522 						pipe->max_packet;
2523 			}
2524 		} else if ((buffer_space_left == 0) ||
2525 			   (bytes_in_last_packet < pipe->max_packet)) {
2526 			pipe->pid_toggle = 1;
2527 			transaction->stage = CVMX_USB_STAGE_STATUS;
2528 		}
2529 		break;
2530 	case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
2531 		if ((buffer_space_left == 0) ||
2532 		    (bytes_in_last_packet < pipe->max_packet)) {
2533 			pipe->pid_toggle = 1;
2534 			transaction->stage = CVMX_USB_STAGE_STATUS;
2535 		} else {
2536 			transaction->stage = CVMX_USB_STAGE_DATA;
2537 		}
2538 		break;
2539 	case CVMX_USB_STAGE_STATUS:
2540 		if (cvmx_usb_pipe_needs_split(usb, pipe))
2541 			transaction->stage =
2542 				CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
2543 		else
2544 			cvmx_usb_complete(usb, pipe, transaction,
2545 					  CVMX_USB_STATUS_OK);
2546 		break;
2547 	case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
2548 		cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2549 		break;
2550 	}
2551 }
2552 
2553 static void cvmx_usb_transfer_bulk(struct octeon_hcd *usb,
2554 				   struct cvmx_usb_pipe *pipe,
2555 				   struct cvmx_usb_transaction *transaction,
2556 				   union cvmx_usbcx_hcintx usbc_hcint,
2557 				   int buffer_space_left,
2558 				   int bytes_in_last_packet)
2559 {
2560 	/*
2561 	 * The only time a bulk transfer isn't complete when it finishes with
2562 	 * an ACK is during a split transaction. For splits we need to continue
2563 	 * the transfer if more data is needed.
2564 	 */
2565 	if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2566 		if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
2567 			transaction->stage =
2568 				CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2569 		else if (buffer_space_left &&
2570 			 (bytes_in_last_packet == pipe->max_packet))
2571 			transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2572 		else
2573 			cvmx_usb_complete(usb, pipe, transaction,
2574 					  CVMX_USB_STATUS_OK);
2575 	} else {
2576 		if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2577 		    (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
2578 		    (usbc_hcint.s.nak))
2579 			pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
2580 		if (!buffer_space_left ||
2581 		    (bytes_in_last_packet < pipe->max_packet))
2582 			cvmx_usb_complete(usb, pipe, transaction,
2583 					  CVMX_USB_STATUS_OK);
2584 	}
2585 }
2586 
2587 static void cvmx_usb_transfer_intr(struct octeon_hcd *usb,
2588 				   struct cvmx_usb_pipe *pipe,
2589 				   struct cvmx_usb_transaction *transaction,
2590 				   int buffer_space_left,
2591 				   int bytes_in_last_packet)
2592 {
2593 	if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2594 		if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL) {
2595 			transaction->stage =
2596 				CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2597 		} else if (buffer_space_left &&
2598 			   (bytes_in_last_packet == pipe->max_packet)) {
2599 			transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2600 		} else {
2601 			pipe->next_tx_frame += pipe->interval;
2602 			cvmx_usb_complete(usb, pipe, transaction,
2603 					  CVMX_USB_STATUS_OK);
2604 		}
2605 	} else if (!buffer_space_left ||
2606 		   (bytes_in_last_packet < pipe->max_packet)) {
2607 		pipe->next_tx_frame += pipe->interval;
2608 		cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2609 	}
2610 }
2611 
2612 static void cvmx_usb_transfer_isoc(struct octeon_hcd *usb,
2613 				   struct cvmx_usb_pipe *pipe,
2614 				   struct cvmx_usb_transaction *transaction,
2615 				   int buffer_space_left,
2616 				   int bytes_in_last_packet,
2617 				   int bytes_this_transfer)
2618 {
2619 	if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2620 		/*
2621 		 * ISOCHRONOUS OUT splits don't require a complete split stage.
2622 		 * Instead they use a sequence of begin OUT splits to transfer
2623 		 * the data 188 bytes at a time. Once the transfer is complete,
2624 		 * the pipe sleeps until the next schedule interval.
2625 		 */
2626 		if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
2627 			/*
2628 			 * If no space left or this wasn't a max size packet
2629 			 * then this transfer is complete. Otherwise start it
2630 			 * again to send the next 188 bytes
2631 			 */
2632 			if (!buffer_space_left || (bytes_this_transfer < 188)) {
2633 				pipe->next_tx_frame += pipe->interval;
2634 				cvmx_usb_complete(usb, pipe, transaction,
2635 						  CVMX_USB_STATUS_OK);
2636 			}
2637 			return;
2638 		}
2639 		if (transaction->stage ==
2640 		    CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
2641 			/*
2642 			 * We are in the incoming data phase. Keep getting data
2643 			 * until we run out of space or get a small packet
2644 			 */
2645 			if ((buffer_space_left == 0) ||
2646 			    (bytes_in_last_packet < pipe->max_packet)) {
2647 				pipe->next_tx_frame += pipe->interval;
2648 				cvmx_usb_complete(usb, pipe, transaction,
2649 						  CVMX_USB_STATUS_OK);
2650 			}
2651 		} else {
2652 			transaction->stage =
2653 				CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2654 		}
2655 	} else {
2656 		pipe->next_tx_frame += pipe->interval;
2657 		cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2658 	}
2659 }
2660 
2661 /**
2662  * Poll a channel for status
2663  *
2664  * @usb:     USB device
2665  * @channel: Channel to poll
2666  *
2667  * Returns: Zero on success
2668  */
2669 static int cvmx_usb_poll_channel(struct octeon_hcd *usb, int channel)
2670 {
2671 	struct usb_hcd *hcd = octeon_to_hcd(usb);
2672 	struct device *dev = hcd->self.controller;
2673 	union cvmx_usbcx_hcintx usbc_hcint;
2674 	union cvmx_usbcx_hctsizx usbc_hctsiz;
2675 	union cvmx_usbcx_hccharx usbc_hcchar;
2676 	struct cvmx_usb_pipe *pipe;
2677 	struct cvmx_usb_transaction *transaction;
2678 	int bytes_this_transfer;
2679 	int bytes_in_last_packet;
2680 	int packets_processed;
2681 	int buffer_space_left;
2682 
2683 	/* Read the interrupt status bits for the channel */
2684 	usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
2685 					     CVMX_USBCX_HCINTX(channel, usb->index));
2686 
2687 	if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2688 		usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2689 						      CVMX_USBCX_HCCHARX(channel,
2690 									 usb->index));
2691 
2692 		if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2693 			/*
2694 			 * There seems to be a bug in CN31XX which can cause
2695 			 * interrupt IN transfers to get stuck until we do a
2696 			 * write of HCCHARX without changing things
2697 			 */
2698 			cvmx_usb_write_csr32(usb,
2699 					     CVMX_USBCX_HCCHARX(channel,
2700 								usb->index),
2701 					     usbc_hcchar.u32);
2702 			return 0;
2703 		}
2704 
2705 		/*
2706 		 * In non DMA mode the channels don't halt themselves. We need
2707 		 * to manually disable channels that are left running
2708 		 */
2709 		if (!usbc_hcint.s.chhltd) {
2710 			if (usbc_hcchar.s.chena) {
2711 				union cvmx_usbcx_hcintmskx hcintmsk;
2712 				/* Disable all interrupts except CHHLTD */
2713 				hcintmsk.u32 = 0;
2714 				hcintmsk.s.chhltdmsk = 1;
2715 				cvmx_usb_write_csr32(usb,
2716 						     CVMX_USBCX_HCINTMSKX(channel, usb->index),
2717 						     hcintmsk.u32);
2718 				usbc_hcchar.s.chdis = 1;
2719 				cvmx_usb_write_csr32(usb,
2720 						     CVMX_USBCX_HCCHARX(channel, usb->index),
2721 						     usbc_hcchar.u32);
2722 				return 0;
2723 			} else if (usbc_hcint.s.xfercompl) {
2724 				/*
2725 				 * Successful IN/OUT with transfer complete.
2726 				 * Channel halt isn't needed.
2727 				 */
2728 			} else {
2729 				dev_err(dev, "USB%d: Channel %d interrupt without halt\n",
2730 					usb->index, channel);
2731 				return 0;
2732 			}
2733 		}
2734 	} else {
2735 		/*
2736 		 * There is are no interrupts that we need to process when the
2737 		 * channel is still running
2738 		 */
2739 		if (!usbc_hcint.s.chhltd)
2740 			return 0;
2741 	}
2742 
2743 	/* Disable the channel interrupts now that it is done */
2744 	cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
2745 	usb->idle_hardware_channels |= (1 << channel);
2746 
2747 	/* Make sure this channel is tied to a valid pipe */
2748 	pipe = usb->pipe_for_channel[channel];
2749 	prefetch(pipe);
2750 	if (!pipe)
2751 		return 0;
2752 	transaction = list_first_entry(&pipe->transactions,
2753 				       typeof(*transaction),
2754 				       node);
2755 	prefetch(transaction);
2756 
2757 	/*
2758 	 * Disconnect this pipe from the HW channel. Later the schedule
2759 	 * function will figure out which pipe needs to go
2760 	 */
2761 	usb->pipe_for_channel[channel] = NULL;
2762 	pipe->flags &= ~CVMX_USB_PIPE_FLAGS_SCHEDULED;
2763 
2764 	/*
2765 	 * Read the channel config info so we can figure out how much data
2766 	 * transferred
2767 	 */
2768 	usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2769 					      CVMX_USBCX_HCCHARX(channel, usb->index));
2770 	usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
2771 					      CVMX_USBCX_HCTSIZX(channel, usb->index));
2772 
2773 	/*
2774 	 * Calculating the number of bytes successfully transferred is dependent
2775 	 * on the transfer direction
2776 	 */
2777 	packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2778 	if (usbc_hcchar.s.epdir) {
2779 		/*
2780 		 * IN transactions are easy. For every byte received the
2781 		 * hardware decrements xfersize. All we need to do is subtract
2782 		 * the current value of xfersize from its starting value and we
2783 		 * know how many bytes were written to the buffer
2784 		 */
2785 		bytes_this_transfer = transaction->xfersize -
2786 			usbc_hctsiz.s.xfersize;
2787 	} else {
2788 		/*
2789 		 * OUT transaction don't decrement xfersize. Instead pktcnt is
2790 		 * decremented on every successful packet send. The hardware
2791 		 * does this when it receives an ACK, or NYET. If it doesn't
2792 		 * receive one of these responses pktcnt doesn't change
2793 		 */
2794 		bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2795 		/*
2796 		 * The last packet may not be a full transfer if we didn't have
2797 		 * enough data
2798 		 */
2799 		if (bytes_this_transfer > transaction->xfersize)
2800 			bytes_this_transfer = transaction->xfersize;
2801 	}
2802 	/* Figure out how many bytes were in the last packet of the transfer */
2803 	if (packets_processed)
2804 		bytes_in_last_packet = bytes_this_transfer -
2805 			(packets_processed - 1) * usbc_hcchar.s.mps;
2806 	else
2807 		bytes_in_last_packet = bytes_this_transfer;
2808 
2809 	/*
2810 	 * As a special case, setup transactions output the setup header, not
2811 	 * the user's data. For this reason we don't count setup data as bytes
2812 	 * transferred
2813 	 */
2814 	if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2815 	    (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2816 		bytes_this_transfer = 0;
2817 
2818 	/*
2819 	 * Add the bytes transferred to the running total. It is important that
2820 	 * bytes_this_transfer doesn't count any data that needs to be
2821 	 * retransmitted
2822 	 */
2823 	transaction->actual_bytes += bytes_this_transfer;
2824 	if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
2825 		buffer_space_left = transaction->iso_packets[0].length -
2826 			transaction->actual_bytes;
2827 	else
2828 		buffer_space_left = transaction->buffer_length -
2829 			transaction->actual_bytes;
2830 
2831 	/*
2832 	 * We need to remember the PID toggle state for the next transaction.
2833 	 * The hardware already updated it for the next transaction
2834 	 */
2835 	pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2836 
2837 	/*
2838 	 * For high speed bulk out, assume the next transaction will need to do
2839 	 * a ping before proceeding. If this isn't true the ACK processing below
2840 	 * will clear this flag
2841 	 */
2842 	if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2843 	    (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2844 	    (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2845 		pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
2846 
2847 	if (WARN_ON_ONCE(bytes_this_transfer < 0)) {
2848 		/*
2849 		 * In some rare cases the DMA engine seems to get stuck and
2850 		 * keeps substracting same byte count over and over again. In
2851 		 * such case we just need to fail every transaction.
2852 		 */
2853 		cvmx_usb_complete(usb, pipe, transaction,
2854 				  CVMX_USB_STATUS_ERROR);
2855 		return 0;
2856 	}
2857 
2858 	if (usbc_hcint.s.stall) {
2859 		/*
2860 		 * STALL as a response means this transaction cannot be
2861 		 * completed because the device can't process transactions. Tell
2862 		 * the user. Any data that was transferred will be counted on
2863 		 * the actual bytes transferred
2864 		 */
2865 		pipe->pid_toggle = 0;
2866 		cvmx_usb_complete(usb, pipe, transaction,
2867 				  CVMX_USB_STATUS_STALL);
2868 	} else if (usbc_hcint.s.xacterr) {
2869 		/*
2870 		 * XactErr as a response means the device signaled
2871 		 * something wrong with the transfer. For example, PID
2872 		 * toggle errors cause these.
2873 		 */
2874 		cvmx_usb_complete(usb, pipe, transaction,
2875 				  CVMX_USB_STATUS_XACTERR);
2876 	} else if (usbc_hcint.s.bblerr) {
2877 		/* Babble Error (BblErr) */
2878 		cvmx_usb_complete(usb, pipe, transaction,
2879 				  CVMX_USB_STATUS_BABBLEERR);
2880 	} else if (usbc_hcint.s.datatglerr) {
2881 		/* Data toggle error */
2882 		cvmx_usb_complete(usb, pipe, transaction,
2883 				  CVMX_USB_STATUS_DATATGLERR);
2884 	} else if (usbc_hcint.s.nyet) {
2885 		/*
2886 		 * NYET as a response is only allowed in three cases: as a
2887 		 * response to a ping, as a response to a split transaction, and
2888 		 * as a response to a bulk out. The ping case is handled by
2889 		 * hardware, so we only have splits and bulk out
2890 		 */
2891 		if (!cvmx_usb_pipe_needs_split(usb, pipe)) {
2892 			transaction->retries = 0;
2893 			/*
2894 			 * If there is more data to go then we need to try
2895 			 * again. Otherwise this transaction is complete
2896 			 */
2897 			if ((buffer_space_left == 0) ||
2898 			    (bytes_in_last_packet < pipe->max_packet))
2899 				cvmx_usb_complete(usb, pipe,
2900 						  transaction,
2901 						  CVMX_USB_STATUS_OK);
2902 		} else {
2903 			/*
2904 			 * Split transactions retry the split complete 4 times
2905 			 * then rewind to the start split and do the entire
2906 			 * transactions again
2907 			 */
2908 			transaction->retries++;
2909 			if ((transaction->retries & 0x3) == 0) {
2910 				/*
2911 				 * Rewind to the beginning of the transaction by
2912 				 * anding off the split complete bit
2913 				 */
2914 				transaction->stage &= ~1;
2915 				pipe->split_sc_frame = -1;
2916 			}
2917 		}
2918 	} else if (usbc_hcint.s.ack) {
2919 		transaction->retries = 0;
2920 		/*
2921 		 * The ACK bit can only be checked after the other error bits.
2922 		 * This is because a multi packet transfer may succeed in a
2923 		 * number of packets and then get a different response on the
2924 		 * last packet. In this case both ACK and the last response bit
2925 		 * will be set. If none of the other response bits is set, then
2926 		 * the last packet must have been an ACK
2927 		 *
2928 		 * Since we got an ACK, we know we don't need to do a ping on
2929 		 * this pipe
2930 		 */
2931 		pipe->flags &= ~CVMX_USB_PIPE_FLAGS_NEED_PING;
2932 
2933 		switch (transaction->type) {
2934 		case CVMX_USB_TRANSFER_CONTROL:
2935 			cvmx_usb_transfer_control(usb, pipe, transaction,
2936 						  usbc_hcchar,
2937 						  buffer_space_left,
2938 						  bytes_in_last_packet);
2939 			break;
2940 		case CVMX_USB_TRANSFER_BULK:
2941 			cvmx_usb_transfer_bulk(usb, pipe, transaction,
2942 					       usbc_hcint, buffer_space_left,
2943 					       bytes_in_last_packet);
2944 			break;
2945 		case CVMX_USB_TRANSFER_INTERRUPT:
2946 			cvmx_usb_transfer_intr(usb, pipe, transaction,
2947 					       buffer_space_left,
2948 					       bytes_in_last_packet);
2949 			break;
2950 		case CVMX_USB_TRANSFER_ISOCHRONOUS:
2951 			cvmx_usb_transfer_isoc(usb, pipe, transaction,
2952 					       buffer_space_left,
2953 					       bytes_in_last_packet,
2954 					       bytes_this_transfer);
2955 			break;
2956 		}
2957 	} else if (usbc_hcint.s.nak) {
2958 		/*
2959 		 * If this was a split then clear our split in progress marker.
2960 		 */
2961 		if (usb->active_split == transaction)
2962 			usb->active_split = NULL;
2963 		/*
2964 		 * NAK as a response means the device couldn't accept the
2965 		 * transaction, but it should be retried in the future. Rewind
2966 		 * to the beginning of the transaction by anding off the split
2967 		 * complete bit. Retry in the next interval
2968 		 */
2969 		transaction->retries = 0;
2970 		transaction->stage &= ~1;
2971 		pipe->next_tx_frame += pipe->interval;
2972 		if (pipe->next_tx_frame < usb->frame_number)
2973 			pipe->next_tx_frame = usb->frame_number +
2974 				pipe->interval -
2975 				(usb->frame_number - pipe->next_tx_frame) %
2976 				pipe->interval;
2977 	} else {
2978 		struct cvmx_usb_port_status port;
2979 
2980 		port = cvmx_usb_get_status(usb);
2981 		if (port.port_enabled) {
2982 			/* We'll retry the exact same transaction again */
2983 			transaction->retries++;
2984 		} else {
2985 			/*
2986 			 * We get channel halted interrupts with no result bits
2987 			 * sets when the cable is unplugged
2988 			 */
2989 			cvmx_usb_complete(usb, pipe, transaction,
2990 					  CVMX_USB_STATUS_ERROR);
2991 		}
2992 	}
2993 	return 0;
2994 }
2995 
2996 static void octeon_usb_port_callback(struct octeon_hcd *usb)
2997 {
2998 	spin_unlock(&usb->lock);
2999 	usb_hcd_poll_rh_status(octeon_to_hcd(usb));
3000 	spin_lock(&usb->lock);
3001 }
3002 
3003 /**
3004  * Poll the USB block for status and call all needed callback
3005  * handlers. This function is meant to be called in the interrupt
3006  * handler for the USB controller. It can also be called
3007  * periodically in a loop for non-interrupt based operation.
3008  *
3009  * @usb: USB device state populated by cvmx_usb_initialize().
3010  *
3011  * Returns: 0 or a negative error code.
3012  */
3013 static int cvmx_usb_poll(struct octeon_hcd *usb)
3014 {
3015 	union cvmx_usbcx_hfnum usbc_hfnum;
3016 	union cvmx_usbcx_gintsts usbc_gintsts;
3017 
3018 	prefetch_range(usb, sizeof(*usb));
3019 
3020 	/* Update the frame counter */
3021 	usbc_hfnum.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
3022 	if ((usb->frame_number & 0x3fff) > usbc_hfnum.s.frnum)
3023 		usb->frame_number += 0x4000;
3024 	usb->frame_number &= ~0x3fffull;
3025 	usb->frame_number |= usbc_hfnum.s.frnum;
3026 
3027 	/* Read the pending interrupts */
3028 	usbc_gintsts.u32 = cvmx_usb_read_csr32(usb,
3029 					       CVMX_USBCX_GINTSTS(usb->index));
3030 
3031 	/* Clear the interrupts now that we know about them */
3032 	cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index),
3033 			     usbc_gintsts.u32);
3034 
3035 	if (usbc_gintsts.s.rxflvl) {
3036 		/*
3037 		 * RxFIFO Non-Empty (RxFLvl)
3038 		 * Indicates that there is at least one packet pending to be
3039 		 * read from the RxFIFO.
3040 		 *
3041 		 * In DMA mode this is handled by hardware
3042 		 */
3043 		if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3044 			cvmx_usb_poll_rx_fifo(usb);
3045 	}
3046 	if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
3047 		/* Fill the Tx FIFOs when not in DMA mode */
3048 		if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3049 			cvmx_usb_poll_tx_fifo(usb);
3050 	}
3051 	if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
3052 		union cvmx_usbcx_hprt usbc_hprt;
3053 		/*
3054 		 * Disconnect Detected Interrupt (DisconnInt)
3055 		 * Asserted when a device disconnect is detected.
3056 		 *
3057 		 * Host Port Interrupt (PrtInt)
3058 		 * The core sets this bit to indicate a change in port status of
3059 		 * one of the O2P USB core ports in Host mode. The application
3060 		 * must read the Host Port Control and Status (HPRT) register to
3061 		 * determine the exact event that caused this interrupt. The
3062 		 * application must clear the appropriate status bit in the Host
3063 		 * Port Control and Status register to clear this bit.
3064 		 *
3065 		 * Call the user's port callback
3066 		 */
3067 		octeon_usb_port_callback(usb);
3068 		/* Clear the port change bits */
3069 		usbc_hprt.u32 =
3070 			cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
3071 		usbc_hprt.s.prtena = 0;
3072 		cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index),
3073 				     usbc_hprt.u32);
3074 	}
3075 	if (usbc_gintsts.s.hchint) {
3076 		/*
3077 		 * Host Channels Interrupt (HChInt)
3078 		 * The core sets this bit to indicate that an interrupt is
3079 		 * pending on one of the channels of the core (in Host mode).
3080 		 * The application must read the Host All Channels Interrupt
3081 		 * (HAINT) register to determine the exact number of the channel
3082 		 * on which the interrupt occurred, and then read the
3083 		 * corresponding Host Channel-n Interrupt (HCINTn) register to
3084 		 * determine the exact cause of the interrupt. The application
3085 		 * must clear the appropriate status bit in the HCINTn register
3086 		 * to clear this bit.
3087 		 */
3088 		union cvmx_usbcx_haint usbc_haint;
3089 
3090 		usbc_haint.u32 = cvmx_usb_read_csr32(usb,
3091 						     CVMX_USBCX_HAINT(usb->index));
3092 		while (usbc_haint.u32) {
3093 			int channel;
3094 
3095 			channel = __fls(usbc_haint.u32);
3096 			cvmx_usb_poll_channel(usb, channel);
3097 			usbc_haint.u32 ^= 1 << channel;
3098 		}
3099 	}
3100 
3101 	cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
3102 
3103 	return 0;
3104 }
3105 
3106 /* convert between an HCD pointer and the corresponding struct octeon_hcd */
3107 static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
3108 {
3109 	return (struct octeon_hcd *)(hcd->hcd_priv);
3110 }
3111 
3112 static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
3113 {
3114 	struct octeon_hcd *usb = hcd_to_octeon(hcd);
3115 	unsigned long flags;
3116 
3117 	spin_lock_irqsave(&usb->lock, flags);
3118 	cvmx_usb_poll(usb);
3119 	spin_unlock_irqrestore(&usb->lock, flags);
3120 	return IRQ_HANDLED;
3121 }
3122 
3123 static int octeon_usb_start(struct usb_hcd *hcd)
3124 {
3125 	hcd->state = HC_STATE_RUNNING;
3126 	return 0;
3127 }
3128 
3129 static void octeon_usb_stop(struct usb_hcd *hcd)
3130 {
3131 	hcd->state = HC_STATE_HALT;
3132 }
3133 
3134 static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
3135 {
3136 	struct octeon_hcd *usb = hcd_to_octeon(hcd);
3137 
3138 	return cvmx_usb_get_frame_number(usb);
3139 }
3140 
3141 static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
3142 				  struct urb *urb,
3143 				  gfp_t mem_flags)
3144 {
3145 	struct octeon_hcd *usb = hcd_to_octeon(hcd);
3146 	struct device *dev = hcd->self.controller;
3147 	struct cvmx_usb_transaction *transaction = NULL;
3148 	struct cvmx_usb_pipe *pipe;
3149 	unsigned long flags;
3150 	struct cvmx_usb_iso_packet *iso_packet;
3151 	struct usb_host_endpoint *ep = urb->ep;
3152 	int rc;
3153 
3154 	urb->status = 0;
3155 	spin_lock_irqsave(&usb->lock, flags);
3156 
3157 	rc = usb_hcd_link_urb_to_ep(hcd, urb);
3158 	if (rc) {
3159 		spin_unlock_irqrestore(&usb->lock, flags);
3160 		return rc;
3161 	}
3162 
3163 	if (!ep->hcpriv) {
3164 		enum cvmx_usb_transfer transfer_type;
3165 		enum cvmx_usb_speed speed;
3166 		int split_device = 0;
3167 		int split_port = 0;
3168 
3169 		switch (usb_pipetype(urb->pipe)) {
3170 		case PIPE_ISOCHRONOUS:
3171 			transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
3172 			break;
3173 		case PIPE_INTERRUPT:
3174 			transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
3175 			break;
3176 		case PIPE_CONTROL:
3177 			transfer_type = CVMX_USB_TRANSFER_CONTROL;
3178 			break;
3179 		default:
3180 			transfer_type = CVMX_USB_TRANSFER_BULK;
3181 			break;
3182 		}
3183 		switch (urb->dev->speed) {
3184 		case USB_SPEED_LOW:
3185 			speed = CVMX_USB_SPEED_LOW;
3186 			break;
3187 		case USB_SPEED_FULL:
3188 			speed = CVMX_USB_SPEED_FULL;
3189 			break;
3190 		default:
3191 			speed = CVMX_USB_SPEED_HIGH;
3192 			break;
3193 		}
3194 		/*
3195 		 * For slow devices on high speed ports we need to find the hub
3196 		 * that does the speed translation so we know where to send the
3197 		 * split transactions.
3198 		 */
3199 		if (speed != CVMX_USB_SPEED_HIGH) {
3200 			/*
3201 			 * Start at this device and work our way up the usb
3202 			 * tree.
3203 			 */
3204 			struct usb_device *dev = urb->dev;
3205 
3206 			while (dev->parent) {
3207 				/*
3208 				 * If our parent is high speed then he'll
3209 				 * receive the splits.
3210 				 */
3211 				if (dev->parent->speed == USB_SPEED_HIGH) {
3212 					split_device = dev->parent->devnum;
3213 					split_port = dev->portnum;
3214 					break;
3215 				}
3216 				/*
3217 				 * Move up the tree one level. If we make it all
3218 				 * the way up the tree, then the port must not
3219 				 * be in high speed mode and we don't need a
3220 				 * split.
3221 				 */
3222 				dev = dev->parent;
3223 			}
3224 		}
3225 		pipe = cvmx_usb_open_pipe(usb, usb_pipedevice(urb->pipe),
3226 					  usb_pipeendpoint(urb->pipe), speed,
3227 					  le16_to_cpu(ep->desc.wMaxPacketSize)
3228 					  & 0x7ff,
3229 					  transfer_type,
3230 					  usb_pipein(urb->pipe) ?
3231 						CVMX_USB_DIRECTION_IN :
3232 						CVMX_USB_DIRECTION_OUT,
3233 					  urb->interval,
3234 					  (le16_to_cpu(ep->desc.wMaxPacketSize)
3235 					   >> 11) & 0x3,
3236 					  split_device, split_port);
3237 		if (!pipe) {
3238 			usb_hcd_unlink_urb_from_ep(hcd, urb);
3239 			spin_unlock_irqrestore(&usb->lock, flags);
3240 			dev_dbg(dev, "Failed to create pipe\n");
3241 			return -ENOMEM;
3242 		}
3243 		ep->hcpriv = pipe;
3244 	} else {
3245 		pipe = ep->hcpriv;
3246 	}
3247 
3248 	switch (usb_pipetype(urb->pipe)) {
3249 	case PIPE_ISOCHRONOUS:
3250 		dev_dbg(dev, "Submit isochronous to %d.%d\n",
3251 			usb_pipedevice(urb->pipe),
3252 			usb_pipeendpoint(urb->pipe));
3253 		/*
3254 		 * Allocate a structure to use for our private list of
3255 		 * isochronous packets.
3256 		 */
3257 		iso_packet = kmalloc_objs(struct cvmx_usb_iso_packet,
3258 					  urb->number_of_packets, GFP_ATOMIC);
3259 		if (iso_packet) {
3260 			int i;
3261 			/* Fill the list with the data from the URB */
3262 			for (i = 0; i < urb->number_of_packets; i++) {
3263 				iso_packet[i].offset =
3264 					urb->iso_frame_desc[i].offset;
3265 				iso_packet[i].length =
3266 					urb->iso_frame_desc[i].length;
3267 				iso_packet[i].status = CVMX_USB_STATUS_ERROR;
3268 			}
3269 			/*
3270 			 * Store a pointer to the list in the URB setup_packet
3271 			 * field. We know this currently isn't being used and
3272 			 * this saves us a bunch of logic.
3273 			 */
3274 			urb->setup_packet = (char *)iso_packet;
3275 			transaction = cvmx_usb_submit_isochronous(usb,
3276 								  pipe, urb);
3277 			/*
3278 			 * If submit failed we need to free our private packet
3279 			 * list.
3280 			 */
3281 			if (!transaction) {
3282 				urb->setup_packet = NULL;
3283 				kfree(iso_packet);
3284 			}
3285 		}
3286 		break;
3287 	case PIPE_INTERRUPT:
3288 		dev_dbg(dev, "Submit interrupt to %d.%d\n",
3289 			usb_pipedevice(urb->pipe),
3290 			usb_pipeendpoint(urb->pipe));
3291 		transaction = cvmx_usb_submit_interrupt(usb, pipe, urb);
3292 		break;
3293 	case PIPE_CONTROL:
3294 		dev_dbg(dev, "Submit control to %d.%d\n",
3295 			usb_pipedevice(urb->pipe),
3296 			usb_pipeendpoint(urb->pipe));
3297 		transaction = cvmx_usb_submit_control(usb, pipe, urb);
3298 		break;
3299 	case PIPE_BULK:
3300 		dev_dbg(dev, "Submit bulk to %d.%d\n",
3301 			usb_pipedevice(urb->pipe),
3302 			usb_pipeendpoint(urb->pipe));
3303 		transaction = cvmx_usb_submit_bulk(usb, pipe, urb);
3304 		break;
3305 	}
3306 	if (!transaction) {
3307 		usb_hcd_unlink_urb_from_ep(hcd, urb);
3308 		spin_unlock_irqrestore(&usb->lock, flags);
3309 		dev_dbg(dev, "Failed to submit\n");
3310 		return -ENOMEM;
3311 	}
3312 	urb->hcpriv = transaction;
3313 	spin_unlock_irqrestore(&usb->lock, flags);
3314 	return 0;
3315 }
3316 
3317 static int octeon_usb_urb_dequeue(struct usb_hcd *hcd,
3318 				  struct urb *urb,
3319 				  int status)
3320 {
3321 	struct octeon_hcd *usb = hcd_to_octeon(hcd);
3322 	unsigned long flags;
3323 	int rc;
3324 
3325 	if (!urb->dev)
3326 		return -EINVAL;
3327 
3328 	spin_lock_irqsave(&usb->lock, flags);
3329 
3330 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
3331 	if (rc)
3332 		goto out;
3333 
3334 	urb->status = status;
3335 	cvmx_usb_cancel(usb, urb->ep->hcpriv, urb->hcpriv);
3336 
3337 out:
3338 	spin_unlock_irqrestore(&usb->lock, flags);
3339 
3340 	return rc;
3341 }
3342 
3343 static void octeon_usb_endpoint_disable(struct usb_hcd *hcd,
3344 					struct usb_host_endpoint *ep)
3345 {
3346 	struct device *dev = hcd->self.controller;
3347 
3348 	if (ep->hcpriv) {
3349 		struct octeon_hcd *usb = hcd_to_octeon(hcd);
3350 		struct cvmx_usb_pipe *pipe = ep->hcpriv;
3351 		unsigned long flags;
3352 
3353 		spin_lock_irqsave(&usb->lock, flags);
3354 		cvmx_usb_cancel_all(usb, pipe);
3355 		if (cvmx_usb_close_pipe(usb, pipe))
3356 			dev_dbg(dev, "Closing pipe %p failed\n", pipe);
3357 		spin_unlock_irqrestore(&usb->lock, flags);
3358 		ep->hcpriv = NULL;
3359 	}
3360 }
3361 
3362 static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
3363 {
3364 	struct octeon_hcd *usb = hcd_to_octeon(hcd);
3365 	struct cvmx_usb_port_status port_status;
3366 	unsigned long flags;
3367 
3368 	spin_lock_irqsave(&usb->lock, flags);
3369 	port_status = cvmx_usb_get_status(usb);
3370 	spin_unlock_irqrestore(&usb->lock, flags);
3371 	buf[0] = port_status.connect_change << 1;
3372 
3373 	return buf[0] != 0;
3374 }
3375 
3376 static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3377 				  u16 wIndex, char *buf, u16 wLength)
3378 {
3379 	struct octeon_hcd *usb = hcd_to_octeon(hcd);
3380 	struct device *dev = hcd->self.controller;
3381 	struct cvmx_usb_port_status usb_port_status;
3382 	int port_status;
3383 	struct usb_hub_descriptor *desc;
3384 	unsigned long flags;
3385 
3386 	switch (typeReq) {
3387 	case ClearHubFeature:
3388 		dev_dbg(dev, "ClearHubFeature\n");
3389 		switch (wValue) {
3390 		case C_HUB_LOCAL_POWER:
3391 		case C_HUB_OVER_CURRENT:
3392 			/* Nothing required here */
3393 			break;
3394 		default:
3395 			return -EINVAL;
3396 		}
3397 		break;
3398 	case ClearPortFeature:
3399 		dev_dbg(dev, "ClearPortFeature\n");
3400 		if (wIndex != 1) {
3401 			dev_dbg(dev, " INVALID\n");
3402 			return -EINVAL;
3403 		}
3404 
3405 		switch (wValue) {
3406 		case USB_PORT_FEAT_ENABLE:
3407 			dev_dbg(dev, " ENABLE\n");
3408 			spin_lock_irqsave(&usb->lock, flags);
3409 			cvmx_usb_disable(usb);
3410 			spin_unlock_irqrestore(&usb->lock, flags);
3411 			break;
3412 		case USB_PORT_FEAT_SUSPEND:
3413 			dev_dbg(dev, " SUSPEND\n");
3414 			/* Not supported on Octeon */
3415 			break;
3416 		case USB_PORT_FEAT_POWER:
3417 			dev_dbg(dev, " POWER\n");
3418 			/* Not supported on Octeon */
3419 			break;
3420 		case USB_PORT_FEAT_INDICATOR:
3421 			dev_dbg(dev, " INDICATOR\n");
3422 			/* Port indicator not supported */
3423 			break;
3424 		case USB_PORT_FEAT_C_CONNECTION:
3425 			dev_dbg(dev, " C_CONNECTION\n");
3426 			/* Clears drivers internal connect status change flag */
3427 			spin_lock_irqsave(&usb->lock, flags);
3428 			usb->port_status = cvmx_usb_get_status(usb);
3429 			spin_unlock_irqrestore(&usb->lock, flags);
3430 			break;
3431 		case USB_PORT_FEAT_C_RESET:
3432 			dev_dbg(dev, " C_RESET\n");
3433 			/*
3434 			 * Clears the driver's internal Port Reset Change flag.
3435 			 */
3436 			spin_lock_irqsave(&usb->lock, flags);
3437 			usb->port_status = cvmx_usb_get_status(usb);
3438 			spin_unlock_irqrestore(&usb->lock, flags);
3439 			break;
3440 		case USB_PORT_FEAT_C_ENABLE:
3441 			dev_dbg(dev, " C_ENABLE\n");
3442 			/*
3443 			 * Clears the driver's internal Port Enable/Disable
3444 			 * Change flag.
3445 			 */
3446 			spin_lock_irqsave(&usb->lock, flags);
3447 			usb->port_status = cvmx_usb_get_status(usb);
3448 			spin_unlock_irqrestore(&usb->lock, flags);
3449 			break;
3450 		case USB_PORT_FEAT_C_SUSPEND:
3451 			dev_dbg(dev, " C_SUSPEND\n");
3452 			/*
3453 			 * Clears the driver's internal Port Suspend Change
3454 			 * flag, which is set when resume signaling on the host
3455 			 * port is complete.
3456 			 */
3457 			break;
3458 		case USB_PORT_FEAT_C_OVER_CURRENT:
3459 			dev_dbg(dev, " C_OVER_CURRENT\n");
3460 			/* Clears the driver's overcurrent Change flag */
3461 			spin_lock_irqsave(&usb->lock, flags);
3462 			usb->port_status = cvmx_usb_get_status(usb);
3463 			spin_unlock_irqrestore(&usb->lock, flags);
3464 			break;
3465 		default:
3466 			dev_dbg(dev, " UNKNOWN\n");
3467 			return -EINVAL;
3468 		}
3469 		break;
3470 	case GetHubDescriptor:
3471 		dev_dbg(dev, "GetHubDescriptor\n");
3472 		desc = (struct usb_hub_descriptor *)buf;
3473 		desc->bDescLength = 9;
3474 		desc->bDescriptorType = 0x29;
3475 		desc->bNbrPorts = 1;
3476 		desc->wHubCharacteristics = cpu_to_le16(0x08);
3477 		desc->bPwrOn2PwrGood = 1;
3478 		desc->bHubContrCurrent = 0;
3479 		desc->u.hs.DeviceRemovable[0] = 0;
3480 		desc->u.hs.DeviceRemovable[1] = 0xff;
3481 		break;
3482 	case GetHubStatus:
3483 		dev_dbg(dev, "GetHubStatus\n");
3484 		*(__le32 *)buf = 0;
3485 		break;
3486 	case GetPortStatus:
3487 		dev_dbg(dev, "GetPortStatus\n");
3488 		if (wIndex != 1) {
3489 			dev_dbg(dev, " INVALID\n");
3490 			return -EINVAL;
3491 		}
3492 
3493 		spin_lock_irqsave(&usb->lock, flags);
3494 		usb_port_status = cvmx_usb_get_status(usb);
3495 		spin_unlock_irqrestore(&usb->lock, flags);
3496 		port_status = 0;
3497 
3498 		if (usb_port_status.connect_change) {
3499 			port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
3500 			dev_dbg(dev, " C_CONNECTION\n");
3501 		}
3502 
3503 		if (usb_port_status.port_enabled) {
3504 			port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
3505 			dev_dbg(dev, " C_ENABLE\n");
3506 		}
3507 
3508 		if (usb_port_status.connected) {
3509 			port_status |= (1 << USB_PORT_FEAT_CONNECTION);
3510 			dev_dbg(dev, " CONNECTION\n");
3511 		}
3512 
3513 		if (usb_port_status.port_enabled) {
3514 			port_status |= (1 << USB_PORT_FEAT_ENABLE);
3515 			dev_dbg(dev, " ENABLE\n");
3516 		}
3517 
3518 		if (usb_port_status.port_over_current) {
3519 			port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
3520 			dev_dbg(dev, " OVER_CURRENT\n");
3521 		}
3522 
3523 		if (usb_port_status.port_powered) {
3524 			port_status |= (1 << USB_PORT_FEAT_POWER);
3525 			dev_dbg(dev, " POWER\n");
3526 		}
3527 
3528 		if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
3529 			port_status |= USB_PORT_STAT_HIGH_SPEED;
3530 			dev_dbg(dev, " HIGHSPEED\n");
3531 		} else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
3532 			port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
3533 			dev_dbg(dev, " LOWSPEED\n");
3534 		}
3535 
3536 		*((__le32 *)buf) = cpu_to_le32(port_status);
3537 		break;
3538 	case SetHubFeature:
3539 		dev_dbg(dev, "SetHubFeature\n");
3540 		/* No HUB features supported */
3541 		break;
3542 	case SetPortFeature:
3543 		dev_dbg(dev, "SetPortFeature\n");
3544 		if (wIndex != 1) {
3545 			dev_dbg(dev, " INVALID\n");
3546 			return -EINVAL;
3547 		}
3548 
3549 		switch (wValue) {
3550 		case USB_PORT_FEAT_SUSPEND:
3551 			dev_dbg(dev, " SUSPEND\n");
3552 			return -EINVAL;
3553 		case USB_PORT_FEAT_POWER:
3554 			dev_dbg(dev, " POWER\n");
3555 			/*
3556 			 * Program the port power bit to drive VBUS on the USB.
3557 			 */
3558 			spin_lock_irqsave(&usb->lock, flags);
3559 			USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index),
3560 					cvmx_usbcx_hprt, prtpwr, 1);
3561 			spin_unlock_irqrestore(&usb->lock, flags);
3562 			return 0;
3563 		case USB_PORT_FEAT_RESET:
3564 			dev_dbg(dev, " RESET\n");
3565 			spin_lock_irqsave(&usb->lock, flags);
3566 			cvmx_usb_reset_port(usb);
3567 			spin_unlock_irqrestore(&usb->lock, flags);
3568 			return 0;
3569 		case USB_PORT_FEAT_INDICATOR:
3570 			dev_dbg(dev, " INDICATOR\n");
3571 			/* Not supported */
3572 			break;
3573 		default:
3574 			dev_dbg(dev, " UNKNOWN\n");
3575 			return -EINVAL;
3576 		}
3577 		break;
3578 	default:
3579 		dev_dbg(dev, "Unknown root hub request\n");
3580 		return -EINVAL;
3581 	}
3582 	return 0;
3583 }
3584 
3585 static const struct hc_driver octeon_hc_driver = {
3586 	.description		= "Octeon USB",
3587 	.product_desc		= "Octeon Host Controller",
3588 	.hcd_priv_size		= sizeof(struct octeon_hcd),
3589 	.irq			= octeon_usb_irq,
3590 	.flags			= HCD_MEMORY | HCD_DMA | HCD_USB2,
3591 	.start			= octeon_usb_start,
3592 	.stop			= octeon_usb_stop,
3593 	.urb_enqueue		= octeon_usb_urb_enqueue,
3594 	.urb_dequeue		= octeon_usb_urb_dequeue,
3595 	.endpoint_disable	= octeon_usb_endpoint_disable,
3596 	.get_frame_number	= octeon_usb_get_frame_number,
3597 	.hub_status_data	= octeon_usb_hub_status_data,
3598 	.hub_control		= octeon_usb_hub_control,
3599 	.map_urb_for_dma	= octeon_map_urb_for_dma,
3600 	.unmap_urb_for_dma	= octeon_unmap_urb_for_dma,
3601 };
3602 
3603 static int octeon_usb_probe(struct platform_device *pdev)
3604 {
3605 	int status;
3606 	int initialize_flags;
3607 	int usb_num;
3608 	struct resource *res_mem;
3609 	struct device_node *usbn_node;
3610 	int irq = platform_get_irq(pdev, 0);
3611 	struct device *dev = &pdev->dev;
3612 	struct octeon_hcd *usb;
3613 	struct usb_hcd *hcd;
3614 	u32 clock_rate = 48000000;
3615 	bool is_crystal_clock = false;
3616 	const char *clock_type;
3617 	int i;
3618 
3619 	if (!dev->of_node) {
3620 		dev_err(dev, "Error: empty of_node\n");
3621 		return -ENXIO;
3622 	}
3623 	usbn_node = dev->of_node->parent;
3624 
3625 	i = of_property_read_u32(usbn_node,
3626 				 "clock-frequency", &clock_rate);
3627 	if (i)
3628 		i = of_property_read_u32(usbn_node,
3629 					 "refclk-frequency", &clock_rate);
3630 	if (i) {
3631 		dev_err(dev, "No USBN \"clock-frequency\"\n");
3632 		return -ENXIO;
3633 	}
3634 	switch (clock_rate) {
3635 	case 12000000:
3636 		initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
3637 		break;
3638 	case 24000000:
3639 		initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
3640 		break;
3641 	case 48000000:
3642 		initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
3643 		break;
3644 	default:
3645 		dev_err(dev, "Illegal USBN \"clock-frequency\" %u\n",
3646 			clock_rate);
3647 		return -ENXIO;
3648 	}
3649 
3650 	i = of_property_read_string(usbn_node,
3651 				    "cavium,refclk-type", &clock_type);
3652 	if (i)
3653 		i = of_property_read_string(usbn_node,
3654 					    "refclk-type", &clock_type);
3655 
3656 	if (!i && strcmp("crystal", clock_type) == 0)
3657 		is_crystal_clock = true;
3658 
3659 	if (is_crystal_clock)
3660 		initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
3661 	else
3662 		initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
3663 
3664 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3665 	if (!res_mem) {
3666 		dev_err(dev, "found no memory resource\n");
3667 		return -ENXIO;
3668 	}
3669 	usb_num = (res_mem->start >> 44) & 1;
3670 
3671 	if (irq < 0) {
3672 		/* Defective device tree, but we know how to fix it. */
3673 		irq_hw_number_t hwirq = usb_num ? (1 << 6) + 17 : 56;
3674 
3675 		irq = irq_create_mapping(NULL, hwirq);
3676 	}
3677 
3678 	/*
3679 	 * Set the DMA mask to 64bits so we get buffers already translated for
3680 	 * DMA.
3681 	 */
3682 	i = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
3683 	if (i)
3684 		return i;
3685 
3686 	/*
3687 	 * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
3688 	 * IOB priority registers.  Under heavy network load USB
3689 	 * hardware can be starved by the IOB causing a crash.  Give
3690 	 * it a priority boost if it has been waiting more than 400
3691 	 * cycles to avoid this situation.
3692 	 *
3693 	 * Testing indicates that a cnt_val of 8192 is not sufficient,
3694 	 * but no failures are seen with 4096.  We choose a value of
3695 	 * 400 to give a safety factor of 10.
3696 	 */
3697 	if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
3698 		union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
3699 
3700 		pri_cnt.u64 = 0;
3701 		pri_cnt.s.cnt_enb = 1;
3702 		pri_cnt.s.cnt_val = 400;
3703 		cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
3704 	}
3705 
3706 	hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
3707 	if (!hcd) {
3708 		dev_dbg(dev, "Failed to allocate memory for HCD\n");
3709 		return -1;
3710 	}
3711 	hcd->uses_new_polling = 1;
3712 	usb = (struct octeon_hcd *)hcd->hcd_priv;
3713 
3714 	spin_lock_init(&usb->lock);
3715 	hrtimer_setup(&usb->sof_timer, octeon_usb_sof_timer, CLOCK_MONOTONIC,
3716 		      HRTIMER_MODE_REL);
3717 
3718 	usb->init_flags = initialize_flags;
3719 
3720 	/* Initialize the USB state structure */
3721 	usb->index = usb_num;
3722 	INIT_LIST_HEAD(&usb->idle_pipes);
3723 	for (i = 0; i < ARRAY_SIZE(usb->active_pipes); i++)
3724 		INIT_LIST_HEAD(&usb->active_pipes[i]);
3725 
3726 	/* Due to an errata, CN31XX doesn't support DMA */
3727 	if (OCTEON_IS_MODEL(OCTEON_CN31XX)) {
3728 		usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
3729 		/* Only use one channel with non DMA */
3730 		usb->idle_hardware_channels = 0x1;
3731 	} else if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) {
3732 		/* CN5XXX have an errata with channel 3 */
3733 		usb->idle_hardware_channels = 0xf7;
3734 	} else {
3735 		usb->idle_hardware_channels = 0xff;
3736 	}
3737 
3738 	status = cvmx_usb_initialize(dev, usb);
3739 	if (status) {
3740 		dev_dbg(dev, "USB initialization failed with %d\n", status);
3741 		usb_put_hcd(hcd);
3742 		return -1;
3743 	}
3744 
3745 	status = usb_add_hcd(hcd, irq, 0);
3746 	if (status) {
3747 		dev_dbg(dev, "USB add HCD failed with %d\n", status);
3748 		usb_put_hcd(hcd);
3749 		return -1;
3750 	}
3751 	device_wakeup_enable(hcd->self.controller);
3752 
3753 	dev_info(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
3754 
3755 	return 0;
3756 }
3757 
3758 static void octeon_usb_remove(struct platform_device *pdev)
3759 {
3760 	int status;
3761 	struct device *dev = &pdev->dev;
3762 	struct usb_hcd *hcd = dev_get_drvdata(dev);
3763 	struct octeon_hcd *usb = hcd_to_octeon(hcd);
3764 	unsigned long flags;
3765 
3766 	usb_remove_hcd(hcd);
3767 	hrtimer_cancel(&usb->sof_timer);
3768 	spin_lock_irqsave(&usb->lock, flags);
3769 	status = cvmx_usb_shutdown(usb);
3770 	spin_unlock_irqrestore(&usb->lock, flags);
3771 	if (status)
3772 		dev_dbg(dev, "USB shutdown failed with %d\n", status);
3773 
3774 	usb_put_hcd(hcd);
3775 }
3776 
3777 static const struct of_device_id octeon_usb_match[] = {
3778 	{
3779 		.compatible = "cavium,octeon-5750-usbc",
3780 	},
3781 	{},
3782 };
3783 MODULE_DEVICE_TABLE(of, octeon_usb_match);
3784 
3785 static struct platform_driver octeon_usb_driver = {
3786 	.driver = {
3787 		.name		= "octeon-hcd",
3788 		.of_match_table = octeon_usb_match,
3789 	},
3790 	.probe = octeon_usb_probe,
3791 	.remove = octeon_usb_remove,
3792 };
3793 
3794 static int __init octeon_usb_driver_init(void)
3795 {
3796 	if (usb_disabled())
3797 		return 0;
3798 
3799 	return platform_driver_register(&octeon_usb_driver);
3800 }
3801 module_init(octeon_usb_driver_init);
3802 
3803 static void __exit octeon_usb_driver_exit(void)
3804 {
3805 	if (usb_disabled())
3806 		return;
3807 
3808 	platform_driver_unregister(&octeon_usb_driver);
3809 }
3810 module_exit(octeon_usb_driver_exit);
3811 
3812 MODULE_LICENSE("GPL");
3813 MODULE_AUTHOR("Cavium, Inc. <support@cavium.com>");
3814 MODULE_DESCRIPTION("Cavium Inc. OCTEON USB Host driver.");
3815