mtu3.h 12.4 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
C
Chunfeng Yun 已提交
2 3 4 5 6 7 8 9 10 11 12
/*
 * mtu3.h - MediaTek USB3 DRD header
 *
 * Copyright (C) 2016 MediaTek Inc.
 *
 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
 */

#ifndef __MTU3_H__
#define __MTU3_H__

13
#include <linux/clk.h>
C
Chunfeng Yun 已提交
14 15
#include <linux/device.h>
#include <linux/dmapool.h>
C
Chunfeng Yun 已提交
16
#include <linux/extcon.h>
C
Chunfeng Yun 已提交
17 18 19 20 21 22 23 24
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/phy/phy.h>
#include <linux/regulator/consumer.h>
#include <linux/usb.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#include <linux/usb/otg.h>
25
#include <linux/usb/role.h>
C
Chunfeng Yun 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

struct mtu3;
struct mtu3_ep;
struct mtu3_request;

#include "mtu3_hw_regs.h"
#include "mtu3_qmu.h"

#define	MU3D_EP_TXCR0(epnum)	(U3D_TX1CSR0 + (((epnum) - 1) * 0x10))
#define	MU3D_EP_TXCR1(epnum)	(U3D_TX1CSR1 + (((epnum) - 1) * 0x10))
#define	MU3D_EP_TXCR2(epnum)	(U3D_TX1CSR2 + (((epnum) - 1) * 0x10))

#define	MU3D_EP_RXCR0(epnum)	(U3D_RX1CSR0 + (((epnum) - 1) * 0x10))
#define	MU3D_EP_RXCR1(epnum)	(U3D_RX1CSR1 + (((epnum) - 1) * 0x10))
#define	MU3D_EP_RXCR2(epnum)	(U3D_RX1CSR2 + (((epnum) - 1) * 0x10))

42 43 44
#define USB_QMU_TQHIAR(epnum)	(U3D_TXQHIAR1 + (((epnum) - 1) * 0x4))
#define USB_QMU_RQHIAR(epnum)	(U3D_RXQHIAR1 + (((epnum) - 1) * 0x4))

C
Chunfeng Yun 已提交
45 46 47 48 49 50 51 52
#define USB_QMU_RQCSR(epnum)	(U3D_RXQCSR1 + (((epnum) - 1) * 0x10))
#define USB_QMU_RQSAR(epnum)	(U3D_RXQSAR1 + (((epnum) - 1) * 0x10))
#define USB_QMU_RQCPR(epnum)	(U3D_RXQCPR1 + (((epnum) - 1) * 0x10))

#define USB_QMU_TQCSR(epnum)	(U3D_TXQCSR1 + (((epnum) - 1) * 0x10))
#define USB_QMU_TQSAR(epnum)	(U3D_TXQSAR1 + (((epnum) - 1) * 0x10))
#define USB_QMU_TQCPR(epnum)	(U3D_TXQCPR1 + (((epnum) - 1) * 0x10))

53
#define SSUSB_U3_CTRL(p)	(U3D_SSUSB_U3_CTRL_0P + ((p) * 0x08))
C
Chunfeng Yun 已提交
54 55 56 57 58 59 60 61 62 63
#define SSUSB_U2_CTRL(p)	(U3D_SSUSB_U2_CTRL_0P + ((p) * 0x08))

#define MTU3_DRIVER_NAME	"mtu3"
#define	DMA_ADDR_INVALID	(~(dma_addr_t)0)

#define MTU3_EP_ENABLED		BIT(0)
#define MTU3_EP_STALL		BIT(1)
#define MTU3_EP_WEDGE		BIT(2)
#define MTU3_EP_BUSY		BIT(3)

64
#define MTU3_U3_IP_SLOT_DEFAULT 2
C
Chunfeng Yun 已提交
65 66
#define MTU3_U2_IP_SLOT_DEFAULT 1

67 68 69 70 71 72 73 74 75
/**
 * IP TRUNK version
 * from 0x1003 version, USB3 Gen2 is supported, two changes affect driver:
 * 1. MAXPKT and MULTI bits layout of TXCSR1 and RXCSR1 are adjusted,
 *    but not backward compatible
 * 2. QMU extend buffer length supported
 */
