18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * mtu3.h - MediaTek USB3 DRD header
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 MediaTek Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __MTU3_H__
118c2ecf20Sopenharmony_ci#define __MTU3_H__
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/device.h>
148c2ecf20Sopenharmony_ci#include <linux/dmapool.h>
158c2ecf20Sopenharmony_ci#include <linux/extcon.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/list.h>
188c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
198c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
208c2ecf20Sopenharmony_ci#include <linux/usb.h>
218c2ecf20Sopenharmony_ci#include <linux/usb/ch9.h>
228c2ecf20Sopenharmony_ci#include <linux/usb/gadget.h>
238c2ecf20Sopenharmony_ci#include <linux/usb/otg.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct mtu3;
268c2ecf20Sopenharmony_cistruct mtu3_ep;
278c2ecf20Sopenharmony_cistruct mtu3_request;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include "mtu3_hw_regs.h"
308c2ecf20Sopenharmony_ci#include "mtu3_qmu.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define	MU3D_EP_TXCR0(epnum)	(U3D_TX1CSR0 + (((epnum) - 1) * 0x10))
338c2ecf20Sopenharmony_ci#define	MU3D_EP_TXCR1(epnum)	(U3D_TX1CSR1 + (((epnum) - 1) * 0x10))
348c2ecf20Sopenharmony_ci#define	MU3D_EP_TXCR2(epnum)	(U3D_TX1CSR2 + (((epnum) - 1) * 0x10))
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define	MU3D_EP_RXCR0(epnum)	(U3D_RX1CSR0 + (((epnum) - 1) * 0x10))
378c2ecf20Sopenharmony_ci#define	MU3D_EP_RXCR1(epnum)	(U3D_RX1CSR1 + (((epnum) - 1) * 0x10))
388c2ecf20Sopenharmony_ci#define	MU3D_EP_RXCR2(epnum)	(U3D_RX1CSR2 + (((epnum) - 1) * 0x10))
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define USB_QMU_TQHIAR(epnum)	(U3D_TXQHIAR1 + (((epnum) - 1) * 0x4))
418c2ecf20Sopenharmony_ci#define USB_QMU_RQHIAR(epnum)	(U3D_RXQHIAR1 + (((epnum) - 1) * 0x4))
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define USB_QMU_RQCSR(epnum)	(U3D_RXQCSR1 + (((epnum) - 1) * 0x10))
448c2ecf20Sopenharmony_ci#define USB_QMU_RQSAR(epnum)	(U3D_RXQSAR1 + (((epnum) - 1) * 0x10))
458c2ecf20Sopenharmony_ci#define USB_QMU_RQCPR(epnum)	(U3D_RXQCPR1 + (((epnum) - 1) * 0x10))
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define USB_QMU_TQCSR(epnum)	(U3D_TXQCSR1 + (((epnum) - 1) * 0x10))
488c2ecf20Sopenharmony_ci#define USB_QMU_TQSAR(epnum)	(U3D_TXQSAR1 + (((epnum) - 1) * 0x10))
498c2ecf20Sopenharmony_ci#define USB_QMU_TQCPR(epnum)	(U3D_TXQCPR1 + (((epnum) - 1) * 0x10))
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define SSUSB_U3_CTRL(p)	(U3D_SSUSB_U3_CTRL_0P + ((p) * 0x08))
528c2ecf20Sopenharmony_ci#define SSUSB_U2_CTRL(p)	(U3D_SSUSB_U2_CTRL_0P + ((p) * 0x08))
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define MTU3_DRIVER_NAME	"mtu3"
558c2ecf20Sopenharmony_ci#define	DMA_ADDR_INVALID	(~(dma_addr_t)0)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define MTU3_EP_ENABLED		BIT(0)
588c2ecf20Sopenharmony_ci#define MTU3_EP_STALL		BIT(1)
598c2ecf20Sopenharmony_ci#define MTU3_EP_WEDGE		BIT(2)
608c2ecf20Sopenharmony_ci#define MTU3_EP_BUSY		BIT(3)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define MTU3_U3_IP_SLOT_DEFAULT 2
638c2ecf20Sopenharmony_ci#define MTU3_U2_IP_SLOT_DEFAULT 1
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/**
668c2ecf20Sopenharmony_ci * IP TRUNK version
678c2ecf20Sopenharmony_ci * from 0x1003 version, USB3 Gen2 is supported, two changes affect driver:
688c2ecf20Sopenharmony_ci * 1. MAXPKT and MULTI bits layout of TXCSR1 and RXCSR1 are adjusted,
698c2ecf20Sopenharmony_ci *    but not backward compatible
708c2ecf20Sopenharmony_ci * 2. QMU extend buffer length supported
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_ci#define MTU3_TRUNK_VERS_1003	0x1003
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * Normally the device works on HS or SS, to simplify fifo management,
768c2ecf20Sopenharmony_ci * devide fifo into some 512B parts, use bitmap to manage it; And
778c2ecf20Sopenharmony_ci * 128 bits size of bitmap is large enough, that means it can manage
788c2ecf20Sopenharmony_ci * up to 64KB fifo size.
798c2ecf20Sopenharmony_ci * NOTE: MTU3_EP_FIFO_UNIT should be power of two
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_ci#define MTU3_EP_FIFO_UNIT		(1 << 9)
828c2ecf20Sopenharmony_ci#define MTU3_FIFO_BIT_SIZE		128
838c2ecf20Sopenharmony_ci#define MTU3_U2_IP_EP0_FIFO_SIZE	64
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/**
868c2ecf20Sopenharmony_ci * Maximum size of ep0 response buffer for ch9 requests,
878c2ecf20Sopenharmony_ci * the SET_SEL request uses 6 so far, and GET_STATUS is 2
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ci#define EP0_RESPONSE_BUF  6
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* device operated link and speed got from DEVICE_CONF register */
928c2ecf20Sopenharmony_cienum mtu3_speed {
938c2ecf20Sopenharmony_ci	MTU3_SPEED_INACTIVE = 0,
948c2ecf20Sopenharmony_ci	MTU3_SPEED_FULL = 1,
958c2ecf20Sopenharmony_ci	MTU3_SPEED_HIGH = 3,
968c2ecf20Sopenharmony_ci	MTU3_SPEED_SUPER = 4,
978c2ecf20Sopenharmony_ci	MTU3_SPEED_SUPER_PLUS = 5,
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/**
1018c2ecf20Sopenharmony_ci * @MU3D_EP0_STATE_SETUP: waits for SETUP or received a SETUP
1028c2ecf20Sopenharmony_ci *		without data stage.
1038c2ecf20Sopenharmony_ci * @MU3D_EP0_STATE_TX: IN data stage
1048c2ecf20Sopenharmony_ci * @MU3D_EP0_STATE_RX: OUT data stage
1058c2ecf20Sopenharmony_ci * @MU3D_EP0_STATE_TX_END: the last IN data is transferred, and
1068c2ecf20Sopenharmony_ci *		waits for its completion interrupt
1078c2ecf20Sopenharmony_ci * @MU3D_EP0_STATE_STALL: ep0 is in stall status, will be auto-cleared
1088c2ecf20Sopenharmony_ci *		after receives a SETUP.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_cienum mtu3_g_ep0_state {
1118c2ecf20Sopenharmony_ci	MU3D_EP0_STATE_SETUP = 1,
1128c2ecf20Sopenharmony_ci	MU3D_EP0_STATE_TX,
1138c2ecf20Sopenharmony_ci	MU3D_EP0_STATE_RX,
1148c2ecf20Sopenharmony_ci	MU3D_EP0_STATE_TX_END,
1158c2ecf20Sopenharmony_ci	MU3D_EP0_STATE_STALL,
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/**
1198c2ecf20Sopenharmony_ci * MTU3_DR_FORCE_NONE: automatically switch host and periperal mode
1208c2ecf20Sopenharmony_ci *		by IDPIN signal.
1218c2ecf20Sopenharmony_ci * MTU3_DR_FORCE_HOST: force to enter host mode and override OTG
1228c2ecf20Sopenharmony_ci *		IDPIN signal.
1238c2ecf20Sopenharmony_ci * MTU3_DR_FORCE_DEVICE: force to enter peripheral mode.
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_cienum mtu3_dr_force_mode {
1268c2ecf20Sopenharmony_ci	MTU3_DR_FORCE_NONE = 0,
1278c2ecf20Sopenharmony_ci	MTU3_DR_FORCE_HOST,
1288c2ecf20Sopenharmony_ci	MTU3_DR_FORCE_DEVICE,
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/**
1328c2ecf20Sopenharmony_ci * @base: the base address of fifo
1338c2ecf20Sopenharmony_ci * @limit: the bitmap size in bits
1348c2ecf20Sopenharmony_ci * @bitmap: fifo bitmap in unit of @MTU3_EP_FIFO_UNIT
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_cistruct mtu3_fifo_info {
1378c2ecf20Sopenharmony_ci	u32 base;
1388c2ecf20Sopenharmony_ci	u32 limit;
1398c2ecf20Sopenharmony_ci	DECLARE_BITMAP(bitmap, MTU3_FIFO_BIT_SIZE);
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * General Purpose Descriptor (GPD):
1448c2ecf20Sopenharmony_ci *	The format of TX GPD is a little different from RX one.
1458c2ecf20Sopenharmony_ci *	And the size of GPD is 16 bytes.
1468c2ecf20Sopenharmony_ci *
1478c2ecf20Sopenharmony_ci * @dw0_info:
1488c2ecf20Sopenharmony_ci *	bit0: Hardware Own (HWO)
1498c2ecf20Sopenharmony_ci *	bit1: Buffer Descriptor Present (BDP), always 0, BD is not supported
1508c2ecf20Sopenharmony_ci *	bit2: Bypass (BPS), 1: HW skips this GPD if HWO = 1
1518c2ecf20Sopenharmony_ci *	bit6: [EL] Zero Length Packet (ZLP), moved from @dw3_info[29]
1528c2ecf20Sopenharmony_ci *	bit7: Interrupt On Completion (IOC)
1538c2ecf20Sopenharmony_ci *	bit[31:16]: ([EL] bit[31:12]) allow data buffer length (RX ONLY),
1548c2ecf20Sopenharmony_ci *		the buffer length of the data to receive
1558c2ecf20Sopenharmony_ci *	bit[23:16]: ([EL] bit[31:24]) extension address (TX ONLY),
1568c2ecf20Sopenharmony_ci *		lower 4 bits are extension bits of @buffer,
1578c2ecf20Sopenharmony_ci *		upper 4 bits are extension bits of @next_gpd
1588c2ecf20Sopenharmony_ci * @next_gpd: Physical address of the next GPD
1598c2ecf20Sopenharmony_ci * @buffer: Physical address of the data buffer
1608c2ecf20Sopenharmony_ci * @dw3_info:
1618c2ecf20Sopenharmony_ci *	bit[15:0]: ([EL] bit[19:0]) data buffer length,
1628c2ecf20Sopenharmony_ci *		(TX): the buffer length of the data to transmit
1638c2ecf20Sopenharmony_ci *		(RX): The total length of data received
1648c2ecf20Sopenharmony_ci *	bit[23:16]: ([EL] bit[31:24]) extension address (RX ONLY),
1658c2ecf20Sopenharmony_ci *		lower 4 bits are extension bits of @buffer,
1668c2ecf20Sopenharmony_ci *		upper 4 bits are extension bits of @next_gpd
1678c2ecf20Sopenharmony_ci *	bit29: ([EL] abandoned) Zero Length Packet (ZLP) (TX ONLY)
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_cistruct qmu_gpd {
1708c2ecf20Sopenharmony_ci	__le32 dw0_info;
1718c2ecf20Sopenharmony_ci	__le32 next_gpd;
1728c2ecf20Sopenharmony_ci	__le32 buffer;
1738c2ecf20Sopenharmony_ci	__le32 dw3_info;
1748c2ecf20Sopenharmony_ci} __packed;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/**
1778c2ecf20Sopenharmony_ci* dma: physical base address of GPD segment
1788c2ecf20Sopenharmony_ci* start: virtual base address of GPD segment
1798c2ecf20Sopenharmony_ci* end: the last GPD element
1808c2ecf20Sopenharmony_ci* enqueue: the first empty GPD to use
1818c2ecf20Sopenharmony_ci* dequeue: the first completed GPD serviced by ISR
1828c2ecf20Sopenharmony_ci* NOTE: the size of GPD ring should be >= 2
1838c2ecf20Sopenharmony_ci*/
1848c2ecf20Sopenharmony_cistruct mtu3_gpd_ring {
1858c2ecf20Sopenharmony_ci	dma_addr_t dma;
1868c2ecf20Sopenharmony_ci	struct qmu_gpd *start;
1878c2ecf20Sopenharmony_ci	struct qmu_gpd *end;
1888c2ecf20Sopenharmony_ci	struct qmu_gpd *enqueue;
1898c2ecf20Sopenharmony_ci	struct qmu_gpd *dequeue;
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/**
1938c2ecf20Sopenharmony_ci* @vbus: vbus 5V used by host mode
1948c2ecf20Sopenharmony_ci* @edev: external connector used to detect vbus and iddig changes
1958c2ecf20Sopenharmony_ci* @vbus_nb: notifier for vbus detection
1968c2ecf20Sopenharmony_ci* @vbus_work : work of vbus detection notifier, used to avoid sleep in
1978c2ecf20Sopenharmony_ci*		notifier callback which is atomic context
1988c2ecf20Sopenharmony_ci* @vbus_event : event of vbus detecion notifier
1998c2ecf20Sopenharmony_ci* @id_nb : notifier for iddig(idpin) detection
2008c2ecf20Sopenharmony_ci* @id_work : work of iddig detection notifier
2018c2ecf20Sopenharmony_ci* @id_event : event of iddig detecion notifier
2028c2ecf20Sopenharmony_ci* @role_sw : use USB Role Switch to support dual-role switch, can't use
2038c2ecf20Sopenharmony_ci*		extcon at the same time, and extcon is deprecated.
2048c2ecf20Sopenharmony_ci* @role_sw_used : true when the USB Role Switch is used.
2058c2ecf20Sopenharmony_ci* @is_u3_drd: whether port0 supports usb3.0 dual-role device or not
2068c2ecf20Sopenharmony_ci* @manual_drd_enabled: it's true when supports dual-role device by debugfs
2078c2ecf20Sopenharmony_ci*		to switch host/device modes depending on user input.
2088c2ecf20Sopenharmony_ci*/
2098c2ecf20Sopenharmony_cistruct otg_switch_mtk {
2108c2ecf20Sopenharmony_ci	struct regulator *vbus;
2118c2ecf20Sopenharmony_ci	struct extcon_dev *edev;
2128c2ecf20Sopenharmony_ci	struct notifier_block vbus_nb;
2138c2ecf20Sopenharmony_ci	struct work_struct vbus_work;
2148c2ecf20Sopenharmony_ci	unsigned long vbus_event;
2158c2ecf20Sopenharmony_ci	struct notifier_block id_nb;
2168c2ecf20Sopenharmony_ci	struct work_struct id_work;
2178c2ecf20Sopenharmony_ci	unsigned long id_event;
2188c2ecf20Sopenharmony_ci	struct usb_role_switch *role_sw;
2198c2ecf20Sopenharmony_ci	bool role_sw_used;
2208c2ecf20Sopenharmony_ci	bool is_u3_drd;
2218c2ecf20Sopenharmony_ci	bool manual_drd_enabled;
2228c2ecf20Sopenharmony_ci};
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/**
2258c2ecf20Sopenharmony_ci * @mac_base: register base address of device MAC, exclude xHCI's
2268c2ecf20Sopenharmony_ci * @ippc_base: register base address of IP Power and Clock interface (IPPC)
2278c2ecf20Sopenharmony_ci * @vusb33: usb3.3V shared by device/host IP
2288c2ecf20Sopenharmony_ci * @sys_clk: system clock of mtu3, shared by device/host IP
2298c2ecf20Sopenharmony_ci * @ref_clk: reference clock
2308c2ecf20Sopenharmony_ci * @mcu_clk: mcu_bus_ck clock for AHB bus etc
2318c2ecf20Sopenharmony_ci * @dma_clk: dma_bus_ck clock for AXI bus etc
2328c2ecf20Sopenharmony_ci * @dr_mode: works in which mode:
2338c2ecf20Sopenharmony_ci *		host only, device only or dual-role mode
2348c2ecf20Sopenharmony_ci * @u2_ports: number of usb2.0 host ports
2358c2ecf20Sopenharmony_ci * @u3_ports: number of usb3.0 host ports
2368c2ecf20Sopenharmony_ci * @u3p_dis_msk: mask of disabling usb3 ports, for example, bit0==1 to
2378c2ecf20Sopenharmony_ci *		disable u3port0, bit1==1 to disable u3port1,... etc
2388c2ecf20Sopenharmony_ci * @dbgfs_root: only used when supports manual dual-role switch via debugfs
2398c2ecf20Sopenharmony_ci * @uwk_en: it's true when supports remote wakeup in host mode
2408c2ecf20Sopenharmony_ci * @uwk: syscon including usb wakeup glue layer between SSUSB IP and SPM
2418c2ecf20Sopenharmony_ci * @uwk_reg_base: the base address of the wakeup glue layer in @uwk
2428c2ecf20Sopenharmony_ci * @uwk_vers: the version of the wakeup glue layer
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_cistruct ssusb_mtk {
2458c2ecf20Sopenharmony_ci	struct device *dev;
2468c2ecf20Sopenharmony_ci	struct mtu3 *u3d;
2478c2ecf20Sopenharmony_ci	void __iomem *mac_base;
2488c2ecf20Sopenharmony_ci	void __iomem *ippc_base;
2498c2ecf20Sopenharmony_ci	struct phy **phys;
2508c2ecf20Sopenharmony_ci	int num_phys;
2518c2ecf20Sopenharmony_ci	/* common power & clock */
2528c2ecf20Sopenharmony_ci	struct regulator *vusb33;
2538c2ecf20Sopenharmony_ci	struct clk *sys_clk;
2548c2ecf20Sopenharmony_ci	struct clk *ref_clk;
2558c2ecf20Sopenharmony_ci	struct clk *mcu_clk;
2568c2ecf20Sopenharmony_ci	struct clk *dma_clk;
2578c2ecf20Sopenharmony_ci	/* otg */
2588c2ecf20Sopenharmony_ci	struct otg_switch_mtk otg_switch;
2598c2ecf20Sopenharmony_ci	enum usb_dr_mode dr_mode;
2608c2ecf20Sopenharmony_ci	bool is_host;
2618c2ecf20Sopenharmony_ci	int u2_ports;
2628c2ecf20Sopenharmony_ci	int u3_ports;
2638c2ecf20Sopenharmony_ci	int u3p_dis_msk;
2648c2ecf20Sopenharmony_ci	struct dentry *dbgfs_root;
2658c2ecf20Sopenharmony_ci	/* usb wakeup for host mode */
2668c2ecf20Sopenharmony_ci	bool uwk_en;
2678c2ecf20Sopenharmony_ci	struct regmap *uwk;
2688c2ecf20Sopenharmony_ci	u32 uwk_reg_base;
2698c2ecf20Sopenharmony_ci	u32 uwk_vers;
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci/**
2738c2ecf20Sopenharmony_ci * @fifo_size: it is (@slot + 1) * @fifo_seg_size
2748c2ecf20Sopenharmony_ci * @fifo_seg_size: it is roundup_pow_of_two(@maxp)
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_cistruct mtu3_ep {
2778c2ecf20Sopenharmony_ci	struct usb_ep ep;
2788c2ecf20Sopenharmony_ci	char name[12];
2798c2ecf20Sopenharmony_ci	struct mtu3 *mtu;
2808c2ecf20Sopenharmony_ci	u8 epnum;
2818c2ecf20Sopenharmony_ci	u8 type;
2828c2ecf20Sopenharmony_ci	u8 is_in;
2838c2ecf20Sopenharmony_ci	u16 maxp;
2848c2ecf20Sopenharmony_ci	int slot;
2858c2ecf20Sopenharmony_ci	u32 fifo_size;
2868c2ecf20Sopenharmony_ci	u32 fifo_addr;
2878c2ecf20Sopenharmony_ci	u32 fifo_seg_size;
2888c2ecf20Sopenharmony_ci	struct mtu3_fifo_info *fifo;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	struct list_head req_list;
2918c2ecf20Sopenharmony_ci	struct mtu3_gpd_ring gpd_ring;
2928c2ecf20Sopenharmony_ci	const struct usb_ss_ep_comp_descriptor *comp_desc;
2938c2ecf20Sopenharmony_ci	const struct usb_endpoint_descriptor *desc;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	int flags;
2968c2ecf20Sopenharmony_ci};
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistruct mtu3_request {
2998c2ecf20Sopenharmony_ci	struct usb_request request;
3008c2ecf20Sopenharmony_ci	struct list_head list;
3018c2ecf20Sopenharmony_ci	struct mtu3_ep *mep;
3028c2ecf20Sopenharmony_ci	struct mtu3 *mtu;
3038c2ecf20Sopenharmony_ci	struct qmu_gpd *gpd;
3048c2ecf20Sopenharmony_ci	int epnum;
3058c2ecf20Sopenharmony_ci};
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic inline struct ssusb_mtk *dev_to_ssusb(struct device *dev)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	return dev_get_drvdata(dev);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/**
3138c2ecf20Sopenharmony_ci * struct mtu3 - device driver instance data.
3148c2ecf20Sopenharmony_ci * @slot: MTU3_U2_IP_SLOT_DEFAULT for U2 IP only,
3158c2ecf20Sopenharmony_ci *		MTU3_U3_IP_SLOT_DEFAULT for U3 IP
3168c2ecf20Sopenharmony_ci * @may_wakeup: means device's remote wakeup is enabled
3178c2ecf20Sopenharmony_ci * @is_self_powered: is reported in device status and the config descriptor
3188c2ecf20Sopenharmony_ci * @delayed_status: true when function drivers ask for delayed status
3198c2ecf20Sopenharmony_ci * @gen2cp: compatible with USB3 Gen2 IP
3208c2ecf20Sopenharmony_ci * @ep0_req: dummy request used while handling standard USB requests
3218c2ecf20Sopenharmony_ci *		for GET_STATUS and SET_SEL
3228c2ecf20Sopenharmony_ci * @setup_buf: ep0 response buffer for GET_STATUS and SET_SEL requests
3238c2ecf20Sopenharmony_ci */
3248c2ecf20Sopenharmony_cistruct mtu3 {
3258c2ecf20Sopenharmony_ci	spinlock_t lock;
3268c2ecf20Sopenharmony_ci	struct ssusb_mtk *ssusb;
3278c2ecf20Sopenharmony_ci	struct device *dev;
3288c2ecf20Sopenharmony_ci	void __iomem *mac_base;
3298c2ecf20Sopenharmony_ci	void __iomem *ippc_base;
3308c2ecf20Sopenharmony_ci	int irq;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	struct mtu3_fifo_info tx_fifo;
3338c2ecf20Sopenharmony_ci	struct mtu3_fifo_info rx_fifo;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	struct mtu3_ep *ep_array;
3368c2ecf20Sopenharmony_ci	struct mtu3_ep *in_eps;
3378c2ecf20Sopenharmony_ci	struct mtu3_ep *out_eps;
3388c2ecf20Sopenharmony_ci	struct mtu3_ep *ep0;
3398c2ecf20Sopenharmony_ci	int num_eps;
3408c2ecf20Sopenharmony_ci	int slot;
3418c2ecf20Sopenharmony_ci	int active_ep;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	struct dma_pool	*qmu_gpd_pool;
3448c2ecf20Sopenharmony_ci	enum mtu3_g_ep0_state ep0_state;
3458c2ecf20Sopenharmony_ci	struct usb_gadget g;	/* the gadget */
3468c2ecf20Sopenharmony_ci	struct usb_gadget_driver *gadget_driver;
3478c2ecf20Sopenharmony_ci	struct mtu3_request ep0_req;
3488c2ecf20Sopenharmony_ci	u8 setup_buf[EP0_RESPONSE_BUF];
3498c2ecf20Sopenharmony_ci	enum usb_device_speed max_speed;
3508c2ecf20Sopenharmony_ci	enum usb_device_speed speed;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	unsigned is_active:1;
3538c2ecf20Sopenharmony_ci	unsigned may_wakeup:1;
3548c2ecf20Sopenharmony_ci	unsigned is_self_powered:1;
3558c2ecf20Sopenharmony_ci	unsigned test_mode:1;
3568c2ecf20Sopenharmony_ci	unsigned softconnect:1;
3578c2ecf20Sopenharmony_ci	unsigned u1_enable:1;
3588c2ecf20Sopenharmony_ci	unsigned u2_enable:1;
3598c2ecf20Sopenharmony_ci	unsigned is_u3_ip:1;
3608c2ecf20Sopenharmony_ci	unsigned delayed_status:1;
3618c2ecf20Sopenharmony_ci	unsigned gen2cp:1;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	u8 address;
3648c2ecf20Sopenharmony_ci	u8 test_mode_nr;
3658c2ecf20Sopenharmony_ci	u32 hw_version;
3668c2ecf20Sopenharmony_ci};
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic inline struct mtu3 *gadget_to_mtu3(struct usb_gadget *g)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	return container_of(g, struct mtu3, g);
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic inline struct mtu3_request *to_mtu3_request(struct usb_request *req)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	return req ? container_of(req, struct mtu3_request, request) : NULL;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic inline struct mtu3_ep *to_mtu3_ep(struct usb_ep *ep)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	return ep ? container_of(ep, struct mtu3_ep, ep) : NULL;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic inline struct mtu3_request *next_request(struct mtu3_ep *mep)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	return list_first_entry_or_null(&mep->req_list, struct mtu3_request,
3868c2ecf20Sopenharmony_ci					list);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic inline void mtu3_writel(void __iomem *base, u32 offset, u32 data)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	writel(data, base + offset);
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic inline u32 mtu3_readl(void __iomem *base, u32 offset)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	return readl(base + offset);
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic inline void mtu3_setbits(void __iomem *base, u32 offset, u32 bits)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	void __iomem *addr = base + offset;
4028c2ecf20Sopenharmony_ci	u32 tmp = readl(addr);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	writel((tmp | (bits)), addr);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic inline void mtu3_clrbits(void __iomem *base, u32 offset, u32 bits)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	void __iomem *addr = base + offset;
4108c2ecf20Sopenharmony_ci	u32 tmp = readl(addr);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	writel((tmp & ~(bits)), addr);
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ciint ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks);
4168c2ecf20Sopenharmony_cistruct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
4178c2ecf20Sopenharmony_civoid mtu3_free_request(struct usb_ep *ep, struct usb_request *req);
4188c2ecf20Sopenharmony_civoid mtu3_req_complete(struct mtu3_ep *mep,
4198c2ecf20Sopenharmony_ci		struct usb_request *req, int status);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ciint mtu3_config_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
4228c2ecf20Sopenharmony_ci		int interval, int burst, int mult);
4238c2ecf20Sopenharmony_civoid mtu3_deconfig_ep(struct mtu3 *mtu, struct mtu3_ep *mep);
4248c2ecf20Sopenharmony_civoid mtu3_ep_stall_set(struct mtu3_ep *mep, bool set);
4258c2ecf20Sopenharmony_civoid mtu3_ep0_setup(struct mtu3 *mtu);
4268c2ecf20Sopenharmony_civoid mtu3_start(struct mtu3 *mtu);
4278c2ecf20Sopenharmony_civoid mtu3_stop(struct mtu3 *mtu);
4288c2ecf20Sopenharmony_civoid mtu3_dev_on_off(struct mtu3 *mtu, int is_on);
4298c2ecf20Sopenharmony_civoid mtu3_set_speed(struct mtu3 *mtu, enum usb_device_speed speed);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ciint mtu3_gadget_setup(struct mtu3 *mtu);
4328c2ecf20Sopenharmony_civoid mtu3_gadget_cleanup(struct mtu3 *mtu);
4338c2ecf20Sopenharmony_civoid mtu3_gadget_reset(struct mtu3 *mtu);
4348c2ecf20Sopenharmony_civoid mtu3_gadget_suspend(struct mtu3 *mtu);
4358c2ecf20Sopenharmony_civoid mtu3_gadget_resume(struct mtu3 *mtu);
4368c2ecf20Sopenharmony_civoid mtu3_gadget_disconnect(struct mtu3 *mtu);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciirqreturn_t mtu3_ep0_isr(struct mtu3 *mtu);
4398c2ecf20Sopenharmony_ciextern const struct usb_ep_ops mtu3_ep0_ops;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci#endif
442