blob: 0a448aead872f6dcbf4d754b5687553e1c46d3ec [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>
50#include <linux/interrupt.h>
51#include <linux/platform_device.h>
Aaro Koskinenb1649352013-06-01 21:42:58 +030052#include <linux/usb.h>
53
Aaro Koskinen2a81d2b2013-06-13 01:00:35 +030054#include <linux/time.h>
55#include <linux/delay.h>
Aaro Koskinenb1649352013-06-01 21:42:58 +030056
57#include <asm/octeon/cvmx.h>
Aaro Koskinenb1649352013-06-01 21:42:58 +030058#include <asm/octeon/cvmx-iob-defs.h>
59
60#include <linux/usb/hcd.h>
61
Devendra Naga55fa3282013-06-04 02:46:20 +053062#include <linux/err.h>
63
Aaro Koskinen6570b4a2013-10-06 22:22:24 +030064#include <asm/octeon/octeon.h>
65#include <asm/octeon/cvmx-helper.h>
66#include <asm/octeon/cvmx-sysinfo.h>
67#include <asm/octeon/cvmx-helper-board.h>
68
69#include "cvmx-usbcx-defs.h"
70#include "cvmx-usbnx-defs.h"
71
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/**
173 * union cvmx_usb_control_header - the structure of a Control packet header
174 *
175 * @s.request_type: Bit 7 tells the direction: 1=IN, 0=OUT
176 * @s.request The standard usb request to make
177 * @s.value Value parameter for the request in little endian format
178 * @s.index Index for the request in little endian format
179 * @s.length Length of the data associated with this request in
180 * little endian format
181 */
182union cvmx_usb_control_header {
183 uint64_t u64;
184 struct {
185 uint64_t request_type : 8;
186 uint64_t request : 8;
187 uint64_t value : 16;
188 uint64_t index : 16;
189 uint64_t length : 16;
190 } s;
191};
192
193/**
194 * struct cvmx_usb_iso_packet - descriptor for Isochronous packets
195 *
196 * @offset: This is the offset in bytes into the main buffer where this data
197 * is stored.
198 * @length: This is the length in bytes of the data.
199 * @status: This is the status of this individual packet transfer.
200 */
201struct cvmx_usb_iso_packet {
202 int offset;
203 int length;
204 enum cvmx_usb_complete status;
205};
206
207/**
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300208 * enum cvmx_usb_initialize_flags - flags used by the initialization function
209 *
210 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI: The USB port uses a 12MHz crystal
211 * as clock source at USB_XO and
212 * USB_XI.
213 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND: The USB port uses 12/24/48MHz 2.5V
214 * board clock source at USB_XO.
215 * USB_XI should be tied to GND.
216 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK: Mask for clock speed field
217 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ: Speed of reference clock or
218 * crystal
219 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ: Speed of reference clock
220 * @CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ: Speed of reference clock
221 * @CVMX_USB_INITIALIZE_FLAGS_NO_DMA: Disable DMA and used polled IO for
222 * data transfer use for the USB
223 */
224enum cvmx_usb_initialize_flags {
225 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI = 1 << 0,
226 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND = 1 << 1,
227 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK = 3 << 3,
228 CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ = 1 << 3,
229 CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ = 2 << 3,
230 CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ = 3 << 3,
231 /* Bits 3-4 used to encode the clock frequency */
232 CVMX_USB_INITIALIZE_FLAGS_NO_DMA = 1 << 5,
233};
234
235/**
236 * enum cvmx_usb_pipe_flags - internal flags for a pipe.
237 *
238 * @__CVMX_USB_PIPE_FLAGS_OPEN: Used internally to determine if a pipe is
239 * open. Do not use.
240 * @__CVMX_USB_PIPE_FLAGS_SCHEDULED: Used internally to determine if a pipe is
241 * actively using hardware. Do not use.
242 * @__CVMX_USB_PIPE_FLAGS_NEED_PING: Used internally to determine if a high
243 * speed pipe is in the ping state. Do not
244 * use.
245 */
246enum cvmx_usb_pipe_flags {
247 __CVMX_USB_PIPE_FLAGS_OPEN = 1 << 16,
248 __CVMX_USB_PIPE_FLAGS_SCHEDULED = 1 << 17,
249 __CVMX_USB_PIPE_FLAGS_NEED_PING = 1 << 18,
250};
251
252/* Normal prefetch that use the pref instruction. */
253#define CVMX_PREFETCH(address, offset) asm volatile ("pref %[type], %[off](%[rbase])" : : [rbase] "d" (address), [off] "I" (offset), [type] "n" (0))
254
255/* Maximum number of times to retry failed transactions */
256#define MAX_RETRIES 3
257
258/* Maximum number of pipes that can be open at once */
259#define MAX_PIPES 32
260
261/* Maximum number of outstanding transactions across all pipes */
262#define MAX_TRANSACTIONS 256
263
264/* Maximum number of hardware channels supported by the USB block */
265#define MAX_CHANNELS 8
266
267/* The highest valid USB device address */
268#define MAX_USB_ADDRESS 127
269
270/* The highest valid USB endpoint number */
271#define MAX_USB_ENDPOINT 15
272
273/* The highest valid port number on a hub */
274#define MAX_USB_HUB_PORT 15
275
276/*
277 * The low level hardware can transfer a maximum of this number of bytes in each
278 * transfer. The field is 19 bits wide
279 */
280#define MAX_TRANSFER_BYTES ((1<<19)-1)
281
282/*
283 * The low level hardware can transfer a maximum of this number of packets in
284 * each transfer. The field is 10 bits wide
285 */
286#define MAX_TRANSFER_PACKETS ((1<<10)-1)
287
288enum cvmx_usb_transaction_flags {
289 __CVMX_USB_TRANSACTION_FLAGS_IN_USE = 1<<16,
290};
291
292enum {
293 USB_CLOCK_TYPE_REF_12,
294 USB_CLOCK_TYPE_REF_24,
295 USB_CLOCK_TYPE_REF_48,
296 USB_CLOCK_TYPE_CRYSTAL_12,
297};
298
299/**
300 * Logical transactions may take numerous low level
301 * transactions, especially when splits are concerned. This
302 * enum represents all of the possible stages a transaction can
303 * be in. Note that split completes are always even. This is so
304 * the NAK handler can backup to the previous low level
305 * transaction with a simple clearing of bit 0.
306 */
307enum cvmx_usb_stage {
308 CVMX_USB_STAGE_NON_CONTROL,
309 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
310 CVMX_USB_STAGE_SETUP,
311 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
312 CVMX_USB_STAGE_DATA,
313 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
314 CVMX_USB_STAGE_STATUS,
315 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
316};
317
318/**
319 * struct cvmx_usb_transaction - describes each pending USB transaction
320 * regardless of type. These are linked together
321 * to form a list of pending requests for a pipe.
322 *
323 * @prev: Transaction before this one in the pipe.
324 * @next: Transaction after this one in the pipe.
325 * @type: Type of transaction, duplicated of the pipe.
326 * @flags: State flags for this transaction.
327 * @buffer: User's physical buffer address to read/write.
328 * @buffer_length: Size of the user's buffer in bytes.
329 * @control_header: For control transactions, physical address of the 8
330 * byte standard header.
331 * @iso_start_frame: For ISO transactions, the starting frame number.
332 * @iso_number_packets: For ISO transactions, the number of packets in the
333 * request.
334 * @iso_packets: For ISO transactions, the sub packets in the request.
335 * @actual_bytes: Actual bytes transfer for this transaction.
336 * @stage: For control transactions, the current stage.
Aaro Koskinen0cce1002013-10-06 22:22:29 +0300337 * @urb: URB.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300338 */
339struct cvmx_usb_transaction {
340 struct cvmx_usb_transaction *prev;
341 struct cvmx_usb_transaction *next;
342 enum cvmx_usb_transfer type;
343 enum cvmx_usb_transaction_flags flags;
344 uint64_t buffer;
345 int buffer_length;
346 uint64_t control_header;
347 int iso_start_frame;
348 int iso_number_packets;
349 struct cvmx_usb_iso_packet *iso_packets;
350 int xfersize;
351 int pktcnt;
352 int retries;
353 int actual_bytes;
354 enum cvmx_usb_stage stage;
Aaro Koskinen0cce1002013-10-06 22:22:29 +0300355 struct urb *urb;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300356};
357
358/**
359 * struct cvmx_usb_pipe - a pipe represents a virtual connection between Octeon
360 * and some USB device. It contains a list of pending
361 * request to the device.
362 *
363 * @prev: Pipe before this one in the list
364 * @next: Pipe after this one in the list
365 * @head: The first pending transaction
366 * @tail: The last pending transaction
367 * @interval: For periodic pipes, the interval between packets in
368 * frames
369 * @next_tx_frame: The next frame this pipe is allowed to transmit on
370 * @flags: State flags for this pipe
371 * @device_speed: Speed of device connected to this pipe
372 * @transfer_type: Type of transaction supported by this pipe
373 * @transfer_dir: IN or OUT. Ignored for Control
374 * @multi_count: Max packet in a row for the device
375 * @max_packet: The device's maximum packet size in bytes
376 * @device_addr: USB device address at other end of pipe
377 * @endpoint_num: USB endpoint number at other end of pipe
378 * @hub_device_addr: Hub address this device is connected to
379 * @hub_port: Hub port this device is connected to
380 * @pid_toggle: This toggles between 0/1 on every packet send to track
381 * the data pid needed
382 * @channel: Hardware DMA channel for this pipe
383 * @split_sc_frame: The low order bits of the frame number the split
384 * complete should be sent on
385 */
386struct cvmx_usb_pipe {
387 struct cvmx_usb_pipe *prev;
388 struct cvmx_usb_pipe *next;
389 struct cvmx_usb_transaction *head;
390 struct cvmx_usb_transaction *tail;
391 uint64_t interval;
392 uint64_t next_tx_frame;
393 enum cvmx_usb_pipe_flags flags;
394 enum cvmx_usb_speed device_speed;
395 enum cvmx_usb_transfer transfer_type;
396 enum cvmx_usb_direction transfer_dir;
397 int multi_count;
398 uint16_t max_packet;
399 uint8_t device_addr;
400 uint8_t endpoint_num;
401 uint8_t hub_device_addr;
402 uint8_t hub_port;
403 uint8_t pid_toggle;
404 uint8_t channel;
405 int8_t split_sc_frame;
406};
407
408/**
409 * struct cvmx_usb_pipe_list
410 *
411 * @head: Head of the list, or NULL if empty.
412 * @tail: Tail if the list, or NULL if empty.
413 */
414struct cvmx_usb_pipe_list {
415 struct cvmx_usb_pipe *head;
416 struct cvmx_usb_pipe *tail;
417};
418
419struct cvmx_usb_tx_fifo {
420 struct {
421 int channel;
422 int size;
423 uint64_t address;
424 } entry[MAX_CHANNELS+1];
425 int head;
426 int tail;
427};
428
429/**
Aaro Koskinencb61c602013-10-06 22:22:25 +0300430 * struct cvmx_usb_state - the state of the USB block
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300431 *
432 * init_flags: Flags passed to initialize.
433 * index: Which USB block this is for.
434 * idle_hardware_channels: Bit set for every idle hardware channel.
435 * usbcx_hprt: Stored port status so we don't need to read a CSR to
436 * determine splits.
437 * pipe_for_channel: Map channels to pipes.
438 * free_transaction_head: List of free transactions head.
439 * free_transaction_tail: List of free transactions tail.
440 * pipe: Storage for pipes.
441 * transaction: Storage for transactions.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300442 * indent: Used by debug output to indent functions.
443 * port_status: Last port status used for change notification.
444 * free_pipes: List of all pipes that are currently closed.
445 * idle_pipes: List of open pipes that have no transactions.
446 * active_pipes: Active pipes indexed by transfer type.
447 * frame_number: Increments every SOF interrupt for time keeping.
448 * active_split: Points to the current active split, or NULL.
449 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300450struct cvmx_usb_state {
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300451 int init_flags;
452 int index;
453 int idle_hardware_channels;
454 union cvmx_usbcx_hprt usbcx_hprt;
455 struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
456 struct cvmx_usb_transaction *free_transaction_head;
457 struct cvmx_usb_transaction *free_transaction_tail;
458 struct cvmx_usb_pipe pipe[MAX_PIPES];
459 struct cvmx_usb_transaction transaction[MAX_TRANSACTIONS];
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300460 int indent;
461 struct cvmx_usb_port_status port_status;
462 struct cvmx_usb_pipe_list free_pipes;
463 struct cvmx_usb_pipe_list idle_pipes;
464 struct cvmx_usb_pipe_list active_pipes[4];
465 uint64_t frame_number;
466 struct cvmx_usb_transaction *active_split;
467 struct cvmx_usb_tx_fifo periodic;
468 struct cvmx_usb_tx_fifo nonperiodic;
469};
470
Aaro Koskinenb1649352013-06-01 21:42:58 +0300471struct octeon_hcd {
Aaro Koskinen771378b2013-06-13 01:00:31 +0300472 spinlock_t lock;
Aaro Koskinena24ed352013-07-30 23:43:05 +0300473 struct cvmx_usb_state usb;
Aaro Koskinen771378b2013-06-13 01:00:31 +0300474 struct tasklet_struct dequeue_tasklet;
475 struct list_head dequeue_list;
Aaro Koskinenb1649352013-06-01 21:42:58 +0300476};
477
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300478/* This macro spins on a field waiting for it to reach a value */
479#define CVMX_WAIT_FOR_FIELD32(address, type, field, op, value, timeout_usec)\
480 ({int result; \
481 do { \
482 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
483 octeon_get_clock_rate() / 1000000; \
484 type c; \
485 while (1) { \
486 c.u32 = __cvmx_usb_read_csr32(usb, address); \
487 if (c.s.field op (value)) { \
488 result = 0; \
489 break; \
490 } else if (cvmx_get_cycle() > done) { \
491 result = -1; \
492 break; \
493 } else \
494 cvmx_wait(100); \
495 } \
496 } while (0); \
497 result; })
498
499/*
500 * This macro logically sets a single field in a CSR. It does the sequence
501 * read, modify, and write
502 */
503#define USB_SET_FIELD32(address, type, field, value) \
504 do { \
505 type c; \
506 c.u32 = __cvmx_usb_read_csr32(usb, address); \
507 c.s.field = value; \
508 __cvmx_usb_write_csr32(usb, address, c.u32); \
509 } while (0)
510
511/* Returns the IO address to push/pop stuff data from the FIFOs */
512#define USB_FIFO_ADDRESS(channel, usb_index) (CVMX_USBCX_GOTGCTL(usb_index) + ((channel)+1)*0x1000)
513
514static int octeon_usb_get_clock_type(void)
515{
516 switch (cvmx_sysinfo_get()->board_type) {
517 case CVMX_BOARD_TYPE_BBGW_REF:
518 case CVMX_BOARD_TYPE_LANAI2_A:
519 case CVMX_BOARD_TYPE_LANAI2_U:
520 case CVMX_BOARD_TYPE_LANAI2_G:
521 case CVMX_BOARD_TYPE_UBNT_E100:
522 return USB_CLOCK_TYPE_CRYSTAL_12;
523 }
524 return USB_CLOCK_TYPE_REF_48;
525}
526
527/**
528 * Read a USB 32bit CSR. It performs the necessary address swizzle
529 * for 32bit CSRs and logs the value in a readable format if
530 * debugging is on.
531 *
532 * @usb: USB block this access is for
533 * @address: 64bit address to read
534 *
535 * Returns: Result of the read
536 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300537static inline uint32_t __cvmx_usb_read_csr32(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300538 uint64_t address)
539{
540 uint32_t result = cvmx_read64_uint32(address ^ 4);
541 return result;
542}
543
544
545/**
546 * Write a USB 32bit CSR. It performs the necessary address
547 * swizzle for 32bit CSRs and logs the value in a readable format
548 * if debugging is on.
549 *
550 * @usb: USB block this access is for
551 * @address: 64bit address to write
552 * @value: Value to write
553 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300554static inline void __cvmx_usb_write_csr32(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300555 uint64_t address, uint32_t value)
556{
557 cvmx_write64_uint32(address ^ 4, value);
558 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
559}
560
561
562/**
563 * Read a USB 64bit CSR. It logs the value in a readable format if
564 * debugging is on.
565 *
566 * @usb: USB block this access is for
567 * @address: 64bit address to read
568 *
569 * Returns: Result of the read
570 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300571static inline uint64_t __cvmx_usb_read_csr64(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300572 uint64_t address)
573{
574 uint64_t result = cvmx_read64_uint64(address);
575 return result;
576}
577
578
579/**
580 * Write a USB 64bit CSR. It logs the value in a readable format
581 * if debugging is on.
582 *
583 * @usb: USB block this access is for
584 * @address: 64bit address to write
585 * @value: Value to write
586 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300587static inline void __cvmx_usb_write_csr64(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300588 uint64_t address, uint64_t value)
589{
590 cvmx_write64_uint64(address, value);
591}
592
593/**
594 * Return non zero if this pipe connects to a non HIGH speed
595 * device through a high speed hub.
596 *
597 * @usb: USB block this access is for
598 * @pipe: Pipe to check
599 *
600 * Returns: Non zero if we need to do split transactions
601 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300602static inline int __cvmx_usb_pipe_needs_split(struct cvmx_usb_state *usb,
603 struct cvmx_usb_pipe *pipe)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300604{
605 return ((pipe->device_speed != CVMX_USB_SPEED_HIGH) && (usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH));
606}
607
608
609/**
610 * Trivial utility function to return the correct PID for a pipe
611 *
612 * @pipe: pipe to check
613 *
614 * Returns: PID for pipe
615 */
616static inline int __cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
617{
618 if (pipe->pid_toggle)
619 return 2; /* Data1 */
620 else
621 return 0; /* Data0 */
622}
623
624
625/**
626 * Return the number of USB ports supported by this Octeon
627 * chip. If the chip doesn't support USB, or is not supported
628 * by this API, a zero will be returned. Most Octeon chips
629 * support one usb port, but some support two ports.
630 * cvmx_usb_initialize() must be called on independent
631 * struct cvmx_usb_state.
632 *
633 * Returns: Number of port, zero if usb isn't supported
634 */
635static int cvmx_usb_get_num_ports(void)
636{
637 int arch_ports = 0;
638
639 if (OCTEON_IS_MODEL(OCTEON_CN56XX))
640 arch_ports = 1;
641 else if (OCTEON_IS_MODEL(OCTEON_CN52XX))
642 arch_ports = 2;
643 else if (OCTEON_IS_MODEL(OCTEON_CN50XX))
644 arch_ports = 1;
645 else if (OCTEON_IS_MODEL(OCTEON_CN31XX))
646 arch_ports = 1;
647 else if (OCTEON_IS_MODEL(OCTEON_CN30XX))
648 arch_ports = 1;
649 else
650 arch_ports = 0;
651
652 return arch_ports;
653}
654
655
656/**
657 * Allocate a usb transaction for use
658 *
659 * @usb: USB device state populated by
660 * cvmx_usb_initialize().
661 *
662 * Returns: Transaction or NULL
663 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300664static inline struct cvmx_usb_transaction *__cvmx_usb_alloc_transaction(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300665{
666 struct cvmx_usb_transaction *t;
667 t = usb->free_transaction_head;
668 if (t) {
669 usb->free_transaction_head = t->next;
670 if (!usb->free_transaction_head)
671 usb->free_transaction_tail = NULL;
672 }
673 if (t) {
674 memset(t, 0, sizeof(*t));
675 t->flags = __CVMX_USB_TRANSACTION_FLAGS_IN_USE;
676 }
677 return t;
678}
679
680
681/**
682 * Free a usb transaction
683 *
684 * @usb: USB device state populated by
685 * cvmx_usb_initialize().
686 * @transaction:
687 * Transaction to free
688 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300689static inline void __cvmx_usb_free_transaction(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300690 struct cvmx_usb_transaction *transaction)
691{
692 transaction->flags = 0;
693 transaction->prev = NULL;
694 transaction->next = NULL;
695 if (usb->free_transaction_tail)
696 usb->free_transaction_tail->next = transaction;
697 else
698 usb->free_transaction_head = transaction;
699 usb->free_transaction_tail = transaction;
700}
701
702
703/**
704 * Add a pipe to the tail of a list
705 * @list: List to add pipe to
706 * @pipe: Pipe to add
707 */
708static inline void __cvmx_usb_append_pipe(struct cvmx_usb_pipe_list *list, struct cvmx_usb_pipe *pipe)
709{
710 pipe->next = NULL;
711 pipe->prev = list->tail;
712 if (list->tail)
713 list->tail->next = pipe;
714 else
715 list->head = pipe;
716 list->tail = pipe;
717}
718
719
720/**
721 * Remove a pipe from a list
722 * @list: List to remove pipe from
723 * @pipe: Pipe to remove
724 */
725static inline void __cvmx_usb_remove_pipe(struct cvmx_usb_pipe_list *list, struct cvmx_usb_pipe *pipe)
726{
727 if (list->head == pipe) {
728 list->head = pipe->next;
729 pipe->next = NULL;
730 if (list->head)
731 list->head->prev = NULL;
732 else
733 list->tail = NULL;
734 } else if (list->tail == pipe) {
735 list->tail = pipe->prev;
736 list->tail->next = NULL;
737 pipe->prev = NULL;
738 } else {
739 pipe->prev->next = pipe->next;
740 pipe->next->prev = pipe->prev;
741 pipe->prev = NULL;
742 pipe->next = NULL;
743 }
744}
745
746
747/**
748 * Initialize a USB port for use. This must be called before any
749 * other access to the Octeon USB port is made. The port starts
750 * off in the disabled state.
751 *
Aaro Koskinencb61c602013-10-06 22:22:25 +0300752 * @usb: Pointer to an empty struct cvmx_usb_state
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300753 * that will be populated by the initialize call.
754 * This structure is then passed to all other USB
755 * functions.
756 * @usb_port_number:
757 * Which Octeon USB port to initialize.
758 *
759 * Returns: 0 or a negative error code.
760 */
Aaro Koskinencb61c602013-10-06 22:22:25 +0300761static int cvmx_usb_initialize(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300762 int usb_port_number)
763{
764 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
765 union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300766 enum cvmx_usb_initialize_flags flags = 0;
767
Aaro Koskinen6570b4a2013-10-06 22:22:24 +0300768 /* At first allow 0-1 for the usb port number */
769 if ((usb_port_number < 0) || (usb_port_number > 1))
770 return -EINVAL;
771 /* For all chips except 52XX there is only one port */
772 if (!OCTEON_IS_MODEL(OCTEON_CN52XX) && (usb_port_number > 0))
773 return -EINVAL;
774 /* Try to determine clock type automatically */
775 if (octeon_usb_get_clock_type() == USB_CLOCK_TYPE_CRYSTAL_12) {
776 /* Only 12 MHZ crystals are supported */
777 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
778 } else {
779 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
780
781 switch (octeon_usb_get_clock_type()) {
782 case USB_CLOCK_TYPE_REF_12:
783 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
784 break;
785 case USB_CLOCK_TYPE_REF_24:
786 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
787 break;
788 case USB_CLOCK_TYPE_REF_48:
789 flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
790 break;
791 default:
792 return -EINVAL;
793 break;
794 }
795 }
796
797 memset(usb, 0, sizeof(*usb));
798 usb->init_flags = flags;
799
800 /* Initialize the USB state structure */
801 {
802 int i;
803 usb->index = usb_port_number;
804
805 /* Initialize the transaction double linked list */
806 usb->free_transaction_head = NULL;
807 usb->free_transaction_tail = NULL;
808 for (i = 0; i < MAX_TRANSACTIONS; i++)
809 __cvmx_usb_free_transaction(usb, usb->transaction + i);
810 for (i = 0; i < MAX_PIPES; i++)
811 __cvmx_usb_append_pipe(&usb->free_pipes, usb->pipe + i);
812 }
813
814 /*
815 * Power On Reset and PHY Initialization
816 *
817 * 1. Wait for DCOK to assert (nothing to do)
818 *
819 * 2a. Write USBN0/1_CLK_CTL[POR] = 1 and
820 * USBN0/1_CLK_CTL[HRST,PRST,HCLK_RST] = 0
821 */
822 usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
823 usbn_clk_ctl.s.por = 1;
824 usbn_clk_ctl.s.hrst = 0;
825 usbn_clk_ctl.s.prst = 0;
826 usbn_clk_ctl.s.hclk_rst = 0;
827 usbn_clk_ctl.s.enable = 0;
828 /*
829 * 2b. Select the USB reference clock/crystal parameters by writing
830 * appropriate values to USBN0/1_CLK_CTL[P_C_SEL, P_RTYPE, P_COM_ON]
831 */
832 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
833 /*
834 * The USB port uses 12/24/48MHz 2.5V board clock
835 * source at USB_XO. USB_XI should be tied to GND.
836 * Most Octeon evaluation boards require this setting
837 */
838 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
839 /* From CN31XX,CN30XX manual */
840 usbn_clk_ctl.cn31xx.p_rclk = 1;
841 usbn_clk_ctl.cn31xx.p_xenbn = 0;
842 } else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
843 /* From CN56XX,CN50XX manual */
844 usbn_clk_ctl.cn56xx.p_rtype = 2;
845 else
846 /* From CN52XX manual */
847 usbn_clk_ctl.cn52xx.p_rtype = 1;
848
849 switch (flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
850 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
851 usbn_clk_ctl.s.p_c_sel = 0;
852 break;
853 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
854 usbn_clk_ctl.s.p_c_sel = 1;
855 break;
856 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
857 usbn_clk_ctl.s.p_c_sel = 2;
858 break;
859 }
860 } else {
861 /*
862 * The USB port uses a 12MHz crystal as clock source
863 * at USB_XO and USB_XI
864 */
865 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
866 /* From CN31XX,CN30XX manual */
867 usbn_clk_ctl.cn31xx.p_rclk = 1;
868 usbn_clk_ctl.cn31xx.p_xenbn = 1;
869 } else if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
870 /* From CN56XX,CN50XX manual */
871 usbn_clk_ctl.cn56xx.p_rtype = 0;
872 else
873 /* From CN52XX manual */
874 usbn_clk_ctl.cn52xx.p_rtype = 0;
875
876 usbn_clk_ctl.s.p_c_sel = 0;
877 }
878 /*
879 * 2c. Select the HCLK via writing USBN0/1_CLK_CTL[DIVIDE, DIVIDE2] and
880 * setting USBN0/1_CLK_CTL[ENABLE] = 1. Divide the core clock down
881 * such that USB is as close as possible to 125Mhz
882 */
883 {
884 int divisor = (octeon_get_clock_rate()+125000000-1)/125000000;
885 /* Lower than 4 doesn't seem to work properly */
886 if (divisor < 4)
887 divisor = 4;
888 usbn_clk_ctl.s.divide = divisor;
889 usbn_clk_ctl.s.divide2 = 0;
890 }
891 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
892 usbn_clk_ctl.u64);
893 /* 2d. Write USBN0/1_CLK_CTL[HCLK_RST] = 1 */
894 usbn_clk_ctl.s.hclk_rst = 1;
895 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
896 usbn_clk_ctl.u64);
897 /* 2e. Wait 64 core-clock cycles for HCLK to stabilize */
898 cvmx_wait(64);
899 /*
900 * 3. Program the power-on reset field in the USBN clock-control
901 * register:
902 * USBN_CLK_CTL[POR] = 0
903 */
904 usbn_clk_ctl.s.por = 0;
905 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
906 usbn_clk_ctl.u64);
907 /* 4. Wait 1 ms for PHY clock to start */
908 mdelay(1);
909 /*
910 * 5. Program the Reset input from automatic test equipment field in the
911 * USBP control and status register:
912 * USBN_USBP_CTL_STATUS[ATE_RESET] = 1
913 */
914 usbn_usbp_ctl_status.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index));
915 usbn_usbp_ctl_status.s.ate_reset = 1;
916 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
917 usbn_usbp_ctl_status.u64);
918 /* 6. Wait 10 cycles */
919 cvmx_wait(10);
920 /*
921 * 7. Clear ATE_RESET field in the USBN clock-control register:
922 * USBN_USBP_CTL_STATUS[ATE_RESET] = 0
923 */
924 usbn_usbp_ctl_status.s.ate_reset = 0;
925 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
926 usbn_usbp_ctl_status.u64);
927 /*
928 * 8. Program the PHY reset field in the USBN clock-control register:
929 * USBN_CLK_CTL[PRST] = 1
930 */
931 usbn_clk_ctl.s.prst = 1;
932 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
933 usbn_clk_ctl.u64);
934 /*
935 * 9. Program the USBP control and status register to select host or
936 * device mode. USBN_USBP_CTL_STATUS[HST_MODE] = 0 for host, = 1 for
937 * device
938 */
939 usbn_usbp_ctl_status.s.hst_mode = 0;
940 __cvmx_usb_write_csr64(usb, CVMX_USBNX_USBP_CTL_STATUS(usb->index),
941 usbn_usbp_ctl_status.u64);
942 /* 10. Wait 1 us */
943 udelay(1);
944 /*
945 * 11. Program the hreset_n field in the USBN clock-control register:
946 * USBN_CLK_CTL[HRST] = 1
947 */
948 usbn_clk_ctl.s.hrst = 1;
949 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
950 usbn_clk_ctl.u64);
951 /* 12. Proceed to USB core initialization */
952 usbn_clk_ctl.s.enable = 1;
953 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
954 usbn_clk_ctl.u64);
955 udelay(1);
956
957 /*
958 * USB Core Initialization
959 *
960 * 1. Read USBC_GHWCFG1, USBC_GHWCFG2, USBC_GHWCFG3, USBC_GHWCFG4 to
961 * determine USB core configuration parameters.
962 *
963 * Nothing needed
964 *
965 * 2. Program the following fields in the global AHB configuration
966 * register (USBC_GAHBCFG)
967 * DMA mode, USBC_GAHBCFG[DMAEn]: 1 = DMA mode, 0 = slave mode
968 * Burst length, USBC_GAHBCFG[HBSTLEN] = 0
969 * Nonperiodic TxFIFO empty level (slave mode only),
970 * USBC_GAHBCFG[NPTXFEMPLVL]
971 * Periodic TxFIFO empty level (slave mode only),
972 * USBC_GAHBCFG[PTXFEMPLVL]
973 * Global interrupt mask, USBC_GAHBCFG[GLBLINTRMSK] = 1
974 */
975 {
976 union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
977 /* Due to an errata, CN31XX doesn't support DMA */
978 if (OCTEON_IS_MODEL(OCTEON_CN31XX))
979 usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
980 usbcx_gahbcfg.u32 = 0;
981 usbcx_gahbcfg.s.dmaen = !(usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
982 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
983 /* Only use one channel with non DMA */
984 usb->idle_hardware_channels = 0x1;
985 else if (OCTEON_IS_MODEL(OCTEON_CN5XXX))
986 /* CN5XXX have an errata with channel 3 */
987 usb->idle_hardware_channels = 0xf7;
988 else
989 usb->idle_hardware_channels = 0xff;
990 usbcx_gahbcfg.s.hbstlen = 0;
991 usbcx_gahbcfg.s.nptxfemplvl = 1;
992 usbcx_gahbcfg.s.ptxfemplvl = 1;
993 usbcx_gahbcfg.s.glblintrmsk = 1;
994 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
995 usbcx_gahbcfg.u32);
996 }
997 /*
998 * 3. Program the following fields in USBC_GUSBCFG register.
999 * HS/FS timeout calibration, USBC_GUSBCFG[TOUTCAL] = 0
1000 * ULPI DDR select, USBC_GUSBCFG[DDRSEL] = 0
1001 * USB turnaround time, USBC_GUSBCFG[USBTRDTIM] = 0x5
1002 * PHY low-power clock select, USBC_GUSBCFG[PHYLPWRCLKSEL] = 0
1003 */
1004 {
1005 union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
1006 usbcx_gusbcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index));
1007 usbcx_gusbcfg.s.toutcal = 0;
1008 usbcx_gusbcfg.s.ddrsel = 0;
1009 usbcx_gusbcfg.s.usbtrdtim = 0x5;
1010 usbcx_gusbcfg.s.phylpwrclksel = 0;
1011 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
1012 usbcx_gusbcfg.u32);
1013 }
1014 /*
1015 * 4. The software must unmask the following bits in the USBC_GINTMSK
1016 * register.
1017 * OTG interrupt mask, USBC_GINTMSK[OTGINTMSK] = 1
1018 * Mode mismatch interrupt mask, USBC_GINTMSK[MODEMISMSK] = 1
1019 */
1020 {
1021 union cvmx_usbcx_gintmsk usbcx_gintmsk;
1022 int channel;
1023
1024 usbcx_gintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTMSK(usb->index));
1025 usbcx_gintmsk.s.otgintmsk = 1;
1026 usbcx_gintmsk.s.modemismsk = 1;
1027 usbcx_gintmsk.s.hchintmsk = 1;
1028 usbcx_gintmsk.s.sofmsk = 0;
1029 /* We need RX FIFO interrupts if we don't have DMA */
1030 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1031 usbcx_gintmsk.s.rxflvlmsk = 1;
1032 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
1033 usbcx_gintmsk.u32);
1034
1035 /*
1036 * Disable all channel interrupts. We'll enable them per channel
1037 * later.
1038 */
1039 for (channel = 0; channel < 8; channel++)
1040 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
1041 }
1042
1043 {
1044 /*
1045 * Host Port Initialization
1046 *
1047 * 1. Program the host-port interrupt-mask field to unmask,
1048 * USBC_GINTMSK[PRTINT] = 1
1049 */
1050 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk,
1051 prtintmsk, 1);
1052 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk,
1053 disconnintmsk, 1);
1054 /*
1055 * 2. Program the USBC_HCFG register to select full-speed host
1056 * or high-speed host.
1057 */
1058 {
1059 union cvmx_usbcx_hcfg usbcx_hcfg;
1060 usbcx_hcfg.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
1061 usbcx_hcfg.s.fslssupp = 0;
1062 usbcx_hcfg.s.fslspclksel = 0;
1063 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
1064 }
1065 /*
1066 * 3. Program the port power bit to drive VBUS on the USB,
1067 * USBC_HPRT[PRTPWR] = 1
1068 */
1069 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtpwr, 1);
1070
1071 /*
1072 * Steps 4-15 from the manual are done later in the port enable
1073 */
1074 }
1075
1076 return 0;
1077}
1078
1079
1080/**
1081 * Shutdown a USB port after a call to cvmx_usb_initialize().
1082 * The port should be disabled with all pipes closed when this
1083 * function is called.
1084 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001085 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001086 *
1087 * Returns: 0 or a negative error code.
1088 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001089static int cvmx_usb_shutdown(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001090{
1091 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001092
1093 /* Make sure all pipes are closed */
1094 if (usb->idle_pipes.head ||
1095 usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS].head ||
1096 usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT].head ||
1097 usb->active_pipes[CVMX_USB_TRANSFER_CONTROL].head ||
1098 usb->active_pipes[CVMX_USB_TRANSFER_BULK].head)
1099 return -EBUSY;
1100
1101 /* Disable the clocks and put them in power on reset */
1102 usbn_clk_ctl.u64 = __cvmx_usb_read_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index));
1103 usbn_clk_ctl.s.enable = 1;
1104 usbn_clk_ctl.s.por = 1;
1105 usbn_clk_ctl.s.hclk_rst = 1;
1106 usbn_clk_ctl.s.prst = 0;
1107 usbn_clk_ctl.s.hrst = 0;
1108 __cvmx_usb_write_csr64(usb, CVMX_USBNX_CLK_CTL(usb->index),
1109 usbn_clk_ctl.u64);
1110 return 0;
1111}
1112
1113
1114/**
1115 * Enable a USB port. After this call succeeds, the USB port is
1116 * online and servicing requests.
1117 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001118 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001119 *
1120 * Returns: 0 or a negative error code.
1121 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001122static int cvmx_usb_enable(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001123{
1124 union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001125
1126 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1127
1128 /*
1129 * If the port is already enabled the just return. We don't need to do
1130 * anything
1131 */
1132 if (usb->usbcx_hprt.s.prtena)
1133 return 0;
1134
1135 /* If there is nothing plugged into the port then fail immediately */
1136 if (!usb->usbcx_hprt.s.prtconnsts) {
1137 return -ETIMEDOUT;
1138 }
1139
1140 /* Program the port reset bit to start the reset process */
1141 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtrst, 1);
1142
1143 /*
1144 * Wait at least 50ms (high speed), or 10ms (full speed) for the reset
1145 * process to complete.
1146 */
1147 mdelay(50);
1148
1149 /* Program the port reset bit to 0, USBC_HPRT[PRTRST] = 0 */
1150 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtrst, 0);
1151
1152 /* Wait for the USBC_HPRT[PRTENA]. */
1153 if (CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt,
1154 prtena, ==, 1, 100000))
1155 return -ETIMEDOUT;
1156
1157 /*
1158 * Read the port speed field to get the enumerated speed,
1159 * USBC_HPRT[PRTSPD].
1160 */
1161 usb->usbcx_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1162 usbcx_ghwcfg3.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GHWCFG3(usb->index));
1163
1164 /*
1165 * 13. Program the USBC_GRXFSIZ register to select the size of the
1166 * receive FIFO (25%).
1167 */
1168 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), union cvmx_usbcx_grxfsiz,
1169 rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
1170 /*
1171 * 14. Program the USBC_GNPTXFSIZ register to select the size and the
1172 * start address of the non- periodic transmit FIFO for nonperiodic
1173 * transactions (50%).
1174 */
1175 {
1176 union cvmx_usbcx_gnptxfsiz siz;
1177 siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
1178 siz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
1179 siz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
1180 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), siz.u32);
1181 }
1182 /*
1183 * 15. Program the USBC_HPTXFSIZ register to select the size and start
1184 * address of the periodic transmit FIFO for periodic transactions
1185 * (25%).
1186 */
1187 {
1188 union cvmx_usbcx_hptxfsiz siz;
1189 siz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
1190 siz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
1191 siz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
1192 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), siz.u32);
1193 }
1194 /* Flush all FIFOs */
1195 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl, txfnum, 0x10);
1196 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl, txfflsh, 1);
1197 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl,
1198 txfflsh, ==, 0, 100);
1199 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl, rxfflsh, 1);
1200 CVMX_WAIT_FOR_FIELD32(CVMX_USBCX_GRSTCTL(usb->index), union cvmx_usbcx_grstctl,
1201 rxfflsh, ==, 0, 100);
1202
1203 return 0;
1204}
1205
1206
1207/**
1208 * Disable a USB port. After this call the USB port will not
1209 * generate data transfers and will not generate events.
1210 * Transactions in process will fail and call their
1211 * associated callbacks.
1212 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001213 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001214 *
1215 * Returns: 0 or a negative error code.
1216 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001217static int cvmx_usb_disable(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001218{
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001219 /* Disable the port */
1220 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), union cvmx_usbcx_hprt, prtena, 1);
1221 return 0;
1222}
1223
1224
1225/**
1226 * Get the current state of the USB port. Use this call to
1227 * determine if the usb port has anything connected, is enabled,
1228 * or has some sort of error condition. The return value of this
1229 * call has "changed" bits to signal of the value of some fields
Aaro Koskinen29a202f2013-10-06 22:22:26 +03001230 * have changed between calls.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001231 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001232 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001233 *
1234 * Returns: Port status information
1235 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001236static struct cvmx_usb_port_status cvmx_usb_get_status(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001237{
1238 union cvmx_usbcx_hprt usbc_hprt;
1239 struct cvmx_usb_port_status result;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001240
1241 memset(&result, 0, sizeof(result));
1242
1243 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1244 result.port_enabled = usbc_hprt.s.prtena;
1245 result.port_over_current = usbc_hprt.s.prtovrcurract;
1246 result.port_powered = usbc_hprt.s.prtpwr;
1247 result.port_speed = usbc_hprt.s.prtspd;
1248 result.connected = usbc_hprt.s.prtconnsts;
1249 result.connect_change = (result.connected != usb->port_status.connected);
1250
1251 return result;
1252}
1253
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001254/**
1255 * Convert a USB transaction into a handle
1256 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001257 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001258 * @transaction:
1259 * Transaction to get handle for
1260 *
1261 * Returns: Handle
1262 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001263static inline int __cvmx_usb_get_submit_handle(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001264 struct cvmx_usb_transaction *transaction)
1265{
1266 return ((unsigned long)transaction - (unsigned long)usb->transaction) /
1267 sizeof(*transaction);
1268}
1269
1270
1271/**
1272 * Convert a USB pipe into a handle
1273 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001274 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001275 * @pipe: Pipe to get handle for
1276 *
1277 * Returns: Handle
1278 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001279static inline int __cvmx_usb_get_pipe_handle(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001280 struct cvmx_usb_pipe *pipe)
1281{
1282 return ((unsigned long)pipe - (unsigned long)usb->pipe) / sizeof(*pipe);
1283}
1284
1285
1286/**
1287 * Open a virtual pipe between the host and a USB device. A pipe
1288 * must be opened before data can be transferred between a device
1289 * and Octeon.
1290 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001291 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001292 * @device_addr:
1293 * USB device address to open the pipe to
1294 * (0-127).
1295 * @endpoint_num:
1296 * USB endpoint number to open the pipe to
1297 * (0-15).
1298 * @device_speed:
1299 * The speed of the device the pipe is going
1300 * to. This must match the device's speed,
1301 * which may be different than the port speed.
1302 * @max_packet: The maximum packet length the device can
1303 * transmit/receive (low speed=0-8, full
1304 * speed=0-1023, high speed=0-1024). This value
1305 * comes from the standard endpoint descriptor
1306 * field wMaxPacketSize bits <10:0>.
1307 * @transfer_type:
1308 * The type of transfer this pipe is for.
1309 * @transfer_dir:
1310 * The direction the pipe is in. This is not
1311 * used for control pipes.
1312 * @interval: For ISOCHRONOUS and INTERRUPT transfers,
1313 * this is how often the transfer is scheduled
1314 * for. All other transfers should specify
1315 * zero. The units are in frames (8000/sec at
1316 * high speed, 1000/sec for full speed).
1317 * @multi_count:
1318 * For high speed devices, this is the maximum
1319 * allowed number of packet per microframe.
1320 * Specify zero for non high speed devices. This
1321 * value comes from the standard endpoint descriptor
1322 * field wMaxPacketSize bits <12:11>.
1323 * @hub_device_addr:
1324 * Hub device address this device is connected
1325 * to. Devices connected directly to Octeon
1326 * use zero. This is only used when the device
1327 * is full/low speed behind a high speed hub.
1328 * The address will be of the high speed hub,
1329 * not and full speed hubs after it.
1330 * @hub_port: Which port on the hub the device is
1331 * connected. Use zero for devices connected
1332 * directly to Octeon. Like hub_device_addr,
1333 * this is only used for full/low speed
1334 * devices behind a high speed hub.
1335 *
1336 * Returns: A non negative value is a pipe handle. Negative
1337 * values are error codes.
1338 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001339static int cvmx_usb_open_pipe(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001340 int device_addr, int endpoint_num,
1341 enum cvmx_usb_speed device_speed, int max_packet,
1342 enum cvmx_usb_transfer transfer_type,
1343 enum cvmx_usb_direction transfer_dir,
1344 int interval, int multi_count,
1345 int hub_device_addr, int hub_port)
1346{
1347 struct cvmx_usb_pipe *pipe;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001348
1349 if (unlikely((device_addr < 0) || (device_addr > MAX_USB_ADDRESS)))
1350 return -EINVAL;
1351 if (unlikely((endpoint_num < 0) || (endpoint_num > MAX_USB_ENDPOINT)))
1352 return -EINVAL;
1353 if (unlikely(device_speed > CVMX_USB_SPEED_LOW))
1354 return -EINVAL;
1355 if (unlikely((max_packet <= 0) || (max_packet > 1024)))
1356 return -EINVAL;
1357 if (unlikely(transfer_type > CVMX_USB_TRANSFER_INTERRUPT))
1358 return -EINVAL;
1359 if (unlikely((transfer_dir != CVMX_USB_DIRECTION_OUT) &&
1360 (transfer_dir != CVMX_USB_DIRECTION_IN)))
1361 return -EINVAL;
1362 if (unlikely(interval < 0))
1363 return -EINVAL;
1364 if (unlikely((transfer_type == CVMX_USB_TRANSFER_CONTROL) && interval))
1365 return -EINVAL;
1366 if (unlikely(multi_count < 0))
1367 return -EINVAL;
1368 if (unlikely((device_speed != CVMX_USB_SPEED_HIGH) &&
1369 (multi_count != 0)))
1370 return -EINVAL;
1371 if (unlikely((hub_device_addr < 0) || (hub_device_addr > MAX_USB_ADDRESS)))
1372 return -EINVAL;
1373 if (unlikely((hub_port < 0) || (hub_port > MAX_USB_HUB_PORT)))
1374 return -EINVAL;
1375
1376 /* Find a free pipe */
1377 pipe = usb->free_pipes.head;
1378 if (!pipe)
1379 return -ENOMEM;
1380 __cvmx_usb_remove_pipe(&usb->free_pipes, pipe);
1381 pipe->flags = __CVMX_USB_PIPE_FLAGS_OPEN;
1382 if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1383 (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1384 (transfer_type == CVMX_USB_TRANSFER_BULK))
1385 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
1386 pipe->device_addr = device_addr;
1387 pipe->endpoint_num = endpoint_num;
1388 pipe->device_speed = device_speed;
1389 pipe->max_packet = max_packet;
1390 pipe->transfer_type = transfer_type;
1391 pipe->transfer_dir = transfer_dir;
1392 /*
1393 * All pipes use interval to rate limit NAK processing. Force an
1394 * interval if one wasn't supplied
1395 */
1396 if (!interval)
1397 interval = 1;
1398 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1399 pipe->interval = interval*8;
1400 /* Force start splits to be schedule on uFrame 0 */
1401 pipe->next_tx_frame = ((usb->frame_number+7)&~7) + pipe->interval;
1402 } else {
1403 pipe->interval = interval;
1404 pipe->next_tx_frame = usb->frame_number + pipe->interval;
1405 }
1406 pipe->multi_count = multi_count;
1407 pipe->hub_device_addr = hub_device_addr;
1408 pipe->hub_port = hub_port;
1409 pipe->pid_toggle = 0;
1410 pipe->split_sc_frame = -1;
1411 __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
1412
1413 /*
1414 * We don't need to tell the hardware about this pipe yet since
1415 * it doesn't have any submitted requests
1416 */
1417
1418 return __cvmx_usb_get_pipe_handle(usb, pipe);
1419}
1420
1421
1422/**
1423 * Poll the RX FIFOs and remove data as needed. This function is only used
1424 * in non DMA mode. It is very important that this function be called quickly
1425 * enough to prevent FIFO overflow.
1426 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001427 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001428 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001429static void __cvmx_usb_poll_rx_fifo(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001430{
1431 union cvmx_usbcx_grxstsph rx_status;
1432 int channel;
1433 int bytes;
1434 uint64_t address;
1435 uint32_t *ptr;
1436
1437 rx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GRXSTSPH(usb->index));
1438 /* Only read data if IN data is there */
1439 if (rx_status.s.pktsts != 2)
1440 return;
1441 /* Check if no data is available */
1442 if (!rx_status.s.bcnt)
1443 return;
1444
1445 channel = rx_status.s.chnum;
1446 bytes = rx_status.s.bcnt;
1447 if (!bytes)
1448 return;
1449
1450 /* Get where the DMA engine would have written this data */
1451 address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8);
1452 ptr = cvmx_phys_to_ptr(address);
1453 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, address + bytes);
1454
1455 /* Loop writing the FIFO data for this packet into memory */
1456 while (bytes > 0) {
1457 *ptr++ = __cvmx_usb_read_csr32(usb, USB_FIFO_ADDRESS(channel, usb->index));
1458 bytes -= 4;
1459 }
1460 CVMX_SYNCW;
1461
1462 return;
1463}
1464
1465
1466/**
1467 * Fill the TX hardware fifo with data out of the software
1468 * fifos
1469 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001470 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001471 * @fifo: Software fifo to use
1472 * @available: Amount of space in the hardware fifo
1473 *
1474 * Returns: Non zero if the hardware fifo was too small and needs
1475 * to be serviced again.
1476 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001477static int __cvmx_usb_fill_tx_hw(struct cvmx_usb_state *usb,
1478 struct cvmx_usb_tx_fifo *fifo, int available)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001479{
1480 /*
1481 * We're done either when there isn't anymore space or the software FIFO
1482 * is empty
1483 */
1484 while (available && (fifo->head != fifo->tail)) {
1485 int i = fifo->tail;
1486 const uint32_t *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1487 uint64_t csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel, usb->index) ^ 4;
1488 int words = available;
1489
1490 /* Limit the amount of data to waht the SW fifo has */
1491 if (fifo->entry[i].size <= available) {
1492 words = fifo->entry[i].size;
1493 fifo->tail++;
1494 if (fifo->tail > MAX_CHANNELS)
1495 fifo->tail = 0;
1496 }
1497
1498 /* Update the next locations and counts */
1499 available -= words;
1500 fifo->entry[i].address += words * 4;
1501 fifo->entry[i].size -= words;
1502
1503 /*
1504 * Write the HW fifo data. The read every three writes is due
1505 * to an errata on CN3XXX chips
1506 */
1507 while (words > 3) {
1508 cvmx_write64_uint32(csr_address, *ptr++);
1509 cvmx_write64_uint32(csr_address, *ptr++);
1510 cvmx_write64_uint32(csr_address, *ptr++);
1511 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1512 words -= 3;
1513 }
1514 cvmx_write64_uint32(csr_address, *ptr++);
1515 if (--words) {
1516 cvmx_write64_uint32(csr_address, *ptr++);
1517 if (--words)
1518 cvmx_write64_uint32(csr_address, *ptr++);
1519 }
1520 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1521 }
1522 return fifo->head != fifo->tail;
1523}
1524
1525
1526/**
1527 * Check the hardware FIFOs and fill them as needed
1528 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001529 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001530 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001531static void __cvmx_usb_poll_tx_fifo(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001532{
1533 if (usb->periodic.head != usb->periodic.tail) {
1534 union cvmx_usbcx_hptxsts tx_status;
1535 tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXSTS(usb->index));
1536 if (__cvmx_usb_fill_tx_hw(usb, &usb->periodic, tx_status.s.ptxfspcavail))
1537 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, ptxfempmsk, 1);
1538 else
1539 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, ptxfempmsk, 0);
1540 }
1541
1542 if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1543 union cvmx_usbcx_gnptxsts tx_status;
1544 tx_status.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXSTS(usb->index));
1545 if (__cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic, tx_status.s.nptxfspcavail))
1546 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, nptxfempmsk, 1);
1547 else
1548 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, nptxfempmsk, 0);
1549 }
1550
1551 return;
1552}
1553
1554
1555/**
1556 * Fill the TX FIFO with an outgoing packet
1557 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001558 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001559 * @channel: Channel number to get packet from
1560 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001561static void __cvmx_usb_fill_tx_fifo(struct cvmx_usb_state *usb, int channel)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001562{
1563 union cvmx_usbcx_hccharx hcchar;
1564 union cvmx_usbcx_hcspltx usbc_hcsplt;
1565 union cvmx_usbcx_hctsizx usbc_hctsiz;
1566 struct cvmx_usb_tx_fifo *fifo;
1567
1568 /* We only need to fill data on outbound channels */
1569 hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
1570 if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1571 return;
1572
1573 /* OUT Splits only have data on the start and not the complete */
1574 usbc_hcsplt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index));
1575 if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1576 return;
1577
1578 /*
1579 * Find out how many bytes we need to fill and convert it into 32bit
1580 * words.
1581 */
1582 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1583 if (!usbc_hctsiz.s.xfersize)
1584 return;
1585
1586 if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1587 (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1588 fifo = &usb->periodic;
1589 else
1590 fifo = &usb->nonperiodic;
1591
1592 fifo->entry[fifo->head].channel = channel;
1593 fifo->entry[fifo->head].address = __cvmx_usb_read_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8);
1594 fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize+3)>>2;
1595 fifo->head++;
1596 if (fifo->head > MAX_CHANNELS)
1597 fifo->head = 0;
1598
1599 __cvmx_usb_poll_tx_fifo(usb);
1600
1601 return;
1602}
1603
1604/**
1605 * Perform channel specific setup for Control transactions. All
1606 * the generic stuff will already have been done in
1607 * __cvmx_usb_start_channel()
1608 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001609 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001610 * @channel: Channel to setup
1611 * @pipe: Pipe for control transaction
1612 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001613static void __cvmx_usb_start_channel_control(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001614 int channel,
1615 struct cvmx_usb_pipe *pipe)
1616{
1617 struct cvmx_usb_transaction *transaction = pipe->head;
1618 union cvmx_usb_control_header *header =
1619 cvmx_phys_to_ptr(transaction->control_header);
1620 int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1621 int packets_to_transfer;
1622 union cvmx_usbcx_hctsizx usbc_hctsiz;
1623
1624 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
1625
1626 switch (transaction->stage) {
1627 case CVMX_USB_STAGE_NON_CONTROL:
1628 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1629 cvmx_dprintf("%s: ERROR - Non control stage\n", __FUNCTION__);
1630 break;
1631 case CVMX_USB_STAGE_SETUP:
1632 usbc_hctsiz.s.pid = 3; /* Setup */
1633 bytes_to_transfer = sizeof(*header);
1634 /* All Control operations start with a setup going OUT */
1635 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir, CVMX_USB_DIRECTION_OUT);
1636 /*
1637 * Setup send the control header instead of the buffer data. The
1638 * buffer data will be used in the next stage
1639 */
1640 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, transaction->control_header);
1641 break;
1642 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1643 usbc_hctsiz.s.pid = 3; /* Setup */
1644 bytes_to_transfer = 0;
1645 /* All Control operations start with a setup going OUT */
1646 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir, CVMX_USB_DIRECTION_OUT);
1647 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), union cvmx_usbcx_hcspltx, compsplt, 1);
1648 break;
1649 case CVMX_USB_STAGE_DATA:
1650 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1651 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1652 if (header->s.request_type & 0x80)
1653 bytes_to_transfer = 0;
1654 else if (bytes_to_transfer > pipe->max_packet)
1655 bytes_to_transfer = pipe->max_packet;
1656 }
1657 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1658 union cvmx_usbcx_hccharx, epdir,
1659 ((header->s.request_type & 0x80) ?
1660 CVMX_USB_DIRECTION_IN :
1661 CVMX_USB_DIRECTION_OUT));
1662 break;
1663 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1664 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1665 if (!(header->s.request_type & 0x80))
1666 bytes_to_transfer = 0;
1667 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1668 union cvmx_usbcx_hccharx, epdir,
1669 ((header->s.request_type & 0x80) ?
1670 CVMX_USB_DIRECTION_IN :
1671 CVMX_USB_DIRECTION_OUT));
1672 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), union cvmx_usbcx_hcspltx, compsplt, 1);
1673 break;
1674 case CVMX_USB_STAGE_STATUS:
1675 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1676 bytes_to_transfer = 0;
1677 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir,
1678 ((header->s.request_type & 0x80) ?
1679 CVMX_USB_DIRECTION_OUT :
1680 CVMX_USB_DIRECTION_IN));
1681 break;
1682 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1683 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1684 bytes_to_transfer = 0;
1685 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, epdir,
1686 ((header->s.request_type & 0x80) ?
1687 CVMX_USB_DIRECTION_OUT :
1688 CVMX_USB_DIRECTION_IN));
1689 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index), union cvmx_usbcx_hcspltx, compsplt, 1);
1690 break;
1691 }
1692
1693 /*
1694 * Make sure the transfer never exceeds the byte limit of the hardware.
1695 * Further bytes will be sent as continued transactions
1696 */
1697 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1698 /* Round MAX_TRANSFER_BYTES to a multiple of out packet size */
1699 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1700 bytes_to_transfer *= pipe->max_packet;
1701 }
1702
1703 /*
1704 * Calculate the number of packets to transfer. If the length is zero
1705 * we still need to transfer one packet
1706 */
1707 packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1708 if (packets_to_transfer == 0)
1709 packets_to_transfer = 1;
1710 else if ((packets_to_transfer > 1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1711 /*
1712 * Limit to one packet when not using DMA. Channels must be
1713 * restarted between every packet for IN transactions, so there
1714 * is no reason to do multiple packets in a row
1715 */
1716 packets_to_transfer = 1;
1717 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1718 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1719 /*
1720 * Limit the number of packet and data transferred to what the
1721 * hardware can handle
1722 */
1723 packets_to_transfer = MAX_TRANSFER_PACKETS;
1724 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1725 }
1726
1727 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1728 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1729
1730 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1731 return;
1732}
1733
1734
1735/**
1736 * Start a channel to perform the pipe's head transaction
1737 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03001738 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001739 * @channel: Channel to setup
1740 * @pipe: Pipe to start
1741 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03001742static void __cvmx_usb_start_channel(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03001743 int channel,
1744 struct cvmx_usb_pipe *pipe)
1745{
1746 struct cvmx_usb_transaction *transaction = pipe->head;
1747
1748 /* Make sure all writes to the DMA region get flushed */
1749 CVMX_SYNCW;
1750
1751 /* Attach the channel to the pipe */
1752 usb->pipe_for_channel[channel] = pipe;
1753 pipe->channel = channel;
1754 pipe->flags |= __CVMX_USB_PIPE_FLAGS_SCHEDULED;
1755
1756 /* Mark this channel as in use */
1757 usb->idle_hardware_channels &= ~(1<<channel);
1758
1759 /* Enable the channel interrupt bits */
1760 {
1761 union cvmx_usbcx_hcintx usbc_hcint;
1762 union cvmx_usbcx_hcintmskx usbc_hcintmsk;
1763 union cvmx_usbcx_haintmsk usbc_haintmsk;
1764
1765 /* Clear all channel status bits */
1766 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
1767 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index), usbc_hcint.u32);
1768
1769 usbc_hcintmsk.u32 = 0;
1770 usbc_hcintmsk.s.chhltdmsk = 1;
1771 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1772 /*
1773 * Channels need these extra interrupts when we aren't
1774 * in DMA mode.
1775 */
1776 usbc_hcintmsk.s.datatglerrmsk = 1;
1777 usbc_hcintmsk.s.frmovrunmsk = 1;
1778 usbc_hcintmsk.s.bblerrmsk = 1;
1779 usbc_hcintmsk.s.xacterrmsk = 1;
1780 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1781 /*
1782 * Splits don't generate xfercompl, so we need
1783 * ACK and NYET.
1784 */
1785 usbc_hcintmsk.s.nyetmsk = 1;
1786 usbc_hcintmsk.s.ackmsk = 1;
1787 }
1788 usbc_hcintmsk.s.nakmsk = 1;
1789 usbc_hcintmsk.s.stallmsk = 1;
1790 usbc_hcintmsk.s.xfercomplmsk = 1;
1791 }
1792 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), usbc_hcintmsk.u32);
1793
1794 /* Enable the channel interrupt to propagate */
1795 usbc_haintmsk.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index));
1796 usbc_haintmsk.s.haintmsk |= 1<<channel;
1797 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index), usbc_haintmsk.u32);
1798 }
1799
1800 /* Setup the locations the DMA engines use */
1801 {
1802 uint64_t dma_address = transaction->buffer + transaction->actual_bytes;
1803 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1804 dma_address = transaction->buffer + transaction->iso_packets[0].offset + transaction->actual_bytes;
1805 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) + channel*8, dma_address);
1806 __cvmx_usb_write_csr64(usb, CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel*8, dma_address);
1807 }
1808
1809 /* Setup both the size of the transfer and the SPLIT characteristics */
1810 {
1811 union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
1812 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
1813 int packets_to_transfer;
1814 int bytes_to_transfer = transaction->buffer_length - transaction->actual_bytes;
1815
1816 /*
1817 * ISOCHRONOUS transactions store each individual transfer size
1818 * in the packet structure, not the global buffer_length
1819 */
1820 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1821 bytes_to_transfer = transaction->iso_packets[0].length - transaction->actual_bytes;
1822
1823 /*
1824 * We need to do split transactions when we are talking to non
1825 * high speed devices that are behind a high speed hub
1826 */
1827 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
1828 /*
1829 * On the start split phase (stage is even) record the
1830 * frame number we will need to send the split complete.
1831 * We only store the lower two bits since the time ahead
1832 * can only be two frames
1833 */
1834 if ((transaction->stage&1) == 0) {
1835 if (transaction->type == CVMX_USB_TRANSFER_BULK)
1836 pipe->split_sc_frame = (usb->frame_number + 1) & 0x7f;
1837 else
1838 pipe->split_sc_frame = (usb->frame_number + 2) & 0x7f;
1839 } else
1840 pipe->split_sc_frame = -1;
1841
1842 usbc_hcsplt.s.spltena = 1;
1843 usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1844 usbc_hcsplt.s.prtaddr = pipe->hub_port;
1845 usbc_hcsplt.s.compsplt = (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1846
1847 /*
1848 * SPLIT transactions can only ever transmit one data
1849 * packet so limit the transfer size to the max packet
1850 * size
1851 */
1852 if (bytes_to_transfer > pipe->max_packet)
1853 bytes_to_transfer = pipe->max_packet;
1854
1855 /*
1856 * ISOCHRONOUS OUT splits are unique in that they limit
1857 * data transfers to 188 byte chunks representing the
1858 * begin/middle/end of the data or all
1859 */
1860 if (!usbc_hcsplt.s.compsplt &&
1861 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1862 (pipe->transfer_type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1863 /*
1864 * Clear the split complete frame number as
1865 * there isn't going to be a split complete
1866 */
1867 pipe->split_sc_frame = -1;
1868 /*
1869 * See if we've started this transfer and sent
1870 * data
1871 */
1872 if (transaction->actual_bytes == 0) {
1873 /*
1874 * Nothing sent yet, this is either a
1875 * begin or the entire payload
1876 */
1877 if (bytes_to_transfer <= 188)
1878 /* Entire payload in one go */
1879 usbc_hcsplt.s.xactpos = 3;
1880 else
1881 /* First part of payload */
1882 usbc_hcsplt.s.xactpos = 2;
1883 } else {
1884 /*
1885 * Continuing the previous data, we must
1886 * either be in the middle or at the end
1887 */
1888 if (bytes_to_transfer <= 188)
1889 /* End of payload */
1890 usbc_hcsplt.s.xactpos = 1;
1891 else
1892 /* Middle of payload */
1893 usbc_hcsplt.s.xactpos = 0;
1894 }
1895 /*
1896 * Again, the transfer size is limited to 188
1897 * bytes
1898 */
1899 if (bytes_to_transfer > 188)
1900 bytes_to_transfer = 188;
1901 }
1902 }
1903
1904 /*
1905 * Make sure the transfer never exceeds the byte limit of the
1906 * hardware. Further bytes will be sent as continued
1907 * transactions
1908 */
1909 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1910 /*
1911 * Round MAX_TRANSFER_BYTES to a multiple of out packet
1912 * size
1913 */
1914 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1915 bytes_to_transfer *= pipe->max_packet;
1916 }
1917
1918 /*
1919 * Calculate the number of packets to transfer. If the length is
1920 * zero we still need to transfer one packet
1921 */
1922 packets_to_transfer = (bytes_to_transfer + pipe->max_packet - 1) / pipe->max_packet;
1923 if (packets_to_transfer == 0)
1924 packets_to_transfer = 1;
1925 else if ((packets_to_transfer > 1) && (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1926 /*
1927 * Limit to one packet when not using DMA. Channels must
1928 * be restarted between every packet for IN
1929 * transactions, so there is no reason to do multiple
1930 * packets in a row
1931 */
1932 packets_to_transfer = 1;
1933 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1934 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1935 /*
1936 * Limit the number of packet and data transferred to
1937 * what the hardware can handle
1938 */
1939 packets_to_transfer = MAX_TRANSFER_PACKETS;
1940 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1941 }
1942
1943 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1944 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1945
1946 /* Update the DATA0/DATA1 toggle */
1947 usbc_hctsiz.s.pid = __cvmx_usb_get_data_pid(pipe);
1948 /*
1949 * High speed pipes may need a hardware ping before they start
1950 */
1951 if (pipe->flags & __CVMX_USB_PIPE_FLAGS_NEED_PING)
1952 usbc_hctsiz.s.dopng = 1;
1953
1954 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCSPLTX(channel, usb->index), usbc_hcsplt.u32);
1955 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index), usbc_hctsiz.u32);
1956 }
1957
1958 /* Setup the Host Channel Characteristics Register */
1959 {
1960 union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
1961
1962 /*
1963 * Set the startframe odd/even properly. This is only used for
1964 * periodic
1965 */
1966 usbc_hcchar.s.oddfrm = usb->frame_number&1;
1967
1968 /*
1969 * Set the number of back to back packets allowed by this
1970 * endpoint. Split transactions interpret "ec" as the number of
1971 * immediate retries of failure. These retries happen too
1972 * quickly, so we disable these entirely for splits
1973 */
1974 if (__cvmx_usb_pipe_needs_split(usb, pipe))
1975 usbc_hcchar.s.ec = 1;
1976 else if (pipe->multi_count < 1)
1977 usbc_hcchar.s.ec = 1;
1978 else if (pipe->multi_count > 3)
1979 usbc_hcchar.s.ec = 3;
1980 else
1981 usbc_hcchar.s.ec = pipe->multi_count;
1982
1983 /* Set the rest of the endpoint specific settings */
1984 usbc_hcchar.s.devaddr = pipe->device_addr;
1985 usbc_hcchar.s.eptype = transaction->type;
1986 usbc_hcchar.s.lspddev = (pipe->device_speed == CVMX_USB_SPEED_LOW);
1987 usbc_hcchar.s.epdir = pipe->transfer_dir;
1988 usbc_hcchar.s.epnum = pipe->endpoint_num;
1989 usbc_hcchar.s.mps = pipe->max_packet;
1990 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
1991 }
1992
1993 /* Do transaction type specific fixups as needed */
1994 switch (transaction->type) {
1995 case CVMX_USB_TRANSFER_CONTROL:
1996 __cvmx_usb_start_channel_control(usb, channel, pipe);
1997 break;
1998 case CVMX_USB_TRANSFER_BULK:
1999 case CVMX_USB_TRANSFER_INTERRUPT:
2000 break;
2001 case CVMX_USB_TRANSFER_ISOCHRONOUS:
2002 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
2003 /*
2004 * ISO transactions require different PIDs depending on
2005 * direction and how many packets are needed
2006 */
2007 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
2008 if (pipe->multi_count < 2) /* Need DATA0 */
2009 USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), union cvmx_usbcx_hctsizx, pid, 0);
2010 else /* Need MDATA */
2011 USB_SET_FIELD32(CVMX_USBCX_HCTSIZX(channel, usb->index), union cvmx_usbcx_hctsizx, pid, 3);
2012 }
2013 }
2014 break;
2015 }
2016 {
2017 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index))};
2018 transaction->xfersize = usbc_hctsiz.s.xfersize;
2019 transaction->pktcnt = usbc_hctsiz.s.pktcnt;
2020 }
2021 /* Remeber when we start a split transaction */
2022 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2023 usb->active_split = transaction;
2024 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index), union cvmx_usbcx_hccharx, chena, 1);
2025 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
2026 __cvmx_usb_fill_tx_fifo(usb, channel);
2027 return;
2028}
2029
2030
2031/**
2032 * Find a pipe that is ready to be scheduled to hardware.
Aaro Koskinencb61c602013-10-06 22:22:25 +03002033 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002034 * @list: Pipe list to search
2035 * @current_frame:
2036 * Frame counter to use as a time reference.
2037 *
2038 * Returns: Pipe or NULL if none are ready
2039 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002040static struct cvmx_usb_pipe *__cvmx_usb_find_ready_pipe(struct cvmx_usb_state *usb, struct cvmx_usb_pipe_list *list, uint64_t current_frame)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002041{
2042 struct cvmx_usb_pipe *pipe = list->head;
2043 while (pipe) {
2044 if (!(pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED) && pipe->head &&
2045 (pipe->next_tx_frame <= current_frame) &&
2046 ((pipe->split_sc_frame == -1) || ((((int)current_frame - (int)pipe->split_sc_frame) & 0x7f) < 0x40)) &&
2047 (!usb->active_split || (usb->active_split == pipe->head))) {
2048 CVMX_PREFETCH(pipe, 128);
2049 CVMX_PREFETCH(pipe->head, 0);
2050 return pipe;
2051 }
2052 pipe = pipe->next;
2053 }
2054 return NULL;
2055}
2056
2057
2058/**
2059 * Called whenever a pipe might need to be scheduled to the
2060 * hardware.
2061 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002062 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002063 * @is_sof: True if this schedule was called on a SOF interrupt.
2064 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002065static void __cvmx_usb_schedule(struct cvmx_usb_state *usb, int is_sof)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002066{
2067 int channel;
2068 struct cvmx_usb_pipe *pipe;
2069 int need_sof;
2070 enum cvmx_usb_transfer ttype;
2071
2072 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2073 /*
2074 * Without DMA we need to be careful to not schedule something
2075 * at the end of a frame and cause an overrun.
2076 */
2077 union cvmx_usbcx_hfnum hfnum = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index))};
2078 union cvmx_usbcx_hfir hfir = {.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFIR(usb->index))};
2079 if (hfnum.s.frrem < hfir.s.frint/4)
2080 goto done;
2081 }
2082
2083 while (usb->idle_hardware_channels) {
2084 /* Find an idle channel */
2085 channel = __fls(usb->idle_hardware_channels);
2086 if (unlikely(channel > 7))
2087 break;
2088
2089 /* Find a pipe needing service */
2090 pipe = NULL;
2091 if (is_sof) {
2092 /*
2093 * Only process periodic pipes on SOF interrupts. This
2094 * way we are sure that the periodic data is sent in the
2095 * beginning of the frame
2096 */
2097 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_ISOCHRONOUS, usb->frame_number);
2098 if (likely(!pipe))
2099 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_INTERRUPT, usb->frame_number);
2100 }
2101 if (likely(!pipe)) {
2102 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_CONTROL, usb->frame_number);
2103 if (likely(!pipe))
2104 pipe = __cvmx_usb_find_ready_pipe(usb, usb->active_pipes + CVMX_USB_TRANSFER_BULK, usb->frame_number);
2105 }
2106 if (!pipe)
2107 break;
2108
2109 __cvmx_usb_start_channel(usb, channel, pipe);
2110 }
2111
2112done:
2113 /*
2114 * Only enable SOF interrupts when we have transactions pending in the
2115 * future that might need to be scheduled
2116 */
2117 need_sof = 0;
2118 for (ttype = CVMX_USB_TRANSFER_CONTROL; ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
2119 pipe = usb->active_pipes[ttype].head;
2120 while (pipe) {
2121 if (pipe->next_tx_frame > usb->frame_number) {
2122 need_sof = 1;
2123 break;
2124 }
2125 pipe = pipe->next;
2126 }
2127 }
2128 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index), union cvmx_usbcx_gintmsk, sofmsk, need_sof);
2129 return;
2130}
2131
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002132static inline struct octeon_hcd *cvmx_usb_to_octeon(struct cvmx_usb_state *p)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002133{
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002134 return container_of(p, struct octeon_hcd, usb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002135}
2136
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002137static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
2138{
2139 return container_of((void *)p, struct usb_hcd, hcd_priv);
2140}
2141
2142static void octeon_usb_urb_complete_callback(struct cvmx_usb_state *usb,
2143 enum cvmx_usb_complete status,
2144 int pipe_handle,
2145 int submit_handle,
2146 int bytes_transferred,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002147 struct urb *urb)
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002148{
2149 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
2150 struct usb_hcd *hcd = octeon_to_hcd(priv);
2151 struct device *dev = hcd->self.controller;
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002152
2153 urb->actual_length = bytes_transferred;
2154 urb->hcpriv = NULL;
2155
2156 if (!list_empty(&urb->urb_list)) {
2157 /*
2158 * It is on the dequeue_list, but we are going to call
2159 * usb_hcd_giveback_urb(), so we must clear it from
2160 * the list. We got to it before the
2161 * octeon_usb_urb_dequeue_work() tasklet did.
2162 */
2163 list_del(&urb->urb_list);
2164 /* No longer on the dequeue_list. */
2165 INIT_LIST_HEAD(&urb->urb_list);
2166 }
2167
2168 /* For Isochronous transactions we need to update the URB packet status
2169 list from data in our private copy */
2170 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2171 int i;
2172 /*
2173 * The pointer to the private list is stored in the setup_packet
2174 * field.
2175 */
2176 struct cvmx_usb_iso_packet *iso_packet =
2177 (struct cvmx_usb_iso_packet *) urb->setup_packet;
2178 /* Recalculate the transfer size by adding up each packet */
2179 urb->actual_length = 0;
2180 for (i = 0; i < urb->number_of_packets; i++) {
2181 if (iso_packet[i].status == CVMX_USB_COMPLETE_SUCCESS) {
2182 urb->iso_frame_desc[i].status = 0;
2183 urb->iso_frame_desc[i].actual_length = iso_packet[i].length;
2184 urb->actual_length += urb->iso_frame_desc[i].actual_length;
2185 } else {
2186 dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%d submit=%d size=%d\n",
2187 i, urb->number_of_packets,
2188 iso_packet[i].status, pipe_handle,
2189 submit_handle, iso_packet[i].length);
2190 urb->iso_frame_desc[i].status = -EREMOTEIO;
2191 }
2192 }
2193 /* Free the private list now that we don't need it anymore */
2194 kfree(iso_packet);
2195 urb->setup_packet = NULL;
2196 }
2197
2198 switch (status) {
2199 case CVMX_USB_COMPLETE_SUCCESS:
2200 urb->status = 0;
2201 break;
2202 case CVMX_USB_COMPLETE_CANCEL:
2203 if (urb->status == 0)
2204 urb->status = -ENOENT;
2205 break;
2206 case CVMX_USB_COMPLETE_STALL:
2207 dev_dbg(dev, "status=stall pipe=%d submit=%d size=%d\n",
2208 pipe_handle, submit_handle, bytes_transferred);
2209 urb->status = -EPIPE;
2210 break;
2211 case CVMX_USB_COMPLETE_BABBLEERR:
2212 dev_dbg(dev, "status=babble pipe=%d submit=%d size=%d\n",
2213 pipe_handle, submit_handle, bytes_transferred);
2214 urb->status = -EPIPE;
2215 break;
2216 case CVMX_USB_COMPLETE_SHORT:
2217 dev_dbg(dev, "status=short pipe=%d submit=%d size=%d\n",
2218 pipe_handle, submit_handle, bytes_transferred);
2219 urb->status = -EREMOTEIO;
2220 break;
2221 case CVMX_USB_COMPLETE_ERROR:
2222 case CVMX_USB_COMPLETE_XACTERR:
2223 case CVMX_USB_COMPLETE_DATATGLERR:
2224 case CVMX_USB_COMPLETE_FRAMEERR:
2225 dev_dbg(dev, "status=%d pipe=%d submit=%d size=%d\n",
2226 status, pipe_handle, submit_handle, bytes_transferred);
2227 urb->status = -EPROTO;
2228 break;
2229 }
2230 spin_unlock(&priv->lock);
2231 usb_hcd_giveback_urb(octeon_to_hcd(priv), urb, urb->status);
2232 spin_lock(&priv->lock);
2233}
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002234
2235/**
2236 * Signal the completion of a transaction and free it. The
2237 * transaction will be removed from the pipe transaction list.
2238 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002239 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002240 * @pipe: Pipe the transaction is on
2241 * @transaction:
2242 * Transaction that completed
2243 * @complete_code:
2244 * Completion code
2245 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002246static void __cvmx_usb_perform_complete(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002247 struct cvmx_usb_pipe *pipe,
2248 struct cvmx_usb_transaction *transaction,
2249 enum cvmx_usb_complete complete_code)
2250{
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002251 int pipe_handle;
2252 int submit_handle;
2253
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002254 /* If this was a split then clear our split in progress marker */
2255 if (usb->active_split == transaction)
2256 usb->active_split = NULL;
2257
2258 /*
2259 * Isochronous transactions need extra processing as they might not be
2260 * done after a single data transfer
2261 */
2262 if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
2263 /* Update the number of bytes transferred in this ISO packet */
2264 transaction->iso_packets[0].length = transaction->actual_bytes;
2265 transaction->iso_packets[0].status = complete_code;
2266
2267 /*
2268 * If there are more ISOs pending and we succeeded, schedule the
2269 * next one
2270 */
2271 if ((transaction->iso_number_packets > 1) && (complete_code == CVMX_USB_COMPLETE_SUCCESS)) {
2272 /* No bytes transferred for this packet as of yet */
2273 transaction->actual_bytes = 0;
2274 /* One less ISO waiting to transfer */
2275 transaction->iso_number_packets--;
2276 /* Increment to the next location in our packet array */
2277 transaction->iso_packets++;
2278 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2279 goto done;
2280 }
2281 }
2282
2283 /* Remove the transaction from the pipe list */
2284 if (transaction->next)
2285 transaction->next->prev = transaction->prev;
2286 else
2287 pipe->tail = transaction->prev;
2288 if (transaction->prev)
2289 transaction->prev->next = transaction->next;
2290 else
2291 pipe->head = transaction->next;
2292 if (!pipe->head) {
2293 __cvmx_usb_remove_pipe(usb->active_pipes + pipe->transfer_type, pipe);
2294 __cvmx_usb_append_pipe(&usb->idle_pipes, pipe);
2295
2296 }
Aaro Koskinen75ee5122013-10-06 22:22:28 +03002297 pipe_handle = __cvmx_usb_get_pipe_handle(usb, pipe);
2298 submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
2299 octeon_usb_urb_complete_callback(usb, complete_code, pipe_handle,
2300 submit_handle,
2301 transaction->actual_bytes,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002302 transaction->urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002303 __cvmx_usb_free_transaction(usb, transaction);
2304done:
2305 return;
2306}
2307
2308
2309/**
2310 * Submit a usb transaction to a pipe. Called for all types
2311 * of transactions.
2312 *
2313 * @usb:
2314 * @pipe_handle:
2315 * Which pipe to submit to. Will be validated in this function.
2316 * @type: Transaction type
2317 * @buffer: User buffer for the transaction
2318 * @buffer_length:
2319 * User buffer's length in bytes
2320 * @control_header:
2321 * For control transactions, the 8 byte standard header
2322 * @iso_start_frame:
2323 * For ISO transactions, the start frame
2324 * @iso_number_packets:
2325 * For ISO, the number of packet in the transaction.
2326 * @iso_packets:
2327 * A description of each ISO packet
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002328 * @urb: URB for the callback
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002329 *
2330 * Returns: Submit handle or negative on failure. Matches the result
2331 * in the external API.
2332 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002333static int __cvmx_usb_submit_transaction(struct cvmx_usb_state *usb,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002334 int pipe_handle,
2335 enum cvmx_usb_transfer type,
2336 uint64_t buffer,
2337 int buffer_length,
2338 uint64_t control_header,
2339 int iso_start_frame,
2340 int iso_number_packets,
2341 struct cvmx_usb_iso_packet *iso_packets,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002342 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002343{
2344 int submit_handle;
2345 struct cvmx_usb_transaction *transaction;
2346 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2347
2348 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2349 return -EINVAL;
2350 /* Fail if the pipe isn't open */
2351 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2352 return -EINVAL;
2353 if (unlikely(pipe->transfer_type != type))
2354 return -EINVAL;
2355
2356 transaction = __cvmx_usb_alloc_transaction(usb);
2357 if (unlikely(!transaction))
2358 return -ENOMEM;
2359
2360 transaction->type = type;
2361 transaction->buffer = buffer;
2362 transaction->buffer_length = buffer_length;
2363 transaction->control_header = control_header;
2364 /* FIXME: This is not used, implement it. */
2365 transaction->iso_start_frame = iso_start_frame;
2366 transaction->iso_number_packets = iso_number_packets;
2367 transaction->iso_packets = iso_packets;
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002368 transaction->urb = urb;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002369 if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2370 transaction->stage = CVMX_USB_STAGE_SETUP;
2371 else
2372 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2373
2374 transaction->next = NULL;
2375 if (pipe->tail) {
2376 transaction->prev = pipe->tail;
2377 transaction->prev->next = transaction;
2378 } else {
2379 if (pipe->next_tx_frame < usb->frame_number)
2380 pipe->next_tx_frame = usb->frame_number + pipe->interval -
2381 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2382 transaction->prev = NULL;
2383 pipe->head = transaction;
2384 __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2385 __cvmx_usb_append_pipe(usb->active_pipes + pipe->transfer_type, pipe);
2386 }
2387 pipe->tail = transaction;
2388
2389 submit_handle = __cvmx_usb_get_submit_handle(usb, transaction);
2390
2391 /* We may need to schedule the pipe if this was the head of the pipe */
2392 if (!transaction->prev)
2393 __cvmx_usb_schedule(usb, 0);
2394
2395 return submit_handle;
2396}
2397
2398
2399/**
2400 * Call to submit a USB Bulk transfer to a pipe.
2401 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002402 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002403 * @pipe_handle:
2404 * Handle to the pipe for the transfer.
Aaro Koskinen9ccca702013-10-06 22:22:30 +03002405 * @urb: URB.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002406 *
2407 * Returns: A submitted transaction handle or negative on
2408 * failure. Negative values are error codes.
2409 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002410static int cvmx_usb_submit_bulk(struct cvmx_usb_state *usb, int pipe_handle,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002411 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002412{
2413 int submit_handle;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002414
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002415 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2416 CVMX_USB_TRANSFER_BULK,
Aaro Koskinen9ccca702013-10-06 22:22:30 +03002417 urb->transfer_dma,
2418 urb->transfer_buffer_length,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002419 0, /* control_header */
2420 0, /* iso_start_frame */
2421 0, /* iso_number_packets */
2422 NULL, /* iso_packets */
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002423 urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002424 return submit_handle;
2425}
2426
2427
2428/**
2429 * Call to submit a USB Interrupt transfer to a pipe.
2430 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002431 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002432 * @pipe_handle:
2433 * Handle to the pipe for the transfer.
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002434 * @urb: URB returned when the callback is called.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002435 *
2436 * Returns: A submitted transaction handle or negative on
2437 * failure. Negative values are error codes.
2438 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002439static int cvmx_usb_submit_interrupt(struct cvmx_usb_state *usb,
Aaro Koskinen9fdbb692013-10-06 22:22:32 +03002440 int pipe_handle, struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002441{
2442 int submit_handle;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002443
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002444 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2445 CVMX_USB_TRANSFER_INTERRUPT,
Aaro Koskinen9fdbb692013-10-06 22:22:32 +03002446 urb->transfer_dma,
2447 urb->transfer_buffer_length,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002448 0, /* control_header */
2449 0, /* iso_start_frame */
2450 0, /* iso_number_packets */
2451 NULL, /* iso_packets */
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002452 urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002453 return submit_handle;
2454}
2455
2456
2457/**
2458 * Call to submit a USB Control transfer to a pipe.
2459 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002460 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002461 * @pipe_handle:
2462 * Handle to the pipe for the transfer.
Aaro Koskinen2ae09e82013-10-06 22:22:31 +03002463 * @urb: URB.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002464 *
2465 * Returns: A submitted transaction handle or negative on
2466 * failure. Negative values are error codes.
2467 */
Aaro Koskinen2ae09e82013-10-06 22:22:31 +03002468static int cvmx_usb_submit_control(struct cvmx_usb_state *usb, int pipe_handle,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002469 struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002470{
2471 int submit_handle;
Aaro Koskinen2ae09e82013-10-06 22:22:31 +03002472 int buffer_length = urb->transfer_buffer_length;
2473 uint64_t control_header = urb->setup_dma;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002474 union cvmx_usb_control_header *header =
2475 cvmx_phys_to_ptr(control_header);
2476
2477 /* Pipe handle checking is done later in a common place */
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002478 if ((header->s.request_type & 0x80) == 0)
2479 buffer_length = le16_to_cpu(header->s.length);
2480
2481 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2482 CVMX_USB_TRANSFER_CONTROL,
Aaro Koskinen2ae09e82013-10-06 22:22:31 +03002483 urb->transfer_dma,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002484 buffer_length,
2485 control_header,
2486 0, /* iso_start_frame */
2487 0, /* iso_number_packets */
2488 NULL, /* iso_packets */
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002489 urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002490 return submit_handle;
2491}
2492
2493
2494/**
2495 * Call to submit a USB Isochronous transfer to a pipe.
2496 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002497 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002498 * @pipe_handle:
2499 * Handle to the pipe for the transfer.
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002500 * @urb: URB returned when the callback is called.
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002501 *
2502 * Returns: A submitted transaction handle or negative on
2503 * failure. Negative values are error codes.
2504 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002505static int cvmx_usb_submit_isochronous(struct cvmx_usb_state *usb,
Aaro Koskinene16b5e32013-10-06 22:22:33 +03002506 int pipe_handle, struct urb *urb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002507{
2508 int submit_handle;
Aaro Koskinene16b5e32013-10-06 22:22:33 +03002509 struct cvmx_usb_iso_packet *packets;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002510
Aaro Koskinene16b5e32013-10-06 22:22:33 +03002511 packets = (struct cvmx_usb_iso_packet *) urb->setup_packet;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002512 submit_handle = __cvmx_usb_submit_transaction(usb, pipe_handle,
2513 CVMX_USB_TRANSFER_ISOCHRONOUS,
Aaro Koskinene16b5e32013-10-06 22:22:33 +03002514 urb->transfer_dma,
2515 urb->transfer_buffer_length,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002516 0, /* control_header */
Aaro Koskinene16b5e32013-10-06 22:22:33 +03002517 urb->start_frame,
2518 urb->number_of_packets,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002519 packets,
Aaro Koskinen0cce1002013-10-06 22:22:29 +03002520 urb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002521 return submit_handle;
2522}
2523
2524
2525/**
2526 * Cancel one outstanding request in a pipe. Canceling a request
2527 * can fail if the transaction has already completed before cancel
2528 * is called. Even after a successful cancel call, it may take
2529 * a frame or two for the cvmx_usb_poll() function to call the
2530 * associated callback.
2531 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002532 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002533 * @pipe_handle:
2534 * Pipe handle to cancel requests in.
2535 * @submit_handle:
2536 * Handle to transaction to cancel, returned by the submit
2537 * function.
2538 *
2539 * Returns: 0 or a negative error code.
2540 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002541static int cvmx_usb_cancel(struct cvmx_usb_state *usb, int pipe_handle,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002542 int submit_handle)
2543{
2544 struct cvmx_usb_transaction *transaction;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002545 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2546
2547 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2548 return -EINVAL;
2549 if (unlikely((submit_handle < 0) || (submit_handle >= MAX_TRANSACTIONS)))
2550 return -EINVAL;
2551
2552 /* Fail if the pipe isn't open */
2553 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2554 return -EINVAL;
2555
2556 transaction = usb->transaction + submit_handle;
2557
2558 /* Fail if this transaction already completed */
2559 if (unlikely((transaction->flags & __CVMX_USB_TRANSACTION_FLAGS_IN_USE) == 0))
2560 return -EINVAL;
2561
2562 /*
2563 * If the transaction is the HEAD of the queue and scheduled. We need to
2564 * treat it special
2565 */
2566 if ((pipe->head == transaction) &&
2567 (pipe->flags & __CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
2568 union cvmx_usbcx_hccharx usbc_hcchar;
2569
2570 usb->pipe_for_channel[pipe->channel] = NULL;
2571 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2572
2573 CVMX_SYNCW;
2574
2575 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
2576 /*
2577 * If the channel isn't enabled then the transaction already
2578 * completed.
2579 */
2580 if (usbc_hcchar.s.chena) {
2581 usbc_hcchar.s.chdis = 1;
2582 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(pipe->channel, usb->index), usbc_hcchar.u32);
2583 }
2584 }
2585 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_CANCEL);
2586 return 0;
2587}
2588
2589
2590/**
2591 * Cancel all outstanding requests in a pipe. Logically all this
2592 * does is call cvmx_usb_cancel() in a loop.
2593 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002594 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002595 * @pipe_handle:
2596 * Pipe handle to cancel requests in.
2597 *
2598 * Returns: 0 or a negative error code.
2599 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002600static int cvmx_usb_cancel_all(struct cvmx_usb_state *usb, int pipe_handle)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002601{
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002602 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2603
2604 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2605 return -EINVAL;
2606
2607 /* Fail if the pipe isn't open */
2608 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2609 return -EINVAL;
2610
2611 /* Simply loop through and attempt to cancel each transaction */
2612 while (pipe->head) {
Aaro Koskinencb61c602013-10-06 22:22:25 +03002613 int result = cvmx_usb_cancel(usb, pipe_handle,
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002614 __cvmx_usb_get_submit_handle(usb, pipe->head));
2615 if (unlikely(result != 0))
2616 return result;
2617 }
2618 return 0;
2619}
2620
2621
2622/**
2623 * Close a pipe created with cvmx_usb_open_pipe().
2624 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002625 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002626 * @pipe_handle:
2627 * Pipe handle to close.
2628 *
2629 * Returns: 0 or a negative error code. EBUSY is returned if the pipe has
2630 * outstanding transfers.
2631 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002632static int cvmx_usb_close_pipe(struct cvmx_usb_state *usb, int pipe_handle)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002633{
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002634 struct cvmx_usb_pipe *pipe = usb->pipe + pipe_handle;
2635
2636 if (unlikely((pipe_handle < 0) || (pipe_handle >= MAX_PIPES)))
2637 return -EINVAL;
2638
2639 /* Fail if the pipe isn't open */
2640 if (unlikely((pipe->flags & __CVMX_USB_PIPE_FLAGS_OPEN) == 0))
2641 return -EINVAL;
2642
2643 /* Fail if the pipe has pending transactions */
2644 if (unlikely(pipe->head))
2645 return -EBUSY;
2646
2647 pipe->flags = 0;
2648 __cvmx_usb_remove_pipe(&usb->idle_pipes, pipe);
2649 __cvmx_usb_append_pipe(&usb->free_pipes, pipe);
2650
2651 return 0;
2652}
2653
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002654/**
2655 * Get the current USB protocol level frame number. The frame
2656 * number is always in the range of 0-0x7ff.
2657 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03002658 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002659 *
2660 * Returns: USB frame number
2661 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002662static int cvmx_usb_get_frame_number(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002663{
2664 int frame_number;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002665 union cvmx_usbcx_hfnum usbc_hfnum;
2666
2667 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2668 frame_number = usbc_hfnum.s.frnum;
2669
2670 return frame_number;
2671}
2672
2673
2674/**
2675 * Poll a channel for status
2676 *
2677 * @usb: USB device
2678 * @channel: Channel to poll
2679 *
2680 * Returns: Zero on success
2681 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03002682static int __cvmx_usb_poll_channel(struct cvmx_usb_state *usb, int channel)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03002683{
2684 union cvmx_usbcx_hcintx usbc_hcint;
2685 union cvmx_usbcx_hctsizx usbc_hctsiz;
2686 union cvmx_usbcx_hccharx usbc_hcchar;
2687 struct cvmx_usb_pipe *pipe;
2688 struct cvmx_usb_transaction *transaction;
2689 int bytes_this_transfer;
2690 int bytes_in_last_packet;
2691 int packets_processed;
2692 int buffer_space_left;
2693
2694 /* Read the interrupt status bits for the channel */
2695 usbc_hcint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCINTX(channel, usb->index));
2696
2697 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2698 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2699
2700 if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2701 /*
2702 * There seems to be a bug in CN31XX which can cause
2703 * interrupt IN transfers to get stuck until we do a
2704 * write of HCCHARX without changing things
2705 */
2706 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2707 return 0;
2708 }
2709
2710 /*
2711 * In non DMA mode the channels don't halt themselves. We need
2712 * to manually disable channels that are left running
2713 */
2714 if (!usbc_hcint.s.chhltd) {
2715 if (usbc_hcchar.s.chena) {
2716 union cvmx_usbcx_hcintmskx hcintmsk;
2717 /* Disable all interrupts except CHHLTD */
2718 hcintmsk.u32 = 0;
2719 hcintmsk.s.chhltdmsk = 1;
2720 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), hcintmsk.u32);
2721 usbc_hcchar.s.chdis = 1;
2722 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index), usbc_hcchar.u32);
2723 return 0;
2724 } else if (usbc_hcint.s.xfercompl) {
2725 /*
2726 * Successful IN/OUT with transfer complete.
2727 * Channel halt isn't needed.
2728 */
2729 } else {
2730 cvmx_dprintf("USB%d: Channel %d interrupt without halt\n", usb->index, channel);
2731 return 0;
2732 }
2733 }
2734 } else {
2735 /*
2736 * There is are no interrupts that we need to process when the
2737 * channel is still running
2738 */
2739 if (!usbc_hcint.s.chhltd)
2740 return 0;
2741 }
2742
2743 /* Disable the channel interrupts now that it is done */
2744 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
2745 usb->idle_hardware_channels |= (1<<channel);
2746
2747 /* Make sure this channel is tied to a valid pipe */
2748 pipe = usb->pipe_for_channel[channel];
2749 CVMX_PREFETCH(pipe, 0);
2750 CVMX_PREFETCH(pipe, 128);
2751 if (!pipe)
2752 return 0;
2753 transaction = pipe->head;
2754 CVMX_PREFETCH(transaction, 0);
2755
2756 /*
2757 * Disconnect this pipe from the HW channel. Later the schedule
2758 * function will figure out which pipe needs to go
2759 */
2760 usb->pipe_for_channel[channel] = NULL;
2761 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_SCHEDULED;
2762
2763 /*
2764 * Read the channel config info so we can figure out how much data
2765 * transfered
2766 */
2767 usbc_hcchar.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCCHARX(channel, usb->index));
2768 usbc_hctsiz.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index));
2769
2770 /*
2771 * Calculating the number of bytes successfully transferred is dependent
2772 * on the transfer direction
2773 */
2774 packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2775 if (usbc_hcchar.s.epdir) {
2776 /*
2777 * IN transactions are easy. For every byte received the
2778 * hardware decrements xfersize. All we need to do is subtract
2779 * the current value of xfersize from its starting value and we
2780 * know how many bytes were written to the buffer
2781 */
2782 bytes_this_transfer = transaction->xfersize - usbc_hctsiz.s.xfersize;
2783 } else {
2784 /*
2785 * OUT transaction don't decrement xfersize. Instead pktcnt is
2786 * decremented on every successful packet send. The hardware
2787 * does this when it receives an ACK, or NYET. If it doesn't
2788 * receive one of these responses pktcnt doesn't change
2789 */
2790 bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2791 /*
2792 * The last packet may not be a full transfer if we didn't have
2793 * enough data
2794 */
2795 if (bytes_this_transfer > transaction->xfersize)
2796 bytes_this_transfer = transaction->xfersize;
2797 }
2798 /* Figure out how many bytes were in the last packet of the transfer */
2799 if (packets_processed)
2800 bytes_in_last_packet = bytes_this_transfer - (packets_processed-1) * usbc_hcchar.s.mps;
2801 else
2802 bytes_in_last_packet = bytes_this_transfer;
2803
2804 /*
2805 * As a special case, setup transactions output the setup header, not
2806 * the user's data. For this reason we don't count setup data as bytes
2807 * transferred
2808 */
2809 if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2810 (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2811 bytes_this_transfer = 0;
2812
2813 /*
2814 * Add the bytes transferred to the running total. It is important that
2815 * bytes_this_transfer doesn't count any data that needs to be
2816 * retransmitted
2817 */
2818 transaction->actual_bytes += bytes_this_transfer;
2819 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
2820 buffer_space_left = transaction->iso_packets[0].length - transaction->actual_bytes;
2821 else
2822 buffer_space_left = transaction->buffer_length - transaction->actual_bytes;
2823
2824 /*
2825 * We need to remember the PID toggle state for the next transaction.
2826 * The hardware already updated it for the next transaction
2827 */
2828 pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2829
2830 /*
2831 * For high speed bulk out, assume the next transaction will need to do
2832 * a ping before proceeding. If this isn't true the ACK processing below
2833 * will clear this flag
2834 */
2835 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2836 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2837 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2838 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
2839
2840 if (usbc_hcint.s.stall) {
2841 /*
2842 * STALL as a response means this transaction cannot be
2843 * completed because the device can't process transactions. Tell
2844 * the user. Any data that was transferred will be counted on
2845 * the actual bytes transferred
2846 */
2847 pipe->pid_toggle = 0;
2848 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_STALL);
2849 } else if (usbc_hcint.s.xacterr) {
2850 /*
2851 * We know at least one packet worked if we get a ACK or NAK.
2852 * Reset the retry counter
2853 */
2854 if (usbc_hcint.s.nak || usbc_hcint.s.ack)
2855 transaction->retries = 0;
2856 transaction->retries++;
2857 if (transaction->retries > MAX_RETRIES) {
2858 /*
2859 * XactErr as a response means the device signaled
2860 * something wrong with the transfer. For example, PID
2861 * toggle errors cause these
2862 */
2863 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_XACTERR);
2864 } else {
2865 /*
2866 * If this was a split then clear our split in progress
2867 * marker
2868 */
2869 if (usb->active_split == transaction)
2870 usb->active_split = NULL;
2871 /*
2872 * Rewind to the beginning of the transaction by anding
2873 * off the split complete bit
2874 */
2875 transaction->stage &= ~1;
2876 pipe->split_sc_frame = -1;
2877 pipe->next_tx_frame += pipe->interval;
2878 if (pipe->next_tx_frame < usb->frame_number)
2879 pipe->next_tx_frame = usb->frame_number + pipe->interval -
2880 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
2881 }
2882 } else if (usbc_hcint.s.bblerr) {
2883 /* Babble Error (BblErr) */
2884 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_BABBLEERR);
2885 } else if (usbc_hcint.s.datatglerr) {
2886 /* We'll retry the exact same transaction again */
2887 transaction->retries++;
2888 } else if (usbc_hcint.s.nyet) {
2889 /*
2890 * NYET as a response is only allowed in three cases: as a
2891 * response to a ping, as a response to a split transaction, and
2892 * as a response to a bulk out. The ping case is handled by
2893 * hardware, so we only have splits and bulk out
2894 */
2895 if (!__cvmx_usb_pipe_needs_split(usb, pipe)) {
2896 transaction->retries = 0;
2897 /*
2898 * If there is more data to go then we need to try
2899 * again. Otherwise this transaction is complete
2900 */
2901 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet))
2902 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2903 } else {
2904 /*
2905 * Split transactions retry the split complete 4 times
2906 * then rewind to the start split and do the entire
2907 * transactions again
2908 */
2909 transaction->retries++;
2910 if ((transaction->retries & 0x3) == 0) {
2911 /*
2912 * Rewind to the beginning of the transaction by
2913 * anding off the split complete bit
2914 */
2915 transaction->stage &= ~1;
2916 pipe->split_sc_frame = -1;
2917 }
2918 }
2919 } else if (usbc_hcint.s.ack) {
2920 transaction->retries = 0;
2921 /*
2922 * The ACK bit can only be checked after the other error bits.
2923 * This is because a multi packet transfer may succeed in a
2924 * number of packets and then get a different response on the
2925 * last packet. In this case both ACK and the last response bit
2926 * will be set. If none of the other response bits is set, then
2927 * the last packet must have been an ACK
2928 *
2929 * Since we got an ACK, we know we don't need to do a ping on
2930 * this pipe
2931 */
2932 pipe->flags &= ~__CVMX_USB_PIPE_FLAGS_NEED_PING;
2933
2934 switch (transaction->type) {
2935 case CVMX_USB_TRANSFER_CONTROL:
2936 switch (transaction->stage) {
2937 case CVMX_USB_STAGE_NON_CONTROL:
2938 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2939 /* This should be impossible */
2940 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
2941 break;
2942 case CVMX_USB_STAGE_SETUP:
2943 pipe->pid_toggle = 1;
2944 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2945 transaction->stage = CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
2946 else {
2947 union cvmx_usb_control_header *header =
2948 cvmx_phys_to_ptr(transaction->control_header);
2949 if (header->s.length)
2950 transaction->stage = CVMX_USB_STAGE_DATA;
2951 else
2952 transaction->stage = CVMX_USB_STAGE_STATUS;
2953 }
2954 break;
2955 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2956 {
2957 union cvmx_usb_control_header *header =
2958 cvmx_phys_to_ptr(transaction->control_header);
2959 if (header->s.length)
2960 transaction->stage = CVMX_USB_STAGE_DATA;
2961 else
2962 transaction->stage = CVMX_USB_STAGE_STATUS;
2963 }
2964 break;
2965 case CVMX_USB_STAGE_DATA:
2966 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
2967 transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
2968 /*
2969 * For setup OUT data that are splits,
2970 * the hardware doesn't appear to count
2971 * transferred data. Here we manually
2972 * update the data transferred
2973 */
2974 if (!usbc_hcchar.s.epdir) {
2975 if (buffer_space_left < pipe->max_packet)
2976 transaction->actual_bytes += buffer_space_left;
2977 else
2978 transaction->actual_bytes += pipe->max_packet;
2979 }
2980 } else if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2981 pipe->pid_toggle = 1;
2982 transaction->stage = CVMX_USB_STAGE_STATUS;
2983 }
2984 break;
2985 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
2986 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
2987 pipe->pid_toggle = 1;
2988 transaction->stage = CVMX_USB_STAGE_STATUS;
2989 } else {
2990 transaction->stage = CVMX_USB_STAGE_DATA;
2991 }
2992 break;
2993 case CVMX_USB_STAGE_STATUS:
2994 if (__cvmx_usb_pipe_needs_split(usb, pipe))
2995 transaction->stage = CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
2996 else
2997 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
2998 break;
2999 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
3000 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3001 break;
3002 }
3003 break;
3004 case CVMX_USB_TRANSFER_BULK:
3005 case CVMX_USB_TRANSFER_INTERRUPT:
3006 /*
3007 * The only time a bulk transfer isn't complete when it
3008 * finishes with an ACK is during a split transaction.
3009 * For splits we need to continue the transfer if more
3010 * data is needed
3011 */
3012 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
3013 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
3014 transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
3015 else {
3016 if (buffer_space_left && (bytes_in_last_packet == pipe->max_packet))
3017 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
3018 else {
3019 if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
3020 pipe->next_tx_frame += pipe->interval;
3021 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3022 }
3023 }
3024 } else {
3025 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
3026 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
3027 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
3028 (usbc_hcint.s.nak))
3029 pipe->flags |= __CVMX_USB_PIPE_FLAGS_NEED_PING;
3030 if (!buffer_space_left || (bytes_in_last_packet < pipe->max_packet)) {
3031 if (transaction->type == CVMX_USB_TRANSFER_INTERRUPT)
3032 pipe->next_tx_frame += pipe->interval;
3033 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3034 }
3035 }
3036 break;
3037 case CVMX_USB_TRANSFER_ISOCHRONOUS:
3038 if (__cvmx_usb_pipe_needs_split(usb, pipe)) {
3039 /*
3040 * ISOCHRONOUS OUT splits don't require a
3041 * complete split stage. Instead they use a
3042 * sequence of begin OUT splits to transfer the
3043 * data 188 bytes at a time. Once the transfer
3044 * is complete, the pipe sleeps until the next
3045 * schedule interval
3046 */
3047 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
3048 /*
3049 * If no space left or this wasn't a max
3050 * size packet then this transfer is
3051 * complete. Otherwise start it again to
3052 * send the next 188 bytes
3053 */
3054 if (!buffer_space_left || (bytes_this_transfer < 188)) {
3055 pipe->next_tx_frame += pipe->interval;
3056 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3057 }
3058 } else {
3059 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
3060 /*
3061 * We are in the incoming data
3062 * phase. Keep getting data
3063 * until we run out of space or
3064 * get a small packet
3065 */
3066 if ((buffer_space_left == 0) || (bytes_in_last_packet < pipe->max_packet)) {
3067 pipe->next_tx_frame += pipe->interval;
3068 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3069 }
3070 } else
3071 transaction->stage = CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
3072 }
3073 } else {
3074 pipe->next_tx_frame += pipe->interval;
3075 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_SUCCESS);
3076 }
3077 break;
3078 }
3079 } else if (usbc_hcint.s.nak) {
3080 /*
3081 * If this was a split then clear our split in progress marker.
3082 */
3083 if (usb->active_split == transaction)
3084 usb->active_split = NULL;
3085 /*
3086 * NAK as a response means the device couldn't accept the
3087 * transaction, but it should be retried in the future. Rewind
3088 * to the beginning of the transaction by anding off the split
3089 * complete bit. Retry in the next interval
3090 */
3091 transaction->retries = 0;
3092 transaction->stage &= ~1;
3093 pipe->next_tx_frame += pipe->interval;
3094 if (pipe->next_tx_frame < usb->frame_number)
3095 pipe->next_tx_frame = usb->frame_number + pipe->interval -
3096 (usb->frame_number - pipe->next_tx_frame) % pipe->interval;
3097 } else {
3098 struct cvmx_usb_port_status port;
Aaro Koskinencb61c602013-10-06 22:22:25 +03003099 port = cvmx_usb_get_status(usb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003100 if (port.port_enabled) {
3101 /* We'll retry the exact same transaction again */
3102 transaction->retries++;
3103 } else {
3104 /*
3105 * We get channel halted interrupts with no result bits
3106 * sets when the cable is unplugged
3107 */
3108 __cvmx_usb_perform_complete(usb, pipe, transaction, CVMX_USB_COMPLETE_ERROR);
3109 }
3110 }
3111 return 0;
3112}
3113
Aaro Koskinen393e2142013-10-06 22:22:27 +03003114static void octeon_usb_port_callback(struct cvmx_usb_state *usb)
3115{
3116 struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
3117
3118 spin_unlock(&priv->lock);
3119 usb_hcd_poll_rh_status(octeon_to_hcd(priv));
3120 spin_lock(&priv->lock);
3121}
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003122
3123/**
3124 * Poll the USB block for status and call all needed callback
3125 * handlers. This function is meant to be called in the interrupt
3126 * handler for the USB controller. It can also be called
3127 * periodically in a loop for non-interrupt based operation.
3128 *
Aaro Koskinencb61c602013-10-06 22:22:25 +03003129 * @usb: USB device state populated by cvmx_usb_initialize().
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003130 *
3131 * Returns: 0 or a negative error code.
3132 */
Aaro Koskinencb61c602013-10-06 22:22:25 +03003133static int cvmx_usb_poll(struct cvmx_usb_state *usb)
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003134{
3135 union cvmx_usbcx_hfnum usbc_hfnum;
3136 union cvmx_usbcx_gintsts usbc_gintsts;
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003137
3138 CVMX_PREFETCH(usb, 0);
3139 CVMX_PREFETCH(usb, 1*128);
3140 CVMX_PREFETCH(usb, 2*128);
3141 CVMX_PREFETCH(usb, 3*128);
3142 CVMX_PREFETCH(usb, 4*128);
3143
3144 /* Update the frame counter */
3145 usbc_hfnum.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
3146 if ((usb->frame_number&0x3fff) > usbc_hfnum.s.frnum)
3147 usb->frame_number += 0x4000;
3148 usb->frame_number &= ~0x3fffull;
3149 usb->frame_number |= usbc_hfnum.s.frnum;
3150
3151 /* Read the pending interrupts */
3152 usbc_gintsts.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_GINTSTS(usb->index));
3153
3154 /* Clear the interrupts now that we know about them */
3155 __cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index), usbc_gintsts.u32);
3156
3157 if (usbc_gintsts.s.rxflvl) {
3158 /*
3159 * RxFIFO Non-Empty (RxFLvl)
3160 * Indicates that there is at least one packet pending to be
3161 * read from the RxFIFO.
3162 *
3163 * In DMA mode this is handled by hardware
3164 */
3165 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3166 __cvmx_usb_poll_rx_fifo(usb);
3167 }
3168 if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
3169 /* Fill the Tx FIFOs when not in DMA mode */
3170 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
3171 __cvmx_usb_poll_tx_fifo(usb);
3172 }
3173 if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
3174 union cvmx_usbcx_hprt usbc_hprt;
3175 /*
3176 * Disconnect Detected Interrupt (DisconnInt)
3177 * Asserted when a device disconnect is detected.
3178 *
3179 * Host Port Interrupt (PrtInt)
3180 * The core sets this bit to indicate a change in port status of
3181 * one of the O2P USB core ports in Host mode. The application
3182 * must read the Host Port Control and Status (HPRT) register to
3183 * determine the exact event that caused this interrupt. The
3184 * application must clear the appropriate status bit in the Host
3185 * Port Control and Status register to clear this bit.
3186 *
3187 * Call the user's port callback
3188 */
Aaro Koskinen393e2142013-10-06 22:22:27 +03003189 octeon_usb_port_callback(usb);
Aaro Koskinen6570b4a2013-10-06 22:22:24 +03003190 /* Clear the port change bits */
3191 usbc_hprt.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
3192 usbc_hprt.s.prtena = 0;
3193 __cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index), usbc_hprt.u32);
3194 }
3195 if (usbc_gintsts.s.hchint) {
3196 /*
3197 * Host Channels Interrupt (HChInt)
3198 * The core sets this bit to indicate that an interrupt is
3199 * pending on one of the channels of the core (in Host mode).
3200 * The application must read the Host All Channels Interrupt
3201 * (HAINT) register to determine the exact number of the channel
3202 * on which the interrupt occurred, and then read the
3203 * corresponding Host Channel-n Interrupt (HCINTn) register to
3204 * determine the exact cause of the interrupt. The application
3205 * must clear the appropriate status bit in the HCINTn register
3206 * to clear this bit.
3207 */
3208 union cvmx_usbcx_haint usbc_haint;
3209 usbc_haint.u32 = __cvmx_usb_read_csr32(usb, CVMX_USBCX_HAINT(usb->index));
3210 while (usbc_haint.u32) {
3211 int channel;
3212
3213 channel = __fls(usbc_haint.u32);
3214 __cvmx_usb_poll_channel(usb, channel);
3215 usbc_haint.u32 ^= 1<<channel;
3216 }
3217 }
3218
3219 __cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
3220
3221 return 0;
3222}
3223
Aaro Koskinenb1649352013-06-01 21:42:58 +03003224/* convert between an HCD pointer and the corresponding struct octeon_hcd */
3225static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
3226{
3227 return (struct octeon_hcd *)(hcd->hcd_priv);
3228}
3229
Aaro Koskinenb1649352013-06-01 21:42:58 +03003230static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
3231{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003232 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3233 unsigned long flags;
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003234
Aaro Koskinen771378b2013-06-13 01:00:31 +03003235 spin_lock_irqsave(&priv->lock, flags);
3236 cvmx_usb_poll(&priv->usb);
3237 spin_unlock_irqrestore(&priv->lock, flags);
3238 return IRQ_HANDLED;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003239}
3240
Aaro Koskinenb1649352013-06-01 21:42:58 +03003241static int octeon_usb_start(struct usb_hcd *hcd)
3242{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003243 hcd->state = HC_STATE_RUNNING;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003244 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003245}
3246
3247static void octeon_usb_stop(struct usb_hcd *hcd)
3248{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003249 hcd->state = HC_STATE_HALT;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003250}
3251
3252static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
3253{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003254 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003255
Aaro Koskinen771378b2013-06-13 01:00:31 +03003256 return cvmx_usb_get_frame_number(&priv->usb);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003257}
3258
Aaro Koskinenb1649352013-06-01 21:42:58 +03003259static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
Aaro Koskinen771378b2013-06-13 01:00:31 +03003260 struct urb *urb,
3261 gfp_t mem_flags)
Aaro Koskinenb1649352013-06-01 21:42:58 +03003262{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003263 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003264 struct device *dev = hcd->self.controller;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003265 int submit_handle = -1;
3266 int pipe_handle;
3267 unsigned long flags;
Aaro Koskinen6e0e1b02013-07-30 23:43:04 +03003268 struct cvmx_usb_iso_packet *iso_packet;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003269 struct usb_host_endpoint *ep = urb->ep;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003270
Aaro Koskinen771378b2013-06-13 01:00:31 +03003271 urb->status = 0;
3272 INIT_LIST_HEAD(&urb->urb_list); /* not enqueued on dequeue_list */
3273 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003274
Aaro Koskinen771378b2013-06-13 01:00:31 +03003275 if (!ep->hcpriv) {
Aaro Koskinen394d4e02013-07-30 23:42:54 +03003276 enum cvmx_usb_transfer transfer_type;
Aaro Koskinen49180722013-07-30 23:42:53 +03003277 enum cvmx_usb_speed speed;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003278 int split_device = 0;
3279 int split_port = 0;
3280 switch (usb_pipetype(urb->pipe)) {
3281 case PIPE_ISOCHRONOUS:
3282 transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
3283 break;
3284 case PIPE_INTERRUPT:
3285 transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
3286 break;
3287 case PIPE_CONTROL:
3288 transfer_type = CVMX_USB_TRANSFER_CONTROL;
3289 break;
3290 default:
3291 transfer_type = CVMX_USB_TRANSFER_BULK;
3292 break;
3293 }
3294 switch (urb->dev->speed) {
3295 case USB_SPEED_LOW:
3296 speed = CVMX_USB_SPEED_LOW;
3297 break;
3298 case USB_SPEED_FULL:
3299 speed = CVMX_USB_SPEED_FULL;
3300 break;
3301 default:
3302 speed = CVMX_USB_SPEED_HIGH;
3303 break;
3304 }
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003305 /*
3306 * For slow devices on high speed ports we need to find the hub
3307 * that does the speed translation so we know where to send the
3308 * split transactions.
3309 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003310 if (speed != CVMX_USB_SPEED_HIGH) {
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003311 /*
3312 * Start at this device and work our way up the usb
3313 * tree.
3314 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003315 struct usb_device *dev = urb->dev;
3316 while (dev->parent) {
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003317 /*
3318 * If our parent is high speed then he'll
3319 * receive the splits.
3320 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003321 if (dev->parent->speed == USB_SPEED_HIGH) {
3322 split_device = dev->parent->devnum;
3323 split_port = dev->portnum;
3324 break;
3325 }
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003326 /*
3327 * Move up the tree one level. If we make it all
3328 * the way up the tree, then the port must not
3329 * be in high speed mode and we don't need a
3330 * split.
3331 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003332 dev = dev->parent;
3333 }
3334 }
3335 pipe_handle = cvmx_usb_open_pipe(&priv->usb,
Aaro Koskinen771378b2013-06-13 01:00:31 +03003336 usb_pipedevice(urb->pipe),
3337 usb_pipeendpoint(urb->pipe),
3338 speed,
3339 le16_to_cpu(ep->desc.wMaxPacketSize) & 0x7ff,
3340 transfer_type,
3341 usb_pipein(urb->pipe) ? CVMX_USB_DIRECTION_IN : CVMX_USB_DIRECTION_OUT,
3342 urb->interval,
3343 (le16_to_cpu(ep->desc.wMaxPacketSize) >> 11) & 0x3,
3344 split_device,
3345 split_port);
3346 if (pipe_handle < 0) {
3347 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003348 dev_dbg(dev, "Failed to create pipe\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003349 return -ENOMEM;
3350 }
3351 ep->hcpriv = (void *)(0x10000L + pipe_handle);
Aaro Koskinenc7609ea2013-06-13 01:00:33 +03003352 } else {
Aaro Koskinen771378b2013-06-13 01:00:31 +03003353 pipe_handle = 0xffff & (long)ep->hcpriv;
Aaro Koskinenc7609ea2013-06-13 01:00:33 +03003354 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003355
Aaro Koskinen771378b2013-06-13 01:00:31 +03003356 switch (usb_pipetype(urb->pipe)) {
3357 case PIPE_ISOCHRONOUS:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003358 dev_dbg(dev, "Submit isochronous to %d.%d\n",
3359 usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003360 /*
3361 * Allocate a structure to use for our private list of
3362 * isochronous packets.
3363 */
Aaro Koskinen6e0e1b02013-07-30 23:43:04 +03003364 iso_packet = kmalloc(urb->number_of_packets *
3365 sizeof(struct cvmx_usb_iso_packet),
3366 GFP_ATOMIC);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003367 if (iso_packet) {
3368 int i;
3369 /* Fill the list with the data from the URB */
3370 for (i = 0; i < urb->number_of_packets; i++) {
3371 iso_packet[i].offset = urb->iso_frame_desc[i].offset;
3372 iso_packet[i].length = urb->iso_frame_desc[i].length;
3373 iso_packet[i].status = CVMX_USB_COMPLETE_ERROR;
3374 }
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003375 /*
3376 * Store a pointer to the list in the URB setup_packet
3377 * field. We know this currently isn't being used and
3378 * this saves us a bunch of logic.
3379 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003380 urb->setup_packet = (char *)iso_packet;
Aaro Koskinene16b5e32013-10-06 22:22:33 +03003381 submit_handle = cvmx_usb_submit_isochronous(&priv->usb,
3382 pipe_handle,
3383 urb);
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003384 /*
3385 * If submit failed we need to free our private packet
3386 * list.
3387 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003388 if (submit_handle < 0) {
3389 urb->setup_packet = NULL;
3390 kfree(iso_packet);
3391 }
3392 }
3393 break;
3394 case PIPE_INTERRUPT:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003395 dev_dbg(dev, "Submit interrupt to %d.%d\n",
3396 usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
Aaro Koskinen9fdbb692013-10-06 22:22:32 +03003397 submit_handle = cvmx_usb_submit_interrupt(&priv->usb,
3398 pipe_handle, urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003399 break;
3400 case PIPE_CONTROL:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003401 dev_dbg(dev, "Submit control to %d.%d\n",
3402 usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
Aaro Koskinen771378b2013-06-13 01:00:31 +03003403 submit_handle = cvmx_usb_submit_control(&priv->usb, pipe_handle,
Aaro Koskinen2ae09e82013-10-06 22:22:31 +03003404 urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003405 break;
3406 case PIPE_BULK:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003407 dev_dbg(dev, "Submit bulk to %d.%d\n",
3408 usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
Aaro Koskinen771378b2013-06-13 01:00:31 +03003409 submit_handle = cvmx_usb_submit_bulk(&priv->usb, pipe_handle,
Aaro Koskinen9ccca702013-10-06 22:22:30 +03003410 urb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003411 break;
3412 }
3413 if (submit_handle < 0) {
3414 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003415 dev_dbg(dev, "Failed to submit\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003416 return -ENOMEM;
3417 }
3418 urb->hcpriv = (void *)(long)(((submit_handle & 0xffff) << 16) | pipe_handle);
3419 spin_unlock_irqrestore(&priv->lock, flags);
3420 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003421}
3422
3423static void octeon_usb_urb_dequeue_work(unsigned long arg)
3424{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003425 unsigned long flags;
3426 struct octeon_hcd *priv = (struct octeon_hcd *)arg;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003427
Aaro Koskinen771378b2013-06-13 01:00:31 +03003428 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003429
Aaro Koskinen771378b2013-06-13 01:00:31 +03003430 while (!list_empty(&priv->dequeue_list)) {
3431 int pipe_handle;
3432 int submit_handle;
3433 struct urb *urb = container_of(priv->dequeue_list.next, struct urb, urb_list);
3434 list_del(&urb->urb_list);
3435 /* not enqueued on dequeue_list */
3436 INIT_LIST_HEAD(&urb->urb_list);
3437 pipe_handle = 0xffff & (long)urb->hcpriv;
3438 submit_handle = ((long)urb->hcpriv) >> 16;
3439 cvmx_usb_cancel(&priv->usb, pipe_handle, submit_handle);
3440 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003441
Aaro Koskinen771378b2013-06-13 01:00:31 +03003442 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003443}
3444
3445static int octeon_usb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
3446{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003447 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3448 unsigned long flags;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003449
Aaro Koskinen771378b2013-06-13 01:00:31 +03003450 if (!urb->dev)
3451 return -EINVAL;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003452
Aaro Koskinen771378b2013-06-13 01:00:31 +03003453 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003454
Aaro Koskinen771378b2013-06-13 01:00:31 +03003455 urb->status = status;
3456 list_add_tail(&urb->urb_list, &priv->dequeue_list);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003457
Aaro Koskinen771378b2013-06-13 01:00:31 +03003458 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003459
Aaro Koskinen771378b2013-06-13 01:00:31 +03003460 tasklet_schedule(&priv->dequeue_tasklet);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003461
Aaro Koskinen771378b2013-06-13 01:00:31 +03003462 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003463}
3464
3465static void octeon_usb_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
3466{
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003467 struct device *dev = hcd->self.controller;
3468
Aaro Koskinen771378b2013-06-13 01:00:31 +03003469 if (ep->hcpriv) {
3470 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3471 int pipe_handle = 0xffff & (long)ep->hcpriv;
3472 unsigned long flags;
3473 spin_lock_irqsave(&priv->lock, flags);
3474 cvmx_usb_cancel_all(&priv->usb, pipe_handle);
3475 if (cvmx_usb_close_pipe(&priv->usb, pipe_handle))
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003476 dev_dbg(dev, "Closing pipe %d failed\n", pipe_handle);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003477 spin_unlock_irqrestore(&priv->lock, flags);
3478 ep->hcpriv = NULL;
3479 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003480}
3481
3482static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
3483{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003484 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen51a19622013-07-30 23:43:03 +03003485 struct cvmx_usb_port_status port_status;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003486 unsigned long flags;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003487
Aaro Koskinen771378b2013-06-13 01:00:31 +03003488 spin_lock_irqsave(&priv->lock, flags);
3489 port_status = cvmx_usb_get_status(&priv->usb);
3490 spin_unlock_irqrestore(&priv->lock, flags);
3491 buf[0] = 0;
3492 buf[0] = port_status.connect_change << 1;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003493
Aaro Koskinen771378b2013-06-13 01:00:31 +03003494 return (buf[0] != 0);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003495}
3496
3497static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
3498{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003499 struct octeon_hcd *priv = hcd_to_octeon(hcd);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003500 struct device *dev = hcd->self.controller;
Aaro Koskinen51a19622013-07-30 23:43:03 +03003501 struct cvmx_usb_port_status usb_port_status;
Aaro Koskinen771378b2013-06-13 01:00:31 +03003502 int port_status;
3503 struct usb_hub_descriptor *desc;
3504 unsigned long flags;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003505
Aaro Koskinen771378b2013-06-13 01:00:31 +03003506 switch (typeReq) {
3507 case ClearHubFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003508 dev_dbg(dev, "ClearHubFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003509 switch (wValue) {
3510 case C_HUB_LOCAL_POWER:
3511 case C_HUB_OVER_CURRENT:
3512 /* Nothing required here */
3513 break;
3514 default:
3515 return -EINVAL;
3516 }
3517 break;
3518 case ClearPortFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003519 dev_dbg(dev, "ClearPortFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003520 if (wIndex != 1) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003521 dev_dbg(dev, " INVALID\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003522 return -EINVAL;
3523 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003524
Aaro Koskinen771378b2013-06-13 01:00:31 +03003525 switch (wValue) {
3526 case USB_PORT_FEAT_ENABLE:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003527 dev_dbg(dev, " ENABLE\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003528 spin_lock_irqsave(&priv->lock, flags);
3529 cvmx_usb_disable(&priv->usb);
3530 spin_unlock_irqrestore(&priv->lock, flags);
3531 break;
3532 case USB_PORT_FEAT_SUSPEND:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003533 dev_dbg(dev, " SUSPEND\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003534 /* Not supported on Octeon */
3535 break;
3536 case USB_PORT_FEAT_POWER:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003537 dev_dbg(dev, " POWER\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003538 /* Not supported on Octeon */
3539 break;
3540 case USB_PORT_FEAT_INDICATOR:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003541 dev_dbg(dev, " INDICATOR\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003542 /* Port inidicator not supported */
3543 break;
3544 case USB_PORT_FEAT_C_CONNECTION:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003545 dev_dbg(dev, " C_CONNECTION\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003546 /* Clears drivers internal connect status change flag */
3547 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinen29a202f2013-10-06 22:22:26 +03003548 priv->usb.port_status = cvmx_usb_get_status(&priv->usb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003549 spin_unlock_irqrestore(&priv->lock, flags);
3550 break;
3551 case USB_PORT_FEAT_C_RESET:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003552 dev_dbg(dev, " C_RESET\n");
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003553 /*
3554 * Clears the driver's internal Port Reset Change flag.
3555 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003556 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinen29a202f2013-10-06 22:22:26 +03003557 priv->usb.port_status = 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);
Aaro Koskinen29a202f2013-10-06 22:22:26 +03003567 priv->usb.port_status = cvmx_usb_get_status(&priv->usb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003568 spin_unlock_irqrestore(&priv->lock, flags);
3569 break;
3570 case USB_PORT_FEAT_C_SUSPEND:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003571 dev_dbg(dev, " C_SUSPEND\n");
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003572 /*
3573 * Clears the driver's internal Port Suspend Change
3574 * flag, which is set when resume signaling on the host
3575 * port is complete.
3576 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003577 break;
3578 case USB_PORT_FEAT_C_OVER_CURRENT:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003579 dev_dbg(dev, " C_OVER_CURRENT\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003580 /* Clears the driver's overcurrent Change flag */
3581 spin_lock_irqsave(&priv->lock, flags);
Aaro Koskinen29a202f2013-10-06 22:22:26 +03003582 priv->usb.port_status = cvmx_usb_get_status(&priv->usb);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003583 spin_unlock_irqrestore(&priv->lock, flags);
3584 break;
3585 default:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003586 dev_dbg(dev, " UNKNOWN\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003587 return -EINVAL;
3588 }
Aaro Koskinen771378b2013-06-13 01:00:31 +03003589 break;
3590 case GetHubDescriptor:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003591 dev_dbg(dev, "GetHubDescriptor\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003592 desc = (struct usb_hub_descriptor *)buf;
3593 desc->bDescLength = 9;
3594 desc->bDescriptorType = 0x29;
3595 desc->bNbrPorts = 1;
3596 desc->wHubCharacteristics = 0x08;
3597 desc->bPwrOn2PwrGood = 1;
3598 desc->bHubContrCurrent = 0;
3599 desc->u.hs.DeviceRemovable[0] = 0;
3600 desc->u.hs.DeviceRemovable[1] = 0xff;
3601 break;
3602 case GetHubStatus:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003603 dev_dbg(dev, "GetHubStatus\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003604 *(__le32 *) buf = 0;
3605 break;
3606 case GetPortStatus:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003607 dev_dbg(dev, "GetPortStatus\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003608 if (wIndex != 1) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003609 dev_dbg(dev, " INVALID\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003610 return -EINVAL;
3611 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003612
Aaro Koskinen771378b2013-06-13 01:00:31 +03003613 spin_lock_irqsave(&priv->lock, flags);
3614 usb_port_status = cvmx_usb_get_status(&priv->usb);
3615 spin_unlock_irqrestore(&priv->lock, flags);
3616 port_status = 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003617
Aaro Koskinen771378b2013-06-13 01:00:31 +03003618 if (usb_port_status.connect_change) {
3619 port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003620 dev_dbg(dev, " C_CONNECTION\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003621 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003622
Aaro Koskinen771378b2013-06-13 01:00:31 +03003623 if (usb_port_status.port_enabled) {
3624 port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003625 dev_dbg(dev, " C_ENABLE\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003626 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003627
Aaro Koskinen771378b2013-06-13 01:00:31 +03003628 if (usb_port_status.connected) {
3629 port_status |= (1 << USB_PORT_FEAT_CONNECTION);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003630 dev_dbg(dev, " CONNECTION\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003631 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003632
Aaro Koskinen771378b2013-06-13 01:00:31 +03003633 if (usb_port_status.port_enabled) {
3634 port_status |= (1 << USB_PORT_FEAT_ENABLE);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003635 dev_dbg(dev, " ENABLE\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003636 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003637
Aaro Koskinen771378b2013-06-13 01:00:31 +03003638 if (usb_port_status.port_over_current) {
3639 port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003640 dev_dbg(dev, " OVER_CURRENT\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003641 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003642
Aaro Koskinen771378b2013-06-13 01:00:31 +03003643 if (usb_port_status.port_powered) {
3644 port_status |= (1 << USB_PORT_FEAT_POWER);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003645 dev_dbg(dev, " POWER\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003646 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003647
Aaro Koskinen771378b2013-06-13 01:00:31 +03003648 if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
3649 port_status |= USB_PORT_STAT_HIGH_SPEED;
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003650 dev_dbg(dev, " HIGHSPEED\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003651 } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
3652 port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003653 dev_dbg(dev, " LOWSPEED\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003654 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003655
Aaro Koskinen771378b2013-06-13 01:00:31 +03003656 *((__le32 *) buf) = cpu_to_le32(port_status);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003657 break;
3658 case SetHubFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003659 dev_dbg(dev, "SetHubFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003660 /* No HUB features supported */
3661 break;
3662 case SetPortFeature:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003663 dev_dbg(dev, "SetPortFeature\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003664 if (wIndex != 1) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003665 dev_dbg(dev, " INVALID\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003666 return -EINVAL;
3667 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003668
Aaro Koskinen771378b2013-06-13 01:00:31 +03003669 switch (wValue) {
3670 case USB_PORT_FEAT_SUSPEND:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003671 dev_dbg(dev, " SUSPEND\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003672 return -EINVAL;
3673 case USB_PORT_FEAT_POWER:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003674 dev_dbg(dev, " POWER\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003675 return -EINVAL;
3676 case USB_PORT_FEAT_RESET:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003677 dev_dbg(dev, " RESET\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003678 spin_lock_irqsave(&priv->lock, flags);
3679 cvmx_usb_disable(&priv->usb);
3680 if (cvmx_usb_enable(&priv->usb))
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003681 dev_dbg(dev, "Failed to enable the port\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003682 spin_unlock_irqrestore(&priv->lock, flags);
3683 return 0;
3684 case USB_PORT_FEAT_INDICATOR:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003685 dev_dbg(dev, " INDICATOR\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003686 /* Not supported */
3687 break;
3688 default:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003689 dev_dbg(dev, " UNKNOWN\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003690 return -EINVAL;
3691 }
3692 break;
3693 default:
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003694 dev_dbg(dev, "Unknown root hub request\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003695 return -EINVAL;
3696 }
3697 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003698}
3699
3700
3701static const struct hc_driver octeon_hc_driver = {
Aaro Koskinen771378b2013-06-13 01:00:31 +03003702 .description = "Octeon USB",
3703 .product_desc = "Octeon Host Controller",
3704 .hcd_priv_size = sizeof(struct octeon_hcd),
3705 .irq = octeon_usb_irq,
3706 .flags = HCD_MEMORY | HCD_USB2,
3707 .start = octeon_usb_start,
3708 .stop = octeon_usb_stop,
3709 .urb_enqueue = octeon_usb_urb_enqueue,
3710 .urb_dequeue = octeon_usb_urb_dequeue,
3711 .endpoint_disable = octeon_usb_endpoint_disable,
3712 .get_frame_number = octeon_usb_get_frame_number,
3713 .hub_status_data = octeon_usb_hub_status_data,
3714 .hub_control = octeon_usb_hub_control,
Aaro Koskinenb1649352013-06-01 21:42:58 +03003715};
3716
3717
3718static int octeon_usb_driver_probe(struct device *dev)
3719{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003720 int status;
3721 int usb_num = to_platform_device(dev)->id;
3722 int irq = platform_get_irq(to_platform_device(dev), 0);
3723 struct octeon_hcd *priv;
3724 struct usb_hcd *hcd;
3725 unsigned long flags;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003726
Aaro Koskinen87e7e572013-06-18 02:36:19 +03003727 /*
3728 * Set the DMA mask to 64bits so we get buffers already translated for
3729 * DMA.
3730 */
Aaro Koskinen771378b2013-06-13 01:00:31 +03003731 dev->coherent_dma_mask = ~0;
3732 dev->dma_mask = &dev->coherent_dma_mask;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003733
Aaro Koskinen771378b2013-06-13 01:00:31 +03003734 hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
3735 if (!hcd) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003736 dev_dbg(dev, "Failed to allocate memory for HCD\n");
Aaro Koskinen771378b2013-06-13 01:00:31 +03003737 return -1;
3738 }
3739 hcd->uses_new_polling = 1;
3740 priv = (struct octeon_hcd *)hcd->hcd_priv;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003741
Aaro Koskinen771378b2013-06-13 01:00:31 +03003742 spin_lock_init(&priv->lock);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003743
Aaro Koskinen771378b2013-06-13 01:00:31 +03003744 tasklet_init(&priv->dequeue_tasklet, octeon_usb_urb_dequeue_work, (unsigned long)priv);
3745 INIT_LIST_HEAD(&priv->dequeue_list);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003746
Aaro Koskinen9be317e2013-10-01 23:43:09 +03003747 status = cvmx_usb_initialize(&priv->usb, usb_num);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003748 if (status) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003749 dev_dbg(dev, "USB initialization failed with %d\n", status);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003750 kfree(hcd);
3751 return -1;
3752 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003753
Aaro Koskinen771378b2013-06-13 01:00:31 +03003754 /* This delay is needed for CN3010, but I don't know why... */
3755 mdelay(10);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003756
Aaro Koskinen771378b2013-06-13 01:00:31 +03003757 spin_lock_irqsave(&priv->lock, flags);
3758 cvmx_usb_poll(&priv->usb);
3759 spin_unlock_irqrestore(&priv->lock, flags);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003760
Aaro Koskinen771378b2013-06-13 01:00:31 +03003761 status = usb_add_hcd(hcd, irq, IRQF_SHARED);
3762 if (status) {
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003763 dev_dbg(dev, "USB add HCD failed with %d\n", status);
Aaro Koskinen771378b2013-06-13 01:00:31 +03003764 kfree(hcd);
3765 return -1;
3766 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003767
Aaro Koskinend9352172013-06-18 02:36:20 +03003768 dev_dbg(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003769
Aaro Koskinen771378b2013-06-13 01:00:31 +03003770 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003771}
3772
3773static int octeon_usb_driver_remove(struct device *dev)
3774{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003775 int status;
3776 struct usb_hcd *hcd = dev_get_drvdata(dev);
3777 struct octeon_hcd *priv = hcd_to_octeon(hcd);
3778 unsigned long flags;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003779
Aaro Koskinen771378b2013-06-13 01:00:31 +03003780 usb_remove_hcd(hcd);
3781 tasklet_kill(&priv->dequeue_tasklet);
3782 spin_lock_irqsave(&priv->lock, flags);
3783 status = cvmx_usb_shutdown(&priv->usb);
3784 spin_unlock_irqrestore(&priv->lock, flags);
3785 if (status)
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003786 dev_dbg(dev, "USB shutdown failed with %d\n", status);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003787
Aaro Koskinen771378b2013-06-13 01:00:31 +03003788 kfree(hcd);
Aaro Koskinenb1649352013-06-01 21:42:58 +03003789
Aaro Koskinen771378b2013-06-13 01:00:31 +03003790 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003791}
3792
3793static struct device_driver octeon_usb_driver = {
Aaro Koskinen771378b2013-06-13 01:00:31 +03003794 .name = "OcteonUSB",
3795 .bus = &platform_bus_type,
3796 .probe = octeon_usb_driver_probe,
3797 .remove = octeon_usb_driver_remove,
Aaro Koskinenb1649352013-06-01 21:42:58 +03003798};
3799
3800
3801#define MAX_USB_PORTS 10
Aaro Koskinenf5ed3a32013-06-13 01:00:38 +03003802static struct platform_device *pdev_glob[MAX_USB_PORTS];
Aaro Koskinenb1649352013-06-01 21:42:58 +03003803static int octeon_usb_registered;
3804static int __init octeon_usb_module_init(void)
3805{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003806 int num_devices = cvmx_usb_get_num_ports();
3807 int device;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003808
Aaro Koskinen771378b2013-06-13 01:00:31 +03003809 if (usb_disabled() || num_devices == 0)
3810 return -ENODEV;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003811
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003812 if (driver_register(&octeon_usb_driver))
Aaro Koskinen771378b2013-06-13 01:00:31 +03003813 return -ENOMEM;
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003814
Aaro Koskinen771378b2013-06-13 01:00:31 +03003815 octeon_usb_registered = 1;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003816
3817 /*
3818 * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
3819 * IOB priority registers. Under heavy network load USB
3820 * hardware can be starved by the IOB causing a crash. Give
3821 * it a priority boost if it has been waiting more than 400
3822 * cycles to avoid this situation.
3823 *
3824 * Testing indicates that a cnt_val of 8192 is not sufficient,
3825 * but no failures are seen with 4096. We choose a value of
3826 * 400 to give a safety factor of 10.
3827 */
3828 if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
3829 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
3830
3831 pri_cnt.u64 = 0;
3832 pri_cnt.s.cnt_enb = 1;
3833 pri_cnt.s.cnt_val = 400;
3834 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
3835 }
3836
Aaro Koskinen771378b2013-06-13 01:00:31 +03003837 for (device = 0; device < num_devices; device++) {
3838 struct resource irq_resource;
3839 struct platform_device *pdev;
3840 memset(&irq_resource, 0, sizeof(irq_resource));
3841 irq_resource.start = (device == 0) ? OCTEON_IRQ_USB0 : OCTEON_IRQ_USB1;
3842 irq_resource.end = irq_resource.start;
3843 irq_resource.flags = IORESOURCE_IRQ;
3844 pdev = platform_device_register_simple((char *)octeon_usb_driver. name, device, &irq_resource, 1);
3845 if (IS_ERR(pdev)) {
Aaro Koskinen771378b2013-06-13 01:00:31 +03003846 driver_unregister(&octeon_usb_driver);
3847 octeon_usb_registered = 0;
3848 return PTR_ERR(pdev);
3849 }
3850 if (device < MAX_USB_PORTS)
3851 pdev_glob[device] = pdev;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003852
Aaro Koskinen771378b2013-06-13 01:00:31 +03003853 }
3854 return 0;
Aaro Koskinenb1649352013-06-01 21:42:58 +03003855}
3856
3857static void __exit octeon_usb_module_cleanup(void)
3858{
Aaro Koskinen771378b2013-06-13 01:00:31 +03003859 int i;
Aaro Koskinen71e06db2013-06-18 02:36:17 +03003860
Aaro Koskinen771378b2013-06-13 01:00:31 +03003861 for (i = 0; i < MAX_USB_PORTS; i++)
3862 if (pdev_glob[i]) {
3863 platform_device_unregister(pdev_glob[i]);
3864 pdev_glob[i] = NULL;
3865 }
Aaro Koskinenb1649352013-06-01 21:42:58 +03003866 if (octeon_usb_registered)
3867 driver_unregister(&octeon_usb_driver);
3868}
3869
3870MODULE_LICENSE("GPL");
3871MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
3872MODULE_DESCRIPTION("Cavium Networks Octeon USB Host driver.");
3873module_init(octeon_usb_module_init);
3874module_exit(octeon_usb_module_cleanup);