#define MTU3_TRUNK_VERS_1003	0x1003

C
Chunfeng Yun 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/**
 * Normally the device works on HS or SS, to simplify fifo management,
 * devide fifo into some 512B parts, use bitmap to manage it; And
 * 128 bits size of bitmap is large enough, that means it can manage
 * up to 64KB fifo size.
 * NOTE: MTU3_EP_FIFO_UNIT should be power of two
 */
#define MTU3_EP_FIFO_UNIT		(1 << 9)
#define MTU3_FIFO_BIT_SIZE		128
#define MTU3_U2_IP_EP0_FIFO_SIZE	64

/**
 * Maximum size of ep0 response buffer for ch9 requests,
 * the SET_SEL request uses 6 so far, and GET_STATUS is 2
 */
#define EP0_RESPONSE_BUF  6

93 94
#define BULK_CLKS_CNT	4

C
Chunfeng Yun 已提交
95 96 97 98 99
/* device operated link and speed got from DEVICE_CONF register */
enum mtu3_speed {
	MTU3_SPEED_INACTIVE = 0,
	MTU3_SPEED_FULL = 1,
	MTU3_SPEED_HIGH = 3,
100
	MTU3_SPEED_SUPER = 4,
101
	MTU3_SPEED_SUPER_PLUS = 5,
C
Chunfeng Yun 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
};

/**
 * @MU3D_EP0_STATE_SETUP: waits for SETUP or received a SETUP
 *		without data stage.
 * @MU3D_EP0_STATE_TX: IN data stage
 * @MU3D_EP0_STATE_RX: OUT data stage
 * @MU3D_EP0_STATE_TX_END: the last IN data is transferred, and
 *		waits for its completion interrupt
 * @MU3D_EP0_STATE_STALL: ep0 is in stall status, will be auto-cleared
 *		after receives a SETUP.
 */
enum mtu3_g_ep0_state {
	MU3D_EP0_STATE_SETUP = 1,
	MU3D_EP0_STATE_TX,
	MU3D_EP0_STATE_RX,
	MU3D_EP0_STATE_TX_END,
	MU3D_EP0_STATE_STALL,
};

122 123 124 125 126 127 128 129 130 131 132 133 134
/**
 * MTU3_DR_FORCE_NONE: automatically switch host and periperal mode
 *		by IDPIN signal.
 * MTU3_DR_FORCE_HOST: force to enter host mode and override OTG
 *		IDPIN signal.
 * MTU3_DR_FORCE_DEVICE: force to enter peripheral mode.
 */
enum mtu3_dr_force_mode {
	MTU3_DR_FORCE_NONE = 0,
	MTU3_DR_FORCE_HOST,
	MTU3_DR_FORCE_DEVICE,
};

C
Chunfeng Yun 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/**
 * @base: the base address of fifo
 * @limit: the bitmap size in bits
 * @bitmap: fifo bitmap in unit of @MTU3_EP_FIFO_UNIT
 */
struct mtu3_fifo_info {
	u32 base;
	u32 limit;
	DECLARE_BITMAP(bitmap, MTU3_FIFO_BIT_SIZE);
};

