blob: 5f9db4cbb38170064a3f269d87a1975f67a70b8f [file] [log] [blame]
Aaro Koskinenb1649352013-06-01 21:42:58 +03001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2008 Cavium Networks
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03007 *
8 * Some parts of the code were originally released under BSD license:
9 *
10 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
24 *
25 * * Neither the name of Cavium Networks nor the names of
26 * its contributors may be used to endorse or promote products
27 * derived from this software without specific prior written
28 * permission.
29 *
30 * This Software, including technical data, may be subject to U.S. export
31 * control laws, including the U.S. Export Administration Act and its associated
32 * regulations, and may be subject to export or import regulations in other
33 * countries.
34 *
35 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
36 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
37 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
38 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION
39 * OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
40 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
41 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
42 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
43 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
44 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
Aaro Koskinenb1649352013-06-01 21:42:58 +030045 */
46#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/init.h>
49#include <linux/pci.h>
Aaro Koskinen20f6b822014-05-11 14:15:20 +030050#include <linux/prefetch.h>
Aaro Koskinenb1649352013-06-01 21:42:58 +030051#include <linux/interrupt.h>
52#include <linux/platform_device.h>
Aaro Koskinenb1649352013-06-01 21:42:58 +030053#include <linux/usb.h>
54
Aaro Koskinen2a81d2b2013-06-13 01:00:35 +030055#include <linux/time.h>
56#include <linux/delay.h>
Aaro Koskinenb1649352013-06-01 21:42:58 +030057
58#include <asm/octeon/cvmx.h>
Aaro Koskinenb1649352013-06-01 21:42:58 +030059#include <asm/octeon/cvmx-iob-defs.h>
60
61#include <linux/usb/hcd.h>
62
Devendra Naga55fa3282013-06-04 02:46:20 +053063#include <linux/err.h>
64
Aaro Koskinen6570b4a2013-10-06 22:22:24 +030065#include <asm/octeon/octeon.h>
66#include <asm/octeon/cvmx-helper.h>
67#include <asm/octeon/cvmx-sysinfo.h>
68#include <asm/octeon/cvmx-helper-board.h>
69
Aaro Koskinenf1219102013-10-10 23:25:30 +030070#include "octeon-hcd.h"
Aaro Koskinen6570b4a2013-10-06 22:22:24 +030071
72/**
73 * enum cvmx_usb_speed - the possible USB device speeds
74 *
75 * @CVMX_USB_SPEED_HIGH: Device is operation at 480Mbps
76 * @CVMX_USB_SPEED_FULL: Device is operation at 12Mbps
77 * @CVMX_USB_SPEED_LOW: Device is operation at 1.5Mbps
78 */
79enum cvmx_usb_speed {
80 CVMX_USB_SPEED_HIGH = 0,
81 CVMX_USB_SPEED_FULL = 1,
82 CVMX_USB_SPEED_LOW = 2,
83};
84
85/**
86 * enum cvmx_usb_transfer - the possible USB transfer types
87 *
88 * @CVMX_USB_TRANSFER_CONTROL: USB transfer type control for hub and status
89 * transfers
90 * @CVMX_USB_TRANSFER_ISOCHRONOUS: USB transfer type isochronous for low
91 * priority periodic transfers
92 * @CVMX_USB_TRANSFER_BULK: USB transfer type bulk for large low priority
93 * transfers
94 * @CVMX_USB_TRANSFER_INTERRUPT: USB transfer type interrupt for high priority
95 * periodic transfers
96 */
97enum cvmx_usb_transfer {
98 CVMX_USB_TRANSFER_CONTROL = 0,
99 CVMX_USB_TRANSFER_ISOCHRONOUS = 1,
100 CVMX_USB_TRANSFER_BULK = 2,
101 CVMX_USB_TRANSFER_INTERRUPT = 3,
102};
103
104/**
105 * enum cvmx_usb_direction - the transfer directions
106 *
107 * @CVMX_USB_DIRECTION_OUT: Data is transferring from Octeon to the device/host
108 * @CVMX_USB_DIRECTION_IN: Data is transferring from the device/host to Octeon
109 */
110enum cvmx_usb_direction {
111 CVMX_USB_DIRECTION_OUT,
112 CVMX_USB_DIRECTION_IN,
113};
114
115/**
116 * enum cvmx_usb_complete - possible callback function status codes
117 *
118 * @CVMX_USB_COMPLETE_SUCCESS: The transaction / operation finished without
119 * any errors
120 * @CVMX_USB_COMPLETE_SHORT: FIXME: This is currently not implemented
121 * @CVMX_USB_COMPLETE_CANCEL: The transaction was canceled while in flight
122 * by a user call to cvmx_usb_cancel
123 * @CVMX_USB_COMPLETE_ERROR: The transaction aborted with an unexpected
124 * error status
125 * @CVMX_USB_COMPLETE_STALL: The transaction received a USB STALL response
126 * from the device
127 * @CVMX_USB_COMPLETE_XACTERR: The transaction failed with an error from the
128 * device even after a number of retries
129 * @CVMX_USB_COMPLETE_DATATGLERR: The transaction failed with a data toggle
130 * error even after a number of retries
131 * @CVMX_USB_COMPLETE_BABBLEERR: The transaction failed with a babble error
132 * @CVMX_USB_COMPLETE_FRAMEERR: The transaction failed with a frame error
133 * even after a number of retries
134 */
135enum cvmx_usb_complete {
136 CVMX_USB_COMPLETE_SUCCESS,
137 CVMX_USB_COMPLETE_SHORT,
138 CVMX_USB_COMPLETE_CANCEL,
139 CVMX_USB_COMPLETE_ERROR,
140 CVMX_USB_COMPLETE_STALL,
141 CVMX_USB_COMPLETE_XACTERR,
142 CVMX_USB_COMPLETE_DATATGLERR,
143 CVMX_USB_COMPLETE_BABBLEERR,
144 CVMX_USB_COMPLETE_FRAMEERR,
145};
146
147/**
148 * struct cvmx_usb_port_status - the USB port status information
149 *
150 * @port_enabled: 1 = Usb port is enabled, 0 = disabled
151 * @port_over_current: 1 = Over current detected, 0 = Over current not
152 * detected. Octeon doesn't support over current detection.
153 * @port_powered: 1 = Port power is being supplied to the device, 0 =
154 * power is off. Octeon doesn't support turning port power
155 * off.
156 * @port_speed: Current port speed.
157 * @connected: 1 = A device is connected to the port, 0 = No device is
158 * connected.
159 * @connect_change: 1 = Device connected state changed since the last set
160 * status call.
161 */
162struct cvmx_usb_port_status {
163 uint32_t reserved : 25;
164 uint32_t port_enabled : 1;
165 uint32_t port_over_current : 1;
166 uint32_t port_powered : 1;
167 enum cvmx_usb_speed port_speed : 2;
168 uint32_t connected : 1;
169 uint32_t connect_change : 1;
170};
171
172/**
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300173 * struct cvmx_usb_iso_packet - descriptor for Isochronous packets
174 *
175 * @offset: This is the offset in bytes into the main buffer where this data
176 * is stored.
177 * @length: This is the length in bytes of the data.
178 * @status: This is the status of this individual packet transfer.
179 */
180struct cvmx_usb_iso_packet {
181 int offset;
182 int length;
183 enum cvmx_usb_complete status;
184};
185
186/**
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300187 * enum cvmx_usb_initialize_flags - flags used by the initialization function
188 *
189 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI: The USB port uses a 12MHz crystal
190 * as clock source at USB_XO and
191 * USB_XI.
192 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND: The USB port uses 12/24/48MHz 2.5V
193 * board clock source at USB_XO.
194 * USB_XI should be tied to GND.
195 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK: Mask for clock speed field
196 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ: Speed of reference clock or
197 * crystal
198 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ: Speed of reference clock
199 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ: Speed of reference clock
200 * @CVMX_USB_INITIALIZE_FLAGS_NO_DMA: Disable DMA and used polled IO for
201 * data transfer use for the USB
202 */
203enum cvmx_usb_initialize_flags {
204 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI = 1 << 0,
205 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND = 1 << 1,
206 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK = 3 << 3,
207 CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ = 1 << 3,
208 CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ = 2 << 3,
209 CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ = 3 << 3,
210 /* Bits 3-4 used to encode the clock frequency */
211 CVMX_USB_INITIALIZE_FLAGS_NO_DMA = 1 << 5,
212};
213
214/**
215 * enum cvmx_usb_pipe_flags - internal flags for a pipe.
216 *
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300217 * @__CVMX_USB_PIPE_FLAGS_SCHEDULED: Used internally to determine if a pipe is
218 * actively using hardware. Do not use.
219 * @__CVMX_USB_PIPE_FLAGS_NEED_PING: Used internally to determine if a high
220 * speed pipe is in the ping state. Do not
221 * use.
222 */
223enum cvmx_usb_pipe_flags {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300224 __CVMX_USB_PIPE_FLAGS_SCHEDULED = 1 << 17,
225 __CVMX_USB_PIPE_FLAGS_NEED_PING = 1 << 18,
226};
227
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300228/* Maximum number of times to retry failed transactions */
229#define MAX_RETRIES 3
230
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300231/* Maximum number of hardware channels supported by the USB block */
232#define MAX_CHANNELS 8
233
234/* The highest valid USB device address */
235#define MAX_USB_ADDRESS 127
236
237/* The highest valid USB endpoint number */
238#define MAX_USB_ENDPOINT 15
239
240/* The highest valid port number on a hub */
241#define MAX_USB_HUB_PORT 15
242
243/*
244 * The low level hardware can transfer a maximum of this number of bytes in each
245 * transfer. The field is 19 bits wide
246 */
247#define MAX_TRANSFER_BYTES ((1<<19)-1)
248
249/*
250 * The low level hardware can transfer a maximum of this number of packets in
251 * each transfer. The field is 10 bits wide
252 */
253#define MAX_TRANSFER_PACKETS ((1<<10)-1)
254
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300255/**
256 * Logical transactions may take numerous low level
257 * transactions, especially when splits are concerned. This
258 * enum represents all of the possible stages a transaction can
259 * be in. Note that split completes are always even. This is so
260 * the NAK handler can backup to the previous low level
261 * transaction with a simple clearing of bit 0.
262 */
263enum cvmx_usb_stage {
264 CVMX_USB_STAGE_NON_CONTROL,
265 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
266 CVMX_USB_STAGE_SETUP,
267 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
268 CVMX_USB_STAGE_DATA,
269 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
270 CVMX_USB_STAGE_STATUS,
271 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
272};
273
274/**
275 * struct cvmx_usb_transaction - describes each pending USB transaction
276 * regardless of type. These are linked together
277 * to form a list of pending requests for a pipe.
278 *
Aaro Koskinen56696012013-10-10 23:25:36 +0300279 * @node: List node for transactions in the pipe.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300280 * @type: Type of transaction, duplicated of the pipe.
281 * @flags: State flags for this transaction.
282 * @buffer: User's physical buffer address to read/write.
283 * @buffer_length: Size of the user's buffer in bytes.
284 * @control_header: For control transactions, physical address of the 8
285 * byte standard header.
286 * @iso_start_frame: For ISO transactions, the starting frame number.
287 * @iso_number_packets: For ISO transactions, the number of packets in the
288 * request.
289 * @iso_packets: For ISO transactions, the sub packets in the request.
290 * @actual_bytes: Actual bytes transfer for this transaction.
291 * @stage: For control transactions, the current stage.
Aaro Koskinen0cce1002013-10-06 22:22:29 +0300292 * @urb: URB.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300293 */
294struct cvmx_usb_transaction {
Aaro Koskinen56696012013-10-10 23:25:36 +0300295 struct list_head node;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300296 enum cvmx_usb_transfer type;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300297 uint64_t buffer;
298 int buffer_length;
299 uint64_t control_header;
300 int iso_start_frame;
301 int iso_number_packets;
302 struct cvmx_usb_iso_packet *iso_packets;
303 int xfersize;
304 int pktcnt;
305 int retries;
306 int actual_bytes;
307 enum cvmx_usb_stage stage;
Aaro Koskinen0cce1002013-10-06 22:22:29 +0300308 struct urb *urb;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300309};
310
311/**
312 * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
313 * and some USB device. It contains a list of pending
314 * request to the device.
315 *
Aaro Koskinen4a23ee12013-10-10 23:25:35 +0300316 * @node: List node for pipe list
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300317 * @next: Pipe after this one in the list
Aaro Koskinen56696012013-10-10 23:25:36 +0300318 * @transactions: List of pending transactions
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300319 * @interval: For periodic pipes, the interval between packets in
320 * frames
321 * @next_tx_frame: The next frame this pipe is allowed to transmit on
322 * @flags: State flags for this pipe
323 * @device_speed: Speed of device connected to this pipe
324 * @transfer_type: Type of transaction supported by this pipe
325 * @transfer_dir: IN or OUT. Ignored for Control
326 * @multi_count: Max packet in a row for the device
327 * @max_packet: The device's maximum packet size in bytes
328 * @device_addr: USB device address at other end of pipe
329 * @endpoint_num: USB endpoint number at other end of pipe
330 * @hub_device_addr: Hub address this device is connected to
331 * @hub_port: Hub port this device is connected to
332 * @pid_toggle: This toggles between 0/1 on every packet send to track
333 * the data pid needed
334 * @channel: Hardware DMA channel for this pipe
335 * @split_sc_frame: The low order bits of the frame number the split
336 * complete should be sent on
337 */
338struct cvmx_usb_pipe {
Aaro Koskinen4a23ee12013-10-10 23:25:35 +0300339 struct list_head node;
Aaro Koskinen56696012013-10-10 23:25:36 +0300340 struct list_head transactions;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300341 uint64_t interval;
342 uint64_t next_tx_frame;
343 enum cvmx_usb_pipe_flags flags;
344 enum cvmx_usb_speed device_speed;
345 enum cvmx_usb_transfer transfer_type;
346 enum cvmx_usb_direction transfer_dir;
347 int multi_count;
348 uint16_t max_packet;
349 uint8_t device_addr;
350 uint8_t endpoint_num;
351 uint8_t hub_device_addr;
352 uint8_t hub_port;
353 uint8_t pid_toggle;
354 uint8_t channel;
355 int8_t split_sc_frame;
356};
357
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300358struct cvmx_usb_tx_fifo {
359 struct {
360 int channel;
361 int size;
362 uint64_t address;
363 } entry[MAX_CHANNELS+1];
364 int head;
365 int tail;
366};
367
368/**
Aaro Koskinencb61c602013-10-06 22:22:25 +0300369 * struct cvmx_usb_state - the state of the USB block
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300370 *
371 * init_flags: Flags passed to initialize.
372 * index: Which USB block this is for.
373 * idle_hardware_channels: Bit set for every idle hardware channel.
374 * usbcx_hprt: Stored port status so we don't need to read a CSR to
375 * determine splits.
376 * pipe_for_channel: Map channels to pipes.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300377 * pipe: Storage for pipes.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300378 * indent: Used by debug output to indent functions.
379 * port_status: Last port status used for change notification.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300380 * idle_pipes: List of open pipes that have no transactions.
381 * active_pipes: Active pipes indexed by transfer type.
382 * frame_number: Increments every SOF interrupt for time keeping.
383 * active_split: Points to the current active split, or NULL.
384 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300385struct cvmx_usb_state {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300386 int init_flags;
387 int index;
388 int idle_hardware_channels;
389 union cvmx_usbcx_hprt usbcx_hprt;
390 struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300391 int indent;
392 struct cvmx_usb_port_status port_status;
Aaro Koskinen4a23ee12013-10-10 23:25:35 +0300393 struct list_head idle_pipes;
394 struct list_head active_pipes[4];
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300395 uint64_t frame_number;
396 struct cvmx_usb_transaction *active_split;
397 struct cvmx_usb_tx_fifo periodic;
398 struct cvmx_usb_tx_fifo nonperiodic;
399};
400
Aaro Koskinenb1649352013-06-01 21:42:58 +0300401struct octeon_hcd {
Aaro Koskinen771378b2013-06-13 01:00:31 +0300402 spinlock_t lock;
Aaro Koskinena24ed352013-07-30 23:43:05 +0300403 struct cvmx_usb_state usb;
Aaro Koskinenb1649352013-06-01 21:42:58 +0300404};
405
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300406/* This macro spins on a field waiting for it to reach a value */
407#define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
408 ({int result; \
409 do { \
410 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
411 octeon_get_clock_rate() / 1000000; \
412 type c; \
413 while (1) { \
414 c.u32 = __cvmx_usb_read_csr32(usb, address); \
415 if (c.s.field op (value)) { \
416 result = 0; \
417 break; \
418 } else if (cvmx_get_cycle() > done) { \
419 result = -1; \
420 break; \
421 } else \
422 cvmx_wait(100); \
423 } \
424 } while (0); \
425 result; })
426
427/*
428 * This macro logically sets a single field in a CSR. It does the sequence
429 * read, modify, and write
430 */
431#define USB_SET_FIELD32(address, type, field, value) \
432 do { \
433 type c; \
434 c.u32 = __cvmx_usb_read_csr32(usb, address); \
435 c.s.field = value; \
436 __cvmx_usb_write_csr32(usb, address, c.u32); \
437 } while (0)
438
439/* Returns the IO address to push/pop stuff data from the FIFOs */
Paul Davies Ce725cef2014-05-11 18:41:33 +0530440#define USB_FIFO_ADDRESS(channel, usb_index) \
441 (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300442
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300443/**
Aaro Koskinen120ee592014-03-20 00:52:08 +0200444 * struct octeon_temp_buffer - a bounce buffer for USB transfers
445 * @temp_buffer: the newly allocated temporary buffer (including meta-data)
446 * @orig_buffer: the original buffer passed by the USB stack
447 * @data: the newly allocated temporary buffer (excluding meta-data)
448 *
449 * Both the DMA engine and FIFO mode will always transfer full 32-bit words. If
450 * the buffer is too short, we need to allocate a temporary one, and this struct
451 * represents it.
452 */
453struct octeon_temp_buffer {
454 void *temp_buffer;
455 void *orig_buffer;
456 u8 data[0];
457};
458
459/**
460 * octeon_alloc_temp_buffer - allocate a temporary buffer for USB transfer
461 * (if needed)
462 * @urb: URB.
463 * @mem_flags: Memory allocation flags.
464 *
465 * This function allocates a temporary bounce buffer whenever it's needed
466 * due to HW limitations.
467 */
468static int octeon_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
469{
470 struct octeon_temp_buffer *temp;
471
472 if (urb->num_sgs || urb->sg ||
473 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
474 !(urb->transfer_buffer_length % sizeof(u32)))
475 return 0;
476
477 temp = kmalloc(ALIGN(urb->transfer_buffer_length, sizeof(u32)) +
478 sizeof(*temp), mem_flags);
479 if (!temp)
480 return -ENOMEM;
481
482 temp->temp_buffer = temp;
483 temp->orig_buffer = urb->transfer_buffer;
484 if (usb_urb_dir_out(urb))
485 memcpy(temp->data, urb->transfer_buffer,
486 urb->transfer_buffer_length);
487 urb->transfer_buffer = temp->data;
488 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
489
490 return 0;
491}
492
493/**
494 * octeon_free_temp_buffer - free a temporary buffer used by USB transfers.
495 * @urb: URB.
496 *
497 * Frees a buffer allocated by octeon_alloc_temp_buffer().
498 */
499static void octeon_free_temp_buffer(struct urb *urb)
500{
501 struct octeon_temp_buffer *temp;
502
503 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
504 return;
505
506 temp = container_of(urb->transfer_buffer, struct octeon_temp_buffer,
507 data);
508 if (usb_urb_dir_in(urb))
509 memcpy(temp->orig_buffer, urb->transfer_buffer,
510 urb->actual_length);
511 urb->transfer_buffer = temp->orig_buffer;
512 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
513 kfree(temp->temp_buffer);
514}
515
516/**
517 * octeon_map_urb_for_dma - Octeon-specific map_urb_for_dma().
518 * @hcd: USB HCD structure.
519 * @urb: URB.
520 * @mem_flags: Memory allocation flags.
521 */
522static int octeon_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
523 gfp_t mem_flags)
524{
525 int ret;
526
527 ret = octeon_alloc_temp_buffer(urb, mem_flags);
528 if (ret)
529 return ret;
530
531 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
532 if (ret)
533 octeon_free_temp_buffer(urb);
534
535 return ret;
536}
537
538/**
539 * octeon_unmap_urb_for_dma - Octeon-specific unmap_urb_for_dma()
540 * @hcd: USB HCD structure.
541 * @urb: URB.
542 */
543static void octeon_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
544{
545 usb_hcd_unmap_urb_for_dma(hcd, urb);
546 octeon_free_temp_buffer(urb);
547}
548
549/**
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300550 * Read a USB 32bit CSR. It performs the necessary address swizzle
551 * for 32bit CSRs and logs the value in a readable format if
552 * debugging is on.
553 *
554 * @usb: USB block this access is for
555 * @address: 64bit address to read
556 *
557 * Returns: Result of the read
558 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300559static inline uint32_t __cvmx_usb_read_csr32(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300560 uint64_t address)
561{
562 uint32_t result = cvmx_read64_uint32(address ^ 4);
563 return result;
564}
565
566
567/**
568 * Write a USB 32bit CSR. It performs the necessary address
569 * swizzle for 32bit CSRs and logs the value in a readable format
570 * if debugging is on.
571 *
572 * @usb: USB block this access is for
573 * @address: 64bit address to write
574 * @value: Value to write
575 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300576static inline void __cvmx_usb_write_csr32(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300577 uint64_t address, uint32_t value)
578{
579 cvmx_write64_uint32(address ^ 4, value);
580 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
581}
582
583
584/**
585 * Read a USB 64bit CSR. It logs the value in a readable format if
586 * debugging is on.
587 *
588 * @usb: USB block this access is for
589 * @address: 64bit address to read
590 *
591 * Returns: Result of the read
592 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300593static inline uint64_t __cvmx_usb_read_csr64(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300594 uint64_t address)
595{
596 uint64_t result = cvmx_read64_uint64(address);
597 return result;
598}
599
600
601/**
602 * Write a USB 64bit CSR. It logs the value in a readable format
603 * if debugging is on.
604 *
605 * @usb: USB block this access is for
606 * @address: 64bit address to write
607 * @value: Value to write
608 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300609static inline void __cvmx_usb_write_csr64(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300610 uint64_t address, uint64_t value)
611{
612 cvmx_write64_uint64(address, value);
613}
614
615/**
616 * Return non zero if this pipe connects to a non HIGH speed
617 * device through a high speed hub.
618 *
619 * @usb: USB block this access is for
620 * @pipe: Pipe to check
621 *
622 * Returns: Non zero if we need to do split transactions
623 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300624static inline int __cvmx_usb_pipe_needs_split(struct cvmx_usb_state *usb,
625 struct cvmx_usb_pipe *pipe)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300626{
Aaro Koskinenaa87afe2013-10-06 22:22:37 +0300627 return pipe->device_speed != CVMX_USB_SPEED_HIGH &&
628 usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300629}
630
631
632/**
633 * Trivial utility function to return the correct PID for a pipe
634 *
635 * @pipe: pipe to check
636 *
637 * Returns: PID for pipe
638 */
639static inline int __cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
640{
641 if (pipe->pid_toggle)
642 return 2; /* Data1 */
643 else
644 return 0; /* Data0 */
645}
646
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300647/**
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300648 * Initialize a USB port for use. This must be called before any
649 * other access to the Octeon USB port is made. The port starts
650 * off in the disabled state.
651 *
Aaro Koskinencb61c602013-10-06 22:22:25 +0300652 * @usb: Pointer to an empty struct cvmx_usb_state
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300653 * that will be populated by the initialize call.
654 * This structure is then passed to all other USB
655 * functions.
656 * @usb_port_number:
657 * Which Octeon USB port to initialize.
658 *
659 * Returns: 0 or a negative error code.
660 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300661static int cvmx_usb_initialize(struct cvmx_usb_state *usb,
David Daneyb91619c2014-02-03 19:39:01 +0200662 int usb_port_number,
663 enum cvmx_usb_initialize_flags flags)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300664{
665 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
666 union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
Aaro Koskinen4a23ee12013-10-10 23:25:35 +0300667 int i;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300668
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300669 /* At first allow 0-1 for the usb port number */
670 if ((usb_port_number < 0) || (usb_port_number > 1))
671 return -EINVAL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300672
673 memset(usb, 0, sizeof(*usb));
674 usb->init_flags = flags;
675
676 /* Initialize the USB state structure */
Aaro Koskinend2695a82013-10-10 23:25:32 +0300677 usb->index = usb_port_number;
Aaro Koskinen4a23ee12013-10-10 23:25:35 +0300678 INIT_LIST_HEAD(&usb->idle_pipes);
679 for (i = 0; i < ARRAY_SIZE(usb->active_pipes); i++)
680 INIT_LIST_HEAD(&usb->active_pipes[i]);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300681
682 /*
683 * Power On Reset and PHY Initialization
684 *
685 * 1. Wait for DCOK to assert (nothing to do)
686 *
687 * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
688 * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
689 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200690 usbn_clk_ctl.u64 =
691 __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300692 usbn_clk_ctl.s.por = 1;
693 usbn_clk_ctl.s.hrst = 0;
694 usbn_clk_ctl.s.prst = 0;
695 usbn_clk_ctl.s.hclk_rst = 0;
696 usbn_clk_ctl.s.enable = 0;
697 /*
698 * 2b. Select the USB reference clock/crystal parameters by writing
699 * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
700 */
701 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
702 /*
703 * The USB port uses 12/24/48MHz 2.5V board clock
704 * source at USB_XO. USB_XI should be tied to GND.
705 * Most Octeon evaluation boards require this setting
706 */
Aaro Koskinen7d7bc262013-10-10 23:25:29 +0300707 if (OCTEON_IS_MODEL(OCTEON_CN3XXX) ||
708 OCTEON_IS_MODEL(OCTEON_CN56XX) ||
709 OCTEON_IS_MODEL(OCTEON_CN50XX))
710 /* From CN56XX,CN50XX,CN31XX,CN30XX manuals */
711 usbn_clk_ctl.s.p_rtype = 2; /* p_rclk=1 & p_xenbn=0 */
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300712 else
713 /* From CN52XX manual */
Aaro Koskinen34b70b92013-10-10 23:25:28 +0300714 usbn_clk_ctl.s.p_rtype = 1;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300715
716 switch (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
717 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
718 usbn_clk_ctl.s.p_c_sel = 0;
719 break;
720 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
721 usbn_clk_ctl.s.p_c_sel = 1;
722 break;
723 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
724 usbn_clk_ctl.s.p_c_sel = 2;
725 break;
726 }
727 } else {
728 /*
729 * The USB port uses a 12MHz crystal as clock source
730 * at USB_XO and USB_XI
731 */
Aaro Koskinen7d7bc262013-10-10 23:25:29 +0300732 if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300733 /* From CN31XX,CN30XX manual */
Aaro Koskinen7d7bc262013-10-10 23:25:29 +0300734 usbn_clk_ctl.s.p_rtype = 3; /* p_rclk=1 & p_xenbn=1 */
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300735 else
Aaro Koskinen7d7bc262013-10-10 23:25:29 +0300736 /* From CN56XX,CN52XX,CN50XX manuals. */
Aaro Koskinen34b70b92013-10-10 23:25:28 +0300737 usbn_clk_ctl.s.p_rtype = 0;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300738
739 usbn_clk_ctl.s.p_c_sel = 0;
740 }
741 /*
742 * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
743 * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
744 * such that USB is as close as possible to 125Mhz
745 */
746 {
747 int divisor = (octeon_get_clock_rate()+125000000-1)/125000000;
748 /* Lower than 4 doesn't seem to work properly */
749 if (divisor < 4)
750 divisor = 4;
751 usbn_clk_ctl.s.divide = divisor;
752 usbn_clk_ctl.s.divide2 = 0;
753 }
754 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
755 usbn_clk_ctl.u64);
756 /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
757 usbn_clk_ctl.s.hclk_rst = 1;
758 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
759 usbn_clk_ctl.u64);
760 /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
761 cvmx_wait(64);
762 /*
763 * 3. Program the power-on reset field in the USBN clock-control
764 * register:
765 * USBN_CLK_CTL[POR] = 0
766 */
767 usbn_clk_ctl.s.por = 0;
768 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
769 usbn_clk_ctl.u64);
770 /* 4. Wait 1 ms for PHY clock to start */
771 mdelay(1);
772 /*
773 * 5. Program the Reset input from automatic test equipment field in the
774 * USBP control and status register:
775 * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
776 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200777 usbn_usbp_ctl_status.u64 = __cvmx_usb_read_csr64(usb,
778 CVMX_USBNX_USBP_CTL_STATUS(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300779 usbn_usbp_ctl_status.s.ate_reset = 1;
780 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
781 usbn_usbp_ctl_status.u64);
782 /* 6. Wait 10 cycles */
783 cvmx_wait(10);
784 /*
785 * 7. Clear ATE_RESET field in the USBN clock-control register:
786 * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
787 */
788 usbn_usbp_ctl_status.s.ate_reset = 0;
789 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
790 usbn_usbp_ctl_status.u64);
791 /*
792 * 8. Program the PHY reset field in the USBN clock-control register:
793 * USBN_CLK_CTL[PRST] = 1
794 */
795 usbn_clk_ctl.s.prst = 1;
796 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
797 usbn_clk_ctl.u64);
798 /*
799 * 9. Program the USBP control and status register to select host or
800 * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
801 * device
802 */
803 usbn_usbp_ctl_status.s.hst_mode = 0;
804 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
805 usbn_usbp_ctl_status.u64);
806 /* 10. Wait 1 us */
807 udelay(1);
808 /*
809 * 11. Program the hreset_n field in the USBN clock-control register:
810 * USBN_CLK_CTL[HRST] = 1
811 */
812 usbn_clk_ctl.s.hrst = 1;
813 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
814 usbn_clk_ctl.u64);
815 /* 12. Proceed to USB core initialization */
816 usbn_clk_ctl.s.enable = 1;
817 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
818 usbn_clk_ctl.u64);
819 udelay(1);
820
821 /*
822 * USB Core Initialization
823 *
824 * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
825 * determine USB core configuration parameters.
826 *
827 * Nothing needed
828 *
829 * 2. Program the following fields in the global AHB configuration
830 * register (USBC_GAHBCFG)
831 * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
832 * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
833 * Nonperiodic TxFIFO empty level (slave mode only),
834 * USBC_GAHBCFG[NPTXFEMPLVL]
835 * Periodic TxFIFO empty level (slave mode only),
836 * USBC_GAHBCFG[PTXFEMPLVL]
837 * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
838 */
839 {
840 union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
841 /* Due to an errata, CN31XX doesn't support DMA */
842 if (OCTEON_IS_MODEL(OCTEON_CN31XX))
843 usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
844 usbcx_gahbcfg.u32 = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200845 usbcx_gahbcfg.s.dmaen = !(usb->init_flags &
846 CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300847 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
848 /* Only use one channel with non DMA */
849 usb->idle_hardware_channels = 0x1;
850 else if (OCTEON_IS_MODEL(OCTEON_CN5XXX))
851 /* CN5XXX have an errata with channel 3 */
852 usb->idle_hardware_channels = 0xf7;
853 else
854 usb->idle_hardware_channels = 0xff;
855 usbcx_gahbcfg.s.hbstlen = 0;
856 usbcx_gahbcfg.s.nptxfemplvl = 1;
857 usbcx_gahbcfg.s.ptxfemplvl = 1;
858 usbcx_gahbcfg.s.glblintrmsk = 1;
859 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
860 usbcx_gahbcfg.u32);
861 }
862 /*
863 * 3. Program the following fields in USBC_GUSBCFG register.
864 * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
865 * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
866 * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
867 * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
868 */
869 {
870 union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
Paul Davies Cf5106432014-05-11 18:41:32 +0530871
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200872 usbcx_gusbcfg.u32 = __cvmx_usb_read_csr32(usb,
873 CVMX_USBCX_GUSBCFG(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300874 usbcx_gusbcfg.s.toutcal = 0;
875 usbcx_gusbcfg.s.ddrsel = 0;
876 usbcx_gusbcfg.s.usbtrdtim = 0x5;
877 usbcx_gusbcfg.s.phylpwrclksel = 0;
878 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
879 usbcx_gusbcfg.u32);
880 }
881 /*
882 * 4. The software must unmask the following bits in the USBC_GINTMSK
883 * register.
884 * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
885 * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
886 */
887 {
888 union cvmx_usbcx_gintmsk usbcx_gintmsk;
889 int channel;
890
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200891 usbcx_gintmsk.u32 = __cvmx_usb_read_csr32(usb,
892 CVMX_USBCX_GINTMSK(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300893 usbcx_gintmsk.s.otgintmsk = 1;
894 usbcx_gintmsk.s.modemismsk = 1;
895 usbcx_gintmsk.s.hchintmsk = 1;
896 usbcx_gintmsk.s.sofmsk = 0;
897 /* We need RX FIFO interrupts if we don't have DMA */
898 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
899 usbcx_gintmsk.s.rxflvlmsk = 1;
900 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
901 usbcx_gintmsk.u32);
902
903 /*
904 * Disable all channel interrupts. We'll enable them per channel
905 * later.
906 */
907 for (channel = 0; channel < 8; channel++)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200908 __cvmx_usb_write_csr32(usb,
909 CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300910 }
911
912 {
913 /*
914 * Host Port Initialization
915 *
916 * 1. Program the host-port interrupt-mask field to unmask,
917 * USBC_GINTMSK[PRTINT] = 1
918 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200919 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
920 union cvmx_usbcx_gintmsk, prtintmsk, 1);
921 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
922 union cvmx_usbcx_gintmsk, disconnintmsk, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300923 /*
924 * 2. Program the USBC_HCFG register to select full-speed host
925 * or high-speed host.
926 */
927 {
928 union cvmx_usbcx_hcfg usbcx_hcfg;
Paul Davies Cf5106432014-05-11 18:41:32 +0530929
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200930 usbcx_hcfg.u32 = __cvmx_usb_read_csr32(usb,
931 CVMX_USBCX_HCFG(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300932 usbcx_hcfg.s.fslssupp = 0;
933 usbcx_hcfg.s.fslspclksel = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200934 __cvmx_usb_write_csr32(usb,
935 CVMX_USBCX_HCFG(usb->index),
936 usbcx_hcfg.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300937 }
938 /*
939 * 3. Program the port power bit to drive VBUS on the USB,
940 * USBC_HPRT[PRTPWR] = 1
941 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200942 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index),
943 union cvmx_usbcx_hprt, prtpwr, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300944
945 /*
946 * Steps 4-15 from the manual are done later in the port enable
947 */
948 }
949
950 return 0;
951}
952
953
954/**
955 * Shutdown a USB port after a call to cvmx_usb_initialize().
956 * The port should be disabled with all pipes closed when this
957 * function is called.
958 *
Aaro Koskinencb61c602013-10-06 22:22:25 +0300959 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300960 *
961 * Returns: 0 or a negative error code.
962 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300963static int cvmx_usb_shutdown(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300964{
965 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300966
967 /* Make sure all pipes are closed */
Aaro Koskinen4a23ee12013-10-10 23:25:35 +0300968 if (!list_empty(&usb->idle_pipes) ||
969 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS]) ||
970 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT]) ||
971 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_CONTROL]) ||
972 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_BULK]))
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300973 return -EBUSY;
974
975 /* Disable the clocks and put them in power on reset */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +0200976 usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb,
977 CVMX_USBNX_CLK_CTL(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300978 usbn_clk_ctl.s.enable = 1;
979 usbn_clk_ctl.s.por = 1;
980 usbn_clk_ctl.s.hclk_rst = 1;
981 usbn_clk_ctl.s.prst = 0;
982 usbn_clk_ctl.s.hrst = 0;
983 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
984 usbn_clk_ctl.u64);
985 return 0;
986}
987
988
989/**
990 * Enable a USB port. After this call succeeds, the USB port is
991 * online and servicing requests.
992 *
Aaro Koskinencb61c602013-10-06 22:22:25 +0300993 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300994 *
995 * Returns: 0 or a negative error code.
996 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300997static int cvmx_usb_enable(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300998{
999 union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001000
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001001 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb,
1002 CVMX_USBCX_HPRT(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001003
1004 /*
1005 * If the port is already enabled the just return. We don't need to do
1006 * anything
1007 */
1008 if (usb->usbcx_hprt.s.prtena)
1009 return 0;
1010
1011 /* If there is nothing plugged into the port then fail immediately */
Raluca Oncioiub49f1132014-03-08 00:44:12 +02001012 if (!usb->usbcx_hprt.s.prtconnsts)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001013 return -ETIMEDOUT;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001014
1015 /* Program the port reset bit to start the reset process */
Raluca Oncioiub49f1132014-03-08 00:44:12 +02001016 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
1017 prtrst, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001018
1019 /*
1020 * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
1021 * process to complete.
1022 */
1023 mdelay(50);
1024
1025 /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001026 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
1027 prtrst, 0);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001028
1029 /* Wait for the USBC_HPRT[PRTENA]. */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001030 if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb->index),
1031 union cvmx_usbcx_hprt, prtena, ==, 1, 100000))
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001032 return -ETIMEDOUT;
1033
1034 /*
1035 * Read the port speed field to get the enumerated speed,
1036 * USBC_HPRT[PRTSPD].
1037 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001038 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb,
1039 CVMX_USBCX_HPRT(usb->index));
1040 usbcx_ghwcfg3.u32 = __cvmx_usb_read_csr32(usb,
1041 CVMX_USBCX_GHWCFG3(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001042
1043 /*
1044 * 13. Program the USBC_GRXFSIZ register to select the size of the
1045 * receive FIFO (25%).
1046 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001047 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index),
1048 union cvmx_usbcx_grxfsiz, rxfdep,
1049 usbcx_ghwcfg3.s.dfifodepth / 4);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001050 /*
1051 * 14. Program the USBC_GNPTXFSIZ register to select the size and the
1052 * start address of the non- periodic transmit FIFO for nonperiodic
1053 * transactions (50%).
1054 */
1055 {
1056 union cvmx_usbcx_gnptxfsiz siz;
Paul Davies Cf5106432014-05-11 18:41:32 +05301057
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001058 siz.u32 = __cvmx_usb_read_csr32(usb,
1059 CVMX_USBCX_GNPTXFSIZ(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001060 siz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
1061 siz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001062 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index),
1063 siz.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001064 }
1065 /*
1066 * 15. Program the USBC_HPTXFSIZ register to select the size and start
1067 * address of the periodic transmit FIFO for periodic transactions
1068 * (25%).
1069 */
1070 {
1071 union cvmx_usbcx_hptxfsiz siz;
Paul Davies Cf5106432014-05-11 18:41:32 +05301072
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001073 siz.u32 = __cvmx_usb_read_csr32(usb,
1074 CVMX_USBCX_HPTXFSIZ(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001075 siz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
1076 siz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001077 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index),
1078 siz.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001079 }
1080 /* Flush all FIFOs */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001081 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1082 union cvmx_usbcx_grstctl, txfnum, 0x10);
1083 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1084 union cvmx_usbcx_grstctl, txfflsh, 1);
1085 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1086 union cvmx_usbcx_grstctl,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001087 txfflsh, ==, 0, 100);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001088 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1089 union cvmx_usbcx_grstctl, rxfflsh, 1);
1090 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
1091 union cvmx_usbcx_grstctl,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001092 rxfflsh, ==, 0, 100);
1093
1094 return 0;
1095}
1096
1097
1098/**
1099 * Disable a USB port. After this call the USB port will not
1100 * generate data transfers and will not generate events.
1101 * Transactions in process will fail and call their
1102 * associated callbacks.
1103 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001104 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001105 *
1106 * Returns: 0 or a negative error code.
1107 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001108static int cvmx_usb_disable(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001109{
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001110 /* Disable the port */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001111 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
1112 prtena, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001113 return 0;
1114}
1115
1116
1117/**
1118 * Get the current state of the USB port. Use this call to
1119 * determine if the usb port has anything connected, is enabled,
1120 * or has some sort of error condition. The return value of this
1121 * call has "changed" bits to signal of the value of some fields
Aaro Koskinen29a202f2013-10-06 22:22:26 +03001122 * have changed between calls.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001123 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001124 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001125 *
1126 * Returns: Port status information
1127 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001128static struct cvmx_usb_port_status cvmx_usb_get_status(
1129 struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001130{
1131 union cvmx_usbcx_hprt usbc_hprt;
1132 struct cvmx_usb_port_status result;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001133
1134 memset(&result, 0, sizeof(result));
1135
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001136 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb,
1137 CVMX_USBCX_HPRT(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001138 result.port_enabled = usbc_hprt.s.prtena;
1139 result.port_over_current = usbc_hprt.s.prtovrcurract;
1140 result.port_powered = usbc_hprt.s.prtpwr;
1141 result.port_speed = usbc_hprt.s.prtspd;
1142 result.connected = usbc_hprt.s.prtconnsts;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001143 result.connect_change =
1144 (result.connected != usb->port_status.connected);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001145
1146 return result;
1147}
1148
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001149/**
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001150 * Open a virtual pipe between the host and a USB device. A pipe
1151 * must be opened before data can be transferred between a device
1152 * and Octeon.
1153 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001154 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001155 * @device_addr:
1156 * USB device address to open the pipe to
1157 * (0-127).
1158 * @endpoint_num:
1159 * USB endpoint number to open the pipe to
1160 * (0-15).
1161 * @device_speed:
1162 * The speed of the device the pipe is going
1163 * to. This must match the device's speed,
1164 * which may be different than the port speed.
1165 * @max_packet: The maximum packet length the device can
1166 * transmit/receive (low speed=0-8, full
1167 * speed=0-1023, high speed=0-1024). This value
1168 * comes from the standard endpoint descriptor
1169 * field wMaxPacketSize bits <10:0>.
1170 * @transfer_type:
1171 * The type of transfer this pipe is for.
1172 * @transfer_dir:
1173 * The direction the pipe is in. This is not
1174 * used for control pipes.
1175 * @interval: For ISOCHRONOUS and INTERRUPT transfers,
1176 * this is how often the transfer is scheduled
1177 * for. All other transfers should specify
1178 * zero. The units are in frames (8000/sec at
1179 * high speed, 1000/sec for full speed).
1180 * @multi_count:
1181 * For high speed devices, this is the maximum
1182 * allowed number of packet per microframe.
1183 * Specify zero for non high speed devices. This
1184 * value comes from the standard endpoint descriptor
1185 * field wMaxPacketSize bits <12:11>.
1186 * @hub_device_addr:
1187 * Hub device address this device is connected
1188 * to. Devices connected directly to Octeon
1189 * use zero. This is only used when the device
1190 * is full/low speed behind a high speed hub.
1191 * The address will be of the high speed hub,
1192 * not and full speed hubs after it.
1193 * @hub_port: Which port on the hub the device is
1194 * connected. Use zero for devices connected
1195 * directly to Octeon. Like hub_device_addr,
1196 * this is only used for full/low speed
1197 * devices behind a high speed hub.
1198 *
Aaro Koskinen60f81502013-10-06 22:22:35 +03001199 * Returns: A non-NULL value is a pipe. NULL means an error.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001200 */
Aaro Koskinen60f81502013-10-06 22:22:35 +03001201static struct cvmx_usb_pipe *cvmx_usb_open_pipe(struct cvmx_usb_state *usb,
Paul Davies Cf5106432014-05-11 18:41:32 +05301202 int device_addr,
1203 int endpoint_num,
Aaro Koskinen60f81502013-10-06 22:22:35 +03001204 enum cvmx_usb_speed
1205 device_speed,
1206 int max_packet,
1207 enum cvmx_usb_transfer
1208 transfer_type,
1209 enum cvmx_usb_direction
1210 transfer_dir,
1211 int interval, int multi_count,
1212 int hub_device_addr,
1213 int hub_port)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001214{
1215 struct cvmx_usb_pipe *pipe;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001216
1217 if (unlikely((device_addr < 0) || (device_addr > MAX_USB_ADDRESS)))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001218 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001219 if (unlikely((endpoint_num < 0) || (endpoint_num > MAX_USB_ENDPOINT)))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001220 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001221 if (unlikely(device_speed > CVMX_USB_SPEED_LOW))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001222 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001223 if (unlikely((max_packet <= 0) || (max_packet > 1024)))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001224 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001225 if (unlikely(transfer_type > CVMX_USB_TRANSFER_INTERRUPT))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001226 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001227 if (unlikely((transfer_dir != CVMX_USB_DIRECTION_OUT) &&
1228 (transfer_dir != CVMX_USB_DIRECTION_IN)))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001229 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001230 if (unlikely(interval < 0))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001231 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001232 if (unlikely((transfer_type == CVMX_USB_TRANSFER_CONTROL) && interval))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001233 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001234 if (unlikely(multi_count < 0))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001235 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001236 if (unlikely((device_speed != CVMX_USB_SPEED_HIGH) &&
1237 (multi_count != 0)))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001238 return NULL;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001239 if (unlikely((hub_device_addr < 0) ||
1240 (hub_device_addr > MAX_USB_ADDRESS)))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001241 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001242 if (unlikely((hub_port < 0) || (hub_port > MAX_USB_HUB_PORT)))
Aaro Koskinen60f81502013-10-06 22:22:35 +03001243 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001244
Aaro Koskinend2695a82013-10-10 23:25:32 +03001245 pipe = kzalloc(sizeof(*pipe), GFP_ATOMIC);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001246 if (!pipe)
Aaro Koskinen60f81502013-10-06 22:22:35 +03001247 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001248 if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1249 (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1250 (transfer_type == CVMX_USB_TRANSFER_BULK))
1251 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
1252 pipe->device_addr = device_addr;
1253 pipe->endpoint_num = endpoint_num;
1254 pipe->device_speed = device_speed;
1255 pipe->max_packet = max_packet;
1256 pipe->transfer_type = transfer_type;
1257 pipe->transfer_dir = transfer_dir;
Aaro Koskinen56696012013-10-10 23:25:36 +03001258 INIT_LIST_HEAD(&pipe->transactions);
1259
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001260 /*
1261 * All pipes use interval to rate limit NAK processing. Force an
1262 * interval if one wasn't supplied
1263 */
1264 if (!interval)
1265 interval = 1;
1266 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1267 pipe->interval = interval*8;
1268 /* Force start splits to be schedule on uFrame 0 */
Paul Davies Ce725cef2014-05-11 18:41:33 +05301269 pipe->next_tx_frame = ((usb->frame_number+7)&~7) +
1270 pipe->interval;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001271 } else {
1272 pipe->interval = interval;
1273 pipe->next_tx_frame = usb->frame_number + pipe->interval;
1274 }
1275 pipe->multi_count = multi_count;
1276 pipe->hub_device_addr = hub_device_addr;
1277 pipe->hub_port = hub_port;
1278 pipe->pid_toggle = 0;
1279 pipe->split_sc_frame = -1;
Aaro Koskinen4a23ee12013-10-10 23:25:35 +03001280 list_add_tail(&pipe->node, &usb->idle_pipes);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001281
1282 /*
1283 * We don't need to tell the hardware about this pipe yet since
1284 * it doesn't have any submitted requests
1285 */
1286
Aaro Koskinen60f81502013-10-06 22:22:35 +03001287 return pipe;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001288}
1289
1290
1291/**
1292 * Poll the RX FIFOs and remove data as needed. This function is only used
1293 * in non DMA mode. It is very important that this function be called quickly
1294 * enough to prevent FIFO overflow.
1295 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001296 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001297 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001298static void __cvmx_usb_poll_rx_fifo(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001299{
1300 union cvmx_usbcx_grxstsph rx_status;
1301 int channel;
1302 int bytes;
1303 uint64_t address;
1304 uint32_t *ptr;
1305
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001306 rx_status.u32 = __cvmx_usb_read_csr32(usb,
1307 CVMX_USBCX_GRXSTSPH(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001308 /* Only read data if IN data is there */
1309 if (rx_status.s.pktsts != 2)
1310 return;
1311 /* Check if no data is available */
1312 if (!rx_status.s.bcnt)
1313 return;
1314
1315 channel = rx_status.s.chnum;
1316 bytes = rx_status.s.bcnt;
1317 if (!bytes)
1318 return;
1319
1320 /* Get where the DMA engine would have written this data */
Paul Davies Ce725cef2014-05-11 18:41:33 +05301321 address = __cvmx_usb_read_csr64(usb,
1322 CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8);
1323
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001324 ptr = cvmx_phys_to_ptr(address);
Paul Davies Ce725cef2014-05-11 18:41:33 +05301325 __cvmx_usb_write_csr64(usb,
1326 CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8,
1327 address + bytes);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001328
1329 /* Loop writing the FIFO data for this packet into memory */
1330 while (bytes > 0) {
1331 *ptr++ = __cvmx_usb_read_csr32(usb, USB_FIFO_ADDRESS(channel, usb->index));
1332 bytes -= 4;
1333 }
1334 CVMX_SYNCW;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001335}
1336
1337
1338/**
1339 * Fill the TX hardware fifo with data out of the software
1340 * fifos
1341 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001342 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001343 * @fifo: Software fifo to use
1344 * @available: Amount of space in the hardware fifo
1345 *
1346 * Returns: Non zero if the hardware fifo was too small and needs
1347 * to be serviced again.
1348 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001349static int __cvmx_usb_fill_tx_hw(struct cvmx_usb_state *usb,
1350 struct cvmx_usb_tx_fifo *fifo, int available)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001351{
1352 /*
1353 * We're done either when there isn't anymore space or the software FIFO
1354 * is empty
1355 */
1356 while (available && (fifo->head != fifo->tail)) {
1357 int i = fifo->tail;
1358 const uint32_t *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001359 uint64_t csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel,
1360 usb->index) ^ 4;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001361 int words = available;
1362
1363 /* Limit the amount of data to waht the SW fifo has */
1364 if (fifo->entry[i].size <= available) {
1365 words = fifo->entry[i].size;
1366 fifo->tail++;
1367 if (fifo->tail > MAX_CHANNELS)
1368 fifo->tail = 0;
1369 }
1370
1371 /* Update the next locations and counts */
1372 available -= words;
1373 fifo->entry[i].address += words * 4;
1374 fifo->entry[i].size -= words;
1375
1376 /*
1377 * Write the HW fifo data. The read every three writes is due
1378 * to an errata on CN3XXX chips
1379 */
1380 while (words > 3) {
1381 cvmx_write64_uint32(csr_address, *ptr++);
1382 cvmx_write64_uint32(csr_address, *ptr++);
1383 cvmx_write64_uint32(csr_address, *ptr++);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001384 cvmx_read64_uint64(
1385 CVMX_USBNX_DMA0_INB_CHN0(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001386 words -= 3;
1387 }
1388 cvmx_write64_uint32(csr_address, *ptr++);
1389 if (--words) {
1390 cvmx_write64_uint32(csr_address, *ptr++);
1391 if (--words)
1392 cvmx_write64_uint32(csr_address, *ptr++);
1393 }
1394 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1395 }
1396 return fifo->head != fifo->tail;
1397}
1398
1399
1400/**
1401 * Check the hardware FIFOs and fill them as needed
1402 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001403 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001404 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001405static void __cvmx_usb_poll_tx_fifo(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001406{
1407 if (usb->periodic.head != usb->periodic.tail) {
1408 union cvmx_usbcx_hptxsts tx_status;
Paul Davies Cf5106432014-05-11 18:41:32 +05301409
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001410 tx_status.u32 = __cvmx_usb_read_csr32(usb,
1411 CVMX_USBCX_HPTXSTS(usb->index));
1412 if (__cvmx_usb_fill_tx_hw(usb, &usb->periodic,
1413 tx_status.s.ptxfspcavail))
1414 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1415 union cvmx_usbcx_gintmsk,
1416 ptxfempmsk, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001417 else
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001418 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1419 union cvmx_usbcx_gintmsk,
1420 ptxfempmsk, 0);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001421 }
1422
1423 if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1424 union cvmx_usbcx_gnptxsts tx_status;
Paul Davies Cf5106432014-05-11 18:41:32 +05301425
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001426 tx_status.u32 = __cvmx_usb_read_csr32(usb,
1427 CVMX_USBCX_GNPTXSTS(usb->index));
1428 if (__cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic,
1429 tx_status.s.nptxfspcavail))
1430 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1431 union cvmx_usbcx_gintmsk,
1432 nptxfempmsk, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001433 else
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001434 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1435 union cvmx_usbcx_gintmsk,
1436 nptxfempmsk, 0);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001437 }
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001438}
1439
1440
1441/**
1442 * Fill the TX FIFO with an outgoing packet
1443 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001444 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001445 * @channel: Channel number to get packet from
1446 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001447static void __cvmx_usb_fill_tx_fifo(struct cvmx_usb_state *usb, int channel)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001448{
1449 union cvmx_usbcx_hccharx hcchar;
1450 union cvmx_usbcx_hcspltx usbc_hcsplt;
1451 union cvmx_usbcx_hctsizx usbc_hctsiz;
1452 struct cvmx_usb_tx_fifo *fifo;
1453
1454 /* We only need to fill data on outbound channels */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001455 hcchar.u32 = __cvmx_usb_read_csr32(usb,
1456 CVMX_USBCX_HCCHARX(channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001457 if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1458 return;
1459
1460 /* OUT Splits only have data on the start and not the complete */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001461 usbc_hcsplt.u32 = __cvmx_usb_read_csr32(usb,
1462 CVMX_USBCX_HCSPLTX(channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001463 if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1464 return;
1465
1466 /*
1467 * Find out how many bytes we need to fill and convert it into 32bit
1468 * words.
1469 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001470 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
1471 CVMX_USBCX_HCTSIZX(channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001472 if (!usbc_hctsiz.s.xfersize)
1473 return;
1474
1475 if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1476 (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1477 fifo = &usb->periodic;
1478 else
1479 fifo = &usb->nonperiodic;
1480
1481 fifo->entry[fifo->head].channel = channel;
1482 fifo->entry[fifo->head].address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8);
1483 fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize+3)>>2;
1484 fifo->head++;
1485 if (fifo->head > MAX_CHANNELS)
1486 fifo->head = 0;
1487
1488 __cvmx_usb_poll_tx_fifo(usb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001489}
1490
1491/**
1492 * Perform channel specific setup for Control transactions. All
1493 * the generic stuff will already have been done in
1494 * __cvmx_usb_start_channel()
1495 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001496 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001497 * @channel: Channel to setup
1498 * @pipe: Pipe for control transaction
1499 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001500static void __cvmx_usb_start_channel_control(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001501 int channel,
1502 struct cvmx_usb_pipe *pipe)
1503{
Aaro Koskinen56696012013-10-10 23:25:36 +03001504 struct cvmx_usb_transaction *transaction =
1505 list_first_entry(&pipe->transactions, typeof(*transaction),
1506 node);
Aaro Koskinene301dfb2014-08-31 23:43:50 +03001507 struct usb_ctrlrequest *header =
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001508 cvmx_phys_to_ptr(transaction->control_header);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001509 int bytes_to_transfer = transaction->buffer_length -
1510 transaction->actual_bytes;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001511 int packets_to_transfer;
1512 union cvmx_usbcx_hctsizx usbc_hctsiz;
1513
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001514 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
1515 CVMX_USBCX_HCTSIZX(channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001516
1517 switch (transaction->stage) {
1518 case CVMX_USB_STAGE_NON_CONTROL:
1519 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
Joe Perchesf8628a42014-05-23 22:13:20 -07001520 cvmx_dprintf("%s: ERROR - Non control stage\n", __func__);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001521 break;
1522 case CVMX_USB_STAGE_SETUP:
1523 usbc_hctsiz.s.pid = 3; /* Setup */
1524 bytes_to_transfer = sizeof(*header);
1525 /* All Control operations start with a setup going OUT */
Paul Davies Ce725cef2014-05-11 18:41:33 +05301526 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1527 union cvmx_usbcx_hccharx, epdir,
1528 CVMX_USB_DIRECTION_OUT);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001529 /*
1530 * Setup send the control header instead of the buffer data. The
1531 * buffer data will be used in the next stage
1532 */
Paul Davies Ce725cef2014-05-11 18:41:33 +05301533 __cvmx_usb_write_csr64(usb,
1534 CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8,
1535 transaction->control_header);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001536 break;
1537 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1538 usbc_hctsiz.s.pid = 3; /* Setup */
1539 bytes_to_transfer = 0;
1540 /* All Control operations start with a setup going OUT */
Paul Davies Ce725cef2014-05-11 18:41:33 +05301541 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1542 union cvmx_usbcx_hccharx, epdir,
1543 CVMX_USB_DIRECTION_OUT);
1544
1545 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1546 union cvmx_usbcx_hcspltx, compsplt, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001547 break;
1548 case CVMX_USB_STAGE_DATA:
1549 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1550 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
Aaro Koskinen96ee2cc2014-08-31 23:43:51 +03001551 if (header->bRequestType & USB_DIR_IN)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001552 bytes_to_transfer = 0;
1553 else if (bytes_to_transfer > pipe->max_packet)
1554 bytes_to_transfer = pipe->max_packet;
1555 }
1556 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1557 union cvmx_usbcx_hccharx, epdir,
Aaro Koskinen96ee2cc2014-08-31 23:43:51 +03001558 ((header->bRequestType & USB_DIR_IN) ?
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001559 CVMX_USB_DIRECTION_IN :
1560 CVMX_USB_DIRECTION_OUT));
1561 break;
1562 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1563 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
Aaro Koskinen96ee2cc2014-08-31 23:43:51 +03001564 if (!(header->bRequestType & USB_DIR_IN))
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001565 bytes_to_transfer = 0;
1566 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1567 union cvmx_usbcx_hccharx, epdir,
Aaro Koskinen96ee2cc2014-08-31 23:43:51 +03001568 ((header->bRequestType & USB_DIR_IN) ?
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001569 CVMX_USB_DIRECTION_IN :
1570 CVMX_USB_DIRECTION_OUT));
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001571 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1572 union cvmx_usbcx_hcspltx, compsplt, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001573 break;
1574 case CVMX_USB_STAGE_STATUS:
1575 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1576 bytes_to_transfer = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001577 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1578 union cvmx_usbcx_hccharx, epdir,
Aaro Koskinen96ee2cc2014-08-31 23:43:51 +03001579 ((header->bRequestType & USB_DIR_IN) ?
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001580 CVMX_USB_DIRECTION_OUT :
1581 CVMX_USB_DIRECTION_IN));
1582 break;
1583 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1584 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1585 bytes_to_transfer = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001586 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1587 union cvmx_usbcx_hccharx, epdir,
Aaro Koskinen96ee2cc2014-08-31 23:43:51 +03001588 ((header->bRequestType & USB_DIR_IN) ?
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001589 CVMX_USB_DIRECTION_OUT :
1590 CVMX_USB_DIRECTION_IN));
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001591 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1592 union cvmx_usbcx_hcspltx, compsplt, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001593 break;
1594 }
1595
1596 /*
1597 * Make sure the transfer never exceeds the byte limit of the hardware.
1598 * Further bytes will be sent as continued transactions
1599 */
1600 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1601 /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1602 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1603 bytes_to_transfer *= pipe->max_packet;
1604 }
1605
1606 /*
1607 * Calculate the number of packets to transfer. If the length is zero
1608 * we still need to transfer one packet
1609 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001610 packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) /
1611 pipe->max_packet;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001612 if (packets_to_transfer == 0)
1613 packets_to_transfer = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001614 else if ((packets_to_transfer > 1) &&
1615 (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001616 /*
1617 * Limit to one packet when not using DMA. Channels must be
1618 * restarted between every packet for IN transactions, so there
1619 * is no reason to do multiple packets in a row
1620 */
1621 packets_to_transfer = 1;
1622 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1623 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1624 /*
1625 * Limit the number of packet and data transferred to what the
1626 * hardware can handle
1627 */
1628 packets_to_transfer = MAX_TRANSFER_PACKETS;
1629 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1630 }
1631
1632 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1633 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1634
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001635 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index),
1636 usbc_hctsiz.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001637}
1638
1639
1640/**
1641 * Start a channel to perform the pipe's head transaction
1642 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001643 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001644 * @channel: Channel to setup
1645 * @pipe: Pipe to start
1646 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001647static void __cvmx_usb_start_channel(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001648 int channel,
1649 struct cvmx_usb_pipe *pipe)
1650{
Aaro Koskinen56696012013-10-10 23:25:36 +03001651 struct cvmx_usb_transaction *transaction =
1652 list_first_entry(&pipe->transactions, typeof(*transaction),
1653 node);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001654
1655 /* Make sure all writes to the DMA region get flushed */
1656 CVMX_SYNCW;
1657
1658 /* Attach the channel to the pipe */
1659 usb->pipe_for_channel[channel] = pipe;
1660 pipe->channel = channel;
1661 pipe->flags |= __CVMX_USB_PIPE_FLAGS_SCHEDULED;
1662
1663 /* Mark this channel as in use */
1664 usb->idle_hardware_channels &= ~(1<<channel);
1665
1666 /* Enable the channel interrupt bits */
1667 {
1668 union cvmx_usbcx_hcintx usbc_hcint;
1669 union cvmx_usbcx_hcintmskx usbc_hcintmsk;
1670 union cvmx_usbcx_haintmsk usbc_haintmsk;
1671
1672 /* Clear all channel status bits */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001673 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb,
1674 CVMX_USBCX_HCINTX(channel, usb->index));
Paul Davies Ce725cef2014-05-11 18:41:33 +05301675
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001676 __cvmx_usb_write_csr32(usb,
1677 CVMX_USBCX_HCINTX(channel, usb->index),
1678 usbc_hcint.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001679
1680 usbc_hcintmsk.u32 = 0;
1681 usbc_hcintmsk.s.chhltdmsk = 1;
1682 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1683 /*
1684 * Channels need these extra interrupts when we aren't
1685 * in DMA mode.
1686 */
1687 usbc_hcintmsk.s.datatglerrmsk = 1;
1688 usbc_hcintmsk.s.frmovrunmsk = 1;
1689 usbc_hcintmsk.s.bblerrmsk = 1;
1690 usbc_hcintmsk.s.xacterrmsk = 1;
1691 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1692 /*
1693 * Splits don't generate xfercompl, so we need
1694 * ACK and NYET.
1695 */
1696 usbc_hcintmsk.s.nyetmsk = 1;
1697 usbc_hcintmsk.s.ackmsk = 1;
1698 }
1699 usbc_hcintmsk.s.nakmsk = 1;
1700 usbc_hcintmsk.s.stallmsk = 1;
1701 usbc_hcintmsk.s.xfercomplmsk = 1;
1702 }
1703 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), usbc_hcintmsk.u32);
1704
1705 /* Enable the channel interrupt to propagate */
Paul Davies Ce725cef2014-05-11 18:41:33 +05301706 usbc_haintmsk.u32 = __cvmx_usb_read_csr32(usb,
1707 CVMX_USBCX_HAINTMSK(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001708 usbc_haintmsk.s.haintmsk |= 1<<channel;
Paul Davies Ce725cef2014-05-11 18:41:33 +05301709 __cvmx_usb_write_csr32(usb,
1710 CVMX_USBCX_HAINTMSK(usb->index),
1711 usbc_haintmsk.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001712 }
1713
1714 /* Setup the locations the DMA engines use */
1715 {
Paul Davies Ce725cef2014-05-11 18:41:33 +05301716 uint64_t dma_address = transaction->buffer +
1717 transaction->actual_bytes;
Paul Davies Cf5106432014-05-11 18:41:32 +05301718
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001719 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
Paul Davies Ce725cef2014-05-11 18:41:33 +05301720 dma_address = transaction->buffer +
1721 transaction->iso_packets[0].offset +
1722 transaction->actual_bytes;
1723
1724 __cvmx_usb_write_csr64(usb,
1725 CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8,
1726 dma_address);
1727
1728 __cvmx_usb_write_csr64(usb,
1729 CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8,
1730 dma_address);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001731 }
1732
1733 /* Setup both the size of the transfer and the SPLIT characteristics */
1734 {
1735 union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
1736 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
1737 int packets_to_transfer;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001738 int bytes_to_transfer = transaction->buffer_length -
1739 transaction->actual_bytes;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001740
1741 /*
1742 * ISOCHRONOUS transactions store each individual transfer size
1743 * in the packet structure, not the global buffer_length
1744 */
1745 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001746 bytes_to_transfer =
1747 transaction->iso_packets[0].length -
1748 transaction->actual_bytes;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001749
1750 /*
1751 * We need to do split transactions when we are talking to non
1752 * high speed devices that are behind a high speed hub
1753 */
1754 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1755 /*
1756 * On the start split phase (stage is even) record the
1757 * frame number we will need to send the split complete.
1758 * We only store the lower two bits since the time ahead
1759 * can only be two frames
1760 */
1761 if ((transaction->stage&1) == 0) {
1762 if (transaction->type == CVMX_USB_TRANSFER_BULK)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001763 pipe->split_sc_frame =
1764 (usb->frame_number + 1) & 0x7f;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001765 else
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001766 pipe->split_sc_frame =
1767 (usb->frame_number + 2) & 0x7f;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001768 } else
1769 pipe->split_sc_frame = -1;
1770
1771 usbc_hcsplt.s.spltena = 1;
1772 usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1773 usbc_hcsplt.s.prtaddr = pipe->hub_port;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001774 usbc_hcsplt.s.compsplt = (transaction->stage ==
1775 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001776
1777 /*
1778 * SPLIT transactions can only ever transmit one data
1779 * packet so limit the transfer size to the max packet
1780 * size
1781 */
1782 if (bytes_to_transfer > pipe->max_packet)
1783 bytes_to_transfer = pipe->max_packet;
1784
1785 /*
1786 * ISOCHRONOUS OUT splits are unique in that they limit
1787 * data transfers to 188 byte chunks representing the
1788 * begin/middle/end of the data or all
1789 */
1790 if (!usbc_hcsplt.s.compsplt &&
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001791 (pipe->transfer_dir ==
1792 CVMX_USB_DIRECTION_OUT) &&
1793 (pipe->transfer_type ==
1794 CVMX_USB_TRANSFER_ISOCHRONOUS)) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001795 /*
1796 * Clear the split complete frame number as
1797 * there isn't going to be a split complete
1798 */
1799 pipe->split_sc_frame = -1;
1800 /*
1801 * See if we've started this transfer and sent
1802 * data
1803 */
1804 if (transaction->actual_bytes == 0) {
1805 /*
1806 * Nothing sent yet, this is either a
1807 * begin or the entire payload
1808 */
1809 if (bytes_to_transfer <= 188)
1810 /* Entire payload in one go */
1811 usbc_hcsplt.s.xactpos = 3;
1812 else
1813 /* First part of payload */
1814 usbc_hcsplt.s.xactpos = 2;
1815 } else {
1816 /*
1817 * Continuing the previous data, we must
1818 * either be in the middle or at the end
1819 */
1820 if (bytes_to_transfer <= 188)
1821 /* End of payload */
1822 usbc_hcsplt.s.xactpos = 1;
1823 else
1824 /* Middle of payload */
1825 usbc_hcsplt.s.xactpos = 0;
1826 }
1827 /*
1828 * Again, the transfer size is limited to 188
1829 * bytes
1830 */
1831 if (bytes_to_transfer > 188)
1832 bytes_to_transfer = 188;
1833 }
1834 }
1835
1836 /*
1837 * Make sure the transfer never exceeds the byte limit of the
1838 * hardware. Further bytes will be sent as continued
1839 * transactions
1840 */
1841 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1842 /*
1843 * Round MAX_TRANSFER_BYTES to a multiple of out packet
1844 * size
1845 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001846 bytes_to_transfer = MAX_TRANSFER_BYTES /
1847 pipe->max_packet;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001848 bytes_to_transfer *= pipe->max_packet;
1849 }
1850
1851 /*
1852 * Calculate the number of packets to transfer. If the length is
1853 * zero we still need to transfer one packet
1854 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001855 packets_to_transfer =
1856 (bytes_to_transfer + pipe->max_packet - 1) /
1857 pipe->max_packet;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001858 if (packets_to_transfer == 0)
1859 packets_to_transfer = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001860 else if ((packets_to_transfer > 1) &&
1861 (usb->init_flags &
1862 CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001863 /*
1864 * Limit to one packet when not using DMA. Channels must
1865 * be restarted between every packet for IN
1866 * transactions, so there is no reason to do multiple
1867 * packets in a row
1868 */
1869 packets_to_transfer = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001870 bytes_to_transfer = packets_to_transfer *
1871 pipe->max_packet;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001872 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1873 /*
1874 * Limit the number of packet and data transferred to
1875 * what the hardware can handle
1876 */
1877 packets_to_transfer = MAX_TRANSFER_PACKETS;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001878 bytes_to_transfer = packets_to_transfer *
1879 pipe->max_packet;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001880 }
1881
1882 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1883 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1884
1885 /* Update the DATA0/DATA1 toggle */
1886 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1887 /*
1888 * High speed pipes may need a hardware ping before they start
1889 */
1890 if (pipe->flags & __CVMX_USB_PIPE_FLAGS_NEED_PING)
1891 usbc_hctsiz.s.dopng = 1;
1892
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001893 __cvmx_usb_write_csr32(usb,
1894 CVMX_USBCX_HCSPLTX(channel, usb->index),
1895 usbc_hcsplt.u32);
1896 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel,
1897 usb->index), usbc_hctsiz.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001898 }
1899
1900 /* Setup the Host Channel Characteristics Register */
1901 {
1902 union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
1903
1904 /*
1905 * Set the startframe odd/even properly. This is only used for
1906 * periodic
1907 */
1908 usbc_hcchar.s.oddfrm = usb->frame_number&1;
1909
1910 /*
1911 * Set the number of back to back packets allowed by this
1912 * endpoint. Split transactions interpret "ec" as the number of
1913 * immediate retries of failure. These retries happen too
1914 * quickly, so we disable these entirely for splits
1915 */
1916 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1917 usbc_hcchar.s.ec = 1;
1918 else if (pipe->multi_count < 1)
1919 usbc_hcchar.s.ec = 1;
1920 else if (pipe->multi_count > 3)
1921 usbc_hcchar.s.ec = 3;
1922 else
1923 usbc_hcchar.s.ec = pipe->multi_count;
1924
1925 /* Set the rest of the endpoint specific settings */
1926 usbc_hcchar.s.devaddr = pipe->device_addr;
1927 usbc_hcchar.s.eptype = transaction->type;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001928 usbc_hcchar.s.lspddev =
1929 (pipe->device_speed == CVMX_USB_SPEED_LOW);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001930 usbc_hcchar.s.epdir = pipe->transfer_dir;
1931 usbc_hcchar.s.epnum = pipe->endpoint_num;
1932 usbc_hcchar.s.mps = pipe->max_packet;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001933 __cvmx_usb_write_csr32(usb,
1934 CVMX_USBCX_HCCHARX(channel, usb->index),
1935 usbc_hcchar.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001936 }
1937
1938 /* Do transaction type specific fixups as needed */
1939 switch (transaction->type) {
1940 case CVMX_USB_TRANSFER_CONTROL:
1941 __cvmx_usb_start_channel_control(usb, channel, pipe);
1942 break;
1943 case CVMX_USB_TRANSFER_BULK:
1944 case CVMX_USB_TRANSFER_INTERRUPT:
1945 break;
1946 case CVMX_USB_TRANSFER_ISOCHRONOUS:
1947 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
1948 /*
1949 * ISO transactions require different PIDs depending on
1950 * direction and how many packets are needed
1951 */
1952 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
1953 if (pipe->multi_count < 2) /* Need DATA0 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001954 USB_SET_FIELD32(
1955 CVMX_USBCX_HCTSIZX(channel,
1956 usb->index),
1957 union cvmx_usbcx_hctsizx,
1958 pid, 0);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001959 else /* Need MDATA */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001960 USB_SET_FIELD32(
1961 CVMX_USBCX_HCTSIZX(channel,
1962 usb->index),
1963 union cvmx_usbcx_hctsizx,
1964 pid, 3);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001965 }
1966 }
1967 break;
1968 }
1969 {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001970 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 =
1971 __cvmx_usb_read_csr32(usb,
1972 CVMX_USBCX_HCTSIZX(channel, usb->index))};
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001973 transaction->xfersize = usbc_hctsiz.s.xfersize;
1974 transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1975 }
1976 /* Remeber when we start a split transaction */
1977 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1978 usb->active_split = transaction;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001979 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1980 union cvmx_usbcx_hccharx, chena, 1);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001981 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1982 __cvmx_usb_fill_tx_fifo(usb, channel);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001983}
1984
1985
1986/**
1987 * Find a pipe that is ready to be scheduled to hardware.
Aaro Koskinencb61c602013-10-06 22:22:25 +03001988 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001989 * @list: Pipe list to search
1990 * @current_frame:
1991 * Frame counter to use as a time reference.
1992 *
1993 * Returns: Pipe or NULL if none are ready
1994 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02001995static struct cvmx_usb_pipe *__cvmx_usb_find_ready_pipe(
1996 struct cvmx_usb_state *usb,
1997 struct list_head *list,
1998 uint64_t current_frame)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001999{
Aaro Koskinen4a23ee12013-10-10 23:25:35 +03002000 struct cvmx_usb_pipe *pipe;
2001
2002 list_for_each_entry(pipe, list, node) {
Aaro Koskinen56696012013-10-10 23:25:36 +03002003 struct cvmx_usb_transaction *t =
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002004 list_first_entry(&pipe->transactions, typeof(*t),
2005 node);
Aaro Koskinen56696012013-10-10 23:25:36 +03002006 if (!(pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED) && t &&
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002007 (pipe->next_tx_frame <= current_frame) &&
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002008 ((pipe->split_sc_frame == -1) ||
2009 ((((int)current_frame - (int)pipe->split_sc_frame)
2010 & 0x7f) < 0x40)) &&
Aaro Koskinen56696012013-10-10 23:25:36 +03002011 (!usb->active_split || (usb->active_split == t))) {
Aaro Koskinen20f6b822014-05-11 14:15:20 +03002012 prefetch(t);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002013 return pipe;
2014 }
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002015 }
2016 return NULL;
2017}
2018
2019
2020/**
2021 * Called whenever a pipe might need to be scheduled to the
2022 * hardware.
2023 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002024 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002025 * @is_sof: True if this schedule was called on a SOF interrupt.
2026 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002027static void __cvmx_usb_schedule(struct cvmx_usb_state *usb, int is_sof)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002028{
2029 int channel;
2030 struct cvmx_usb_pipe *pipe;
2031 int need_sof;
2032 enum cvmx_usb_transfer ttype;
2033
2034 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2035 /*
2036 * Without DMA we need to be careful to not schedule something
2037 * at the end of a frame and cause an overrun.
2038 */
Paul Davies Ce725cef2014-05-11 18:41:33 +05302039 union cvmx_usbcx_hfnum hfnum = {
2040 .u32 = __cvmx_usb_read_csr32(usb,
2041 CVMX_USBCX_HFNUM(usb->index))
2042 };
2043
2044 union cvmx_usbcx_hfir hfir = {
2045 .u32 = __cvmx_usb_read_csr32(usb,
2046 CVMX_USBCX_HFIR(usb->index))
2047 };
Paul Davies Cf5106432014-05-11 18:41:32 +05302048
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002049 if (hfnum.s.frrem < hfir.s.frint/4)
2050 goto done;
2051 }
2052
2053 while (usb->idle_hardware_channels) {
2054 /* Find an idle channel */
2055 channel = __fls(usb->idle_hardware_channels);
2056 if (unlikely(channel > 7))
2057 break;
2058
2059 /* Find a pipe needing service */
2060 pipe = NULL;
2061 if (is_sof) {
2062 /*
2063 * Only process periodic pipes on SOF interrupts. This
2064 * way we are sure that the periodic data is sent in the
2065 * beginning of the frame
2066 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002067 pipe = __cvmx_usb_find_ready_pipe(usb,
2068 usb->active_pipes +
2069 CVMX_USB_TRANSFER_ISOCHRONOUS,
2070 usb->frame_number);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002071 if (likely(!pipe))
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002072 pipe = __cvmx_usb_find_ready_pipe(usb,
2073 usb->active_pipes +
2074 CVMX_USB_TRANSFER_INTERRUPT,
2075 usb->frame_number);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002076 }
2077 if (likely(!pipe)) {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002078 pipe = __cvmx_usb_find_ready_pipe(usb,
2079 usb->active_pipes +
2080 CVMX_USB_TRANSFER_CONTROL,
2081 usb->frame_number);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002082 if (likely(!pipe))
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002083 pipe = __cvmx_usb_find_ready_pipe(usb,
2084 usb->active_pipes +
2085 CVMX_USB_TRANSFER_BULK,
2086 usb->frame_number);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002087 }
2088 if (!pipe)
2089 break;
2090
2091 __cvmx_usb_start_channel(usb, channel, pipe);
2092 }
2093
2094done:
2095 /*
2096 * Only enable SOF interrupts when we have transactions pending in the
2097 * future that might need to be scheduled
2098 */
2099 need_sof = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002100 for (ttype = CVMX_USB_TRANSFER_CONTROL;
2101 ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
Aaro Koskinen4a23ee12013-10-10 23:25:35 +03002102 list_for_each_entry(pipe, &usb->active_pipes[ttype], node) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002103 if (pipe->next_tx_frame > usb->frame_number) {
2104 need_sof = 1;
2105 break;
2106 }
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002107 }
2108 }
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002109 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
2110 union cvmx_usbcx_gintmsk, sofmsk, need_sof);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002111}
2112
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002113static inline struct octeon_hcd *cvmx_usb_to_octeon(struct cvmx_usb_state *p)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002114{
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002115 return container_of(p, struct octeon_hcd, usb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002116}
2117
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002118static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
2119{
2120 return container_of((void *)p, struct usb_hcd, hcd_priv);
2121}
2122
2123static void octeon_usb_urb_complete_callback(struct cvmx_usb_state *usb,
2124 enum cvmx_usb_complete status,
Aaro Koskinen60f81502013-10-06 22:22:35 +03002125 struct cvmx_usb_pipe *pipe,
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002126 struct cvmx_usb_transaction
2127 *transaction,
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002128 int bytes_transferred,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002129 struct urb *urb)
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002130{
2131 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
2132 struct usb_hcd *hcd = octeon_to_hcd(priv);
2133 struct device *dev = hcd->self.controller;
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002134
Aaro Koskinen8dcf4ec2014-06-29 22:52:55 +03002135 if (likely(status == CVMX_USB_COMPLETE_SUCCESS))
2136 urb->actual_length = bytes_transferred;
2137 else
2138 urb->actual_length = 0;
2139
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002140 urb->hcpriv = NULL;
2141
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002142 /* For Isochronous transactions we need to update the URB packet status
2143 list from data in our private copy */
2144 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2145 int i;
2146 /*
2147 * The pointer to the private list is stored in the setup_packet
2148 * field.
2149 */
2150 struct cvmx_usb_iso_packet *iso_packet =
2151 (struct cvmx_usb_iso_packet *) urb->setup_packet;
2152 /* Recalculate the transfer size by adding up each packet */
2153 urb->actual_length = 0;
2154 for (i = 0; i < urb->number_of_packets; i++) {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002155 if (iso_packet[i].status ==
2156 CVMX_USB_COMPLETE_SUCCESS) {
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002157 urb->iso_frame_desc[i].status = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002158 urb->iso_frame_desc[i].actual_length =
2159 iso_packet[i].length;
2160 urb->actual_length +=
2161 urb->iso_frame_desc[i].actual_length;
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002162 } else {
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002163 dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%p transaction=%p size=%d\n",
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002164 i, urb->number_of_packets,
Aaro Koskinen60f81502013-10-06 22:22:35 +03002165 iso_packet[i].status, pipe,
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002166 transaction, iso_packet[i].length);
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002167 urb->iso_frame_desc[i].status = -EREMOTEIO;
2168 }
2169 }
2170 /* Free the private list now that we don't need it anymore */
2171 kfree(iso_packet);
2172 urb->setup_packet = NULL;
2173 }
2174
2175 switch (status) {
2176 case CVMX_USB_COMPLETE_SUCCESS:
2177 urb->status = 0;
2178 break;
2179 case CVMX_USB_COMPLETE_CANCEL:
2180 if (urb->status == 0)
2181 urb->status = -ENOENT;
2182 break;
2183 case CVMX_USB_COMPLETE_STALL:
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002184 dev_dbg(dev, "status=stall pipe=%p transaction=%p size=%d\n",
2185 pipe, transaction, bytes_transferred);
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002186 urb->status = -EPIPE;
2187 break;
2188 case CVMX_USB_COMPLETE_BABBLEERR:
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002189 dev_dbg(dev, "status=babble pipe=%p transaction=%p size=%d\n",
2190 pipe, transaction, bytes_transferred);
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002191 urb->status = -EPIPE;
2192 break;
2193 case CVMX_USB_COMPLETE_SHORT:
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002194 dev_dbg(dev, "status=short pipe=%p transaction=%p size=%d\n",
2195 pipe, transaction, bytes_transferred);
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002196 urb->status = -EREMOTEIO;
2197 break;
2198 case CVMX_USB_COMPLETE_ERROR:
2199 case CVMX_USB_COMPLETE_XACTERR:
2200 case CVMX_USB_COMPLETE_DATATGLERR:
2201 case CVMX_USB_COMPLETE_FRAMEERR:
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002202 dev_dbg(dev, "status=%d pipe=%p transaction=%p size=%d\n",
2203 status, pipe, transaction, bytes_transferred);
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002204 urb->status = -EPROTO;
2205 break;
2206 }
Aaro Koskinene5b90892014-06-29 22:52:53 +03002207 usb_hcd_unlink_urb_from_ep(octeon_to_hcd(priv), urb);
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002208 spin_unlock(&priv->lock);
2209 usb_hcd_giveback_urb(octeon_to_hcd(priv), urb, urb->status);
2210 spin_lock(&priv->lock);
2211}
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002212
2213/**
2214 * Signal the completion of a transaction and free it. The
2215 * transaction will be removed from the pipe transaction list.
2216 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002217 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002218 * @pipe: Pipe the transaction is on
2219 * @transaction:
2220 * Transaction that completed
2221 * @complete_code:
2222 * Completion code
2223 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002224static void __cvmx_usb_perform_complete(
2225 struct cvmx_usb_state *usb,
2226 struct cvmx_usb_pipe *pipe,
2227 struct cvmx_usb_transaction *transaction,
2228 enum cvmx_usb_complete complete_code)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002229{
2230 /* If this was a split then clear our split in progress marker */
2231 if (usb->active_split == transaction)
2232 usb->active_split = NULL;
2233
2234 /*
2235 * Isochronous transactions need extra processing as they might not be
2236 * done after a single data transfer
2237 */
2238 if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
2239 /* Update the number of bytes transferred in this ISO packet */
2240 transaction->iso_packets[0].length = transaction->actual_bytes;
2241 transaction->iso_packets[0].status = complete_code;
2242
2243 /*
2244 * If there are more ISOs pending and we succeeded, schedule the
2245 * next one
2246 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002247 if ((transaction->iso_number_packets > 1) &&
2248 (complete_code == CVMX_USB_COMPLETE_SUCCESS)) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002249 /* No bytes transferred for this packet as of yet */
2250 transaction->actual_bytes = 0;
2251 /* One less ISO waiting to transfer */
2252 transaction->iso_number_packets--;
2253 /* Increment to the next location in our packet array */
2254 transaction->iso_packets++;
2255 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
Nitin Kuppelur15ef0cc2014-09-10 03:36:24 +02002256 return;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002257 }
2258 }
2259
2260 /* Remove the transaction from the pipe list */
Aaro Koskinen56696012013-10-10 23:25:36 +03002261 list_del(&transaction->node);
2262 if (list_empty(&pipe->transactions))
Aaro Koskinen4a23ee12013-10-10 23:25:35 +03002263 list_move_tail(&pipe->node, &usb->idle_pipes);
Aaro Koskinen60f81502013-10-06 22:22:35 +03002264 octeon_usb_urb_complete_callback(usb, complete_code, pipe,
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002265 transaction,
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002266 transaction->actual_bytes,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002267 transaction->urb);
Aaro Koskinena2dfef02013-10-10 23:25:31 +03002268 kfree(transaction);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002269}
2270
2271
2272/**
2273 * Submit a usb transaction to a pipe. Called for all types
2274 * of transactions.
2275 *
2276 * @usb:
Aaro Koskinen60f81502013-10-06 22:22:35 +03002277 * @pipe: Which pipe to submit to.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002278 * @type: Transaction type
2279 * @buffer: User buffer for the transaction
2280 * @buffer_length:
2281 * User buffer's length in bytes
2282 * @control_header:
2283 * For control transactions, the 8 byte standard header
2284 * @iso_start_frame:
2285 * For ISO transactions, the start frame
2286 * @iso_number_packets:
2287 * For ISO, the number of packet in the transaction.
2288 * @iso_packets:
2289 * A description of each ISO packet
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002290 * @urb: URB for the callback
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002291 *
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002292 * Returns: Transaction or NULL on failure.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002293 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002294static struct cvmx_usb_transaction *__cvmx_usb_submit_transaction(
2295 struct cvmx_usb_state *usb,
2296 struct cvmx_usb_pipe *pipe,
2297 enum cvmx_usb_transfer type,
2298 uint64_t buffer,
2299 int buffer_length,
2300 uint64_t control_header,
2301 int iso_start_frame,
2302 int iso_number_packets,
2303 struct cvmx_usb_iso_packet *iso_packets,
2304 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002305{
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002306 struct cvmx_usb_transaction *transaction;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002307
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002308 if (unlikely(pipe->transfer_type != type))
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002309 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002310
Aaro Koskinena2dfef02013-10-10 23:25:31 +03002311 transaction = kzalloc(sizeof(*transaction), GFP_ATOMIC);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002312 if (unlikely(!transaction))
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002313 return NULL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002314
2315 transaction->type = type;
2316 transaction->buffer = buffer;
2317 transaction->buffer_length = buffer_length;
2318 transaction->control_header = control_header;
2319 /* FIXME: This is not used, implement it. */
2320 transaction->iso_start_frame = iso_start_frame;
2321 transaction->iso_number_packets = iso_number_packets;
2322 transaction->iso_packets = iso_packets;
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002323 transaction->urb = urb;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002324 if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2325 transaction->stage = CVMX_USB_STAGE_SETUP;
2326 else
2327 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2328
Aaro Koskinen56696012013-10-10 23:25:36 +03002329 if (!list_empty(&pipe->transactions)) {
2330 list_add_tail(&transaction->node, &pipe->transactions);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002331 } else {
Aaro Koskinen56696012013-10-10 23:25:36 +03002332 list_add_tail(&transaction->node, &pipe->transactions);
Aaro Koskinen4a23ee12013-10-10 23:25:35 +03002333 list_move_tail(&pipe->node,
2334 &usb->active_pipes[pipe->transfer_type]);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002335
Aaro Koskinen56696012013-10-10 23:25:36 +03002336 /*
2337 * We may need to schedule the pipe if this was the head of the
2338 * pipe.
2339 */
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002340 __cvmx_usb_schedule(usb, 0);
Aaro Koskinen56696012013-10-10 23:25:36 +03002341 }
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002342
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002343 return transaction;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002344}
2345
2346
2347/**
2348 * Call to submit a USB Bulk transfer to a pipe.
2349 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002350 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen60f81502013-10-06 22:22:35 +03002351 * @pipe: Handle to the pipe for the transfer.
Aaro Koskinen9ccca702013-10-06 22:22:30 +03002352 * @urb: URB.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002353 *
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002354 * Returns: A submitted transaction or NULL on failure.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002355 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002356static struct cvmx_usb_transaction *cvmx_usb_submit_bulk(
2357 struct cvmx_usb_state *usb,
2358 struct cvmx_usb_pipe *pipe,
2359 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002360{
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002361 return __cvmx_usb_submit_transaction(usb, pipe, CVMX_USB_TRANSFER_BULK,
2362 urb->transfer_dma,
2363 urb->transfer_buffer_length,
2364 0, /* control_header */
2365 0, /* iso_start_frame */
2366 0, /* iso_number_packets */
2367 NULL, /* iso_packets */
2368 urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002369}
2370
2371
2372/**
2373 * Call to submit a USB Interrupt transfer to a pipe.
2374 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002375 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen60f81502013-10-06 22:22:35 +03002376 * @pipe: Handle to the pipe for the transfer.
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002377 * @urb: URB returned when the callback is called.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002378 *
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002379 * Returns: A submitted transaction or NULL on failure.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002380 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002381static struct cvmx_usb_transaction *cvmx_usb_submit_interrupt(
2382 struct cvmx_usb_state *usb,
2383 struct cvmx_usb_pipe *pipe,
2384 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002385{
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002386 return __cvmx_usb_submit_transaction(usb, pipe,
2387 CVMX_USB_TRANSFER_INTERRUPT,
2388 urb->transfer_dma,
2389 urb->transfer_buffer_length,
2390 0, /* control_header */
2391 0, /* iso_start_frame */
2392 0, /* iso_number_packets */
2393 NULL, /* iso_packets */
2394 urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002395}
2396
2397
2398/**
2399 * Call to submit a USB Control transfer to a pipe.
2400 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002401 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen60f81502013-10-06 22:22:35 +03002402 * @pipe: Handle to the pipe for the transfer.
Aaro Koskinen2ae09e82013-10-06 22:22:31 +03002403 * @urb: URB.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002404 *
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002405 * Returns: A submitted transaction or NULL on failure.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002406 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002407static struct cvmx_usb_transaction *cvmx_usb_submit_control(
2408 struct cvmx_usb_state *usb,
2409 struct cvmx_usb_pipe *pipe,
2410 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002411{
Aaro Koskinen2ae09e82013-10-06 22:22:31 +03002412 int buffer_length = urb->transfer_buffer_length;
2413 uint64_t control_header = urb->setup_dma;
Aaro Koskinene301dfb2014-08-31 23:43:50 +03002414 struct usb_ctrlrequest *header = cvmx_phys_to_ptr(control_header);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002415
Aaro Koskinen96ee2cc2014-08-31 23:43:51 +03002416 if ((header->bRequestType & USB_DIR_IN) == 0)
Aaro Koskinene301dfb2014-08-31 23:43:50 +03002417 buffer_length = le16_to_cpu(header->wLength);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002418
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002419 return __cvmx_usb_submit_transaction(usb, pipe,
2420 CVMX_USB_TRANSFER_CONTROL,
2421 urb->transfer_dma, buffer_length,
2422 control_header,
2423 0, /* iso_start_frame */
2424 0, /* iso_number_packets */
2425 NULL, /* iso_packets */
2426 urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002427}
2428
2429
2430/**
2431 * Call to submit a USB Isochronous transfer to a pipe.
2432 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002433 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen60f81502013-10-06 22:22:35 +03002434 * @pipe: Handle to the pipe for the transfer.
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002435 * @urb: URB returned when the callback is called.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002436 *
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002437 * Returns: A submitted transaction or NULL on failure.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002438 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002439static struct cvmx_usb_transaction *cvmx_usb_submit_isochronous(
2440 struct cvmx_usb_state *usb,
2441 struct cvmx_usb_pipe *pipe,
2442 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002443{
Aaro Koskinene16b5e32013-10-06 22:22:33 +03002444 struct cvmx_usb_iso_packet *packets;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002445
Aaro Koskinene16b5e32013-10-06 22:22:33 +03002446 packets = (struct cvmx_usb_iso_packet *) urb->setup_packet;
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002447 return __cvmx_usb_submit_transaction(usb, pipe,
2448 CVMX_USB_TRANSFER_ISOCHRONOUS,
2449 urb->transfer_dma,
2450 urb->transfer_buffer_length,
2451 0, /* control_header */
2452 urb->start_frame,
2453 urb->number_of_packets,
2454 packets, urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002455}
2456
2457
2458/**
2459 * Cancel one outstanding request in a pipe. Canceling a request
2460 * can fail if the transaction has already completed before cancel
2461 * is called. Even after a successful cancel call, it may take
2462 * a frame or two for the cvmx_usb_poll() function to call the
2463 * associated callback.
2464 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002465 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen60f81502013-10-06 22:22:35 +03002466 * @pipe: Pipe to cancel requests in.
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002467 * @transaction: Transaction to cancel, returned by the submit function.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002468 *
2469 * Returns: 0 or a negative error code.
2470 */
Aaro Koskinen60f81502013-10-06 22:22:35 +03002471static int cvmx_usb_cancel(struct cvmx_usb_state *usb,
2472 struct cvmx_usb_pipe *pipe,
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03002473 struct cvmx_usb_transaction *transaction)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002474{
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002475 /*
2476 * If the transaction is the HEAD of the queue and scheduled. We need to
2477 * treat it special
2478 */
Aaro Koskinen56696012013-10-10 23:25:36 +03002479 if (list_first_entry(&pipe->transactions, typeof(*transaction), node) ==
2480 transaction && (pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002481 union cvmx_usbcx_hccharx usbc_hcchar;
2482
2483 usb->pipe_for_channel[pipe->channel] = NULL;
2484 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2485
2486 CVMX_SYNCW;
2487
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002488 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
2489 CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002490 /*
2491 * If the channel isn't enabled then the transaction already
2492 * completed.
2493 */
2494 if (usbc_hcchar.s.chena) {
2495 usbc_hcchar.s.chdis = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002496 __cvmx_usb_write_csr32(usb,
2497 CVMX_USBCX_HCCHARX(pipe->channel,
2498 usb->index),
2499 usbc_hcchar.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002500 }
2501 }
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002502 __cvmx_usb_perform_complete(usb, pipe, transaction,
2503 CVMX_USB_COMPLETE_CANCEL);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002504 return 0;
2505}
2506
2507
2508/**
2509 * Cancel all outstanding requests in a pipe. Logically all this
2510 * does is call cvmx_usb_cancel() in a loop.
2511 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002512 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen60f81502013-10-06 22:22:35 +03002513 * @pipe: Pipe to cancel requests in.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002514 *
2515 * Returns: 0 or a negative error code.
2516 */
Aaro Koskinen60f81502013-10-06 22:22:35 +03002517static int cvmx_usb_cancel_all(struct cvmx_usb_state *usb,
2518 struct cvmx_usb_pipe *pipe)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002519{
Aaro Koskinen56696012013-10-10 23:25:36 +03002520 struct cvmx_usb_transaction *transaction, *next;
2521
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002522 /* Simply loop through and attempt to cancel each transaction */
Aaro Koskinen56696012013-10-10 23:25:36 +03002523 list_for_each_entry_safe(transaction, next, &pipe->transactions, node) {
2524 int result = cvmx_usb_cancel(usb, pipe, transaction);
Paul Davies Cf5106432014-05-11 18:41:32 +05302525
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002526 if (unlikely(result != 0))
2527 return result;
2528 }
2529 return 0;
2530}
2531
2532
2533/**
2534 * Close a pipe created with cvmx_usb_open_pipe().
2535 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002536 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen60f81502013-10-06 22:22:35 +03002537 * @pipe: Pipe to close.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002538 *
2539 * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
2540 * outstanding transfers.
2541 */
Aaro Koskinen60f81502013-10-06 22:22:35 +03002542static int cvmx_usb_close_pipe(struct cvmx_usb_state *usb,
2543 struct cvmx_usb_pipe *pipe)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002544{
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002545 /* Fail if the pipe has pending transactions */
Aaro Koskinen56696012013-10-10 23:25:36 +03002546 if (!list_empty(&pipe->transactions))
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002547 return -EBUSY;
2548
Aaro Koskinen4a23ee12013-10-10 23:25:35 +03002549 list_del(&pipe->node);
Aaro Koskinend2695a82013-10-10 23:25:32 +03002550 kfree(pipe);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002551
2552 return 0;
2553}
2554
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002555/**
2556 * Get the current USB protocol level frame number. The frame
2557 * number is always in the range of 0-0x7ff.
2558 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002559 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002560 *
2561 * Returns: USB frame number
2562 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002563static int cvmx_usb_get_frame_number(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002564{
2565 int frame_number;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002566 union cvmx_usbcx_hfnum usbc_hfnum;
2567
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002568 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb,
2569 CVMX_USBCX_HFNUM(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002570 frame_number = usbc_hfnum.s.frnum;
2571
2572 return frame_number;
2573}
2574
2575
2576/**
2577 * Poll a channel for status
2578 *
2579 * @usb: USB device
2580 * @channel: Channel to poll
2581 *
2582 * Returns: Zero on success
2583 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002584static int __cvmx_usb_poll_channel(struct cvmx_usb_state *usb, int channel)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002585{
2586 union cvmx_usbcx_hcintx usbc_hcint;
2587 union cvmx_usbcx_hctsizx usbc_hctsiz;
2588 union cvmx_usbcx_hccharx usbc_hcchar;
2589 struct cvmx_usb_pipe *pipe;
2590 struct cvmx_usb_transaction *transaction;
2591 int bytes_this_transfer;
2592 int bytes_in_last_packet;
2593 int packets_processed;
2594 int buffer_space_left;
2595
2596 /* Read the interrupt status bits for the channel */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002597 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb,
2598 CVMX_USBCX_HCINTX(channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002599
2600 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002601 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
2602 CVMX_USBCX_HCCHARX(channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002603
2604 if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2605 /*
2606 * There seems to be a bug in CN31XX which can cause
2607 * interrupt IN transfers to get stuck until we do a
2608 * write of HCCHARX without changing things
2609 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002610 __cvmx_usb_write_csr32(usb,
2611 CVMX_USBCX_HCCHARX(channel,
2612 usb->index),
2613 usbc_hcchar.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002614 return 0;
2615 }
2616
2617 /*
2618 * In non DMA mode the channels don't halt themselves. We need
2619 * to manually disable channels that are left running
2620 */
2621 if (!usbc_hcint.s.chhltd) {
2622 if (usbc_hcchar.s.chena) {
2623 union cvmx_usbcx_hcintmskx hcintmsk;
2624 /* Disable all interrupts except CHHLTD */
2625 hcintmsk.u32 = 0;
2626 hcintmsk.s.chhltdmsk = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002627 __cvmx_usb_write_csr32(usb,
2628 CVMX_USBCX_HCINTMSKX(channel,
2629 usb->index),
2630 hcintmsk.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002631 usbc_hcchar.s.chdis = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002632 __cvmx_usb_write_csr32(usb,
2633 CVMX_USBCX_HCCHARX(channel,
2634 usb->index),
2635 usbc_hcchar.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002636 return 0;
2637 } else if (usbc_hcint.s.xfercompl) {
2638 /*
2639 * Successful IN/OUT with transfer complete.
2640 * Channel halt isn't needed.
2641 */
2642 } else {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002643 cvmx_dprintf("USB%d: Channel %d interrupt without halt\n",
2644 usb->index, channel);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002645 return 0;
2646 }
2647 }
2648 } else {
2649 /*
2650 * There is are no interrupts that we need to process when the
2651 * channel is still running
2652 */
2653 if (!usbc_hcint.s.chhltd)
2654 return 0;
2655 }
2656
2657 /* Disable the channel interrupts now that it is done */
Paul Davies Ce725cef2014-05-11 18:41:33 +05302658 __cvmx_usb_write_csr32(usb,
2659 CVMX_USBCX_HCINTMSKX(channel, usb->index),
2660 0);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002661 usb->idle_hardware_channels |= (1<<channel);
2662
2663 /* Make sure this channel is tied to a valid pipe */
2664 pipe = usb->pipe_for_channel[channel];
Aaro Koskinen20f6b822014-05-11 14:15:20 +03002665 prefetch(pipe);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002666 if (!pipe)
2667 return 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002668 transaction = list_first_entry(&pipe->transactions,
2669 typeof(*transaction),
Aaro Koskinen56696012013-10-10 23:25:36 +03002670 node);
Aaro Koskinen20f6b822014-05-11 14:15:20 +03002671 prefetch(transaction);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002672
2673 /*
2674 * Disconnect this pipe from the HW channel. Later the schedule
2675 * function will figure out which pipe needs to go
2676 */
2677 usb->pipe_for_channel[channel] = NULL;
2678 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2679
2680 /*
2681 * Read the channel config info so we can figure out how much data
2682 * transfered
2683 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002684 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb,
2685 CVMX_USBCX_HCCHARX(channel, usb->index));
2686 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb,
2687 CVMX_USBCX_HCTSIZX(channel, usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002688
2689 /*
2690 * Calculating the number of bytes successfully transferred is dependent
2691 * on the transfer direction
2692 */
2693 packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2694 if (usbc_hcchar.s.epdir) {
2695 /*
2696 * IN transactions are easy. For every byte received the
2697 * hardware decrements xfersize. All we need to do is subtract
2698 * the current value of xfersize from its starting value and we
2699 * know how many bytes were written to the buffer
2700 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002701 bytes_this_transfer = transaction->xfersize -
2702 usbc_hctsiz.s.xfersize;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002703 } else {
2704 /*
2705 * OUT transaction don't decrement xfersize. Instead pktcnt is
2706 * decremented on every successful packet send. The hardware
2707 * does this when it receives an ACK, or NYET. If it doesn't
2708 * receive one of these responses pktcnt doesn't change
2709 */
2710 bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2711 /*
2712 * The last packet may not be a full transfer if we didn't have
2713 * enough data
2714 */
2715 if (bytes_this_transfer > transaction->xfersize)
2716 bytes_this_transfer = transaction->xfersize;
2717 }
2718 /* Figure out how many bytes were in the last packet of the transfer */
2719 if (packets_processed)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002720 bytes_in_last_packet = bytes_this_transfer -
2721 (packets_processed - 1) * usbc_hcchar.s.mps;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002722 else
2723 bytes_in_last_packet = bytes_this_transfer;
2724
2725 /*
2726 * As a special case, setup transactions output the setup header, not
2727 * the user's data. For this reason we don't count setup data as bytes
2728 * transferred
2729 */
2730 if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2731 (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2732 bytes_this_transfer = 0;
2733
2734 /*
2735 * Add the bytes transferred to the running total. It is important that
2736 * bytes_this_transfer doesn't count any data that needs to be
2737 * retransmitted
2738 */
2739 transaction->actual_bytes += bytes_this_transfer;
2740 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002741 buffer_space_left = transaction->iso_packets[0].length -
2742 transaction->actual_bytes;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002743 else
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002744 buffer_space_left = transaction->buffer_length -
2745 transaction->actual_bytes;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002746
2747 /*
2748 * We need to remember the PID toggle state for the next transaction.
2749 * The hardware already updated it for the next transaction
2750 */
2751 pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2752
2753 /*
2754 * For high speed bulk out, assume the next transaction will need to do
2755 * a ping before proceeding. If this isn't true the ACK processing below
2756 * will clear this flag
2757 */
2758 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2759 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2760 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2761 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
2762
2763 if (usbc_hcint.s.stall) {
2764 /*
2765 * STALL as a response means this transaction cannot be
2766 * completed because the device can't process transactions. Tell
2767 * the user. Any data that was transferred will be counted on
2768 * the actual bytes transferred
2769 */
2770 pipe->pid_toggle = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002771 __cvmx_usb_perform_complete(usb, pipe, transaction,
2772 CVMX_USB_COMPLETE_STALL);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002773 } else if (usbc_hcint.s.xacterr) {
2774 /*
2775 * We know at least one packet worked if we get a ACK or NAK.
2776 * Reset the retry counter
2777 */
2778 if (usbc_hcint.s.nak || usbc_hcint.s.ack)
2779 transaction->retries = 0;
2780 transaction->retries++;
2781 if (transaction->retries > MAX_RETRIES) {
2782 /*
2783 * XactErr as a response means the device signaled
2784 * something wrong with the transfer. For example, PID
2785 * toggle errors cause these
2786 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002787 __cvmx_usb_perform_complete(usb, pipe, transaction,
2788 CVMX_USB_COMPLETE_XACTERR);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002789 } else {
2790 /*
2791 * If this was a split then clear our split in progress
2792 * marker
2793 */
2794 if (usb->active_split == transaction)
2795 usb->active_split = NULL;
2796 /*
2797 * Rewind to the beginning of the transaction by anding
2798 * off the split complete bit
2799 */
2800 transaction->stage &= ~1;
2801 pipe->split_sc_frame = -1;
2802 pipe->next_tx_frame += pipe->interval;
2803 if (pipe->next_tx_frame < usb->frame_number)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002804 pipe->next_tx_frame =
2805 usb->frame_number + pipe->interval -
2806 (usb->frame_number -
2807 pipe->next_tx_frame) % pipe->interval;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002808 }
2809 } else if (usbc_hcint.s.bblerr) {
2810 /* Babble Error (BblErr) */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002811 __cvmx_usb_perform_complete(usb, pipe, transaction,
2812 CVMX_USB_COMPLETE_BABBLEERR);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002813 } else if (usbc_hcint.s.datatglerr) {
Aaro Koskinend8c39d32014-06-29 22:52:54 +03002814 /* Data toggle error */
2815 __cvmx_usb_perform_complete(usb, pipe, transaction,
2816 CVMX_USB_COMPLETE_DATATGLERR);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002817 } else if (usbc_hcint.s.nyet) {
2818 /*
2819 * NYET as a response is only allowed in three cases: as a
2820 * response to a ping, as a response to a split transaction, and
2821 * as a response to a bulk out. The ping case is handled by
2822 * hardware, so we only have splits and bulk out
2823 */
2824 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
2825 transaction->retries = 0;
2826 /*
2827 * If there is more data to go then we need to try
2828 * again. Otherwise this transaction is complete
2829 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002830 if ((buffer_space_left == 0) ||
2831 (bytes_in_last_packet < pipe->max_packet))
2832 __cvmx_usb_perform_complete(usb, pipe,
2833 transaction,
2834 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002835 } else {
2836 /*
2837 * Split transactions retry the split complete 4 times
2838 * then rewind to the start split and do the entire
2839 * transactions again
2840 */
2841 transaction->retries++;
2842 if ((transaction->retries & 0x3) == 0) {
2843 /*
2844 * Rewind to the beginning of the transaction by
2845 * anding off the split complete bit
2846 */
2847 transaction->stage &= ~1;
2848 pipe->split_sc_frame = -1;
2849 }
2850 }
2851 } else if (usbc_hcint.s.ack) {
2852 transaction->retries = 0;
2853 /*
2854 * The ACK bit can only be checked after the other error bits.
2855 * This is because a multi packet transfer may succeed in a
2856 * number of packets and then get a different response on the
2857 * last packet. In this case both ACK and the last response bit
2858 * will be set. If none of the other response bits is set, then
2859 * the last packet must have been an ACK
2860 *
2861 * Since we got an ACK, we know we don't need to do a ping on
2862 * this pipe
2863 */
2864 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_NEED_PING;
2865
2866 switch (transaction->type) {
2867 case CVMX_USB_TRANSFER_CONTROL:
2868 switch (transaction->stage) {
2869 case CVMX_USB_STAGE_NON_CONTROL:
2870 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2871 /* This should be impossible */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002872 __cvmx_usb_perform_complete(usb, pipe,
2873 transaction, CVMX_USB_COMPLETE_ERROR);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002874 break;
2875 case CVMX_USB_STAGE_SETUP:
2876 pipe->pid_toggle = 1;
2877 if (__cvmx_usb_pipe_needs_split(usb, pipe))
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002878 transaction->stage =
2879 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002880 else {
Aaro Koskinene301dfb2014-08-31 23:43:50 +03002881 struct usb_ctrlrequest *header =
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002882 cvmx_phys_to_ptr(transaction->control_header);
Aaro Koskinene301dfb2014-08-31 23:43:50 +03002883 if (header->wLength)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002884 transaction->stage = CVMX_USB_STAGE_DATA;
2885 else
2886 transaction->stage = CVMX_USB_STAGE_STATUS;
2887 }
2888 break;
2889 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2890 {
Aaro Koskinene301dfb2014-08-31 23:43:50 +03002891 struct usb_ctrlrequest *header =
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002892 cvmx_phys_to_ptr(transaction->control_header);
Aaro Koskinene301dfb2014-08-31 23:43:50 +03002893 if (header->wLength)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002894 transaction->stage = CVMX_USB_STAGE_DATA;
2895 else
2896 transaction->stage = CVMX_USB_STAGE_STATUS;
2897 }
2898 break;
2899 case CVMX_USB_STAGE_DATA:
2900 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002901 transaction->stage =
2902 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002903 /*
2904 * For setup OUT data that are splits,
2905 * the hardware doesn't appear to count
2906 * transferred data. Here we manually
2907 * update the data transferred
2908 */
2909 if (!usbc_hcchar.s.epdir) {
2910 if (buffer_space_left < pipe->max_packet)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002911 transaction->actual_bytes +=
2912 buffer_space_left;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002913 else
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002914 transaction->actual_bytes +=
2915 pipe->max_packet;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002916 }
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002917 } else if ((buffer_space_left == 0) ||
2918 (bytes_in_last_packet <
2919 pipe->max_packet)) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002920 pipe->pid_toggle = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002921 transaction->stage =
2922 CVMX_USB_STAGE_STATUS;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002923 }
2924 break;
2925 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002926 if ((buffer_space_left == 0) ||
2927 (bytes_in_last_packet <
2928 pipe->max_packet)) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002929 pipe->pid_toggle = 1;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002930 transaction->stage =
2931 CVMX_USB_STAGE_STATUS;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002932 } else {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002933 transaction->stage =
2934 CVMX_USB_STAGE_DATA;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002935 }
2936 break;
2937 case CVMX_USB_STAGE_STATUS:
2938 if (__cvmx_usb_pipe_needs_split(usb, pipe))
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002939 transaction->stage =
2940 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002941 else
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002942 __cvmx_usb_perform_complete(usb, pipe,
2943 transaction,
2944 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002945 break;
2946 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002947 __cvmx_usb_perform_complete(usb, pipe,
2948 transaction,
2949 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002950 break;
2951 }
2952 break;
2953 case CVMX_USB_TRANSFER_BULK:
2954 case CVMX_USB_TRANSFER_INTERRUPT:
2955 /*
2956 * The only time a bulk transfer isn't complete when it
2957 * finishes with an ACK is during a split transaction.
2958 * For splits we need to continue the transfer if more
2959 * data is needed
2960 */
2961 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002962 if (transaction->stage ==
2963 CVMX_USB_STAGE_NON_CONTROL)
2964 transaction->stage =
2965 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002966 else {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002967 if (buffer_space_left &&
2968 (bytes_in_last_packet ==
2969 pipe->max_packet))
2970 transaction->stage =
2971 CVMX_USB_STAGE_NON_CONTROL;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002972 else {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002973 if (transaction->type ==
2974 CVMX_USB_TRANSFER_INTERRUPT)
2975 pipe->next_tx_frame +=
2976 pipe->interval;
2977 __cvmx_usb_perform_complete(
2978 usb,
2979 pipe,
2980 transaction,
2981 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002982 }
2983 }
2984 } else {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002985 if ((pipe->device_speed ==
2986 CVMX_USB_SPEED_HIGH) &&
2987 (pipe->transfer_type ==
2988 CVMX_USB_TRANSFER_BULK) &&
2989 (pipe->transfer_dir ==
2990 CVMX_USB_DIRECTION_OUT) &&
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002991 (usbc_hcint.s.nak))
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02002992 pipe->flags |=
2993 __CVMX_USB_PIPE_FLAGS_NEED_PING;
2994 if (!buffer_space_left ||
2995 (bytes_in_last_packet <
2996 pipe->max_packet)) {
2997 if (transaction->type ==
2998 CVMX_USB_TRANSFER_INTERRUPT)
2999 pipe->next_tx_frame +=
3000 pipe->interval;
3001 __cvmx_usb_perform_complete(usb,
3002 pipe,
3003 transaction,
3004 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003005 }
3006 }
3007 break;
3008 case CVMX_USB_TRANSFER_ISOCHRONOUS:
3009 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
3010 /*
3011 * ISOCHRONOUS OUT splits don't require a
3012 * complete split stage. Instead they use a
3013 * sequence of begin OUT splits to transfer the
3014 * data 188 bytes at a time. Once the transfer
3015 * is complete, the pipe sleeps until the next
3016 * schedule interval
3017 */
3018 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
3019 /*
3020 * If no space left or this wasn't a max
3021 * size packet then this transfer is
3022 * complete. Otherwise start it again to
3023 * send the next 188 bytes
3024 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003025 if (!buffer_space_left ||
3026 (bytes_this_transfer < 188)) {
Paul Davies Ce725cef2014-05-11 18:41:33 +05303027 pipe->next_tx_frame +=
3028 pipe->interval;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003029 __cvmx_usb_perform_complete(
3030 usb,
3031 pipe,
3032 transaction,
3033 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003034 }
3035 } else {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003036 if (transaction->stage ==
3037 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003038 /*
3039 * We are in the incoming data
3040 * phase. Keep getting data
3041 * until we run out of space or
3042 * get a small packet
3043 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003044 if ((buffer_space_left == 0) ||
3045 (bytes_in_last_packet <
3046 pipe->max_packet)) {
3047 pipe->next_tx_frame +=
3048 pipe->interval;
3049 __cvmx_usb_perform_complete(
3050 usb,
3051 pipe,
3052 transaction,
3053 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003054 }
3055 } else
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003056 transaction->stage =
3057 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003058 }
3059 } else {
3060 pipe->next_tx_frame += pipe->interval;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003061 __cvmx_usb_perform_complete(usb,
3062 pipe,
3063 transaction,
3064 CVMX_USB_COMPLETE_SUCCESS);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003065 }
3066 break;
3067 }
3068 } else if (usbc_hcint.s.nak) {
3069 /*
3070 * If this was a split then clear our split in progress marker.
3071 */
3072 if (usb->active_split == transaction)
3073 usb->active_split = NULL;
3074 /*
3075 * NAK as a response means the device couldn't accept the
3076 * transaction, but it should be retried in the future. Rewind
3077 * to the beginning of the transaction by anding off the split
3078 * complete bit. Retry in the next interval
3079 */
3080 transaction->retries = 0;
3081 transaction->stage &= ~1;
3082 pipe->next_tx_frame += pipe->interval;
3083 if (pipe->next_tx_frame < usb->frame_number)
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003084 pipe->next_tx_frame = usb->frame_number +
3085 pipe->interval -
3086 (usb->frame_number - pipe->next_tx_frame) %
3087 pipe->interval;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003088 } else {
3089 struct cvmx_usb_port_status port;
Paul Davies Cf5106432014-05-11 18:41:32 +05303090
Aaro Koskinencb61c602013-10-06 22:22:25 +03003091 port = cvmx_usb_get_status(usb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003092 if (port.port_enabled) {
3093 /* We'll retry the exact same transaction again */
3094 transaction->retries++;
3095 } else {
3096 /*
3097 * We get channel halted interrupts with no result bits
3098 * sets when the cable is unplugged
3099 */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003100 __cvmx_usb_perform_complete(usb, pipe, transaction,
3101 CVMX_USB_COMPLETE_ERROR);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003102 }
3103 }
3104 return 0;
3105}
3106
Aaro Koskinen393e2142013-10-06 22:22:27 +03003107static void octeon_usb_port_callback(struct cvmx_usb_state *usb)
3108{
3109 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
3110
3111 spin_unlock(&priv->lock);
3112 usb_hcd_poll_rh_status(octeon_to_hcd(priv));
3113 spin_lock(&priv->lock);
3114}
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003115
3116/**
3117 * Poll the USB block for status and call all needed callback
3118 * handlers. This function is meant to be called in the interrupt
3119 * handler for the USB controller. It can also be called
3120 * periodically in a loop for non-interrupt based operation.
3121 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03003122 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003123 *
3124 * Returns: 0 or a negative error code.
3125 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03003126static int cvmx_usb_poll(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003127{
3128 union cvmx_usbcx_hfnum usbc_hfnum;
3129 union cvmx_usbcx_gintsts usbc_gintsts;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003130
Aaro Koskinen20f6b822014-05-11 14:15:20 +03003131 prefetch_range(usb, sizeof(*usb));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003132
3133 /* Update the frame counter */
Paul Davies Ce725cef2014-05-11 18:41:33 +05303134 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb,
3135 CVMX_USBCX_HFNUM(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003136 if ((usb->frame_number&0x3fff) > usbc_hfnum.s.frnum)
3137 usb->frame_number += 0x4000;
3138 usb->frame_number &= ~0x3fffull;
3139 usb->frame_number |= usbc_hfnum.s.frnum;
3140
3141 /* Read the pending interrupts */
Paul Davies Ce725cef2014-05-11 18:41:33 +05303142 usbc_gintsts.u32 = __cvmx_usb_read_csr32(usb,
3143 CVMX_USBCX_GINTSTS(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003144
3145 /* Clear the interrupts now that we know about them */
Paul Davies Ce725cef2014-05-11 18:41:33 +05303146 __cvmx_usb_write_csr32(usb,
3147 CVMX_USBCX_GINTSTS(usb->index),
3148 usbc_gintsts.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003149
3150 if (usbc_gintsts.s.rxflvl) {
3151 /*
3152 * RxFIFO Non-Empty (RxFLvl)
3153 * Indicates that there is at least one packet pending to be
3154 * read from the RxFIFO.
3155 *
3156 * In DMA mode this is handled by hardware
3157 */
3158 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3159 __cvmx_usb_poll_rx_fifo(usb);
3160 }
3161 if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
3162 /* Fill the Tx FIFOs when not in DMA mode */
3163 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3164 __cvmx_usb_poll_tx_fifo(usb);
3165 }
3166 if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
3167 union cvmx_usbcx_hprt usbc_hprt;
3168 /*
3169 * Disconnect Detected Interrupt (DisconnInt)
3170 * Asserted when a device disconnect is detected.
3171 *
3172 * Host Port Interrupt (PrtInt)
3173 * The core sets this bit to indicate a change in port status of
3174 * one of the O2P USB core ports in Host mode. The application
3175 * must read the Host Port Control and Status (HPRT) register to
3176 * determine the exact event that caused this interrupt. The
3177 * application must clear the appropriate status bit in the Host
3178 * Port Control and Status register to clear this bit.
3179 *
3180 * Call the user's port callback
3181 */
Aaro Koskinen393e2142013-10-06 22:22:27 +03003182 octeon_usb_port_callback(usb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003183 /* Clear the port change bits */
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003184 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb,
3185 CVMX_USBCX_HPRT(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003186 usbc_hprt.s.prtena = 0;
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003187 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index),
3188 usbc_hprt.u32);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003189 }
3190 if (usbc_gintsts.s.hchint) {
3191 /*
3192 * Host Channels Interrupt (HChInt)
3193 * The core sets this bit to indicate that an interrupt is
3194 * pending on one of the channels of the core (in Host mode).
3195 * The application must read the Host All Channels Interrupt
3196 * (HAINT) register to determine the exact number of the channel
3197 * on which the interrupt occurred, and then read the
3198 * corresponding Host Channel-n Interrupt (HCINTn) register to
3199 * determine the exact cause of the interrupt. The application
3200 * must clear the appropriate status bit in the HCINTn register
3201 * to clear this bit.
3202 */
3203 union cvmx_usbcx_haint usbc_haint;
Paul Davies Cf5106432014-05-11 18:41:32 +05303204
Paul Davies Ce725cef2014-05-11 18:41:33 +05303205 usbc_haint.u32 = __cvmx_usb_read_csr32(usb,
3206 CVMX_USBCX_HAINT(usb->index));
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003207 while (usbc_haint.u32) {
3208 int channel;
3209
3210 channel = __fls(usbc_haint.u32);
3211 __cvmx_usb_poll_channel(usb, channel);
3212 usbc_haint.u32 ^= 1<<channel;
3213 }
3214 }
3215
3216 __cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
3217
3218 return 0;
3219}
3220
Aaro Koskinenb1649352013-06-01 21:42:58 +03003221/* convert between an HCD pointer and the corresponding struct octeon_hcd */
3222static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
3223{
3224 return (struct octeon_hcd *)(hcd->hcd_priv);
3225}
3226
Aaro Koskinenb1649352013-06-01 21:42:58 +03003227static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
3228{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003229 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3230 unsigned long flags;
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003231
Aaro Koskinen771378b2013-06-13 01:00:31 +03003232 spin_lock_irqsave(&priv->lock, flags);
3233 cvmx_usb_poll(&priv->usb);
3234 spin_unlock_irqrestore(&priv->lock, flags);
3235 return IRQ_HANDLED;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003236}
3237
Aaro Koskinenb1649352013-06-01 21:42:58 +03003238static int octeon_usb_start(struct usb_hcd *hcd)
3239{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003240 hcd->state = HC_STATE_RUNNING;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003241 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003242}
3243
3244static void octeon_usb_stop(struct usb_hcd *hcd)
3245{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003246 hcd->state = HC_STATE_HALT;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003247}
3248
3249static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
3250{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003251 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003252
Aaro Koskinen771378b2013-06-13 01:00:31 +03003253 return cvmx_usb_get_frame_number(&priv->usb);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003254}
3255
Aaro Koskinenb1649352013-06-01 21:42:58 +03003256static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
Aaro Koskinen771378b2013-06-13 01:00:31 +03003257 struct urb *urb,
3258 gfp_t mem_flags)
Aaro Koskinenb1649352013-06-01 21:42:58 +03003259{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003260 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003261 struct device *dev = hcd->self.controller;
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003262 struct cvmx_usb_transaction *transaction = NULL;
Aaro Koskinen60f81502013-10-06 22:22:35 +03003263 struct cvmx_usb_pipe *pipe;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003264 unsigned long flags;
Aaro Koskinen6e0e1b02013-07-30 23:43:04 +03003265 struct cvmx_usb_iso_packet *iso_packet;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003266 struct usb_host_endpoint *ep = urb->ep;
Aaro Koskinene5b90892014-06-29 22:52:53 +03003267 int rc;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003268
Aaro Koskinen771378b2013-06-13 01:00:31 +03003269 urb->status = 0;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003270 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003271
Aaro Koskinene5b90892014-06-29 22:52:53 +03003272 rc = usb_hcd_link_urb_to_ep(hcd, urb);
3273 if (rc) {
3274 spin_unlock_irqrestore(&priv->lock, flags);
3275 return rc;
3276 }
3277
Aaro Koskinen771378b2013-06-13 01:00:31 +03003278 if (!ep->hcpriv) {
Aaro Koskinen394d4e02013-07-30 23:42:54 +03003279 enum cvmx_usb_transfer transfer_type;
Aaro Koskinen49180722013-07-30 23:42:53 +03003280 enum cvmx_usb_speed speed;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003281 int split_device = 0;
3282 int split_port = 0;
Paul Davies Cf5106432014-05-11 18:41:32 +05303283
Aaro Koskinen771378b2013-06-13 01:00:31 +03003284 switch (usb_pipetype(urb->pipe)) {
3285 case PIPE_ISOCHRONOUS:
3286 transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
3287 break;
3288 case PIPE_INTERRUPT:
3289 transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
3290 break;
3291 case PIPE_CONTROL:
3292 transfer_type = CVMX_USB_TRANSFER_CONTROL;
3293 break;
3294 default:
3295 transfer_type = CVMX_USB_TRANSFER_BULK;
3296 break;
3297 }
3298 switch (urb->dev->speed) {
3299 case USB_SPEED_LOW:
3300 speed = CVMX_USB_SPEED_LOW;
3301 break;
3302 case USB_SPEED_FULL:
3303 speed = CVMX_USB_SPEED_FULL;
3304 break;
3305 default:
3306 speed = CVMX_USB_SPEED_HIGH;
3307 break;
3308 }
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003309 /*
3310 * For slow devices on high speed ports we need to find the hub
3311 * that does the speed translation so we know where to send the
3312 * split transactions.
3313 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003314 if (speed != CVMX_USB_SPEED_HIGH) {
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003315 /*
3316 * Start at this device and work our way up the usb
3317 * tree.
3318 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003319 struct usb_device *dev = urb->dev;
Paul Davies Cf5106432014-05-11 18:41:32 +05303320
Aaro Koskinen771378b2013-06-13 01:00:31 +03003321 while (dev->parent) {
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003322 /*
3323 * If our parent is high speed then he'll
3324 * receive the splits.
3325 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003326 if (dev->parent->speed == USB_SPEED_HIGH) {
3327 split_device = dev->parent->devnum;
3328 split_port = dev->portnum;
3329 break;
3330 }
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003331 /*
3332 * Move up the tree one level. If we make it all
3333 * the way up the tree, then the port must not
3334 * be in high speed mode and we don't need a
3335 * split.
3336 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003337 dev = dev->parent;
3338 }
3339 }
Aaro Koskinen60f81502013-10-06 22:22:35 +03003340 pipe = cvmx_usb_open_pipe(&priv->usb, usb_pipedevice(urb->pipe),
3341 usb_pipeendpoint(urb->pipe), speed,
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003342 le16_to_cpu(ep->desc.wMaxPacketSize)
3343 & 0x7ff,
Aaro Koskinen60f81502013-10-06 22:22:35 +03003344 transfer_type,
3345 usb_pipein(urb->pipe) ?
3346 CVMX_USB_DIRECTION_IN :
3347 CVMX_USB_DIRECTION_OUT,
3348 urb->interval,
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003349 (le16_to_cpu(ep->desc.wMaxPacketSize)
3350 >> 11) & 0x3,
Aaro Koskinen60f81502013-10-06 22:22:35 +03003351 split_device, split_port);
3352 if (!pipe) {
Aaro Koskinene5b90892014-06-29 22:52:53 +03003353 usb_hcd_unlink_urb_from_ep(hcd, urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003354 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003355 dev_dbg(dev, "Failed to create pipe\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003356 return -ENOMEM;
3357 }
Aaro Koskinen60f81502013-10-06 22:22:35 +03003358 ep->hcpriv = pipe;
Aaro Koskinenc7609ea2013-06-13 01:00:33 +03003359 } else {
Aaro Koskinen60f81502013-10-06 22:22:35 +03003360 pipe = ep->hcpriv;
Aaro Koskinenc7609ea2013-06-13 01:00:33 +03003361 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003362
Aaro Koskinen771378b2013-06-13 01:00:31 +03003363 switch (usb_pipetype(urb->pipe)) {
3364 case PIPE_ISOCHRONOUS:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003365 dev_dbg(dev, "Submit isochronous to %d.%d\n",
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003366 usb_pipedevice(urb->pipe),
3367 usb_pipeendpoint(urb->pipe));
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003368 /*
3369 * Allocate a structure to use for our private list of
3370 * isochronous packets.
3371 */
Aaro Koskinen6e0e1b02013-07-30 23:43:04 +03003372 iso_packet = kmalloc(urb->number_of_packets *
3373 sizeof(struct cvmx_usb_iso_packet),
3374 GFP_ATOMIC);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003375 if (iso_packet) {
3376 int i;
3377 /* Fill the list with the data from the URB */
3378 for (i = 0; i < urb->number_of_packets; i++) {
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003379 iso_packet[i].offset =
3380 urb->iso_frame_desc[i].offset;
3381 iso_packet[i].length =
3382 urb->iso_frame_desc[i].length;
3383 iso_packet[i].status =
3384 CVMX_USB_COMPLETE_ERROR;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003385 }
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003386 /*
3387 * Store a pointer to the list in the URB setup_packet
3388 * field. We know this currently isn't being used and
3389 * this saves us a bunch of logic.
3390 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003391 urb->setup_packet = (char *)iso_packet;
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003392 transaction = cvmx_usb_submit_isochronous(&priv->usb,
3393 pipe, urb);
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003394 /*
3395 * If submit failed we need to free our private packet
3396 * list.
3397 */
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003398 if (!transaction) {
Aaro Koskinen771378b2013-06-13 01:00:31 +03003399 urb->setup_packet = NULL;
3400 kfree(iso_packet);
3401 }
3402 }
3403 break;
3404 case PIPE_INTERRUPT:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003405 dev_dbg(dev, "Submit interrupt to %d.%d\n",
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003406 usb_pipedevice(urb->pipe),
3407 usb_pipeendpoint(urb->pipe));
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003408 transaction = cvmx_usb_submit_interrupt(&priv->usb, pipe, urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003409 break;
3410 case PIPE_CONTROL:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003411 dev_dbg(dev, "Submit control to %d.%d\n",
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003412 usb_pipedevice(urb->pipe),
3413 usb_pipeendpoint(urb->pipe));
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003414 transaction = cvmx_usb_submit_control(&priv->usb, pipe, urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003415 break;
3416 case PIPE_BULK:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003417 dev_dbg(dev, "Submit bulk to %d.%d\n",
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003418 usb_pipedevice(urb->pipe),
3419 usb_pipeendpoint(urb->pipe));
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003420 transaction = cvmx_usb_submit_bulk(&priv->usb, pipe, urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003421 break;
3422 }
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003423 if (!transaction) {
Aaro Koskinene5b90892014-06-29 22:52:53 +03003424 usb_hcd_unlink_urb_from_ep(hcd, urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003425 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003426 dev_dbg(dev, "Failed to submit\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003427 return -ENOMEM;
3428 }
Aaro Koskinen3e1674c2013-10-06 22:22:36 +03003429 urb->hcpriv = transaction;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003430 spin_unlock_irqrestore(&priv->lock, flags);
3431 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003432}
3433
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003434static int octeon_usb_urb_dequeue(struct usb_hcd *hcd,
3435 struct urb *urb,
3436 int status)
Aaro Koskinenb1649352013-06-01 21:42:58 +03003437{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003438 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3439 unsigned long flags;
Aaro Koskinene5b90892014-06-29 22:52:53 +03003440 int rc;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003441
Aaro Koskinen771378b2013-06-13 01:00:31 +03003442 if (!urb->dev)
3443 return -EINVAL;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003444
Aaro Koskinen771378b2013-06-13 01:00:31 +03003445 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003446
Aaro Koskinene5b90892014-06-29 22:52:53 +03003447 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
3448 if (rc)
3449 goto out;
3450
Aaro Koskinen771378b2013-06-13 01:00:31 +03003451 urb->status = status;
Aaro Koskinencdd15d82014-06-29 22:52:52 +03003452 cvmx_usb_cancel(&priv->usb, urb->ep->hcpriv, urb->hcpriv);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003453
Aaro Koskinene5b90892014-06-29 22:52:53 +03003454out:
Aaro Koskinen771378b2013-06-13 01:00:31 +03003455 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003456
Aaro Koskinene5b90892014-06-29 22:52:53 +03003457 return rc;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003458}
3459
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003460static void octeon_usb_endpoint_disable(struct usb_hcd *hcd,
3461 struct usb_host_endpoint *ep)
Aaro Koskinenb1649352013-06-01 21:42:58 +03003462{
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003463 struct device *dev = hcd->self.controller;
3464
Aaro Koskinen771378b2013-06-13 01:00:31 +03003465 if (ep->hcpriv) {
3466 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen60f81502013-10-06 22:22:35 +03003467 struct cvmx_usb_pipe *pipe = ep->hcpriv;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003468 unsigned long flags;
Paul Davies Cf5106432014-05-11 18:41:32 +05303469
Aaro Koskinen771378b2013-06-13 01:00:31 +03003470 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinen60f81502013-10-06 22:22:35 +03003471 cvmx_usb_cancel_all(&priv->usb, pipe);
3472 if (cvmx_usb_close_pipe(&priv->usb, pipe))
3473 dev_dbg(dev, "Closing pipe %p failed\n", pipe);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003474 spin_unlock_irqrestore(&priv->lock, flags);
3475 ep->hcpriv = NULL;
3476 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003477}
3478
3479static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
3480{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003481 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen51a19622013-07-30 23:43:03 +03003482 struct cvmx_usb_port_status port_status;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003483 unsigned long flags;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003484
Aaro Koskinen771378b2013-06-13 01:00:31 +03003485 spin_lock_irqsave(&priv->lock, flags);
3486 port_status = cvmx_usb_get_status(&priv->usb);
3487 spin_unlock_irqrestore(&priv->lock, flags);
3488 buf[0] = 0;
3489 buf[0] = port_status.connect_change << 1;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003490
Paul McQuade85228512014-04-08 16:32:48 +01003491 return buf[0] != 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003492}
3493
Paul Davies Ce725cef2014-05-11 18:41:33 +05303494static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3495 u16 wIndex, char *buf, u16 wLength)
Aaro Koskinenb1649352013-06-01 21:42:58 +03003496{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003497 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003498 struct device *dev = hcd->self.controller;
Aaro Koskinen51a19622013-07-30 23:43:03 +03003499 struct cvmx_usb_port_status usb_port_status;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003500 int port_status;
3501 struct usb_hub_descriptor *desc;
3502 unsigned long flags;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003503
Aaro Koskinen771378b2013-06-13 01:00:31 +03003504 switch (typeReq) {
3505 case ClearHubFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003506 dev_dbg(dev, "ClearHubFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003507 switch (wValue) {
3508 case C_HUB_LOCAL_POWER:
3509 case C_HUB_OVER_CURRENT:
3510 /* Nothing required here */
3511 break;
3512 default:
3513 return -EINVAL;
3514 }
3515 break;
3516 case ClearPortFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003517 dev_dbg(dev, "ClearPortFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003518 if (wIndex != 1) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003519 dev_dbg(dev, " INVALID\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003520 return -EINVAL;
3521 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003522
Aaro Koskinen771378b2013-06-13 01:00:31 +03003523 switch (wValue) {
3524 case USB_PORT_FEAT_ENABLE:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003525 dev_dbg(dev, " ENABLE\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003526 spin_lock_irqsave(&priv->lock, flags);
3527 cvmx_usb_disable(&priv->usb);
3528 spin_unlock_irqrestore(&priv->lock, flags);
3529 break;
3530 case USB_PORT_FEAT_SUSPEND:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003531 dev_dbg(dev, " SUSPEND\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003532 /* Not supported on Octeon */
3533 break;
3534 case USB_PORT_FEAT_POWER:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003535 dev_dbg(dev, " POWER\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003536 /* Not supported on Octeon */
3537 break;
3538 case USB_PORT_FEAT_INDICATOR:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003539 dev_dbg(dev, " INDICATOR\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003540 /* Port inidicator not supported */
3541 break;
3542 case USB_PORT_FEAT_C_CONNECTION:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003543 dev_dbg(dev, " C_CONNECTION\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003544 /* Clears drivers internal connect status change flag */
3545 spin_lock_irqsave(&priv->lock, flags);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003546 priv->usb.port_status =
3547 cvmx_usb_get_status(&priv->usb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003548 spin_unlock_irqrestore(&priv->lock, flags);
3549 break;
3550 case USB_PORT_FEAT_C_RESET:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003551 dev_dbg(dev, " C_RESET\n");
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003552 /*
3553 * Clears the driver's internal Port Reset Change flag.
3554 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003555 spin_lock_irqsave(&priv->lock, flags);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003556 priv->usb.port_status =
3557 cvmx_usb_get_status(&priv->usb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003558 spin_unlock_irqrestore(&priv->lock, flags);
3559 break;
3560 case USB_PORT_FEAT_C_ENABLE:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003561 dev_dbg(dev, " C_ENABLE\n");
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003562 /*
3563 * Clears the driver's internal Port Enable/Disable
3564 * Change flag.
3565 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003566 spin_lock_irqsave(&priv->lock, flags);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003567 priv->usb.port_status =
3568 cvmx_usb_get_status(&priv->usb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003569 spin_unlock_irqrestore(&priv->lock, flags);
3570 break;
3571 case USB_PORT_FEAT_C_SUSPEND:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003572 dev_dbg(dev, " C_SUSPEND\n");
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003573 /*
3574 * Clears the driver's internal Port Suspend Change
3575 * flag, which is set when resume signaling on the host
3576 * port is complete.
3577 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003578 break;
3579 case USB_PORT_FEAT_C_OVER_CURRENT:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003580 dev_dbg(dev, " C_OVER_CURRENT\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003581 /* Clears the driver's overcurrent Change flag */
3582 spin_lock_irqsave(&priv->lock, flags);
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003583 priv->usb.port_status =
3584 cvmx_usb_get_status(&priv->usb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003585 spin_unlock_irqrestore(&priv->lock, flags);
3586 break;
3587 default:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003588 dev_dbg(dev, " UNKNOWN\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003589 return -EINVAL;
3590 }
Aaro Koskinen771378b2013-06-13 01:00:31 +03003591 break;
3592 case GetHubDescriptor:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003593 dev_dbg(dev, "GetHubDescriptor\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003594 desc = (struct usb_hub_descriptor *)buf;
3595 desc->bDescLength = 9;
3596 desc->bDescriptorType = 0x29;
3597 desc->bNbrPorts = 1;
Aaro Koskinene5873382014-06-29 22:52:56 +03003598 desc->wHubCharacteristics = cpu_to_le16(0x08);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003599 desc->bPwrOn2PwrGood = 1;
3600 desc->bHubContrCurrent = 0;
3601 desc->u.hs.DeviceRemovable[0] = 0;
3602 desc->u.hs.DeviceRemovable[1] = 0xff;
3603 break;
3604 case GetHubStatus:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003605 dev_dbg(dev, "GetHubStatus\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003606 *(__le32 *) buf = 0;
3607 break;
3608 case GetPortStatus:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003609 dev_dbg(dev, "GetPortStatus\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003610 if (wIndex != 1) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003611 dev_dbg(dev, " INVALID\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003612 return -EINVAL;
3613 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003614
Aaro Koskinen771378b2013-06-13 01:00:31 +03003615 spin_lock_irqsave(&priv->lock, flags);
3616 usb_port_status = cvmx_usb_get_status(&priv->usb);
3617 spin_unlock_irqrestore(&priv->lock, flags);
3618 port_status = 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003619
Aaro Koskinen771378b2013-06-13 01:00:31 +03003620 if (usb_port_status.connect_change) {
3621 port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003622 dev_dbg(dev, " C_CONNECTION\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003623 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003624
Aaro Koskinen771378b2013-06-13 01:00:31 +03003625 if (usb_port_status.port_enabled) {
3626 port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003627 dev_dbg(dev, " C_ENABLE\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003628 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003629
Aaro Koskinen771378b2013-06-13 01:00:31 +03003630 if (usb_port_status.connected) {
3631 port_status |= (1 << USB_PORT_FEAT_CONNECTION);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003632 dev_dbg(dev, " CONNECTION\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003633 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003634
Aaro Koskinen771378b2013-06-13 01:00:31 +03003635 if (usb_port_status.port_enabled) {
3636 port_status |= (1 << USB_PORT_FEAT_ENABLE);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003637 dev_dbg(dev, " ENABLE\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003638 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003639
Aaro Koskinen771378b2013-06-13 01:00:31 +03003640 if (usb_port_status.port_over_current) {
3641 port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003642 dev_dbg(dev, " OVER_CURRENT\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003643 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003644
Aaro Koskinen771378b2013-06-13 01:00:31 +03003645 if (usb_port_status.port_powered) {
3646 port_status |= (1 << USB_PORT_FEAT_POWER);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003647 dev_dbg(dev, " POWER\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003648 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003649
Aaro Koskinen771378b2013-06-13 01:00:31 +03003650 if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
3651 port_status |= USB_PORT_STAT_HIGH_SPEED;
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003652 dev_dbg(dev, " HIGHSPEED\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003653 } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
3654 port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003655 dev_dbg(dev, " LOWSPEED\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003656 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003657
Aaro Koskinen771378b2013-06-13 01:00:31 +03003658 *((__le32 *) buf) = cpu_to_le32(port_status);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003659 break;
3660 case SetHubFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003661 dev_dbg(dev, "SetHubFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003662 /* No HUB features supported */
3663 break;
3664 case SetPortFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003665 dev_dbg(dev, "SetPortFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003666 if (wIndex != 1) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003667 dev_dbg(dev, " INVALID\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003668 return -EINVAL;
3669 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003670
Aaro Koskinen771378b2013-06-13 01:00:31 +03003671 switch (wValue) {
3672 case USB_PORT_FEAT_SUSPEND:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003673 dev_dbg(dev, " SUSPEND\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003674 return -EINVAL;
3675 case USB_PORT_FEAT_POWER:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003676 dev_dbg(dev, " POWER\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003677 return -EINVAL;
3678 case USB_PORT_FEAT_RESET:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003679 dev_dbg(dev, " RESET\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003680 spin_lock_irqsave(&priv->lock, flags);
3681 cvmx_usb_disable(&priv->usb);
3682 if (cvmx_usb_enable(&priv->usb))
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003683 dev_dbg(dev, "Failed to enable the port\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003684 spin_unlock_irqrestore(&priv->lock, flags);
3685 return 0;
3686 case USB_PORT_FEAT_INDICATOR:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003687 dev_dbg(dev, " INDICATOR\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003688 /* Not supported */
3689 break;
3690 default:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003691 dev_dbg(dev, " UNKNOWN\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003692 return -EINVAL;
3693 }
3694 break;
3695 default:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003696 dev_dbg(dev, "Unknown root hub request\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003697 return -EINVAL;
3698 }
3699 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003700}
3701
Aaro Koskinenb1649352013-06-01 21:42:58 +03003702static const struct hc_driver octeon_hc_driver = {
Aaro Koskinen771378b2013-06-13 01:00:31 +03003703 .description = "Octeon USB",
3704 .product_desc = "Octeon Host Controller",
3705 .hcd_priv_size = sizeof(struct octeon_hcd),
3706 .irq = octeon_usb_irq,
3707 .flags = HCD_MEMORY | HCD_USB2,
3708 .start = octeon_usb_start,
3709 .stop = octeon_usb_stop,
3710 .urb_enqueue = octeon_usb_urb_enqueue,
3711 .urb_dequeue = octeon_usb_urb_dequeue,
3712 .endpoint_disable = octeon_usb_endpoint_disable,
3713 .get_frame_number = octeon_usb_get_frame_number,
3714 .hub_status_data = octeon_usb_hub_status_data,
3715 .hub_control = octeon_usb_hub_control,
Aaro Koskinen120ee592014-03-20 00:52:08 +02003716 .map_urb_for_dma = octeon_map_urb_for_dma,
3717 .unmap_urb_for_dma = octeon_unmap_urb_for_dma,
Aaro Koskinenb1649352013-06-01 21:42:58 +03003718};
3719
David Daneyb91619c2014-02-03 19:39:01 +02003720static int octeon_usb_probe(struct platform_device *pdev)
Aaro Koskinenb1649352013-06-01 21:42:58 +03003721{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003722 int status;
David Daneyb91619c2014-02-03 19:39:01 +02003723 int initialize_flags;
3724 int usb_num;
3725 struct resource *res_mem;
3726 struct device_node *usbn_node;
3727 int irq = platform_get_irq(pdev, 0);
3728 struct device *dev = &pdev->dev;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003729 struct octeon_hcd *priv;
3730 struct usb_hcd *hcd;
3731 unsigned long flags;
David Daneyb91619c2014-02-03 19:39:01 +02003732 u32 clock_rate = 48000000;
3733 bool is_crystal_clock = false;
3734 const char *clock_type;
3735 int i;
3736
3737 if (dev->of_node == NULL) {
3738 dev_err(dev, "Error: empty of_node\n");
3739 return -ENXIO;
3740 }
3741 usbn_node = dev->of_node->parent;
3742
3743 i = of_property_read_u32(usbn_node,
3744 "refclk-frequency", &clock_rate);
3745 if (i) {
3746 dev_err(dev, "No USBN \"refclk-frequency\"\n");
3747 return -ENXIO;
3748 }
3749 switch (clock_rate) {
3750 case 12000000:
3751 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
3752 break;
3753 case 24000000:
3754 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
3755 break;
3756 case 48000000:
3757 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
3758 break;
3759 default:
Raluca Oncioiu1da69aa2014-03-08 00:32:05 +02003760 dev_err(dev, "Illebal USBN \"refclk-frequency\" %u\n",
3761 clock_rate);
David Daneyb91619c2014-02-03 19:39:01 +02003762 return -ENXIO;
3763
3764 }
3765
3766 i = of_property_read_string(usbn_node,
3767 "refclk-type", &clock_type);
3768
3769 if (!i && strcmp("crystal", clock_type) == 0)
3770 is_crystal_clock = true;
3771
3772 if (is_crystal_clock)
3773 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
3774 else
3775 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
3776
3777 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3778 if (res_mem == NULL) {
3779 dev_err(dev, "found no memory resource\n");
3780 return -ENXIO;
3781 }
3782 usb_num = (res_mem->start >> 44) & 1;
3783
3784 if (irq < 0) {
3785 /* Defective device tree, but we know how to fix it. */
3786 irq_hw_number_t hwirq = usb_num ? (1 << 6) + 17 : 56;
Paul Davies Cf5106432014-05-11 18:41:32 +05303787
David Daneyb91619c2014-02-03 19:39:01 +02003788 irq = irq_create_mapping(NULL, hwirq);
3789 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003790
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003791 /*
3792 * Set the DMA mask to 64bits so we get buffers already translated for
3793 * DMA.
3794 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003795 dev->coherent_dma_mask = ~0;
3796 dev->dma_mask = &dev->coherent_dma_mask;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003797
Aaro Koskinenb1649352013-06-01 21:42:58 +03003798 /*
3799 * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
3800 * IOB priority registers. Under heavy network load USB
3801 * hardware can be starved by the IOB causing a crash. Give
3802 * it a priority boost if it has been waiting more than 400
3803 * cycles to avoid this situation.
3804 *
3805 * Testing indicates that a cnt_val of 8192 is not sufficient,
3806 * but no failures are seen with 4096. We choose a value of
3807 * 400 to give a safety factor of 10.
3808 */
3809 if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
3810 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
3811
3812 pri_cnt.u64 = 0;
3813 pri_cnt.s.cnt_enb = 1;
3814 pri_cnt.s.cnt_val = 400;
3815 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
3816 }
3817
David Daneyb91619c2014-02-03 19:39:01 +02003818 hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
3819 if (!hcd) {
3820 dev_dbg(dev, "Failed to allocate memory for HCD\n");
3821 return -1;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003822 }
David Daneyb91619c2014-02-03 19:39:01 +02003823 hcd->uses_new_polling = 1;
3824 priv = (struct octeon_hcd *)hcd->hcd_priv;
3825
3826 spin_lock_init(&priv->lock);
3827
David Daneyb91619c2014-02-03 19:39:01 +02003828 status = cvmx_usb_initialize(&priv->usb, usb_num, initialize_flags);
3829 if (status) {
3830 dev_dbg(dev, "USB initialization failed with %d\n", status);
3831 kfree(hcd);
3832 return -1;
3833 }
3834
3835 /* This delay is needed for CN3010, but I don't know why... */
3836 mdelay(10);
3837
3838 spin_lock_irqsave(&priv->lock, flags);
3839 cvmx_usb_poll(&priv->usb);
3840 spin_unlock_irqrestore(&priv->lock, flags);
3841
3842 status = usb_add_hcd(hcd, irq, 0);
3843 if (status) {
3844 dev_dbg(dev, "USB add HCD failed with %d\n", status);
3845 kfree(hcd);
3846 return -1;
3847 }
3848 device_wakeup_enable(hcd->self.controller);
3849
3850 dev_info(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
3851
Aaro Koskinen771378b2013-06-13 01:00:31 +03003852 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003853}
3854
David Daneyb91619c2014-02-03 19:39:01 +02003855static int octeon_usb_remove(struct platform_device *pdev)
Aaro Koskinenb1649352013-06-01 21:42:58 +03003856{
David Daneyb91619c2014-02-03 19:39:01 +02003857 int status;
3858 struct device *dev = &pdev->dev;
3859 struct usb_hcd *hcd = dev_get_drvdata(dev);
3860 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3861 unsigned long flags;
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003862
David Daneyb91619c2014-02-03 19:39:01 +02003863 usb_remove_hcd(hcd);
David Daneyb91619c2014-02-03 19:39:01 +02003864 spin_lock_irqsave(&priv->lock, flags);
3865 status = cvmx_usb_shutdown(&priv->usb);
3866 spin_unlock_irqrestore(&priv->lock, flags);
3867 if (status)
3868 dev_dbg(dev, "USB shutdown failed with %d\n", status);
3869
3870 kfree(hcd);
3871
3872 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003873}
3874
David Daneyb91619c2014-02-03 19:39:01 +02003875static struct of_device_id octeon_usb_match[] = {
3876 {
3877 .compatible = "cavium,octeon-5750-usbc",
3878 },
3879 {},
3880};
3881
3882static struct platform_driver octeon_usb_driver = {
3883 .driver = {
3884 .name = "OcteonUSB",
3885 .owner = THIS_MODULE,
3886 .of_match_table = octeon_usb_match,
3887 },
3888 .probe = octeon_usb_probe,
3889 .remove = octeon_usb_remove,
3890};
3891
3892static int __init octeon_usb_driver_init(void)
3893{
3894 if (usb_disabled())
3895 return 0;
3896
3897 return platform_driver_register(&octeon_usb_driver);
3898}
3899module_init(octeon_usb_driver_init);
3900
3901static void __exit octeon_usb_driver_exit(void)
3902{
3903 if (usb_disabled())
3904 return;
3905
3906 platform_driver_unregister(&octeon_usb_driver);
3907}
3908module_exit(octeon_usb_driver_exit);
3909
Aaro Koskinenb1649352013-06-01 21:42:58 +03003910MODULE_LICENSE("GPL");
David Daneyb91619c2014-02-03 19:39:01 +02003911MODULE_AUTHOR("Cavium, Inc. <support@cavium.com>");
3912MODULE_DESCRIPTION("Cavium Inc. OCTEON USB Host driver.");