162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * gadget.h - DesignWare USB3 DRD Gadget Header
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Authors: Felipe Balbi <balbi@ti.com>,
862306a36Sopenharmony_ci *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef __DRIVERS_USB_DWC3_GADGET_H
1262306a36Sopenharmony_ci#define __DRIVERS_USB_DWC3_GADGET_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/list.h>
1562306a36Sopenharmony_ci#include <linux/usb/gadget.h>
1662306a36Sopenharmony_ci#include "io.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistruct dwc3;
1962306a36Sopenharmony_ci#define to_dwc3_ep(ep)		(container_of(ep, struct dwc3_ep, endpoint))
2062306a36Sopenharmony_ci#define gadget_to_dwc(g)	(dev_get_platdata(&g->dev))
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/* DEPCFG parameter 1 */
2362306a36Sopenharmony_ci#define DWC3_DEPCFG_INT_NUM(n)		(((n) & 0x1f) << 0)
2462306a36Sopenharmony_ci#define DWC3_DEPCFG_XFER_COMPLETE_EN	BIT(8)
2562306a36Sopenharmony_ci#define DWC3_DEPCFG_XFER_IN_PROGRESS_EN	BIT(9)
2662306a36Sopenharmony_ci#define DWC3_DEPCFG_XFER_NOT_READY_EN	BIT(10)
2762306a36Sopenharmony_ci#define DWC3_DEPCFG_FIFO_ERROR_EN	BIT(11)
2862306a36Sopenharmony_ci#define DWC3_DEPCFG_STREAM_EVENT_EN	BIT(13)
2962306a36Sopenharmony_ci#define DWC3_DEPCFG_BINTERVAL_M1(n)	(((n) & 0xff) << 16)
3062306a36Sopenharmony_ci#define DWC3_DEPCFG_STREAM_CAPABLE	BIT(24)
3162306a36Sopenharmony_ci#define DWC3_DEPCFG_EP_NUMBER(n)	(((n) & 0x1f) << 25)
3262306a36Sopenharmony_ci#define DWC3_DEPCFG_BULK_BASED		BIT(30)
3362306a36Sopenharmony_ci#define DWC3_DEPCFG_FIFO_BASED		BIT(31)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* DEPCFG parameter 0 */
3662306a36Sopenharmony_ci#define DWC3_DEPCFG_EP_TYPE(n)		(((n) & 0x3) << 1)
3762306a36Sopenharmony_ci#define DWC3_DEPCFG_MAX_PACKET_SIZE(n)	(((n) & 0x7ff) << 3)
3862306a36Sopenharmony_ci#define DWC3_DEPCFG_FIFO_NUMBER(n)	(((n) & 0x1f) << 17)
3962306a36Sopenharmony_ci#define DWC3_DEPCFG_BURST_SIZE(n)	(((n) & 0xf) << 22)
4062306a36Sopenharmony_ci#define DWC3_DEPCFG_DATA_SEQ_NUM(n)	((n) << 26)
4162306a36Sopenharmony_ci/* This applies for core versions earlier than 1.94a */
4262306a36Sopenharmony_ci#define DWC3_DEPCFG_IGN_SEQ_NUM		BIT(31)
4362306a36Sopenharmony_ci/* These apply for core versions 1.94a and later */
4462306a36Sopenharmony_ci#define DWC3_DEPCFG_ACTION_INIT		(0 << 30)
4562306a36Sopenharmony_ci#define DWC3_DEPCFG_ACTION_RESTORE	BIT(30)
4662306a36Sopenharmony_ci#define DWC3_DEPCFG_ACTION_MODIFY	(2 << 30)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/* DEPXFERCFG parameter 0 */
4962306a36Sopenharmony_ci#define DWC3_DEPXFERCFG_NUM_XFER_RES(n)	((n) & 0xffff)
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* U1 Device exit Latency */
5262306a36Sopenharmony_ci#define DWC3_DEFAULT_U1_DEV_EXIT_LAT	0x0A	/* Less then 10 microsec */
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/* U2 Device exit Latency */
5562306a36Sopenharmony_ci#define DWC3_DEFAULT_U2_DEV_EXIT_LAT	0x1FF	/* Less then 511 microsec */
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/* Frame/Microframe Number Mask */
5862306a36Sopenharmony_ci#define DWC3_FRNUMBER_MASK		0x3fff
5962306a36Sopenharmony_ci/* -------------------------------------------------------------------------- */
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define to_dwc3_request(r)	(container_of(r, struct dwc3_request, request))
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/**
6462306a36Sopenharmony_ci * next_request - gets the next request on the given list
6562306a36Sopenharmony_ci * @list: the request list to operate on
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci * Caller should take care of locking. This function return %NULL or the first
6862306a36Sopenharmony_ci * request available on @list.
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_cistatic inline struct dwc3_request *next_request(struct list_head *list)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	return list_first_entry_or_null(list, struct dwc3_request, list);
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/**
7662306a36Sopenharmony_ci * dwc3_gadget_move_started_request - move @req to the started_list
7762306a36Sopenharmony_ci * @req: the request to be moved
7862306a36Sopenharmony_ci *
7962306a36Sopenharmony_ci * Caller should take care of locking. This function will move @req from its
8062306a36Sopenharmony_ci * current list to the endpoint's started_list.
8162306a36Sopenharmony_ci */
8262306a36Sopenharmony_cistatic inline void dwc3_gadget_move_started_request(struct dwc3_request *req)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	struct dwc3_ep		*dep = req->dep;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	req->status = DWC3_REQUEST_STATUS_STARTED;
8762306a36Sopenharmony_ci	list_move_tail(&req->list, &dep->started_list);
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/**
9162306a36Sopenharmony_ci * dwc3_gadget_move_cancelled_request - move @req to the cancelled_list
9262306a36Sopenharmony_ci * @req: the request to be moved
9362306a36Sopenharmony_ci * @reason: cancelled reason for the dwc3 request
9462306a36Sopenharmony_ci *
9562306a36Sopenharmony_ci * Caller should take care of locking. This function will move @req from its
9662306a36Sopenharmony_ci * current list to the endpoint's cancelled_list.
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_cistatic inline void dwc3_gadget_move_cancelled_request(struct dwc3_request *req,
9962306a36Sopenharmony_ci		unsigned int reason)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	struct dwc3_ep		*dep = req->dep;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	req->status = reason;
10462306a36Sopenharmony_ci	list_move_tail(&req->list, &dep->cancelled_list);
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_civoid dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
10862306a36Sopenharmony_ci		int status);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_civoid dwc3_ep0_interrupt(struct dwc3 *dwc,
11162306a36Sopenharmony_ci		const struct dwc3_event_depevt *event);
11262306a36Sopenharmony_civoid dwc3_ep0_out_start(struct dwc3 *dwc);
11362306a36Sopenharmony_civoid dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep);
11462306a36Sopenharmony_civoid dwc3_ep0_stall_and_restart(struct dwc3 *dwc);
11562306a36Sopenharmony_ciint __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value);
11662306a36Sopenharmony_ciint dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value);
11762306a36Sopenharmony_ciint dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
11862306a36Sopenharmony_ci		gfp_t gfp_flags);
11962306a36Sopenharmony_ciint __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol);
12062306a36Sopenharmony_civoid dwc3_ep0_send_delayed_status(struct dwc3 *dwc);
12162306a36Sopenharmony_civoid dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci/**
12462306a36Sopenharmony_ci * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW
12562306a36Sopenharmony_ci * @dep: dwc3 endpoint
12662306a36Sopenharmony_ci *
12762306a36Sopenharmony_ci * Caller should take care of locking. Returns the transfer resource
12862306a36Sopenharmony_ci * index for a given endpoint.
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_cistatic inline void dwc3_gadget_ep_get_transfer_index(struct dwc3_ep *dep)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	u32			res_id;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	res_id = dwc3_readl(dep->regs, DWC3_DEPCMD);
13562306a36Sopenharmony_ci	dep->resource_index = DWC3_DEPCMD_GET_RSC_IDX(res_id);
13662306a36Sopenharmony_ci}
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci/**
13962306a36Sopenharmony_ci * dwc3_gadget_dctl_write_safe - write to DCTL safe from link state change
14062306a36Sopenharmony_ci * @dwc: pointer to our context structure
14162306a36Sopenharmony_ci * @value: value to write to DCTL
14262306a36Sopenharmony_ci *
14362306a36Sopenharmony_ci * Use this function when doing read-modify-write to DCTL. It will not
14462306a36Sopenharmony_ci * send link state change request.
14562306a36Sopenharmony_ci */
14662306a36Sopenharmony_cistatic inline void dwc3_gadget_dctl_write_safe(struct dwc3 *dwc, u32 value)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	value &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
14962306a36Sopenharmony_ci	dwc3_writel(dwc->regs, DWC3_DCTL, value);
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci#endif /* __DRIVERS_USB_DWC3_GADGET_H */
153