/**
 * General Purpose Descriptor (GPD):
 *	The format of TX GPD is a little different from RX one.
 *	And the size of GPD is 16 bytes.
 *
151
 * @dw0_info:
C
Chunfeng Yun 已提交
152 153 154
 *	bit0: Hardware Own (HWO)
 *	bit1: Buffer Descriptor Present (BDP), always 0, BD is not supported
 *	bit2: Bypass (BPS), 1: HW skips this GPD if HWO = 1
155
 *	bit6: [EL] Zero Length Packet (ZLP), moved from @dw3_info[29]
C
Chunfeng Yun 已提交
156
 *	bit7: Interrupt On Completion (IOC)
157
 *	bit[31:16]: ([EL] bit[31:12]) allow data buffer length (RX ONLY),
158
 *		the buffer length of the data to receive
159
 *	bit[23:16]: ([EL] bit[31:24]) extension address (TX ONLY),
160 161
 *		lower 4 bits are extension bits of @buffer,
 *		upper 4 bits are extension bits of @next_gpd
C
Chunfeng Yun 已提交
162 163
 * @next_gpd: Physical address of the next GPD
 * @buffer: Physical address of the data buffer
164
 * @dw3_info:
165
 *	bit[15:0]: ([EL] bit[19:0]) data buffer length,
166 167
 *		(TX): the buffer length of the data to transmit
 *		(RX): The total length of data received
168
 *	bit[23:16]: ([EL] bit[31:24]) extension address (RX ONLY),
169 170
 *		lower 4 bits are extension bits of @buffer,
 *		upper 4 bits are extension bits of @next_gpd
171
 *	bit29: ([EL] abandoned) Zero Length Packet (ZLP) (TX ONLY)
C
Chunfeng Yun 已提交
172 173
 */
struct qmu_gpd {
174
	__le32 dw0_info;
C
Chunfeng Yun 已提交
175 176
	__le32 next_gpd;
	__le32 buffer;
177
	__le32 dw3_info;
C
Chunfeng Yun 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
} __packed;

/**
* dma: physical base address of GPD segment
* start: virtual base address of GPD segment
* end: the last GPD element
* enqueue: the first empty GPD to use
* dequeue: the first completed GPD serviced by ISR
* NOTE: the size of GPD ring should be >= 2
*/
struct mtu3_gpd_ring {
	dma_addr_t dma;
	struct qmu_gpd *start;
	struct qmu_gpd *end;
	struct qmu_gpd *enqueue;
	struct qmu_gpd *dequeue;
};
C
Chunfeng Yun 已提交
195 196 197 198

/**
* @vbus: vbus 5V used by host mode
* @edev: external connector used to detect vbus and iddig changes
199
* @id_nb : notifier for iddig(idpin) detection
200 201
* @dr_work : work for drd mode switch, used to avoid sleep in atomic context
* @desired_role : role desired to switch
202
* @default_role : default mode while usb role is USB_ROLE_NONE
203 204 205
* @role_sw : use USB Role Switch to support dual-role switch, can't use
*		extcon at the same time, and extcon is deprecated.
* @role_sw_used : true when the USB Role Switch is used.
C
Chunfeng Yun 已提交
206 207 208 209 210 211 212 213
* @is_u3_drd: whether port0 supports usb3.0 dual-role device or not
* @manual_drd_enabled: it's true when supports dual-role device by debugfs
*		to switch host/device modes depending on user input.
*/
struct otg_switch_mtk {
	struct regulator *vbus;
	struct extcon_dev *edev;
	struct notifier_block id_nb;
214 215
	struct work_struct dr_work;
	enum usb_role desired_role;
216
	enum usb_role default_role;
217 218
	struct usb_role_switch *role_sw;
	bool role_sw_used;
C
Chunfeng Yun 已提交
219 220 221 222
	bool is_u3_drd;
	bool manual_drd_enabled;
};

C
Chunfeng Yun 已提交
223 224
/**
 * @mac_base: register base address of device MAC, exclude xHCI's
C
Chunfeng Yun 已提交
225
 * @ippc_base: register base address of IP Power and Clock interface (IPPC)
C
Chunfeng Yun 已提交
226 227 228 229 230
 * @vusb33: usb3.3V shared by device/host IP
 * @dr_mode: works in which mode:
 *		host only, device only or dual-role mode
 * @u2_ports: number of usb2.0 host ports
 * @u3_ports: number of usb3.0 host ports
231 232 233
 * @u2p_dis_msk: mask of disabling usb2 ports, e.g. bit0==1 to
 *		disable u2port0, bit1==1 to disable u2port1,... etc,
 *		but when use dual-role mode, can't disable u2port0
234 235
 * @u3p_dis_msk: mask of disabling usb3 ports, for example, bit0==1 to
 *		disable u3port0, bit1==1 to disable u3port1,... etc
C
Chunfeng Yun 已提交
236
 * @dbgfs_root: only used when supports manual dual-role switch via debugfs
237 238 239 240
 * @uwk_en: it's true when supports remote wakeup in host mode
 * @uwk: syscon including usb wakeup glue layer between SSUSB IP and SPM
 * @uwk_reg_base: the base address of the wakeup glue layer in @uwk
 * @uwk_vers: the version of the wakeup glue layer
C
Chunfeng Yun 已提交
241 242 243 244 245 246 247 248
 */
