xref: /openbsd-src/sys/dev/usb/dwc2/dwc2_hcd.h (revision a9beb1ed4245313a4b68e10ec33a67c806523ec4)
1*a9beb1edSmglocker /*	$OpenBSD: dwc2_hcd.h,v 1.16 2022/09/04 08:42:40 mglocker Exp $	*/
225e434cbSuebayasi /*	$NetBSD: dwc2_hcd.h,v 1.9 2014/09/03 10:00:08 skrll Exp $	*/
3578f812dSuebayasi 
4578f812dSuebayasi /*
5578f812dSuebayasi  * hcd.h - DesignWare HS OTG Controller host-mode declarations
6578f812dSuebayasi  *
7578f812dSuebayasi  * Copyright (C) 2004-2013 Synopsys, Inc.
8578f812dSuebayasi  *
9578f812dSuebayasi  * Redistribution and use in source and binary forms, with or without
10578f812dSuebayasi  * modification, are permitted provided that the following conditions
11578f812dSuebayasi  * are met:
12578f812dSuebayasi  * 1. Redistributions of source code must retain the above copyright
13578f812dSuebayasi  *    notice, this list of conditions, and the following disclaimer,
14578f812dSuebayasi  *    without modification.
15578f812dSuebayasi  * 2. Redistributions in binary form must reproduce the above copyright
16578f812dSuebayasi  *    notice, this list of conditions and the following disclaimer in the
17578f812dSuebayasi  *    documentation and/or other materials provided with the distribution.
18578f812dSuebayasi  * 3. The names of the above-listed copyright holders may not be used
19578f812dSuebayasi  *    to endorse or promote products derived from this software without
20578f812dSuebayasi  *    specific prior written permission.
21578f812dSuebayasi  *
22578f812dSuebayasi  * ALTERNATIVELY, this software may be distributed under the terms of the
23578f812dSuebayasi  * GNU General Public License ("GPL") as published by the Free Software
24578f812dSuebayasi  * Foundation; either version 2 of the License, or (at your option) any
25578f812dSuebayasi  * later version.
26578f812dSuebayasi  *
27578f812dSuebayasi  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28578f812dSuebayasi  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29578f812dSuebayasi  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30578f812dSuebayasi  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31578f812dSuebayasi  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32578f812dSuebayasi  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33578f812dSuebayasi  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34578f812dSuebayasi  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35578f812dSuebayasi  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36578f812dSuebayasi  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37578f812dSuebayasi  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38578f812dSuebayasi  */
39578f812dSuebayasi #ifndef __DWC2_HCD_H__
40578f812dSuebayasi #define __DWC2_HCD_H__
41578f812dSuebayasi 
42578f812dSuebayasi /*
43578f812dSuebayasi  * This file contains the structures, constants, and interfaces for the
44578f812dSuebayasi  * Host Contoller Driver (HCD)
45578f812dSuebayasi  *
46578f812dSuebayasi  * The Host Controller Driver (HCD) is responsible for translating requests
47578f812dSuebayasi  * from the USB Driver into the appropriate actions on the DWC_otg controller.
48578f812dSuebayasi  * It isolates the USBD from the specifics of the controller by providing an
49578f812dSuebayasi  * API to the USBD.
50578f812dSuebayasi  */
51578f812dSuebayasi 
52578f812dSuebayasi struct dwc2_qh;
53578f812dSuebayasi 
54578f812dSuebayasi /**
55578f812dSuebayasi  * struct dwc2_host_chan - Software host channel descriptor
56578f812dSuebayasi  *
57578f812dSuebayasi  * @hc_num:             Host channel number, used for register address lookup
58578f812dSuebayasi  * @dev_addr:           Address of the device
59578f812dSuebayasi  * @ep_num:             Endpoint of the device
60578f812dSuebayasi  * @ep_is_in:           Endpoint direction
61578f812dSuebayasi  * @speed:              Device speed. One of the following values:
62578f812dSuebayasi  *                       - USB_SPEED_LOW
63578f812dSuebayasi  *                       - USB_SPEED_FULL
64578f812dSuebayasi  *                       - USB_SPEED_HIGH
65578f812dSuebayasi  * @ep_type:            Endpoint type. One of the following values:
66578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_CONTROL: 0
67578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_ISOC:    1
68578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_BULK:    2
69578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_INTR:    3
70578f812dSuebayasi  * @max_packet:         Max packet size in bytes
71578f812dSuebayasi  * @data_pid_start:     PID for initial transaction.
72578f812dSuebayasi  *                       0: DATA0
73578f812dSuebayasi  *                       1: DATA2
74578f812dSuebayasi  *                       2: DATA1
75578f812dSuebayasi  *                       3: MDATA (non-Control EP),
76578f812dSuebayasi  *                          SETUP (Control EP)
77578f812dSuebayasi  * @multi_count:        Number of additional periodic transactions per
78578f812dSuebayasi  *                      (micro)frame
79578f812dSuebayasi  * @xfer_buf:           Pointer to current transfer buffer position
80578f812dSuebayasi  * @xfer_dma:           DMA address of xfer_buf
81578f812dSuebayasi  * @align_buf:          In Buffer DMA mode this will be used if xfer_buf is not
82578f812dSuebayasi  *                      DWORD aligned
83578f812dSuebayasi  * @xfer_len:           Total number of bytes to transfer
84578f812dSuebayasi  * @xfer_count:         Number of bytes transferred so far
85578f812dSuebayasi  * @start_pkt_count:    Packet count at start of transfer
86578f812dSuebayasi  * @xfer_started:       True if the transfer has been started
87*a9beb1edSmglocker  * @do_ping:            True if a PING request should be issued on this channel
88578f812dSuebayasi  * @error_state:        True if the error count for this transaction is non-zero
89578f812dSuebayasi  * @halt_on_queue:      True if this channel should be halted the next time a
90578f812dSuebayasi  *                      request is queued for the channel. This is necessary in
91578f812dSuebayasi  *                      slave mode if no request queue space is available when
92578f812dSuebayasi  *                      an attempt is made to halt the channel.
93578f812dSuebayasi  * @halt_pending:       True if the host channel has been halted, but the core
94578f812dSuebayasi  *                      is not finished flushing queued requests
95578f812dSuebayasi  * @do_split:           Enable split for the channel
96578f812dSuebayasi  * @complete_split:     Enable complete split
97578f812dSuebayasi  * @hub_addr:           Address of high speed hub for the split
98578f812dSuebayasi  * @hub_port:           Port of the low/full speed device for the split
99578f812dSuebayasi  * @xact_pos:           Split transaction position. One of the following values:
100578f812dSuebayasi  *                       - DWC2_HCSPLT_XACTPOS_MID
101578f812dSuebayasi  *                       - DWC2_HCSPLT_XACTPOS_BEGIN
102578f812dSuebayasi  *                       - DWC2_HCSPLT_XACTPOS_END
103578f812dSuebayasi  *                       - DWC2_HCSPLT_XACTPOS_ALL
104578f812dSuebayasi  * @requests:           Number of requests issued for this channel since it was
105578f812dSuebayasi  *                      assigned to the current transfer (not counting PINGs)
106578f812dSuebayasi  * @schinfo:            Scheduling micro-frame bitmap
107578f812dSuebayasi  * @ntd:                Number of transfer descriptors for the transfer
108578f812dSuebayasi  * @halt_status:        Reason for halting the host channel
109*a9beb1edSmglocker  * @hcint:               Contents of the HCINT register when the interrupt came
110578f812dSuebayasi  * @qh:                 QH for the transfer being processed by this channel
111578f812dSuebayasi  * @hc_list_entry:      For linking to list of host channels
112578f812dSuebayasi  * @desc_list_addr:     Current QH's descriptor list DMA address
113d05ae140Smglocker  * @desc_list_sz:       Current QH's descriptor list size
1149bb9c189Smglocker  * @split_order_list_entry: List entry for keeping track of the order of splits
115578f812dSuebayasi  *
116578f812dSuebayasi  * This structure represents the state of a single host channel when acting in
117578f812dSuebayasi  * host mode. It contains the data items needed to transfer packets to an
118578f812dSuebayasi  * endpoint via a host channel.
119578f812dSuebayasi  */
120578f812dSuebayasi struct dwc2_host_chan {
121578f812dSuebayasi 	u8 hc_num;
122578f812dSuebayasi 
123578f812dSuebayasi 	unsigned dev_addr:7;
124578f812dSuebayasi 	unsigned ep_num:4;
125578f812dSuebayasi 	unsigned ep_is_in:1;
126578f812dSuebayasi 	unsigned speed:4;
127578f812dSuebayasi 	unsigned ep_type:2;
128578f812dSuebayasi 	unsigned max_packet:11;
129578f812dSuebayasi 	unsigned data_pid_start:2;
130578f812dSuebayasi #define DWC2_HC_PID_DATA0	TSIZ_SC_MC_PID_DATA0
131578f812dSuebayasi #define DWC2_HC_PID_DATA2	TSIZ_SC_MC_PID_DATA2
132578f812dSuebayasi #define DWC2_HC_PID_DATA1	TSIZ_SC_MC_PID_DATA1
133578f812dSuebayasi #define DWC2_HC_PID_MDATA	TSIZ_SC_MC_PID_MDATA
134578f812dSuebayasi #define DWC2_HC_PID_SETUP	TSIZ_SC_MC_PID_SETUP
135578f812dSuebayasi 
136578f812dSuebayasi 	unsigned multi_count:2;
137578f812dSuebayasi 
1382043d414Suebayasi 	struct usb_dma *xfer_usbdma;
139578f812dSuebayasi 	u8 *xfer_buf;
140578f812dSuebayasi 	dma_addr_t xfer_dma;
141578f812dSuebayasi 	dma_addr_t align_buf;
142578f812dSuebayasi 	u32 xfer_len;
143578f812dSuebayasi 	u32 xfer_count;
144578f812dSuebayasi 	u16 start_pkt_count;
145578f812dSuebayasi 	u8 xfer_started;
146578f812dSuebayasi 	u8 do_ping;
147578f812dSuebayasi 	u8 error_state;
148578f812dSuebayasi 	u8 halt_on_queue;
149578f812dSuebayasi 	u8 halt_pending;
150578f812dSuebayasi 	u8 do_split;
151578f812dSuebayasi 	u8 complete_split;
152578f812dSuebayasi 	u8 hub_addr;
153578f812dSuebayasi 	u8 hub_port;
154578f812dSuebayasi 	u8 xact_pos;
155578f812dSuebayasi #define DWC2_HCSPLT_XACTPOS_MID	HCSPLT_XACTPOS_MID
156578f812dSuebayasi #define DWC2_HCSPLT_XACTPOS_END	HCSPLT_XACTPOS_END
157578f812dSuebayasi #define DWC2_HCSPLT_XACTPOS_BEGIN HCSPLT_XACTPOS_BEGIN
158578f812dSuebayasi #define DWC2_HCSPLT_XACTPOS_ALL	HCSPLT_XACTPOS_ALL
159578f812dSuebayasi 
160578f812dSuebayasi 	u8 requests;
161578f812dSuebayasi 	u8 schinfo;
162578f812dSuebayasi 	u16 ntd;
163578f812dSuebayasi 	enum dwc2_halt_status halt_status;
164578f812dSuebayasi 	u32 hcint;
165578f812dSuebayasi 	struct dwc2_qh *qh;
166d05ae140Smglocker 	struct list_head hc_list_entry;
167d05ae140Smglocker 	struct usb_dma desc_list_usbdma;
168578f812dSuebayasi 	dma_addr_t desc_list_addr;
169d05ae140Smglocker 	u32 desc_list_sz;
1709bb9c189Smglocker 	struct list_head split_order_list_entry;
171578f812dSuebayasi };
172578f812dSuebayasi 
173578f812dSuebayasi struct dwc2_hcd_pipe_info {
174578f812dSuebayasi 	u8 dev_addr;
175578f812dSuebayasi 	u8 ep_num;
176578f812dSuebayasi 	u8 pipe_type;
177578f812dSuebayasi 	u8 pipe_dir;
178*a9beb1edSmglocker 	u16 maxp;
179*a9beb1edSmglocker 	u16 maxp_mult;
180578f812dSuebayasi };
181578f812dSuebayasi 
182578f812dSuebayasi struct dwc2_hcd_iso_packet_desc {
183578f812dSuebayasi 	u32 offset;
184578f812dSuebayasi 	u32 length;
185578f812dSuebayasi 	u32 actual_length;
186578f812dSuebayasi 	u32 status;
187578f812dSuebayasi };
188578f812dSuebayasi 
189578f812dSuebayasi struct dwc2_qtd;
190578f812dSuebayasi 
191578f812dSuebayasi struct dwc2_hcd_urb {
192*a9beb1edSmglocker 	void *priv;
193578f812dSuebayasi 	struct dwc2_qtd *qtd;
1942043d414Suebayasi 	struct usb_dma *usbdma;
195*a9beb1edSmglocker 	void *buf;
196578f812dSuebayasi 	dma_addr_t dma;
1972043d414Suebayasi 	struct usb_dma *setup_usbdma;
198578f812dSuebayasi 	void *setup_packet;
199578f812dSuebayasi 	dma_addr_t setup_dma;
200578f812dSuebayasi 	u32 length;
201578f812dSuebayasi 	u32 actual_length;
202578f812dSuebayasi 	u32 status;
203578f812dSuebayasi 	u32 error_count;
204578f812dSuebayasi 	u32 packet_count;
205578f812dSuebayasi 	u32 flags;
206578f812dSuebayasi 	u16 interval;
207578f812dSuebayasi 	struct dwc2_hcd_pipe_info pipe_info;
208*a9beb1edSmglocker 	struct dwc2_hcd_iso_packet_desc iso_descs[];
209578f812dSuebayasi };
210578f812dSuebayasi 
211578f812dSuebayasi /* Phases for control transfers */
212578f812dSuebayasi enum dwc2_control_phase {
213578f812dSuebayasi 	DWC2_CONTROL_SETUP,
214578f812dSuebayasi 	DWC2_CONTROL_DATA,
215578f812dSuebayasi 	DWC2_CONTROL_STATUS,
216578f812dSuebayasi };
217578f812dSuebayasi 
218578f812dSuebayasi /* Transaction types */
219578f812dSuebayasi enum dwc2_transaction_type {
220578f812dSuebayasi 	DWC2_TRANSACTION_NONE,
221578f812dSuebayasi 	DWC2_TRANSACTION_PERIODIC,
222578f812dSuebayasi 	DWC2_TRANSACTION_NON_PERIODIC,
223578f812dSuebayasi 	DWC2_TRANSACTION_ALL,
224578f812dSuebayasi };
225578f812dSuebayasi 
226*a9beb1edSmglocker /* The number of elements per LS bitmap (per port on multi_tt) */
227*a9beb1edSmglocker #define DWC2_ELEMENTS_PER_LS_BITMAP	DIV_ROUND_UP(DWC2_LS_SCHEDULE_SLICES, \
228*a9beb1edSmglocker 						     BITS_PER_LONG)
229*a9beb1edSmglocker 
230*a9beb1edSmglocker /**
231*a9beb1edSmglocker  * struct dwc2_tt - dwc2 data associated with a usb_tt
232*a9beb1edSmglocker  *
233*a9beb1edSmglocker  * @refcount:           Number of Queue Heads (QHs) holding a reference.
234*a9beb1edSmglocker  * @usb_tt:             Pointer back to the official usb_tt.
235*a9beb1edSmglocker  * @periodic_bitmaps:   Bitmap for which parts of the 1ms frame are accounted
236*a9beb1edSmglocker  *                      for already.  Each is DWC2_ELEMENTS_PER_LS_BITMAP
237*a9beb1edSmglocker  *			elements (so sizeof(long) times that in bytes).
238*a9beb1edSmglocker  *
239*a9beb1edSmglocker  * This structure is stored in the hcpriv of the official usb_tt.
240*a9beb1edSmglocker  */
241*a9beb1edSmglocker struct dwc2_tt {
242*a9beb1edSmglocker 	int refcount;
243*a9beb1edSmglocker 	struct usbd_tt *usb_tt;
244*a9beb1edSmglocker 	unsigned long periodic_bitmaps[];
245*a9beb1edSmglocker };
246*a9beb1edSmglocker 
247*a9beb1edSmglocker /**
248*a9beb1edSmglocker  * struct dwc2_hs_transfer_time - Info about a transfer on the high speed bus.
249*a9beb1edSmglocker  *
250*a9beb1edSmglocker  * @start_schedule_us:  The start time on the main bus schedule.  Note that
251*a9beb1edSmglocker  *                         the main bus schedule is tightly packed and this
252*a9beb1edSmglocker  *			   time should be interpreted as tightly packed (so
253*a9beb1edSmglocker  *			   uFrame 0 starts at 0 us, uFrame 1 starts at 100 us
254*a9beb1edSmglocker  *			   instead of 125 us).
255*a9beb1edSmglocker  * @duration_us:           How long this transfer goes.
256*a9beb1edSmglocker  */
257*a9beb1edSmglocker 
258*a9beb1edSmglocker struct dwc2_hs_transfer_time {
259*a9beb1edSmglocker 	u32 start_schedule_us;
260*a9beb1edSmglocker 	u16 duration_us;
261*a9beb1edSmglocker };
262*a9beb1edSmglocker 
263578f812dSuebayasi /**
264578f812dSuebayasi  * struct dwc2_qh - Software queue head structure
265578f812dSuebayasi  *
266d05ae140Smglocker  * @hsotg:              The HCD state structure for the DWC OTG controller
267578f812dSuebayasi  * @ep_type:            Endpoint type. One of the following values:
268578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_CONTROL
269578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_BULK
270578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_INT
271578f812dSuebayasi  *                       - USB_ENDPOINT_XFER_ISOC
272578f812dSuebayasi  * @ep_is_in:           Endpoint direction
273578f812dSuebayasi  * @maxp:               Value from wMaxPacketSize field of Endpoint Descriptor
274*a9beb1edSmglocker  * @maxp_mult:          Multiplier for maxp
275578f812dSuebayasi  * @dev_speed:          Device speed. One of the following values:
276578f812dSuebayasi  *                       - USB_SPEED_LOW
277578f812dSuebayasi  *                       - USB_SPEED_FULL
278578f812dSuebayasi  *                       - USB_SPEED_HIGH
279578f812dSuebayasi  * @data_toggle:        Determines the PID of the next data packet for
280578f812dSuebayasi  *                      non-controltransfers. Ignored for control transfers.
281578f812dSuebayasi  *                      One of the following values:
282578f812dSuebayasi  *                       - DWC2_HC_PID_DATA0
283578f812dSuebayasi  *                       - DWC2_HC_PID_DATA1
284578f812dSuebayasi  * @ping_state:         Ping state
285578f812dSuebayasi  * @do_split:           Full/low speed endpoint on high-speed hub requires split
286578f812dSuebayasi  * @td_first:           Index of first activated isochronous transfer descriptor
287578f812dSuebayasi  * @td_last:            Index of last activated isochronous transfer descriptor
288*a9beb1edSmglocker  * @host_us:            Bandwidth in microseconds per transfer as seen by host
289*a9beb1edSmglocker  * @device_us:          Bandwidth in microseconds per transfer as seen by device
290*a9beb1edSmglocker  * @host_interval:      Interval between transfers as seen by the host.  If
291*a9beb1edSmglocker  *                      the host is high speed and the device is low speed this
292*a9beb1edSmglocker  *                      will be 8 times device interval.
293*a9beb1edSmglocker  * @device_interval:    Interval between transfers as seen by the device.
294*a9beb1edSmglocker  *                      interval.
295*a9beb1edSmglocker  * @next_active_frame:  (Micro)frame _before_ we next need to put something on
296*a9beb1edSmglocker  *                      the bus.  We'll move the qh to active here.  If the
297*a9beb1edSmglocker  *                      host is in high speed mode this will be a uframe.  If
298*a9beb1edSmglocker  *                      the host is in low speed mode this will be a full frame.
299*a9beb1edSmglocker  * @start_active_frame: If we are partway through a split transfer, this will be
300*a9beb1edSmglocker  *			what next_active_frame was when we started.  Otherwise
301*a9beb1edSmglocker  *			it should always be the same as next_active_frame.
302*a9beb1edSmglocker  * @num_hs_transfers:   Number of transfers in hs_transfers.
303*a9beb1edSmglocker  *                      Normally this is 1 but can be more than one for splits.
304*a9beb1edSmglocker  *                      Always >= 1 unless the host is in low/full speed mode.
305*a9beb1edSmglocker  * @hs_transfers:       Transfers that are scheduled as seen by the high speed
306*a9beb1edSmglocker  *                      bus.  Not used if host is in low or full speed mode (but
307*a9beb1edSmglocker  *                      note that it IS USED if the device is low or full speed
308*a9beb1edSmglocker  *                      as long as the HOST is in high speed mode).
309*a9beb1edSmglocker  * @ls_start_schedule_slice: Start time (in slices) on the low speed bus
310*a9beb1edSmglocker  *                           schedule that's being used by this device.  This
311*a9beb1edSmglocker  *			     will be on the periodic_bitmap in a
312*a9beb1edSmglocker  *                           "struct dwc2_tt".  Not used if this device is high
313*a9beb1edSmglocker  *                           speed.  Note that this is in "schedule slice" which
314*a9beb1edSmglocker  *                           is tightly packed.
315578f812dSuebayasi  * @ntd:                Actual number of transfer descriptors in a list
316578f812dSuebayasi  * @dw_align_buf:       Used instead of original buffer if its physical address
317578f812dSuebayasi  *                      is not dword-aligned
318d05ae140Smglocker  * @dw_align_buf_dma:   DMA address for dw_align_buf
319578f812dSuebayasi  * @qtd_list:           List of QTDs for this QH
320578f812dSuebayasi  * @channel:            Host channel currently processing transfers for this QH
321578f812dSuebayasi  * @qh_list_entry:      Entry for QH in either the periodic or non-periodic
322578f812dSuebayasi  *                      schedule
323578f812dSuebayasi  * @desc_list:          List of transfer descriptors
324578f812dSuebayasi  * @desc_list_dma:      Physical address of desc_list
325d05ae140Smglocker  * @desc_list_sz:       Size of descriptors list
326578f812dSuebayasi  * @n_bytes:            Xfer Bytes array. Each element corresponds to a transfer
327578f812dSuebayasi  *                      descriptor and indicates original XferSize value for the
328578f812dSuebayasi  *                      descriptor
329*a9beb1edSmglocker  * @unreserve_timer:    Timer for releasing periodic reservation.
330d05ae140Smglocker  * @wait_timer:         Timer used to wait before re-queuing.
331*a9beb1edSmglocker  * @dwc_tt:            Pointer to our tt info (or NULL if no tt).
332*a9beb1edSmglocker  * @ttport:             Port number within our tt.
333578f812dSuebayasi  * @tt_buffer_dirty     True if clear_tt_buffer_complete is pending
334*a9beb1edSmglocker  * @unreserve_pending:  True if we planned to unreserve but haven't yet.
335*a9beb1edSmglocker  * @schedule_low_speed: True if we have a low/full speed component (either the
336*a9beb1edSmglocker  *			host is in low/full speed mode or do_split).
337d05ae140Smglocker  * @want_wait:          We should wait before re-queuing; only matters for non-
338d05ae140Smglocker  *                      periodic transfers and is ignored for periodic ones.
339d05ae140Smglocker  * @wait_timer_cancel:  Set to true to cancel the wait_timer.
340578f812dSuebayasi  *
341*a9beb1edSmglocker  * @tt_buffer_dirty:	True if EP's TT buffer is not clean.
3420f392f15Suebayasi  * A Queue Head (QH) holds the static characteristics of an endpoint and
343578f812dSuebayasi  * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
344578f812dSuebayasi  * be entered in either the non-periodic or periodic schedule.
345578f812dSuebayasi  */
346578f812dSuebayasi struct dwc2_qh {
347d05ae140Smglocker 	struct dwc2_hsotg *hsotg;
348578f812dSuebayasi 	u8 ep_type;
349578f812dSuebayasi 	u8 ep_is_in;
350578f812dSuebayasi 	u16 maxp;
351*a9beb1edSmglocker 	u16 maxp_mult;
352578f812dSuebayasi 	u8 dev_speed;
353578f812dSuebayasi 	u8 data_toggle;
354578f812dSuebayasi 	u8 ping_state;
355578f812dSuebayasi 	u8 do_split;
356578f812dSuebayasi 	u8 td_first;
357578f812dSuebayasi 	u8 td_last;
358*a9beb1edSmglocker 	u16 host_us;
359*a9beb1edSmglocker 	u16 device_us;
360*a9beb1edSmglocker 	u16 host_interval;
361*a9beb1edSmglocker 	u16 device_interval;
362*a9beb1edSmglocker 	u16 next_active_frame;
363*a9beb1edSmglocker 	u16 start_active_frame;
364*a9beb1edSmglocker 	s16 num_hs_transfers;
365*a9beb1edSmglocker 	struct dwc2_hs_transfer_time hs_transfers[DWC2_HS_SCHEDULE_UFRAMES];
366*a9beb1edSmglocker 	u32 ls_start_schedule_slice;
367578f812dSuebayasi 	u16 ntd;
368578f812dSuebayasi 	u8 *dw_align_buf;
369d05ae140Smglocker 	int dw_align_buf_size;
370578f812dSuebayasi 	dma_addr_t dw_align_buf_dma;
371*a9beb1edSmglocker 	struct usb_dma dw_align_buf_usbdma;
372d05ae140Smglocker 	struct list_head qtd_list;
373578f812dSuebayasi 	struct dwc2_host_chan *channel;
374d05ae140Smglocker 	struct list_head qh_list_entry;
3753215ab50Suebayasi 	struct usb_dma desc_list_usbdma;
376*a9beb1edSmglocker 	struct dwc2_dma_desc *desc_list;
377578f812dSuebayasi 	dma_addr_t desc_list_dma;
378d05ae140Smglocker 	u32 desc_list_sz;
379578f812dSuebayasi 	u32 *n_bytes;
380*a9beb1edSmglocker 	struct timeout unreserve_timer;
381d05ae140Smglocker 	struct timeout wait_timer;
382*a9beb1edSmglocker 	struct dwc2_tt *dwc_tt;
383*a9beb1edSmglocker 	int ttport;
384578f812dSuebayasi 	unsigned tt_buffer_dirty:1;
385*a9beb1edSmglocker 	unsigned unreserve_pending:1;
386*a9beb1edSmglocker 	unsigned schedule_low_speed:1;
387d05ae140Smglocker 	unsigned want_wait:1;
388d05ae140Smglocker 	unsigned wait_timer_cancel:1;
389578f812dSuebayasi };
390578f812dSuebayasi 
391578f812dSuebayasi /**
392578f812dSuebayasi  * struct dwc2_qtd - Software queue transfer descriptor (QTD)
393578f812dSuebayasi  *
394578f812dSuebayasi  * @control_phase:      Current phase for control transfers (Setup, Data, or
395578f812dSuebayasi  *                      Status)
396578f812dSuebayasi  * @in_process:         Indicates if this QTD is currently processed by HW
397578f812dSuebayasi  * @data_toggle:        Determines the PID of the next data packet for the
398578f812dSuebayasi  *                      data phase of control transfers. Ignored for other
399578f812dSuebayasi  *                      transfer types. One of the following values:
400578f812dSuebayasi  *                       - DWC2_HC_PID_DATA0
401578f812dSuebayasi  *                       - DWC2_HC_PID_DATA1
402578f812dSuebayasi  * @complete_split:     Keeps track of the current split type for FS/LS
403578f812dSuebayasi  *                      endpoints on a HS Hub
404578f812dSuebayasi  * @isoc_split_pos:     Position of the ISOC split in full/low speed
405578f812dSuebayasi  * @isoc_frame_index:   Index of the next frame descriptor for an isochronous
406578f812dSuebayasi  *                      transfer. A frame descriptor describes the buffer
407578f812dSuebayasi  *                      position and length of the data to be transferred in the
408578f812dSuebayasi  *                      next scheduled (micro)frame of an isochronous transfer.
409578f812dSuebayasi  *                      It also holds status for that transaction. The frame
410578f812dSuebayasi  *                      index starts at 0.
411578f812dSuebayasi  * @isoc_split_offset:  Position of the ISOC split in the buffer for the
412578f812dSuebayasi  *                      current frame
413578f812dSuebayasi  * @ssplit_out_xfer_count: How many bytes transferred during SSPLIT OUT
414578f812dSuebayasi  * @error_count:        Holds the number of bus errors that have occurred for
415578f812dSuebayasi  *                      a transaction within this transfer
416578f812dSuebayasi  * @n_desc:             Number of DMA descriptors for this QTD
417578f812dSuebayasi  * @isoc_frame_index_last: Last activated frame (packet) index, used in
418578f812dSuebayasi  *                      descriptor DMA mode only
419d05ae140Smglocker  * @num_naks:           Number of NAKs received on this QTD.
420578f812dSuebayasi  * @urb:                URB for this transfer
421578f812dSuebayasi  * @qh:                 Queue head for this QTD
422578f812dSuebayasi  * @qtd_list_entry:     For linking to the QH's list of QTDs
423*a9beb1edSmglocker  * @isoc_td_first:	Index of first activated isochronous transfer
424*a9beb1edSmglocker  *			descriptor in Descriptor DMA mode
425*a9beb1edSmglocker  * @isoc_td_last:	Index of last activated isochronous transfer
426*a9beb1edSmglocker  *			descriptor in Descriptor DMA mode
427578f812dSuebayasi  *
428578f812dSuebayasi  * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
429578f812dSuebayasi  * interrupt, or isochronous transfer. A single QTD is created for each URB
430578f812dSuebayasi  * (of one of these types) submitted to the HCD. The transfer associated with
431578f812dSuebayasi  * a QTD may require one or multiple transactions.
432578f812dSuebayasi  *
433578f812dSuebayasi  * A QTD is linked to a Queue Head, which is entered in either the
434578f812dSuebayasi  * non-periodic or periodic schedule for execution. When a QTD is chosen for
435578f812dSuebayasi  * execution, some or all of its transactions may be executed. After
436578f812dSuebayasi  * execution, the state of the QTD is updated. The QTD may be retired if all
437578f812dSuebayasi  * its transactions are complete or if an error occurred. Otherwise, it
438578f812dSuebayasi  * remains in the schedule so more transactions can be executed later.
439578f812dSuebayasi  */
440578f812dSuebayasi struct dwc2_qtd {
441578f812dSuebayasi 	enum dwc2_control_phase control_phase;
442578f812dSuebayasi 	u8 in_process;
443578f812dSuebayasi 	u8 data_toggle;
444578f812dSuebayasi 	u8 complete_split;
445578f812dSuebayasi 	u8 isoc_split_pos;
446578f812dSuebayasi 	u16 isoc_frame_index;
447578f812dSuebayasi 	u16 isoc_split_offset;
448d05ae140Smglocker 	u16 isoc_td_last;
449d05ae140Smglocker 	u16 isoc_td_first;
450578f812dSuebayasi 	u32 ssplit_out_xfer_count;
451578f812dSuebayasi 	u8 error_count;
452578f812dSuebayasi 	u8 n_desc;
453578f812dSuebayasi 	u16 isoc_frame_index_last;
454d05ae140Smglocker 	u16 num_naks;
455578f812dSuebayasi 	struct dwc2_hcd_urb *urb;
456578f812dSuebayasi 	struct dwc2_qh *qh;
457d05ae140Smglocker 	struct list_head qtd_list_entry;
458578f812dSuebayasi };
459578f812dSuebayasi 
460578f812dSuebayasi #ifdef DEBUG
461578f812dSuebayasi struct hc_xfer_info {
462578f812dSuebayasi 	struct dwc2_hsotg *hsotg;
463578f812dSuebayasi 	struct dwc2_host_chan *chan;
464578f812dSuebayasi };
465578f812dSuebayasi #endif
466578f812dSuebayasi 
467*a9beb1edSmglocker u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg);
468*a9beb1edSmglocker 
469*a9beb1edSmglocker #if 0
470578f812dSuebayasi /* Gets the struct usb_hcd that contains a struct dwc2_hsotg */
471d05ae140Smglocker static inline struct usb_hcd *dwc2_hsotg_to_hcd(struct dwc2_hsotg *hsotg)
472578f812dSuebayasi {
473578f812dSuebayasi 	return (struct usb_hcd *)hsotg->priv;
474578f812dSuebayasi }
475*a9beb1edSmglocker #endif
476578f812dSuebayasi 
477578f812dSuebayasi /*
478578f812dSuebayasi  * Inline used to disable one channel interrupt. Channel interrupts are
479578f812dSuebayasi  * disabled when the channel is halted or released by the interrupt handler.
480578f812dSuebayasi  * There is no need to handle further interrupts of that type until the
481578f812dSuebayasi  * channel is re-assigned. In fact, subsequent handling may cause crashes
482578f812dSuebayasi  * because the channel structures are cleaned up when the channel is released.
483578f812dSuebayasi  */
disable_hc_int(struct dwc2_hsotg * hsotg,int chnum,u32 intr)484d05ae140Smglocker static inline void disable_hc_int(struct dwc2_hsotg *hsotg, int chnum, u32 intr)
485578f812dSuebayasi {
486*a9beb1edSmglocker 	u32 mask = dwc2_readl(hsotg, HCINTMSK(chnum));
487578f812dSuebayasi 
488578f812dSuebayasi 	mask &= ~intr;
489*a9beb1edSmglocker 	dwc2_writel(hsotg, mask, HCINTMSK(chnum));
490578f812dSuebayasi }
491578f812dSuebayasi 
492*a9beb1edSmglocker void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan);
493*a9beb1edSmglocker void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
494*a9beb1edSmglocker 		  enum dwc2_halt_status halt_status);
495*a9beb1edSmglocker void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
496*a9beb1edSmglocker 				 struct dwc2_host_chan *chan);
497*a9beb1edSmglocker 
498578f812dSuebayasi /*
499578f812dSuebayasi  * Reads HPRT0 in preparation to modify. It keeps the WC bits 0 so that if they
500578f812dSuebayasi  * are read as 1, they won't clear when written back.
501578f812dSuebayasi  */
dwc2_read_hprt0(struct dwc2_hsotg * hsotg)502d05ae140Smglocker static inline u32 dwc2_read_hprt0(struct dwc2_hsotg *hsotg)
503578f812dSuebayasi {
504*a9beb1edSmglocker 	u32 hprt0 = dwc2_readl(hsotg, HPRT0);
505578f812dSuebayasi 
506578f812dSuebayasi 	hprt0 &= ~(HPRT0_ENA | HPRT0_CONNDET | HPRT0_ENACHG | HPRT0_OVRCURRCHG);
507578f812dSuebayasi 	return hprt0;
508578f812dSuebayasi }
509578f812dSuebayasi 
dwc2_hcd_get_ep_num(struct dwc2_hcd_pipe_info * pipe)510d05ae140Smglocker static inline u8 dwc2_hcd_get_ep_num(struct dwc2_hcd_pipe_info *pipe)
511578f812dSuebayasi {
512578f812dSuebayasi 	return pipe->ep_num;
513578f812dSuebayasi }
514578f812dSuebayasi 
dwc2_hcd_get_pipe_type(struct dwc2_hcd_pipe_info * pipe)515d05ae140Smglocker static inline u8 dwc2_hcd_get_pipe_type(struct dwc2_hcd_pipe_info *pipe)
516578f812dSuebayasi {
517578f812dSuebayasi 	return pipe->pipe_type;
518578f812dSuebayasi }
519578f812dSuebayasi 
dwc2_hcd_get_maxp(struct dwc2_hcd_pipe_info * pipe)520*a9beb1edSmglocker static inline u16 dwc2_hcd_get_maxp(struct dwc2_hcd_pipe_info *pipe)
521578f812dSuebayasi {
522*a9beb1edSmglocker 	return pipe->maxp;
523*a9beb1edSmglocker }
524*a9beb1edSmglocker 
dwc2_hcd_get_maxp_mult(struct dwc2_hcd_pipe_info * pipe)525*a9beb1edSmglocker static inline u16 dwc2_hcd_get_maxp_mult(struct dwc2_hcd_pipe_info *pipe)
526*a9beb1edSmglocker {
527*a9beb1edSmglocker 	return pipe->maxp_mult;
528578f812dSuebayasi }
529578f812dSuebayasi 
dwc2_hcd_get_dev_addr(struct dwc2_hcd_pipe_info * pipe)530d05ae140Smglocker static inline u8 dwc2_hcd_get_dev_addr(struct dwc2_hcd_pipe_info *pipe)
531578f812dSuebayasi {
532578f812dSuebayasi 	return pipe->dev_addr;
533578f812dSuebayasi }
534578f812dSuebayasi 
dwc2_hcd_is_pipe_isoc(struct dwc2_hcd_pipe_info * pipe)535d05ae140Smglocker static inline u8 dwc2_hcd_is_pipe_isoc(struct dwc2_hcd_pipe_info *pipe)
536578f812dSuebayasi {
537578f812dSuebayasi 	return pipe->pipe_type == USB_ENDPOINT_XFER_ISOC;
538578f812dSuebayasi }
539578f812dSuebayasi 
dwc2_hcd_is_pipe_int(struct dwc2_hcd_pipe_info * pipe)540d05ae140Smglocker static inline u8 dwc2_hcd_is_pipe_int(struct dwc2_hcd_pipe_info *pipe)
541578f812dSuebayasi {
542578f812dSuebayasi 	return pipe->pipe_type == USB_ENDPOINT_XFER_INT;
543578f812dSuebayasi }
544578f812dSuebayasi 
dwc2_hcd_is_pipe_bulk(struct dwc2_hcd_pipe_info * pipe)545d05ae140Smglocker static inline u8 dwc2_hcd_is_pipe_bulk(struct dwc2_hcd_pipe_info *pipe)
546578f812dSuebayasi {
547578f812dSuebayasi 	return pipe->pipe_type == USB_ENDPOINT_XFER_BULK;
548578f812dSuebayasi }
549578f812dSuebayasi 
dwc2_hcd_is_pipe_control(struct dwc2_hcd_pipe_info * pipe)550d05ae140Smglocker static inline u8 dwc2_hcd_is_pipe_control(struct dwc2_hcd_pipe_info *pipe)
551578f812dSuebayasi {
552578f812dSuebayasi 	return pipe->pipe_type == USB_ENDPOINT_XFER_CONTROL;
553578f812dSuebayasi }
554578f812dSuebayasi 
dwc2_hcd_is_pipe_in(struct dwc2_hcd_pipe_info * pipe)555d05ae140Smglocker static inline u8 dwc2_hcd_is_pipe_in(struct dwc2_hcd_pipe_info *pipe)
556578f812dSuebayasi {
557578f812dSuebayasi 	return pipe->pipe_dir == USB_DIR_IN;
558578f812dSuebayasi }
559578f812dSuebayasi 
dwc2_hcd_is_pipe_out(struct dwc2_hcd_pipe_info * pipe)560d05ae140Smglocker static inline u8 dwc2_hcd_is_pipe_out(struct dwc2_hcd_pipe_info *pipe)
561578f812dSuebayasi {
562578f812dSuebayasi 	return !dwc2_hcd_is_pipe_in(pipe);
563578f812dSuebayasi }
564578f812dSuebayasi 
565d05ae140Smglocker extern int dwc2_hcd_init(struct dwc2_hsotg *hsotg);
566*a9beb1edSmglocker //extern void dwc2_hcd_remove(struct dwc2_hsotg *hsotg);
567578f812dSuebayasi 
568578f812dSuebayasi /* Transaction Execution Functions */
569578f812dSuebayasi extern enum dwc2_transaction_type dwc2_hcd_select_transactions(
570578f812dSuebayasi 						struct dwc2_hsotg *hsotg);
571578f812dSuebayasi extern void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
572578f812dSuebayasi 					enum dwc2_transaction_type tr_type);
573578f812dSuebayasi 
574578f812dSuebayasi /* Schedule Queue Functions */
575578f812dSuebayasi /* Implemented in hcd_queue.c */
576578f812dSuebayasi extern void dwc2_hcd_init_usecs(struct dwc2_hsotg *hsotg);
577d05ae140Smglocker extern struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
578d05ae140Smglocker 					  struct dwc2_hcd_urb *urb,
579d05ae140Smglocker 					  gfp_t mem_flags);
580578f812dSuebayasi extern void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh);
581578f812dSuebayasi extern int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh);
582578f812dSuebayasi extern void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh);
583578f812dSuebayasi extern void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
584578f812dSuebayasi 				   int sched_csplit);
585578f812dSuebayasi 
586578f812dSuebayasi extern void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb);
587578f812dSuebayasi extern int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
588d05ae140Smglocker 			    struct dwc2_qh *qh);
589578f812dSuebayasi 
590*a9beb1edSmglocker /* Unlinks and frees a QTD */
dwc2_hcd_qtd_unlink_and_free(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh)591*a9beb1edSmglocker static inline void dwc2_hcd_qtd_unlink_and_free(struct dwc2_hsotg *hsotg,
592578f812dSuebayasi 						struct dwc2_qtd *qtd,
593*a9beb1edSmglocker 						struct dwc2_qh *qh)
594*a9beb1edSmglocker {
595*a9beb1edSmglocker 	struct dwc2_softc *sc = hsotg->hsotg_sc;
596*a9beb1edSmglocker 
597*a9beb1edSmglocker 	list_del(&qtd->qtd_list_entry);
598*a9beb1edSmglocker 	pool_put(&sc->sc_qtdpool, qtd);
599*a9beb1edSmglocker }
600578f812dSuebayasi 
601578f812dSuebayasi /* Descriptor DMA support functions */
602578f812dSuebayasi extern void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg,
603578f812dSuebayasi 				     struct dwc2_qh *qh);
604578f812dSuebayasi extern void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
605578f812dSuebayasi 					struct dwc2_host_chan *chan, int chnum,
606578f812dSuebayasi 					enum dwc2_halt_status halt_status);
607578f812dSuebayasi 
608578f812dSuebayasi extern int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
609578f812dSuebayasi 				 gfp_t mem_flags);
610578f812dSuebayasi extern void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh);
611578f812dSuebayasi 
612578f812dSuebayasi /* Check if QH is non-periodic */
613578f812dSuebayasi #define dwc2_qh_is_non_per(_qh_ptr_) \
614578f812dSuebayasi 	((_qh_ptr_)->ep_type == USB_ENDPOINT_XFER_BULK || \
615578f812dSuebayasi 	 (_qh_ptr_)->ep_type == USB_ENDPOINT_XFER_CONTROL)
616578f812dSuebayasi 
617d05ae140Smglocker #ifdef CONFIG_USB_DWC2_DEBUG_PERIODIC
dbg_hc(struct dwc2_host_chan * hc)618d05ae140Smglocker static inline bool dbg_hc(struct dwc2_host_chan *hc) { return true; }
dbg_qh(struct dwc2_qh * qh)619d05ae140Smglocker static inline bool dbg_qh(struct dwc2_qh *qh) { return true; }
dbg_urb(struct urb * urb)620*a9beb1edSmglocker static inline bool dbg_urb(struct urb *urb) { return true; }
dbg_perio(void)621d05ae140Smglocker static inline bool dbg_perio(void) { return true; }
622d05ae140Smglocker #else /* !CONFIG_USB_DWC2_DEBUG_PERIODIC */
dbg_hc(struct dwc2_host_chan * hc)623d05ae140Smglocker static inline bool dbg_hc(struct dwc2_host_chan *hc)
624578f812dSuebayasi {
625578f812dSuebayasi 	return hc->ep_type == USB_ENDPOINT_XFER_BULK ||
626578f812dSuebayasi 	       hc->ep_type == USB_ENDPOINT_XFER_CONTROL;
627578f812dSuebayasi }
628578f812dSuebayasi 
dbg_qh(struct dwc2_qh * qh)629d05ae140Smglocker static inline bool dbg_qh(struct dwc2_qh *qh)
630578f812dSuebayasi {
631578f812dSuebayasi 	return qh->ep_type == USB_ENDPOINT_XFER_BULK ||
632578f812dSuebayasi 	       qh->ep_type == USB_ENDPOINT_XFER_CONTROL;
633578f812dSuebayasi }
634578f812dSuebayasi 
635*a9beb1edSmglocker #if 0
636*a9beb1edSmglocker static inline bool dbg_urb(struct urb *urb)
637*a9beb1edSmglocker {
638*a9beb1edSmglocker 	return usb_pipetype(urb->pipe) == PIPE_BULK ||
639*a9beb1edSmglocker 	       usb_pipetype(urb->pipe) == PIPE_CONTROL;
640*a9beb1edSmglocker }
641*a9beb1edSmglocker #endif
642578f812dSuebayasi 
dbg_perio(void)643d05ae140Smglocker static inline bool dbg_perio(void) { return false; }
644578f812dSuebayasi #endif
645578f812dSuebayasi 
646578f812dSuebayasi /*
647d05ae140Smglocker  * Returns true if frame1 index is greater than frame2 index. The comparison
648d05ae140Smglocker  * is done modulo FRLISTEN_64_SIZE. This accounts for the rollover of the
649d05ae140Smglocker  * frame number when the max index frame number is reached.
650d05ae140Smglocker  */
dwc2_frame_idx_num_gt(u16 fr_idx1,u16 fr_idx2)651d05ae140Smglocker static inline bool dwc2_frame_idx_num_gt(u16 fr_idx1, u16 fr_idx2)
652d05ae140Smglocker {
653d05ae140Smglocker 	u16 diff = fr_idx1 - fr_idx2;
654d05ae140Smglocker 	u16 sign = diff & (FRLISTEN_64_SIZE >> 1);
655d05ae140Smglocker 
656d05ae140Smglocker 	return diff && !sign;
657d05ae140Smglocker }
658d05ae140Smglocker 
659d05ae140Smglocker /*
660578f812dSuebayasi  * Returns true if frame1 is less than or equal to frame2. The comparison is
661578f812dSuebayasi  * done modulo HFNUM_MAX_FRNUM. This accounts for the rollover of the
662578f812dSuebayasi  * frame number when the max frame number is reached.
663578f812dSuebayasi  */
dwc2_frame_num_le(u16 frame1,u16 frame2)664d05ae140Smglocker static inline int dwc2_frame_num_le(u16 frame1, u16 frame2)
665578f812dSuebayasi {
666578f812dSuebayasi 	return ((frame2 - frame1) & HFNUM_MAX_FRNUM) <= (HFNUM_MAX_FRNUM >> 1);
667578f812dSuebayasi }
668578f812dSuebayasi 
669578f812dSuebayasi /*
670578f812dSuebayasi  * Returns true if frame1 is greater than frame2. The comparison is done
671578f812dSuebayasi  * modulo HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
672578f812dSuebayasi  * number when the max frame number is reached.
673578f812dSuebayasi  */
dwc2_frame_num_gt(u16 frame1,u16 frame2)674d05ae140Smglocker static inline int dwc2_frame_num_gt(u16 frame1, u16 frame2)
675578f812dSuebayasi {
676578f812dSuebayasi 	return (frame1 != frame2) &&
677578f812dSuebayasi 	       ((frame1 - frame2) & HFNUM_MAX_FRNUM) < (HFNUM_MAX_FRNUM >> 1);
678578f812dSuebayasi }
679578f812dSuebayasi 
680578f812dSuebayasi /*
681578f812dSuebayasi  * Increments frame by the amount specified by inc. The addition is done
682578f812dSuebayasi  * modulo HFNUM_MAX_FRNUM. Returns the incremented value.
683578f812dSuebayasi  */
dwc2_frame_num_inc(u16 frame,u16 inc)684d05ae140Smglocker static inline u16 dwc2_frame_num_inc(u16 frame, u16 inc)
685578f812dSuebayasi {
686578f812dSuebayasi 	return (frame + inc) & HFNUM_MAX_FRNUM;
687578f812dSuebayasi }
688578f812dSuebayasi 
dwc2_frame_num_dec(u16 frame,u16 dec)689*a9beb1edSmglocker static inline u16 dwc2_frame_num_dec(u16 frame, u16 dec)
690*a9beb1edSmglocker {
691*a9beb1edSmglocker 	return (frame + HFNUM_MAX_FRNUM + 1 - dec) & HFNUM_MAX_FRNUM;
692*a9beb1edSmglocker }
693*a9beb1edSmglocker 
dwc2_full_frame_num(u16 frame)694d05ae140Smglocker static inline u16 dwc2_full_frame_num(u16 frame)
695578f812dSuebayasi {
696578f812dSuebayasi 	return (frame & HFNUM_MAX_FRNUM) >> 3;
697578f812dSuebayasi }
698578f812dSuebayasi 
dwc2_micro_frame_num(u16 frame)699d05ae140Smglocker static inline u16 dwc2_micro_frame_num(u16 frame)
700578f812dSuebayasi {
701578f812dSuebayasi 	return frame & 0x7;
702578f812dSuebayasi }
703578f812dSuebayasi 
704578f812dSuebayasi /*
705578f812dSuebayasi  * Returns the Core Interrupt Status register contents, ANDed with the Core
706578f812dSuebayasi  * Interrupt Mask register contents
707578f812dSuebayasi  */
dwc2_read_core_intr(struct dwc2_hsotg * hsotg)708d05ae140Smglocker static inline u32 dwc2_read_core_intr(struct dwc2_hsotg *hsotg)
709578f812dSuebayasi {
710*a9beb1edSmglocker 	return dwc2_readl(hsotg, GINTSTS) &
711*a9beb1edSmglocker 	       dwc2_readl(hsotg, GINTMSK);
712578f812dSuebayasi }
713578f812dSuebayasi 
dwc2_hcd_urb_get_status(struct dwc2_hcd_urb * dwc2_urb)714d05ae140Smglocker static inline u32 dwc2_hcd_urb_get_status(struct dwc2_hcd_urb *dwc2_urb)
715578f812dSuebayasi {
716578f812dSuebayasi 	return dwc2_urb->status;
717578f812dSuebayasi }
718578f812dSuebayasi 
dwc2_hcd_urb_get_actual_length(struct dwc2_hcd_urb * dwc2_urb)719d05ae140Smglocker static inline u32 dwc2_hcd_urb_get_actual_length(
720578f812dSuebayasi 		struct dwc2_hcd_urb *dwc2_urb)
721578f812dSuebayasi {
722578f812dSuebayasi 	return dwc2_urb->actual_length;
723578f812dSuebayasi }
724578f812dSuebayasi 
dwc2_hcd_urb_get_error_count(struct dwc2_hcd_urb * dwc2_urb)725d05ae140Smglocker static inline u32 dwc2_hcd_urb_get_error_count(struct dwc2_hcd_urb *dwc2_urb)
726578f812dSuebayasi {
727578f812dSuebayasi 	return dwc2_urb->error_count;
728578f812dSuebayasi }
729578f812dSuebayasi 
dwc2_hcd_urb_set_iso_desc_params(struct dwc2_hcd_urb * dwc2_urb,int desc_num,u32 offset,u32 length)730d05ae140Smglocker static inline void dwc2_hcd_urb_set_iso_desc_params(
731578f812dSuebayasi 		struct dwc2_hcd_urb *dwc2_urb, int desc_num, u32 offset,
732578f812dSuebayasi 		u32 length)
733578f812dSuebayasi {
734578f812dSuebayasi 	dwc2_urb->iso_descs[desc_num].offset = offset;
735578f812dSuebayasi 	dwc2_urb->iso_descs[desc_num].length = length;
736578f812dSuebayasi }
737578f812dSuebayasi 
dwc2_hcd_urb_get_iso_desc_status(struct dwc2_hcd_urb * dwc2_urb,int desc_num)738d05ae140Smglocker static inline u32 dwc2_hcd_urb_get_iso_desc_status(
739578f812dSuebayasi 		struct dwc2_hcd_urb *dwc2_urb, int desc_num)
740578f812dSuebayasi {
741578f812dSuebayasi 	return dwc2_urb->iso_descs[desc_num].status;
742578f812dSuebayasi }
743578f812dSuebayasi 
dwc2_hcd_urb_get_iso_desc_actual_length(struct dwc2_hcd_urb * dwc2_urb,int desc_num)744d05ae140Smglocker static inline u32 dwc2_hcd_urb_get_iso_desc_actual_length(
745578f812dSuebayasi 		struct dwc2_hcd_urb *dwc2_urb, int desc_num)
746578f812dSuebayasi {
747578f812dSuebayasi 	return dwc2_urb->iso_descs[desc_num].actual_length;
748578f812dSuebayasi }
749578f812dSuebayasi 
dwc2_hcd_is_bandwidth_allocated(struct dwc2_hsotg * hsotg,struct usbd_xfer * xfer)750d05ae140Smglocker static inline int dwc2_hcd_is_bandwidth_allocated(struct dwc2_hsotg *hsotg,
75140442a57Suebayasi 						  struct usbd_xfer *xfer)
752578f812dSuebayasi {
753578f812dSuebayasi 	struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
754578f812dSuebayasi 	struct dwc2_qh *qh = dpipe->priv;
755578f812dSuebayasi 
756d05ae140Smglocker 	if (qh && !list_empty(&qh->qh_list_entry))
757578f812dSuebayasi 		return 1;
758578f812dSuebayasi 
759578f812dSuebayasi 	return 0;
760578f812dSuebayasi }
761578f812dSuebayasi 
dwc2_hcd_get_ep_bandwidth(struct dwc2_hsotg * hsotg,struct dwc2_pipe * dpipe)762d05ae140Smglocker static inline u16 dwc2_hcd_get_ep_bandwidth(struct dwc2_hsotg *hsotg,
763578f812dSuebayasi 					    struct dwc2_pipe *dpipe)
764578f812dSuebayasi {
765578f812dSuebayasi 	struct dwc2_qh *qh = dpipe->priv;
766578f812dSuebayasi 
767578f812dSuebayasi 	if (!qh) {
768578f812dSuebayasi 		WARN_ON(1);
769578f812dSuebayasi 		return 0;
770578f812dSuebayasi 	}
771578f812dSuebayasi 
772*a9beb1edSmglocker 	return qh->host_us;
773578f812dSuebayasi }
774578f812dSuebayasi 
775578f812dSuebayasi extern void dwc2_hcd_save_data_toggle(struct dwc2_hsotg *hsotg,
776578f812dSuebayasi 			       struct dwc2_host_chan *chan, int chnum,
777578f812dSuebayasi 				      struct dwc2_qtd *qtd);
778578f812dSuebayasi 
779578f812dSuebayasi /* HCD Core API */
780578f812dSuebayasi 
781578f812dSuebayasi /**
782578f812dSuebayasi  * dwc2_handle_hcd_intr() - Called on every hardware interrupt
783578f812dSuebayasi  *
784578f812dSuebayasi  * @hsotg: The DWC2 HCD
785578f812dSuebayasi  *
786578f812dSuebayasi  * Returns IRQ_HANDLED if interrupt is handled
787578f812dSuebayasi  * Return IRQ_NONE if interrupt is not handled
788578f812dSuebayasi  */
789578f812dSuebayasi extern irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg);
790578f812dSuebayasi 
791578f812dSuebayasi /**
792578f812dSuebayasi  * dwc2_hcd_stop() - Halts the DWC_otg host mode operation
793578f812dSuebayasi  *
794578f812dSuebayasi  * @hsotg: The DWC2 HCD
795578f812dSuebayasi  */
796578f812dSuebayasi extern void dwc2_hcd_stop(struct dwc2_hsotg *hsotg);
797578f812dSuebayasi 
798578f812dSuebayasi /**
799578f812dSuebayasi  * dwc2_hcd_is_b_host() - Returns 1 if core currently is acting as B host,
800578f812dSuebayasi  * and 0 otherwise
801578f812dSuebayasi  *
802578f812dSuebayasi  * @hsotg: The DWC2 HCD
803578f812dSuebayasi  */
804578f812dSuebayasi extern int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg);
805578f812dSuebayasi 
806578f812dSuebayasi /**
807578f812dSuebayasi  * dwc2_hcd_dump_state() - Dumps hsotg state
808578f812dSuebayasi  *
809578f812dSuebayasi  * @hsotg: The DWC2 HCD
810578f812dSuebayasi  *
811578f812dSuebayasi  * NOTE: This function will be removed once the peripheral controller code
812578f812dSuebayasi  * is integrated and the driver is stable
813578f812dSuebayasi  */
814578f812dSuebayasi extern void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg);
815578f812dSuebayasi 
816578f812dSuebayasi /* URB interface */
817578f812dSuebayasi 
818578f812dSuebayasi /* Transfer flags */
819578f812dSuebayasi #define URB_GIVEBACK_ASAP	0x1
820578f812dSuebayasi #define URB_SEND_ZERO_PACKET	0x2
821578f812dSuebayasi 
822578f812dSuebayasi /* Host driver callbacks */
823*a9beb1edSmglocker struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg,
824*a9beb1edSmglocker 				      void *context, gfp_t mem_flags,
825*a9beb1edSmglocker 				      int *ttport);
826578f812dSuebayasi 
827*a9beb1edSmglocker void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg,
828*a9beb1edSmglocker 			   struct dwc2_tt *dwc_tt);
829*a9beb1edSmglocker int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context);
830*a9beb1edSmglocker void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
831578f812dSuebayasi 			int status);
832578f812dSuebayasi 
833*a9beb1edSmglocker /* Required for OpenBSD */
834*a9beb1edSmglocker struct dwc2_hcd_urb * dwc2_hcd_urb_alloc(struct dwc2_hsotg *, int, gfp_t);
835*a9beb1edSmglocker void dwc2_hcd_urb_free(struct dwc2_hsotg *, struct dwc2_hcd_urb *, int);
836578f812dSuebayasi int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *, struct dwc2_hcd_urb *);
837578f812dSuebayasi int dwc2_hcd_hub_control(struct dwc2_hsotg *, u16, u16, u16, char *, u16);
838*a9beb1edSmglocker void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *, struct dwc2_hcd_urb *,
839*a9beb1edSmglocker 			       u8, u8, u8, u8, u16, u16);
840*a9beb1edSmglocker 
841d05ae140Smglocker int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
842d05ae140Smglocker                                 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
843d05ae140Smglocker                                 struct dwc2_qtd *qtd);
844*a9beb1edSmglocker void dwc2_allocate_bus_bandwidth(struct dwc2_hsotg *, u16, struct usbd_xfer *);
845578f812dSuebayasi 
846*a9beb1edSmglocker long dwc2_usb_calc_bus_time(int, int, int, int);
847*a9beb1edSmglocker int dwc2_ttthink_to_ns(struct dwc2_hsotg *, void *, int);
848578f812dSuebayasi 
849578f812dSuebayasi #endif /* __DWC2_HCD_H__ */
850