18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * gadget.h - DesignWare USB3 DRD Gadget Header
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Authors: Felipe Balbi <balbi@ti.com>,
88c2ecf20Sopenharmony_ci *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifndef __DRIVERS_USB_DWC3_GADGET_H
128c2ecf20Sopenharmony_ci#define __DRIVERS_USB_DWC3_GADGET_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/list.h>
158c2ecf20Sopenharmony_ci#include <linux/usb/gadget.h>
168c2ecf20Sopenharmony_ci#include "io.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct dwc3;
198c2ecf20Sopenharmony_ci#define to_dwc3_ep(ep)		(container_of(ep, struct dwc3_ep, endpoint))
208c2ecf20Sopenharmony_ci#define gadget_to_dwc(g)	(dev_get_platdata(&g->dev))
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* DEPCFG parameter 1 */
238c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_INT_NUM(n)		(((n) & 0x1f) << 0)
248c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_XFER_COMPLETE_EN	BIT(8)
258c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_XFER_IN_PROGRESS_EN	BIT(9)
268c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_XFER_NOT_READY_EN	BIT(10)
278c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_FIFO_ERROR_EN	BIT(11)
288c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_STREAM_EVENT_EN	BIT(13)
298c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_BINTERVAL_M1(n)	(((n) & 0xff) << 16)
308c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_STREAM_CAPABLE	BIT(24)
318c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_EP_NUMBER(n)	(((n) & 0x1f) << 25)
328c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_BULK_BASED		BIT(30)
338c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_FIFO_BASED		BIT(31)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* DEPCFG parameter 0 */
368c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_EP_TYPE(n)		(((n) & 0x3) << 1)
378c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_MAX_PACKET_SIZE(n)	(((n) & 0x7ff) << 3)
388c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_FIFO_NUMBER(n)	(((n) & 0x1f) << 17)
398c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_BURST_SIZE(n)	(((n) & 0xf) << 22)
408c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_DATA_SEQ_NUM(n)	((n) << 26)
418c2ecf20Sopenharmony_ci/* This applies for core versions earlier than 1.94a */
428c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_IGN_SEQ_NUM		BIT(31)
438c2ecf20Sopenharmony_ci/* These apply for core versions 1.94a and later */
448c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_ACTION_INIT		(0 << 30)
458c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_ACTION_RESTORE	BIT(30)
468c2ecf20Sopenharmony_ci#define DWC3_DEPCFG_ACTION_MODIFY	(2 << 30)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* DEPXFERCFG parameter 0 */
498c2ecf20Sopenharmony_ci#define DWC3_DEPXFERCFG_NUM_XFER_RES(n)	((n) & 0xffff)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* U1 Device exit Latency */
528c2ecf20Sopenharmony_ci#define DWC3_DEFAULT_U1_DEV_EXIT_LAT	0x0A	/* Less then 10 microsec */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* U2 Device exit Latency */
558c2ecf20Sopenharmony_ci#define DWC3_DEFAULT_U2_DEV_EXIT_LAT	0x1FF	/* Less then 511 microsec */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* Frame/Microframe Number Mask */
588c2ecf20Sopenharmony_ci#define DWC3_FRNUMBER_MASK		0x3fff
598c2ecf20Sopenharmony_ci/* -------------------------------------------------------------------------- */
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define to_dwc3_request(r)	(container_of(r, struct dwc3_request, request))
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/**
648c2ecf20Sopenharmony_ci * next_request - gets the next request on the given list
658c2ecf20Sopenharmony_ci * @list: the request list to operate on
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * Caller should take care of locking. This function return %NULL or the first
688c2ecf20Sopenharmony_ci * request available on @list.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_cistatic inline struct dwc3_request *next_request(struct list_head *list)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	return list_first_entry_or_null(list, struct dwc3_request, list);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/**
768c2ecf20Sopenharmony_ci * dwc3_gadget_move_started_request - move @req to the started_list
778c2ecf20Sopenharmony_ci * @req: the request to be moved
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * Caller should take care of locking. This function will move @req from its
808c2ecf20Sopenharmony_ci * current list to the endpoint's started_list.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_cistatic inline void dwc3_gadget_move_started_request(struct dwc3_request *req)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct dwc3_ep		*dep = req->dep;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	req->status = DWC3_REQUEST_STATUS_STARTED;
878c2ecf20Sopenharmony_ci	list_move_tail(&req->list, &dep->started_list);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/**
918c2ecf20Sopenharmony_ci * dwc3_gadget_move_cancelled_request - move @req to the cancelled_list
928c2ecf20Sopenharmony_ci * @req: the request to be moved
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * Caller should take care of locking. This function will move @req from its
958c2ecf20Sopenharmony_ci * current list to the endpoint's cancelled_list.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_cistatic inline void dwc3_gadget_move_cancelled_request(struct dwc3_request *req)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct dwc3_ep		*dep = req->dep;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	req->status = DWC3_REQUEST_STATUS_CANCELLED;
1028c2ecf20Sopenharmony_ci	list_move_tail(&req->list, &dep->cancelled_list);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_civoid dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
1068c2ecf20Sopenharmony_ci		int status);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_civoid dwc3_ep0_interrupt(struct dwc3 *dwc,
1098c2ecf20Sopenharmony_ci		const struct dwc3_event_depevt *event);
1108c2ecf20Sopenharmony_civoid dwc3_ep0_out_start(struct dwc3 *dwc);
1118c2ecf20Sopenharmony_ciint __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value);
1128c2ecf20Sopenharmony_ciint dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value);
1138c2ecf20Sopenharmony_ciint dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
1148c2ecf20Sopenharmony_ci		gfp_t gfp_flags);
1158c2ecf20Sopenharmony_ciint __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol);
1168c2ecf20Sopenharmony_civoid dwc3_ep0_send_delayed_status(struct dwc3 *dwc);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/**
1198c2ecf20Sopenharmony_ci * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW
1208c2ecf20Sopenharmony_ci * @dep: dwc3 endpoint
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * Caller should take care of locking. Returns the transfer resource
1238c2ecf20Sopenharmony_ci * index for a given endpoint.
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_cistatic inline void dwc3_gadget_ep_get_transfer_index(struct dwc3_ep *dep)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	u32			res_id;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	res_id = dwc3_readl(dep->regs, DWC3_DEPCMD);
1308c2ecf20Sopenharmony_ci	dep->resource_index = DWC3_DEPCMD_GET_RSC_IDX(res_id);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/**
1348c2ecf20Sopenharmony_ci * dwc3_gadget_dctl_write_safe - write to DCTL safe from link state change
1358c2ecf20Sopenharmony_ci * @dwc: pointer to our context structure
1368c2ecf20Sopenharmony_ci * @value: value to write to DCTL
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * Use this function when doing read-modify-write to DCTL. It will not
1398c2ecf20Sopenharmony_ci * send link state change request.
1408c2ecf20Sopenharmony_ci */
1418c2ecf20Sopenharmony_cistatic inline void dwc3_gadget_dctl_write_safe(struct dwc3 *dwc, u32 value)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	value &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1448c2ecf20Sopenharmony_ci	dwc3_writel(dwc->regs, DWC3_DCTL, value);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#endif /* __DRIVERS_USB_DWC3_GADGET_H */
148