struct ssusb_mtk {
	struct device *dev;
	struct mtu3 *u3d;
	void __iomem *mac_base;
	void __iomem *ippc_base;
	struct phy **phys;
	int num_phys;
249
	int wakeup_irq;
C
Chunfeng Yun 已提交
250 251
	/* common power & clock */
	struct regulator *vusb33;
252
	struct clk_bulk_data clks[BULK_CLKS_CNT];
C
Chunfeng Yun 已提交
253
	/* otg */
C
Chunfeng Yun 已提交
254
	struct otg_switch_mtk otg_switch;
C
Chunfeng Yun 已提交
255 256 257 258
	enum usb_dr_mode dr_mode;
	bool is_host;
	int u2_ports;
	int u3_ports;
259
	int u2p_dis_msk;
260
	int u3p_dis_msk;
C
Chunfeng Yun 已提交
261
	struct dentry *dbgfs_root;
C
Chunfeng Yun 已提交
262
	/* usb wakeup for host mode */
263 264 265 266
	bool uwk_en;
	struct regmap *uwk;
	u32 uwk_reg_base;
	u32 uwk_vers;
C
Chunfeng Yun 已提交
267
};
C
Chunfeng Yun 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

/**
 * @fifo_size: it is (@slot + 1) * @fifo_seg_size
 * @fifo_seg_size: it is roundup_pow_of_two(@maxp)
 */
struct mtu3_ep {
	struct usb_ep ep;
	char name[12];
	struct mtu3 *mtu;
	u8 epnum;
	u8 type;
	u8 is_in;
	u16 maxp;
	int slot;
	u32 fifo_size;
	u32 fifo_addr;
	u32 fifo_seg_size;
	struct mtu3_fifo_info *fifo;

	struct list_head req_list;
	struct mtu3_gpd_ring gpd_ring;
289
	const struct usb_ss_ep_comp_descriptor *comp_desc;
C
Chunfeng Yun 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303
	const struct usb_endpoint_descriptor *desc;

	int flags;
};

struct mtu3_request {
	struct usb_request request;
	struct list_head list;
	struct mtu3_ep *mep;
	struct mtu3 *mtu;
	struct qmu_gpd *gpd;
	int epnum;
};

C
Chunfeng Yun 已提交
304 305 306 307 308
static inline struct ssusb_mtk *dev_to_ssusb(struct device *dev)
{
	return dev_get_drvdata(dev);
}

C
Chunfeng Yun 已提交
309 310
/**
 * struct mtu3 - device driver instance data.
311 312
 * @slot: MTU3_U2_IP_SLOT_DEFAULT for U2 IP only,
 *		MTU3_U3_IP_SLOT_DEFAULT for U3 IP
C
Chunfeng Yun 已提交
313 314
 * @may_wakeup: means device's remote wakeup is enabled
 * @is_self_powered: is reported in device status and the config descriptor
315
 * @delayed_status: true when function drivers ask for delayed status
316
 * @gen2cp: compatible with USB3 Gen2 IP
C
Chunfeng Yun 已提交
317 318 319 320 321 322
 * @ep0_req: dummy request used while handling standard USB requests
 *		for GET_STATUS and SET_SEL
 * @setup_buf: ep0 response buffer for GET_STATUS and SET_SEL requests
 */
struct mtu3 {
	spinlock_t lock;
C
Chunfeng Yun 已提交
323
	struct ssusb_mtk *ssusb;
C
Chunfeng Yun 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	struct device *dev;
	void __iomem *mac_base;
	void __iomem *ippc_base;
	int irq;

	struct mtu3_fifo_info tx_fifo;
	struct mtu3_fifo_info rx_fifo;

	struct mtu3_ep *ep_array;
	struct mtu3_ep *in_eps;
	struct mtu3_ep *out_eps;
	struct mtu3_ep *ep0;
	int num_eps;
	int slot;
	int active_ep;

	struct dma_pool	*qmu_gpd_pool;
	enum mtu3_g_ep0_state ep0_state;
	struct usb_gadget g;	/* the gadget */
	struct usb_gadget_driver *gadget_driver;
	struct mtu3_request ep0_req;
	u8 setup_buf[EP0_RESPONSE_BUF];
C
Chunfeng Yun 已提交
346 347
	enum usb_device_speed max_speed;
	enum usb_device_speed speed;
C
Chunfeng Yun 已提交
348 349 350 351 352 353

	unsigned is_active:1;
	unsigned may_wakeup:1;
	unsigned is_self_powered:1;
	unsigned test_mode:1;
	unsigned softconnect:1;
354 355 356
	unsigned u1_enable:1;
	unsigned u2_enable:1;
	unsigned is_u3_ip:1;
357
	unsigned delayed_status:1;
358
	unsigned gen2cp:1;
359
	unsigned connected:1;
C
Chunfeng Yun 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

	u8 address;
	u8 test_mode_nr;
	u32 hw_version;
};

static inline struct mtu3 *gadget_to_mtu3(struct usb_gadget *g)
{
	return container_of(g, struct mtu3, g);
}

static inline struct mtu3_request *to_mtu3_request(struct usb_request *req)
{
	return req ? container_of(req, struct mtu3_request, request) : NULL;
}

static inline struct mtu3_ep *to_mtu3_ep(struct usb_ep *ep)
{
	return ep ? container_of(ep, struct mtu3_ep, ep) : NULL;
}

static inline struct mtu3_request *next_request(struct mtu3_ep *mep)
{
383 384
	return list_first_entry_or_null(&mep->req_list, struct mtu3_request,
					list);
C
Chunfeng Yun 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
}

static inline void mtu3_writel(void __iomem *base, u32 offset, u32 data)
{
	writel(data, base + offset);
}

static inline u32 mtu3_readl(void __iomem *base, u32 offset)
{
	return readl(base + offset);
}

static inline void mtu3_setbits(void __iomem *base, u32 offset, u32 bits)
{
	void __iomem *addr = base + offset;
	u32 tmp = readl(addr);

	writel((tmp | (bits)), addr);
}

static inline void mtu3_clrbits(void __iomem *base, u32 offset, u32 bits)
{
	void __iomem *addr = base + offset;
	u32 tmp = readl(addr);

	writel((tmp & ~(bits)), addr);
}

C
Chunfeng Yun 已提交
413
int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks);
C
Chunfeng Yun 已提交
414 415 416 417 418 419 420 421 422 423 424
struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
void mtu3_free_request(struct usb_ep *ep, struct usb_request *req);
void mtu3_req_complete(struct mtu3_ep *mep,
		struct usb_request *req, int status);

int mtu3_config_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
		int interval, int burst, int mult);
void mtu3_deconfig_ep(struct mtu3 *mtu, struct mtu3_ep *mep);
void mtu3_ep_stall_set(struct mtu3_ep *mep, bool set);
void mtu3_start(struct mtu3 *mtu);
void mtu3_stop(struct mtu3 *mtu);
425
void mtu3_dev_on_off(struct mtu3 *mtu, int is_on);
C
Chunfeng Yun 已提交
426 427 428 429 430 431 432 433 434 435 436 437

int mtu3_gadget_setup(struct mtu3 *mtu);
void mtu3_gadget_cleanup(struct mtu3 *mtu);
void mtu3_gadget_reset(struct mtu3 *mtu);
void mtu3_gadget_suspend(struct mtu3 *mtu);
void mtu3_gadget_resume(struct mtu3 *mtu);
void mtu3_gadget_disconnect(struct mtu3 *mtu);

irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu);
extern const struct usb_ep_ops mtu3_ep0_ops;

#endif