1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas USB3.0 Peripheral driver (USB gadget)
4  *
5  * Copyright (C) 2015-2017  Renesas Electronics Corporation
6  */
7 
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/err.h>
12 #include <linux/extcon-provider.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/sizes.h>
21 #include <linux/slab.h>
22 #include <linux/sys_soc.h>
23 #include <linux/uaccess.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/usb/gadget.h>
26 #include <linux/usb/of.h>
27 #include <linux/usb/role.h>
28 
29 /* register definitions */
30 #define USB3_AXI_INT_STA	0x008
31 #define USB3_AXI_INT_ENA	0x00c
32 #define USB3_DMA_INT_STA	0x010
33 #define USB3_DMA_INT_ENA	0x014
34 #define USB3_DMA_CH0_CON(n)	(0x030 + ((n) - 1) * 0x10) /* n = 1 to 4 */
35 #define USB3_DMA_CH0_PRD_ADR(n)	(0x034 + ((n) - 1) * 0x10) /* n = 1 to 4 */
36 #define USB3_USB_COM_CON	0x200
37 #define USB3_USB20_CON		0x204
38 #define USB3_USB30_CON		0x208
39 #define USB3_USB_STA		0x210
40 #define USB3_DRD_CON		0x218
41 #define USB3_USB_INT_STA_1	0x220
42 #define USB3_USB_INT_STA_2	0x224
43 #define USB3_USB_INT_ENA_1	0x228
44 #define USB3_USB_INT_ENA_2	0x22c
45 #define USB3_STUP_DAT_0		0x230
46 #define USB3_STUP_DAT_1		0x234
47 #define USB3_USB_OTG_STA	0x268
48 #define USB3_USB_OTG_INT_STA	0x26c
49 #define USB3_USB_OTG_INT_ENA	0x270
50 #define USB3_P0_MOD		0x280
51 #define USB3_P0_CON		0x288
52 #define USB3_P0_STA		0x28c
53 #define USB3_P0_INT_STA		0x290
54 #define USB3_P0_INT_ENA		0x294
55 #define USB3_P0_LNG		0x2a0
56 #define USB3_P0_READ		0x2a4
57 #define USB3_P0_WRITE		0x2a8
58 #define USB3_PIPE_COM		0x2b0
59 #define USB3_PN_MOD		0x2c0
60 #define USB3_PN_RAMMAP		0x2c4
61 #define USB3_PN_CON		0x2c8
62 #define USB3_PN_STA		0x2cc
63 #define USB3_PN_INT_STA		0x2d0
64 #define USB3_PN_INT_ENA		0x2d4
65 #define USB3_PN_LNG		0x2e0
66 #define USB3_PN_READ		0x2e4
67 #define USB3_PN_WRITE		0x2e8
68 #define USB3_SSIFCMD		0x340
69 
70 /* AXI_INT_ENA and AXI_INT_STA */
71 #define AXI_INT_DMAINT		BIT(31)
72 #define AXI_INT_EPCINT		BIT(30)
73 /* PRD's n = from 1 to 4 */
74 #define AXI_INT_PRDEN_CLR_STA_SHIFT(n)	(16 + (n) - 1)
75 #define AXI_INT_PRDERR_STA_SHIFT(n)	(0 + (n) - 1)
76 #define AXI_INT_PRDEN_CLR_STA(n)	(1 << AXI_INT_PRDEN_CLR_STA_SHIFT(n))
77 #define AXI_INT_PRDERR_STA(n)		(1 << AXI_INT_PRDERR_STA_SHIFT(n))
78 
79 /* DMA_INT_ENA and DMA_INT_STA */
80 #define DMA_INT(n)		BIT(n)
81 
82 /* DMA_CH0_CONn */
83 #define DMA_CON_PIPE_DIR	BIT(15)		/* 1: In Transfer */
84 #define DMA_CON_PIPE_NO_SHIFT	8
85 #define DMA_CON_PIPE_NO_MASK	GENMASK(12, DMA_CON_PIPE_NO_SHIFT)
86 #define DMA_COM_PIPE_NO(n)	(((n) << DMA_CON_PIPE_NO_SHIFT) & \
87 					 DMA_CON_PIPE_NO_MASK)
88 #define DMA_CON_PRD_EN		BIT(0)
89 
90 /* LCLKSEL */
91 #define LCLKSEL_LSEL		BIT(18)
92 
93 /* USB_COM_CON */
94 #define USB_COM_CON_CONF		BIT(24)
95 #define USB_COM_CON_PN_WDATAIF_NL	BIT(23)
96 #define USB_COM_CON_PN_RDATAIF_NL	BIT(22)
97 #define USB_COM_CON_PN_LSTTR_PP		BIT(21)
98 #define USB_COM_CON_SPD_MODE		BIT(17)
99 #define USB_COM_CON_EP0_EN		BIT(16)
100 #define USB_COM_CON_DEV_ADDR_SHIFT	8
101 #define USB_COM_CON_DEV_ADDR_MASK	GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
102 #define USB_COM_CON_DEV_ADDR(n)		(((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
103 					 USB_COM_CON_DEV_ADDR_MASK)
104 #define USB_COM_CON_RX_DETECTION	BIT(1)
105 #define USB_COM_CON_PIPE_CLR		BIT(0)
106 
107 /* USB20_CON */
108 #define USB20_CON_B2_PUE		BIT(31)
109 #define USB20_CON_B2_SUSPEND		BIT(24)
110 #define USB20_CON_B2_CONNECT		BIT(17)
111 #define USB20_CON_B2_TSTMOD_SHIFT	8
112 #define USB20_CON_B2_TSTMOD_MASK	GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
113 #define USB20_CON_B2_TSTMOD(n)		(((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
114 					 USB20_CON_B2_TSTMOD_MASK)
115 #define USB20_CON_B2_TSTMOD_EN		BIT(0)
116 
117 /* USB30_CON */
118 #define USB30_CON_POW_SEL_SHIFT		24
119 #define USB30_CON_POW_SEL_MASK		GENMASK(26, USB30_CON_POW_SEL_SHIFT)
120 #define USB30_CON_POW_SEL_IN_U3		BIT(26)
121 #define USB30_CON_POW_SEL_IN_DISCON	0
122 #define USB30_CON_POW_SEL_P2_TO_P0	BIT(25)
123 #define USB30_CON_POW_SEL_P0_TO_P3	BIT(24)
124 #define USB30_CON_POW_SEL_P0_TO_P2	0
125 #define USB30_CON_B3_PLLWAKE		BIT(23)
126 #define USB30_CON_B3_CONNECT		BIT(17)
127 #define USB30_CON_B3_HOTRST_CMP		BIT(1)
128 
129 /* USB_STA */
130 #define USB_STA_SPEED_MASK	(BIT(2) | BIT(1))
131 #define USB_STA_SPEED_HS	BIT(2)
132 #define USB_STA_SPEED_FS	BIT(1)
133 #define USB_STA_SPEED_SS	0
134 #define USB_STA_VBUS_STA	BIT(0)
135 
136 /* DRD_CON */
137 #define DRD_CON_PERI_CON	BIT(24)
138 #define DRD_CON_VBOUT		BIT(0)
139 
140 /* USB_INT_ENA_1 and USB_INT_STA_1 */
141 #define USB_INT_1_B3_PLLWKUP	BIT(31)
142 #define USB_INT_1_B3_LUPSUCS	BIT(30)
143 #define USB_INT_1_B3_DISABLE	BIT(27)
144 #define USB_INT_1_B3_WRMRST	BIT(21)
145 #define USB_INT_1_B3_HOTRST	BIT(20)
146 #define USB_INT_1_B2_USBRST	BIT(12)
147 #define USB_INT_1_B2_L1SPND	BIT(11)
148 #define USB_INT_1_B2_SPND	BIT(9)
149 #define USB_INT_1_B2_RSUM	BIT(8)
150 #define USB_INT_1_SPEED		BIT(1)
151 #define USB_INT_1_VBUS_CNG	BIT(0)
152 
153 /* USB_INT_ENA_2 and USB_INT_STA_2 */
154 #define USB_INT_2_PIPE(n)	BIT(n)
155 
156 /* USB_OTG_STA, USB_OTG_INT_STA and USB_OTG_INT_ENA */
157 #define USB_OTG_IDMON		BIT(4)
158 
159 /* P0_MOD */
160 #define P0_MOD_DIR		BIT(6)
161 
162 /* P0_CON and PN_CON */
163 #define PX_CON_BYTE_EN_MASK		(BIT(10) | BIT(9))
164 #define PX_CON_BYTE_EN_SHIFT		9
165 #define PX_CON_BYTE_EN_BYTES(n)		(((n) << PX_CON_BYTE_EN_SHIFT) & \
166 					 PX_CON_BYTE_EN_MASK)
167 #define PX_CON_SEND			BIT(8)
168 
169 /* P0_CON */
170 #define P0_CON_ST_RES_MASK		(BIT(27) | BIT(26))
171 #define P0_CON_ST_RES_FORCE_STALL	BIT(27)
172 #define P0_CON_ST_RES_NORMAL		BIT(26)
173 #define P0_CON_ST_RES_FORCE_NRDY	0
174 #define P0_CON_OT_RES_MASK		(BIT(25) | BIT(24))
175 #define P0_CON_OT_RES_FORCE_STALL	BIT(25)
176 #define P0_CON_OT_RES_NORMAL		BIT(24)
177 #define P0_CON_OT_RES_FORCE_NRDY	0
178 #define P0_CON_IN_RES_MASK		(BIT(17) | BIT(16))
179 #define P0_CON_IN_RES_FORCE_STALL	BIT(17)
180 #define P0_CON_IN_RES_NORMAL		BIT(16)
181 #define P0_CON_IN_RES_FORCE_NRDY	0
182 #define P0_CON_RES_WEN			BIT(7)
183 #define P0_CON_BCLR			BIT(1)
184 
185 /* P0_STA and PN_STA */
186 #define PX_STA_BUFSTS		BIT(0)
187 
188 /* P0_INT_ENA and P0_INT_STA */
189 #define P0_INT_STSED		BIT(18)
190 #define P0_INT_STSST		BIT(17)
191 #define P0_INT_SETUP		BIT(16)
192 #define P0_INT_RCVNL		BIT(8)
193 #define P0_INT_ERDY		BIT(7)
194 #define P0_INT_FLOW		BIT(6)
195 #define P0_INT_STALL		BIT(2)
196 #define P0_INT_NRDY		BIT(1)
197 #define P0_INT_BFRDY		BIT(0)
198 #define P0_INT_ALL_BITS		(P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
199 
200 /* PN_MOD */
201 #define PN_MOD_DIR		BIT(6)
202 #define PN_MOD_TYPE_SHIFT	4
203 #define PN_MOD_TYPE_MASK	GENMASK(5, PN_MOD_TYPE_SHIFT)
204 #define PN_MOD_TYPE(n)		(((n) << PN_MOD_TYPE_SHIFT) & \
205 				 PN_MOD_TYPE_MASK)
206 #define PN_MOD_EPNUM_MASK	GENMASK(3, 0)
207 #define PN_MOD_EPNUM(n)		((n) & PN_MOD_EPNUM_MASK)
208 
209 /* PN_RAMMAP */
210 #define PN_RAMMAP_RAMAREA_SHIFT	29
211 #define PN_RAMMAP_RAMAREA_MASK	GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
212 #define PN_RAMMAP_RAMAREA_16KB	BIT(31)
213 #define PN_RAMMAP_RAMAREA_8KB	(BIT(30) | BIT(29))
214 #define PN_RAMMAP_RAMAREA_4KB	BIT(30)
215 #define PN_RAMMAP_RAMAREA_2KB	BIT(29)
216 #define PN_RAMMAP_RAMAREA_1KB	0
217 #define PN_RAMMAP_MPKT_SHIFT	16
218 #define PN_RAMMAP_MPKT_MASK	GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
219 #define PN_RAMMAP_MPKT(n)	(((n) << PN_RAMMAP_MPKT_SHIFT) & \
220 				 PN_RAMMAP_MPKT_MASK)
221 #define PN_RAMMAP_RAMIF_SHIFT	14
222 #define PN_RAMMAP_RAMIF_MASK	GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
223 #define PN_RAMMAP_RAMIF(n)	(((n) << PN_RAMMAP_RAMIF_SHIFT) & \
224 				 PN_RAMMAP_RAMIF_MASK)
225 #define PN_RAMMAP_BASEAD_MASK	GENMASK(13, 0)
226 #define PN_RAMMAP_BASEAD(offs)	(((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
227 #define PN_RAMMAP_DATA(area, ramif, basead)	((PN_RAMMAP_##area) | \
228 						 (PN_RAMMAP_RAMIF(ramif)) | \
229 						 (PN_RAMMAP_BASEAD(basead)))
230 
231 /* PN_CON */
232 #define PN_CON_EN		BIT(31)
233 #define PN_CON_DATAIF_EN	BIT(30)
234 #define PN_CON_RES_MASK		(BIT(17) | BIT(16))
235 #define PN_CON_RES_FORCE_STALL	BIT(17)
236 #define PN_CON_RES_NORMAL	BIT(16)
237 #define PN_CON_RES_FORCE_NRDY	0
238 #define PN_CON_LAST		BIT(11)
239 #define PN_CON_RES_WEN		BIT(7)
240 #define PN_CON_CLR		BIT(0)
241 
242 /* PN_INT_STA and PN_INT_ENA */
243 #define PN_INT_LSTTR	BIT(4)
244 #define PN_INT_BFRDY	BIT(0)
245 
246 /* USB3_SSIFCMD */
247 #define SSIFCMD_URES_U2		BIT(9)
248 #define SSIFCMD_URES_U1		BIT(8)
249 #define SSIFCMD_UDIR_U2		BIT(7)
250 #define SSIFCMD_UDIR_U1		BIT(6)
251 #define SSIFCMD_UREQ_U2		BIT(5)
252 #define SSIFCMD_UREQ_U1		BIT(4)
253 
254 #define USB3_EP0_SS_MAX_PACKET_SIZE	512
255 #define USB3_EP0_HSFS_MAX_PACKET_SIZE	64
256 #define USB3_EP0_BUF_SIZE		8
257 #define USB3_MAX_NUM_PIPES		6	/* This includes PIPE 0 */
258 #define USB3_WAIT_US			3
259 #define USB3_DMA_NUM_SETTING_AREA	4
260 /*
261  * To avoid double-meaning of "0" (xferred 65536 bytes or received zlp if
262  * buffer size is 65536), this driver uses the maximum size per a entry is
263  * 32768 bytes.
264  */
265 #define USB3_DMA_MAX_XFER_SIZE		32768
266 #define USB3_DMA_PRD_SIZE		4096
267 
268 struct renesas_usb3;
269 
270 /* Physical Region Descriptor Table */
271 struct renesas_usb3_prd {
272 	u32 word1;
273 #define USB3_PRD1_E		BIT(30)		/* the end of chain */
274 #define USB3_PRD1_U		BIT(29)		/* completion of transfer */
275 #define USB3_PRD1_D		BIT(28)		/* Error occurred */
276 #define USB3_PRD1_INT		BIT(27)		/* Interrupt occurred */
277 #define USB3_PRD1_LST		BIT(26)		/* Last Packet */
278 #define USB3_PRD1_B_INC		BIT(24)
279 #define USB3_PRD1_MPS_8		0
280 #define USB3_PRD1_MPS_16	BIT(21)
281 #define USB3_PRD1_MPS_32	BIT(22)
282 #define USB3_PRD1_MPS_64	(BIT(22) | BIT(21))
283 #define USB3_PRD1_MPS_512	BIT(23)
284 #define USB3_PRD1_MPS_1024	(BIT(23) | BIT(21))
285 #define USB3_PRD1_MPS_RESERVED	(BIT(23) | BIT(22) | BIT(21))
286 #define USB3_PRD1_SIZE_MASK	GENMASK(15, 0)
287 
288 	u32 bap;
289 };
290 #define USB3_DMA_NUM_PRD_ENTRIES	(USB3_DMA_PRD_SIZE / \
291 					  sizeof(struct renesas_usb3_prd))
292 #define USB3_DMA_MAX_XFER_SIZE_ALL_PRDS	(USB3_DMA_PRD_SIZE / \
293 					 sizeof(struct renesas_usb3_prd) * \
294 					 USB3_DMA_MAX_XFER_SIZE)
295 
296 struct renesas_usb3_dma {
297 	struct renesas_usb3_prd *prd;
298 	dma_addr_t prd_dma;
299 	int num;	/* Setting area number (from 1 to 4) */
300 	bool used;
301 };
302 
303 struct renesas_usb3_request {
304 	struct usb_request	req;
305 	struct list_head	queue;
306 };
307 
308 #define USB3_EP_NAME_SIZE	8
309 struct renesas_usb3_ep {
310 	struct usb_ep ep;
311 	struct renesas_usb3 *usb3;
312 	struct renesas_usb3_dma *dma;
313 	int num;
314 	char ep_name[USB3_EP_NAME_SIZE];
315 	struct list_head queue;
316 	u32 rammap_val;
317 	bool dir_in;
318 	bool halt;
319 	bool wedge;
320 	bool started;
321 };
322 
323 struct renesas_usb3_priv {
324 	int ramsize_per_ramif;		/* unit = bytes */
325 	int num_ramif;
326 	int ramsize_per_pipe;		/* unit = bytes */
327 	bool workaround_for_vbus;	/* if true, don't check vbus signal */
328 };
329 
330 struct renesas_usb3 {
331 	void __iomem *reg;
332 
333 	struct usb_gadget gadget;
334 	struct usb_gadget_driver *driver;
335 	struct extcon_dev *extcon;
336 	struct work_struct extcon_work;
337 	struct phy *phy;
338 	struct dentry *dentry;
339 
340 	struct usb_role_switch *role_sw;
341 	struct device *host_dev;
342 	struct work_struct role_work;
343 	enum usb_role role;
344 
345 	struct renesas_usb3_ep *usb3_ep;
346 	int num_usb3_eps;
347 
348 	struct renesas_usb3_dma dma[USB3_DMA_NUM_SETTING_AREA];
349 
350 	spinlock_t lock;
351 	int disabled_count;
352 
353 	struct usb_request *ep0_req;
354 
355 	enum usb_role connection_state;
356 	u16 test_mode;
357 	u8 ep0_buf[USB3_EP0_BUF_SIZE];
358 	bool softconnect;
359 	bool workaround_for_vbus;
360 	bool extcon_host;		/* check id and set EXTCON_USB_HOST */
361 	bool extcon_usb;		/* check vbus and set EXTCON_USB */
362 	bool forced_b_device;
363 	bool start_to_connect;
364 	bool role_sw_by_connector;
365 };
366 
367 #define gadget_to_renesas_usb3(_gadget)	\
368 		container_of(_gadget, struct renesas_usb3, gadget)
369 #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
370 #define usb3_to_dev(_usb3)	(_usb3->gadget.dev.parent)
371 
372 #define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
373 #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
374 #define usb_req_to_usb3_req(_req) container_of(_req, \
375 					    struct renesas_usb3_request, req)
376 
377 #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
378 #define usb3_for_each_ep(usb3_ep, usb3, i)			\
379 		for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i));	\
380 		     (i) < (usb3)->num_usb3_eps;		\
381 		     (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
382 
383 #define usb3_get_dma(usb3, i)	(&(usb3)->dma[i])
384 #define usb3_for_each_dma(usb3, dma, i)				\
385 		for ((i) = 0, dma = usb3_get_dma((usb3), (i));	\
386 		     (i) < USB3_DMA_NUM_SETTING_AREA;		\
387 		     (i)++, dma = usb3_get_dma((usb3), (i)))
388 
389 static const char udc_name[] = "renesas_usb3";
390 
391 static bool use_dma = 1;
392 module_param(use_dma, bool, 0644);
393 MODULE_PARM_DESC(use_dma, "use dedicated DMAC");
394 
395 static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
396 {
397 	iowrite32(data, usb3->reg + offs);
398 }
399 
400 static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
401 {
402 	return ioread32(usb3->reg + offs);
403 }
404 
405 static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
406 {
407 	u32 val = usb3_read(usb3, offs);
408 
409 	val |= bits;
410 	usb3_write(usb3, val, offs);
411 }
412 
413 static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
414 {
415 	u32 val = usb3_read(usb3, offs);
416 
417 	val &= ~bits;
418 	usb3_write(usb3, val, offs);
419 }
420 
421 static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
422 		     u32 expected)
423 {
424 	int i;
425 
426 	for (i = 0; i < USB3_WAIT_US; i++) {
427 		if ((usb3_read(usb3, reg) & mask) == expected)
428 			return 0;
429 		udelay(1);
430 	}
431 
432 	dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
433 		__func__, reg, mask, expected);
434 
435 	return -EBUSY;
436 }
437 
438 static void renesas_usb3_extcon_work(struct work_struct *work)
439 {
440 	struct renesas_usb3 *usb3 = container_of(work, struct renesas_usb3,
441 						 extcon_work);
442 
443 	extcon_set_state_sync(usb3->extcon, EXTCON_USB_HOST, usb3->extcon_host);
444 	extcon_set_state_sync(usb3->extcon, EXTCON_USB, usb3->extcon_usb);
445 }
446 
447 static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
448 {
449 	usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
450 }
451 
452 static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
453 {
454 	usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
455 }
456 
457 static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
458 {
459 	usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
460 }
461 
462 static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
463 {
464 	usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
465 }
466 
467 static bool usb3_is_host(struct renesas_usb3 *usb3)
468 {
469 	return !(usb3_read(usb3, USB3_DRD_CON) & DRD_CON_PERI_CON);
470 }
471 
472 static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
473 {
474 	/* Set AXI_INT */
475 	usb3_write(usb3, ~0, USB3_DMA_INT_STA);
476 	usb3_write(usb3, 0, USB3_DMA_INT_ENA);
477 	usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
478 }
479 
480 static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
481 {
482 	usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
483 	if (!usb3->workaround_for_vbus)
484 		usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
485 }
486 
487 static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
488 {
489 	if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
490 		return true;	/* already waked it up */
491 
492 	usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
493 	usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
494 
495 	return false;
496 }
497 
498 static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
499 {
500 	u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
501 
502 	if (usb3->softconnect && pullup)
503 		usb3_set_bit(usb3, bits, USB3_USB20_CON);
504 	else
505 		usb3_clear_bit(usb3, bits, USB3_USB20_CON);
506 }
507 
508 static void usb3_set_test_mode(struct renesas_usb3 *usb3)
509 {
510 	u32 val = usb3_read(usb3, USB3_USB20_CON);
511 
512 	val &= ~USB20_CON_B2_TSTMOD_MASK;
513 	val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
514 	usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
515 	if (!usb3->test_mode)
516 		usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
517 }
518 
519 static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
520 {
521 	usb3->disabled_count++;
522 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
523 	usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
524 	usb3_usb2_pullup(usb3, 1);
525 }
526 
527 static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
528 {
529 	return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
530 }
531 
532 static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
533 {
534 	if (!usb3_is_usb3_phy_in_u3(usb3))
535 		return true;	/* already waked it up */
536 
537 	usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
538 	usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
539 
540 	return false;
541 }
542 
543 static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
544 {
545 	u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
546 	u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
547 	u32 val = usb3_read(usb3, USB3_SSIFCMD);
548 	u16 ret = 0;
549 
550 	/* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
551 	if (!(val & mask_u2))
552 		ret |= 1 << USB_DEV_STAT_U2_ENABLED;
553 	if (!(val & mask_u1))
554 		ret |= 1 << USB_DEV_STAT_U1_ENABLED;
555 
556 	return ret;
557 }
558 
559 static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
560 {
561 	u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
562 
563 	/* Enables U2 if the bits of UDIR and UREQ are set to 0 */
564 	if (enable)
565 		usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
566 	else
567 		usb3_set_bit(usb3, bits, USB3_SSIFCMD);
568 }
569 
570 static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
571 {
572 	u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
573 
574 	/* Enables U1 if the bits of UDIR and UREQ are set to 0 */
575 	if (enable)
576 		usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
577 	else
578 		usb3_set_bit(usb3, bits, USB3_SSIFCMD);
579 }
580 
581 static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
582 {
583 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
584 	usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
585 	usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
586 }
587 
588 static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
589 {
590 	usb3_start_operation_for_usb3(usb3);
591 	usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
592 
593 	usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
594 			  USB_INT_1_SPEED);
595 }
596 
597 static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
598 {
599 	usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
600 }
601 
602 static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
603 					     bool is_usb3)
604 {
605 	usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
606 	usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
607 	usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
608 
609 	if (is_usb3)
610 		usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
611 				  USB_INT_1_B3_HOTRST);
612 	else
613 		usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
614 				  USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
615 }
616 
617 static void usb3_connect(struct renesas_usb3 *usb3)
618 {
619 	if (usb3_wakeup_usb3_phy(usb3))
620 		usb3_start_usb3_connection(usb3);
621 }
622 
623 static void usb3_reset_epc(struct renesas_usb3 *usb3)
624 {
625 	usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
626 	usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
627 	usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
628 	usb3->test_mode = 0;
629 	usb3_set_test_mode(usb3);
630 }
631 
632 static void usb3_disconnect(struct renesas_usb3 *usb3)
633 {
634 	usb3->disabled_count = 0;
635 	usb3_usb2_pullup(usb3, 0);
636 	usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
637 	usb3_reset_epc(usb3);
638 	usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM | USB_INT_1_B3_PLLWKUP |
639 			   USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
640 			   USB_INT_1_SPEED | USB_INT_1_B3_WRMRST |
641 			   USB_INT_1_B3_HOTRST | USB_INT_1_B2_SPND |
642 			   USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
643 	usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
644 	usb3_init_epc_registers(usb3);
645 
646 	if (usb3->driver)
647 		usb3->driver->disconnect(&usb3->gadget);
648 }
649 
650 static void usb3_check_vbus(struct renesas_usb3 *usb3)
651 {
652 	if (usb3->workaround_for_vbus) {
653 		usb3_connect(usb3);
654 	} else {
655 		usb3->extcon_usb = !!(usb3_read(usb3, USB3_USB_STA) &
656 							USB_STA_VBUS_STA);
657 		if (usb3->extcon_usb)
658 			usb3_connect(usb3);
659 		else
660 			usb3_disconnect(usb3);
661 
662 		schedule_work(&usb3->extcon_work);
663 	}
664 }
665 
666 static void renesas_usb3_role_work(struct work_struct *work)
667 {
668 	struct renesas_usb3 *usb3 =
669 			container_of(work, struct renesas_usb3, role_work);
670 
671 	usb_role_switch_set_role(usb3->role_sw, usb3->role);
672 }
673 
674 static void usb3_set_mode(struct renesas_usb3 *usb3, bool host)
675 {
676 	if (host)
677 		usb3_clear_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
678 	else
679 		usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
680 }
681 
682 static void usb3_set_mode_by_role_sw(struct renesas_usb3 *usb3, bool host)
683 {
684 	if (usb3->role_sw) {
685 		usb3->role = host ? USB_ROLE_HOST : USB_ROLE_DEVICE;
686 		schedule_work(&usb3->role_work);
687 	} else {
688 		usb3_set_mode(usb3, host);
689 	}
690 }
691 
692 static void usb3_vbus_out(struct renesas_usb3 *usb3, bool enable)
693 {
694 	if (enable)
695 		usb3_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
696 	else
697 		usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
698 }
699 
700 static void usb3_mode_config(struct renesas_usb3 *usb3, bool host, bool a_dev)
701 {
702 	unsigned long flags;
703 
704 	spin_lock_irqsave(&usb3->lock, flags);
705 	if (!usb3->role_sw_by_connector ||
706 	    usb3->connection_state != USB_ROLE_NONE) {
707 		usb3_set_mode_by_role_sw(usb3, host);
708 		usb3_vbus_out(usb3, a_dev);
709 	}
710 	/* for A-Peripheral or forced B-device mode */
711 	if ((!host && a_dev) || usb3->start_to_connect)
712 		usb3_connect(usb3);
713 	spin_unlock_irqrestore(&usb3->lock, flags);
714 }
715 
716 static bool usb3_is_a_device(struct renesas_usb3 *usb3)
717 {
718 	return !(usb3_read(usb3, USB3_USB_OTG_STA) & USB_OTG_IDMON);
719 }
720 
721 static void usb3_check_id(struct renesas_usb3 *usb3)
722 {
723 	usb3->extcon_host = usb3_is_a_device(usb3);
724 
725 	if ((!usb3->role_sw_by_connector && usb3->extcon_host &&
726 	     !usb3->forced_b_device) || usb3->connection_state == USB_ROLE_HOST)
727 		usb3_mode_config(usb3, true, true);
728 	else
729 		usb3_mode_config(usb3, false, false);
730 
731 	schedule_work(&usb3->extcon_work);
732 }
733 
734 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
735 {
736 	usb3_init_axi_bridge(usb3);
737 	usb3_init_epc_registers(usb3);
738 	usb3_set_bit(usb3, USB_COM_CON_PN_WDATAIF_NL |
739 		     USB_COM_CON_PN_RDATAIF_NL | USB_COM_CON_PN_LSTTR_PP,
740 		     USB3_USB_COM_CON);
741 	usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_STA);
742 	usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_ENA);
743 
744 	usb3_check_id(usb3);
745 	usb3_check_vbus(usb3);
746 }
747 
748 static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
749 {
750 	usb3_disconnect(usb3);
751 	usb3_write(usb3, 0, USB3_P0_INT_ENA);
752 	usb3_write(usb3, 0, USB3_USB_OTG_INT_ENA);
753 	usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
754 	usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
755 	usb3_write(usb3, 0, USB3_AXI_INT_ENA);
756 }
757 
758 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
759 {
760 	usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
761 	usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
762 	usb3_start_usb3_connection(usb3);
763 }
764 
765 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
766 {
767 	usb3_transition_to_default_state(usb3, true);
768 }
769 
770 static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
771 {
772 	usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
773 	usb3_start_usb2_connection(usb3);
774 	usb3_transition_to_default_state(usb3, false);
775 }
776 
777 static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
778 {
779 	usb3_stop_usb3_connection(usb3);
780 	if (usb3_wakeup_usb2_phy(usb3))
781 		usb3_irq_epc_int_1_resume(usb3);
782 }
783 
784 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
785 {
786 	usb3_reset_epc(usb3);
787 	if (usb3->disabled_count < 3)
788 		usb3_start_usb3_connection(usb3);
789 	else
790 		usb3_start_usb2_connection(usb3);
791 }
792 
793 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
794 {
795 	usb3_check_vbus(usb3);
796 }
797 
798 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
799 {
800 	usb3_reset_epc(usb3);
801 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
802 
803 	/* This bit shall be set within 12ms from the start of HotReset */
804 	usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
805 }
806 
807 static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
808 {
809 	usb3_reset_epc(usb3);
810 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
811 
812 	usb3_start_operation_for_usb3(usb3);
813 	usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
814 }
815 
816 static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
817 {
818 	u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
819 
820 	switch (speed) {
821 	case USB_STA_SPEED_SS:
822 		usb3->gadget.speed = USB_SPEED_SUPER;
823 		usb3->gadget.ep0->maxpacket = USB3_EP0_SS_MAX_PACKET_SIZE;
824 		break;
825 	case USB_STA_SPEED_HS:
826 		usb3->gadget.speed = USB_SPEED_HIGH;
827 		usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE;
828 		break;
829 	case USB_STA_SPEED_FS:
830 		usb3->gadget.speed = USB_SPEED_FULL;
831 		usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE;
832 		break;
833 	default:
834 		usb3->gadget.speed = USB_SPEED_UNKNOWN;
835 		break;
836 	}
837 }
838 
839 static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
840 {
841 	if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
842 		usb3_irq_epc_int_1_pll_wakeup(usb3);
843 
844 	if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
845 		usb3_irq_epc_int_1_linkup_success(usb3);
846 
847 	if (int_sta_1 & USB_INT_1_B3_HOTRST)
848 		usb3_irq_epc_int_1_hot_reset(usb3);
849 
850 	if (int_sta_1 & USB_INT_1_B3_WRMRST)
851 		usb3_irq_epc_int_1_warm_reset(usb3);
852 
853 	if (int_sta_1 & USB_INT_1_B3_DISABLE)
854 		usb3_irq_epc_int_1_disable(usb3);
855 
856 	if (int_sta_1 & USB_INT_1_B2_USBRST)
857 		usb3_irq_epc_int_1_bus_reset(usb3);
858 
859 	if (int_sta_1 & USB_INT_1_B2_RSUM)
860 		usb3_irq_epc_int_1_resume(usb3);
861 
862 	if (int_sta_1 & USB_INT_1_SPEED)
863 		usb3_irq_epc_int_1_speed(usb3);
864 
865 	if (int_sta_1 & USB_INT_1_VBUS_CNG)
866 		usb3_irq_epc_int_1_vbus_change(usb3);
867 }
868 
869 static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
870 						       *usb3_ep)
871 {
872 	return list_first_entry_or_null(&usb3_ep->queue,
873 					struct renesas_usb3_request, queue);
874 }
875 
876 static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
877 						     *usb3_ep)
878 {
879 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
880 	struct renesas_usb3_request *usb3_req;
881 	unsigned long flags;
882 
883 	spin_lock_irqsave(&usb3->lock, flags);
884 	usb3_req = __usb3_get_request(usb3_ep);
885 	spin_unlock_irqrestore(&usb3->lock, flags);
886 
887 	return usb3_req;
888 }
889 
890 static void __usb3_request_done(struct renesas_usb3_ep *usb3_ep,
891 				struct renesas_usb3_request *usb3_req,
892 				int status)
893 {
894 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
895 
896 	dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
897 		usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
898 		status);
899 	usb3_req->req.status = status;
900 	usb3_ep->started = false;
901 	list_del_init(&usb3_req->queue);
902 	spin_unlock(&usb3->lock);
903 	usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
904 	spin_lock(&usb3->lock);
905 }
906 
907 static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
908 			      struct renesas_usb3_request *usb3_req, int status)
909 {
910 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
911 	unsigned long flags;
912 
913 	spin_lock_irqsave(&usb3->lock, flags);
914 	__usb3_request_done(usb3_ep, usb3_req, status);
915 	spin_unlock_irqrestore(&usb3->lock, flags);
916 }
917 
918 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
919 {
920 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
921 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
922 
923 	if (usb3_req)
924 		usb3_request_done(usb3_ep, usb3_req, 0);
925 	if (usb3->test_mode)
926 		usb3_set_test_mode(usb3);
927 }
928 
929 static void usb3_get_setup_data(struct renesas_usb3 *usb3,
930 				struct usb_ctrlrequest *ctrl)
931 {
932 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
933 	u32 *data = (u32 *)ctrl;
934 
935 	*data++ = usb3_read(usb3, USB3_STUP_DAT_0);
936 	*data = usb3_read(usb3, USB3_STUP_DAT_1);
937 
938 	/* update this driver's flag */
939 	usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
940 }
941 
942 static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
943 {
944 	u32 val = usb3_read(usb3, USB3_P0_CON);
945 
946 	val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
947 	val |= res | P0_CON_RES_WEN;
948 	usb3_write(usb3, val, USB3_P0_CON);
949 }
950 
951 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
952 {
953 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
954 				   P0_CON_OT_RES_FORCE_STALL |
955 				   P0_CON_IN_RES_NORMAL);
956 }
957 
958 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
959 {
960 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
961 				   P0_CON_OT_RES_FORCE_STALL |
962 				   P0_CON_IN_RES_NORMAL);
963 }
964 
965 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
966 {
967 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
968 				   P0_CON_OT_RES_NORMAL |
969 				   P0_CON_IN_RES_FORCE_STALL);
970 }
971 
972 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
973 {
974 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
975 				   P0_CON_OT_RES_NORMAL |
976 				   P0_CON_IN_RES_FORCE_STALL);
977 }
978 
979 static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
980 {
981 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
982 				   P0_CON_OT_RES_FORCE_STALL |
983 				   P0_CON_IN_RES_FORCE_STALL);
984 }
985 
986 static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
987 {
988 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
989 				   P0_CON_OT_RES_FORCE_STALL |
990 				   P0_CON_IN_RES_FORCE_STALL);
991 }
992 
993 static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
994 {
995 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
996 				   P0_CON_OT_RES_FORCE_NRDY |
997 				   P0_CON_IN_RES_FORCE_NRDY);
998 }
999 
1000 static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
1001 {
1002 	if (num == 0 || num > usb3->num_usb3_eps)
1003 		return -ENXIO;
1004 
1005 	usb3_write(usb3, num, USB3_PIPE_COM);
1006 
1007 	return 0;
1008 }
1009 
1010 static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
1011 {
1012 	u32 val = usb3_read(usb3, USB3_PN_CON);
1013 
1014 	val &= ~PN_CON_RES_MASK;
1015 	val |= res & PN_CON_RES_MASK;
1016 	val |= PN_CON_RES_WEN;
1017 	usb3_write(usb3, val, USB3_PN_CON);
1018 }
1019 
1020 static void usb3_pn_start(struct renesas_usb3 *usb3)
1021 {
1022 	usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
1023 }
1024 
1025 static void usb3_pn_stop(struct renesas_usb3 *usb3)
1026 {
1027 	usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
1028 }
1029 
1030 static void usb3_pn_stall(struct renesas_usb3 *usb3)
1031 {
1032 	usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
1033 }
1034 
1035 static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
1036 {
1037 	usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
1038 
1039 	return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
1040 }
1041 
1042 static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
1043 				      struct renesas_usb3_request *usb3_req)
1044 {
1045 	struct usb_request *req = &usb3_req->req;
1046 
1047 	if ((!req->zero && req->actual == req->length) ||
1048 	    (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
1049 		return true;
1050 	else
1051 		return false;
1052 }
1053 
1054 static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
1055 {
1056 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1057 	u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
1058 
1059 	return usb3_wait(usb3, sta_reg, mask, mask);
1060 }
1061 
1062 static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
1063 				 bool last)
1064 {
1065 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1066 	u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
1067 	u32 val = usb3_read(usb3, con_reg);
1068 
1069 	val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
1070 	val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
1071 	usb3_write(usb3, val, con_reg);
1072 }
1073 
1074 static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
1075 			   struct renesas_usb3_request *usb3_req,
1076 			   u32 fifo_reg)
1077 {
1078 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1079 	int i;
1080 	int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
1081 			usb3_ep->ep.maxpacket);
1082 	u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
1083 	u32 tmp = 0;
1084 	bool is_last = !len ? true : false;
1085 
1086 	if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
1087 		return -EBUSY;
1088 
1089 	/* Update gadget driver parameter */
1090 	usb3_req->req.actual += len;
1091 
1092 	/* Write data to the register */
1093 	if (len >= 4) {
1094 		iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
1095 		buf += (len / 4) * 4;
1096 		len %= 4;	/* update len to use usb3_set_pX_con_send() */
1097 	}
1098 
1099 	if (len) {
1100 		for (i = 0; i < len; i++)
1101 			tmp |= buf[i] << (8 * i);
1102 		usb3_write(usb3, tmp, fifo_reg);
1103 	}
1104 
1105 	if (!is_last)
1106 		is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
1107 	/* Send the data */
1108 	usb3_set_px_con_send(usb3_ep, len, is_last);
1109 
1110 	return is_last ? 0 : -EAGAIN;
1111 }
1112 
1113 static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
1114 {
1115 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1116 	u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
1117 
1118 	return usb3_read(usb3, lng_reg);
1119 }
1120 
1121 static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
1122 			  struct renesas_usb3_request *usb3_req, u32 fifo_reg)
1123 {
1124 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1125 	int i;
1126 	int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
1127 			usb3_get_received_length(usb3_ep));
1128 	u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
1129 	u32 tmp = 0;
1130 
1131 	if (!len)
1132 		return 0;
1133 
1134 	/* Update gadget driver parameter */
1135 	usb3_req->req.actual += len;
1136 
1137 	/* Read data from the register */
1138 	if (len >= 4) {
1139 		ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
1140 		buf += (len / 4) * 4;
1141 		len %= 4;
1142 	}
1143 
1144 	if (len) {
1145 		tmp = usb3_read(usb3, fifo_reg);
1146 		for (i = 0; i < len; i++)
1147 			buf[i] = (tmp >> (8 * i)) & 0xff;
1148 	}
1149 
1150 	return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
1151 }
1152 
1153 static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
1154 				  struct renesas_usb3_request *usb3_req)
1155 {
1156 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1157 
1158 	if (usb3_ep->dir_in) {
1159 		usb3_set_p0_con_for_ctrl_read_status(usb3);
1160 	} else {
1161 		if (!usb3_req->req.length)
1162 			usb3_set_p0_con_for_no_data(usb3);
1163 		else
1164 			usb3_set_p0_con_for_ctrl_write_status(usb3);
1165 	}
1166 }
1167 
1168 static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
1169 			 struct renesas_usb3_request *usb3_req)
1170 {
1171 	int ret;
1172 
1173 	if (usb3_ep->dir_in)
1174 		ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
1175 	else
1176 		ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
1177 
1178 	if (!ret)
1179 		usb3_set_status_stage(usb3_ep, usb3_req);
1180 }
1181 
1182 static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
1183 			     struct renesas_usb3_request *usb3_req)
1184 {
1185 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1186 
1187 	if (usb3_ep->started)
1188 		return;
1189 
1190 	usb3_ep->started = true;
1191 
1192 	if (usb3_ep->dir_in) {
1193 		usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
1194 		usb3_set_p0_con_for_ctrl_read_data(usb3);
1195 	} else {
1196 		usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
1197 		if (usb3_req->req.length)
1198 			usb3_set_p0_con_for_ctrl_write_data(usb3);
1199 	}
1200 
1201 	usb3_p0_xfer(usb3_ep, usb3_req);
1202 }
1203 
1204 static void usb3_enable_dma_pipen(struct renesas_usb3 *usb3)
1205 {
1206 	usb3_set_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON);
1207 }
1208 
1209 static void usb3_disable_dma_pipen(struct renesas_usb3 *usb3)
1210 {
1211 	usb3_clear_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON);
1212 }
1213 
1214 static void usb3_enable_dma_irq(struct renesas_usb3 *usb3, int num)
1215 {
1216 	usb3_set_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA);
1217 }
1218 
1219 static void usb3_disable_dma_irq(struct renesas_usb3 *usb3, int num)
1220 {
1221 	usb3_clear_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA);
1222 }
1223 
1224 static u32 usb3_dma_mps_to_prd_word1(struct renesas_usb3_ep *usb3_ep)
1225 {
1226 	switch (usb3_ep->ep.maxpacket) {
1227 	case 8:
1228 		return USB3_PRD1_MPS_8;
1229 	case 16:
1230 		return USB3_PRD1_MPS_16;
1231 	case 32:
1232 		return USB3_PRD1_MPS_32;
1233 	case 64:
1234 		return USB3_PRD1_MPS_64;
1235 	case 512:
1236 		return USB3_PRD1_MPS_512;
1237 	case 1024:
1238 		return USB3_PRD1_MPS_1024;
1239 	default:
1240 		return USB3_PRD1_MPS_RESERVED;
1241 	}
1242 }
1243 
1244 static bool usb3_dma_get_setting_area(struct renesas_usb3_ep *usb3_ep,
1245 				      struct renesas_usb3_request *usb3_req)
1246 {
1247 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1248 	struct renesas_usb3_dma *dma;
1249 	int i;
1250 	bool ret = false;
1251 
1252 	if (usb3_req->req.length > USB3_DMA_MAX_XFER_SIZE_ALL_PRDS) {
1253 		dev_dbg(usb3_to_dev(usb3), "%s: the length is too big (%d)\n",
1254 			__func__, usb3_req->req.length);
1255 		return false;
1256 	}
1257 
1258 	/* The driver doesn't handle zero-length packet via dmac */
1259 	if (!usb3_req->req.length)
1260 		return false;
1261 
1262 	if (usb3_dma_mps_to_prd_word1(usb3_ep) == USB3_PRD1_MPS_RESERVED)
1263 		return false;
1264 
1265 	usb3_for_each_dma(usb3, dma, i) {
1266 		if (dma->used)
1267 			continue;
1268 
1269 		if (usb_gadget_map_request(&usb3->gadget, &usb3_req->req,
1270 					   usb3_ep->dir_in) < 0)
1271 			break;
1272 
1273 		dma->used = true;
1274 		usb3_ep->dma = dma;
1275 		ret = true;
1276 		break;
1277 	}
1278 
1279 	return ret;
1280 }
1281 
1282 static void usb3_dma_put_setting_area(struct renesas_usb3_ep *usb3_ep,
1283 				      struct renesas_usb3_request *usb3_req)
1284 {
1285 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1286 	int i;
1287 	struct renesas_usb3_dma *dma;
1288 
1289 	usb3_for_each_dma(usb3, dma, i) {
1290 		if (usb3_ep->dma == dma) {
1291 			usb_gadget_unmap_request(&usb3->gadget, &usb3_req->req,
1292 						 usb3_ep->dir_in);
1293 			dma->used = false;
1294 			usb3_ep->dma = NULL;
1295 			break;
1296 		}
1297 	}
1298 }
1299 
1300 static void usb3_dma_fill_prd(struct renesas_usb3_ep *usb3_ep,
1301 			      struct renesas_usb3_request *usb3_req)
1302 {
1303 	struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd;
1304 	u32 remain = usb3_req->req.length;
1305 	u32 dma = usb3_req->req.dma;
1306 	u32 len;
1307 	int i = 0;
1308 
1309 	do {
1310 		len = min_t(u32, remain, USB3_DMA_MAX_XFER_SIZE) &
1311 			    USB3_PRD1_SIZE_MASK;
1312 		cur_prd->word1 = usb3_dma_mps_to_prd_word1(usb3_ep) |
1313 				 USB3_PRD1_B_INC | len;
1314 		cur_prd->bap = dma;
1315 		remain -= len;
1316 		dma += len;
1317 		if (!remain || (i + 1) < USB3_DMA_NUM_PRD_ENTRIES)
1318 			break;
1319 
1320 		cur_prd++;
1321 		i++;
1322 	} while (1);
1323 
1324 	cur_prd->word1 |= USB3_PRD1_E | USB3_PRD1_INT;
1325 	if (usb3_ep->dir_in)
1326 		cur_prd->word1 |= USB3_PRD1_LST;
1327 }
1328 
1329 static void usb3_dma_kick_prd(struct renesas_usb3_ep *usb3_ep)
1330 {
1331 	struct renesas_usb3_dma *dma = usb3_ep->dma;
1332 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1333 	u32 dma_con = DMA_COM_PIPE_NO(usb3_ep->num) | DMA_CON_PRD_EN;
1334 
1335 	if (usb3_ep->dir_in)
1336 		dma_con |= DMA_CON_PIPE_DIR;
1337 
1338 	wmb();	/* prd entries should be in system memory here */
1339 
1340 	usb3_write(usb3, 1 << usb3_ep->num, USB3_DMA_INT_STA);
1341 	usb3_write(usb3, AXI_INT_PRDEN_CLR_STA(dma->num) |
1342 		   AXI_INT_PRDERR_STA(dma->num), USB3_AXI_INT_STA);
1343 
1344 	usb3_write(usb3, dma->prd_dma, USB3_DMA_CH0_PRD_ADR(dma->num));
1345 	usb3_write(usb3, dma_con, USB3_DMA_CH0_CON(dma->num));
1346 	usb3_enable_dma_irq(usb3, usb3_ep->num);
1347 }
1348 
1349 static void usb3_dma_stop_prd(struct renesas_usb3_ep *usb3_ep)
1350 {
1351 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1352 	struct renesas_usb3_dma *dma = usb3_ep->dma;
1353 
1354 	usb3_disable_dma_irq(usb3, usb3_ep->num);
1355 	usb3_write(usb3, 0, USB3_DMA_CH0_CON(dma->num));
1356 }
1357 
1358 static int usb3_dma_update_status(struct renesas_usb3_ep *usb3_ep,
1359 				  struct renesas_usb3_request *usb3_req)
1360 {
1361 	struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd;
1362 	struct usb_request *req = &usb3_req->req;
1363 	u32 remain, len;
1364 	int i = 0;
1365 	int status = 0;
1366 
1367 	rmb();	/* The controller updated prd entries */
1368 
1369 	do {
1370 		if (cur_prd->word1 & USB3_PRD1_D)
1371 			status = -EIO;
1372 		if (cur_prd->word1 & USB3_PRD1_E)
1373 			len = req->length % USB3_DMA_MAX_XFER_SIZE;
1374 		else
1375 			len = USB3_DMA_MAX_XFER_SIZE;
1376 		remain = cur_prd->word1 & USB3_PRD1_SIZE_MASK;
1377 		req->actual += len - remain;
1378 
1379 		if (cur_prd->word1 & USB3_PRD1_E ||
1380 		    (i + 1) < USB3_DMA_NUM_PRD_ENTRIES)
1381 			break;
1382 
1383 		cur_prd++;
1384 		i++;
1385 	} while (1);
1386 
1387 	return status;
1388 }
1389 
1390 static bool usb3_dma_try_start(struct renesas_usb3_ep *usb3_ep,
1391 			       struct renesas_usb3_request *usb3_req)
1392 {
1393 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1394 
1395 	if (!use_dma)
1396 		return false;
1397 
1398 	if (usb3_dma_get_setting_area(usb3_ep, usb3_req)) {
1399 		usb3_pn_stop(usb3);
1400 		usb3_enable_dma_pipen(usb3);
1401 		usb3_dma_fill_prd(usb3_ep, usb3_req);
1402 		usb3_dma_kick_prd(usb3_ep);
1403 		usb3_pn_start(usb3);
1404 		return true;
1405 	}
1406 
1407 	return false;
1408 }
1409 
1410 static int usb3_dma_try_stop(struct renesas_usb3_ep *usb3_ep,
1411 			     struct renesas_usb3_request *usb3_req)
1412 {
1413 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1414 	unsigned long flags;
1415 	int status = 0;
1416 
1417 	spin_lock_irqsave(&usb3->lock, flags);
1418 	if (!usb3_ep->dma)
1419 		goto out;
1420 
1421 	if (!usb3_pn_change(usb3, usb3_ep->num))
1422 		usb3_disable_dma_pipen(usb3);
1423 	usb3_dma_stop_prd(usb3_ep);
1424 	status = usb3_dma_update_status(usb3_ep, usb3_req);
1425 	usb3_dma_put_setting_area(usb3_ep, usb3_req);
1426 
1427 out:
1428 	spin_unlock_irqrestore(&usb3->lock, flags);
1429 	return status;
1430 }
1431 
1432 static int renesas_usb3_dma_free_prd(struct renesas_usb3 *usb3,
1433 				     struct device *dev)
1434 {
1435 	int i;
1436 	struct renesas_usb3_dma *dma;
1437 
1438 	usb3_for_each_dma(usb3, dma, i) {
1439 		if (dma->prd) {
1440 			dma_free_coherent(dev, USB3_DMA_PRD_SIZE,
1441 					  dma->prd, dma->prd_dma);
1442 			dma->prd = NULL;
1443 		}
1444 	}
1445 
1446 	return 0;
1447 }
1448 
1449 static int renesas_usb3_dma_alloc_prd(struct renesas_usb3 *usb3,
1450 				      struct device *dev)
1451 {
1452 	int i;
1453 	struct renesas_usb3_dma *dma;
1454 
1455 	if (!use_dma)
1456 		return 0;
1457 
1458 	usb3_for_each_dma(usb3, dma, i) {
1459 		dma->prd = dma_alloc_coherent(dev, USB3_DMA_PRD_SIZE,
1460 					      &dma->prd_dma, GFP_KERNEL);
1461 		if (!dma->prd) {
1462 			renesas_usb3_dma_free_prd(usb3, dev);
1463 			return -ENOMEM;
1464 		}
1465 		dma->num = i + 1;
1466 	}
1467 
1468 	return 0;
1469 }
1470 
1471 static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
1472 			     struct renesas_usb3_request *usb3_req)
1473 {
1474 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1475 	struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
1476 	unsigned long flags;
1477 	int ret = -EAGAIN;
1478 	u32 enable_bits = 0;
1479 
1480 	spin_lock_irqsave(&usb3->lock, flags);
1481 	if (usb3_ep->halt || usb3_ep->started)
1482 		goto out;
1483 	if (usb3_req != usb3_req_first)
1484 		goto out;
1485 
1486 	if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1487 		goto out;
1488 
1489 	usb3_ep->started = true;
1490 
1491 	if (usb3_dma_try_start(usb3_ep, usb3_req))
1492 		goto out;
1493 
1494 	usb3_pn_start(usb3);
1495 
1496 	if (usb3_ep->dir_in) {
1497 		ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1498 		enable_bits |= PN_INT_LSTTR;
1499 	}
1500 
1501 	if (ret < 0)
1502 		enable_bits |= PN_INT_BFRDY;
1503 
1504 	if (enable_bits) {
1505 		usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1506 		usb3_enable_pipe_irq(usb3, usb3_ep->num);
1507 	}
1508 out:
1509 	spin_unlock_irqrestore(&usb3->lock, flags);
1510 }
1511 
1512 static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1513 				 gfp_t gfp_flags)
1514 {
1515 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1516 	struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1517 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1518 	unsigned long flags;
1519 
1520 	dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1521 		_req->length);
1522 
1523 	_req->status = -EINPROGRESS;
1524 	_req->actual = 0;
1525 	spin_lock_irqsave(&usb3->lock, flags);
1526 	list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1527 	spin_unlock_irqrestore(&usb3->lock, flags);
1528 
1529 	if (!usb3_ep->num)
1530 		usb3_start_pipe0(usb3_ep, usb3_req);
1531 	else
1532 		usb3_start_pipen(usb3_ep, usb3_req);
1533 
1534 	return 0;
1535 }
1536 
1537 static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1538 {
1539 	/* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1540 	usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1541 }
1542 
1543 static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1544 				     struct usb_ctrlrequest *ctrl)
1545 {
1546 	if (ctrl->wValue >= 128)
1547 		return true;	/* stall */
1548 
1549 	usb3_set_device_address(usb3, ctrl->wValue);
1550 	usb3_set_p0_con_for_no_data(usb3);
1551 
1552 	return false;
1553 }
1554 
1555 static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1556 				     void *tx_data, size_t len,
1557 				     void (*complete)(struct usb_ep *ep,
1558 						      struct usb_request *req))
1559 {
1560 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1561 
1562 	if (tx_data)
1563 		memcpy(usb3->ep0_buf, tx_data,
1564 		       min_t(size_t, len, USB3_EP0_BUF_SIZE));
1565 
1566 	usb3->ep0_req->buf = &usb3->ep0_buf;
1567 	usb3->ep0_req->length = len;
1568 	usb3->ep0_req->complete = complete;
1569 	renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1570 }
1571 
1572 static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1573 					     struct usb_request *req)
1574 {
1575 }
1576 
1577 static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1578 				    struct usb_ctrlrequest *ctrl)
1579 {
1580 	bool stall = false;
1581 	struct renesas_usb3_ep *usb3_ep;
1582 	int num;
1583 	u16 status = 0;
1584 
1585 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
1586 	case USB_RECIP_DEVICE:
1587 		if (usb3->gadget.is_selfpowered)
1588 			status |= 1 << USB_DEVICE_SELF_POWERED;
1589 		if (usb3->gadget.speed == USB_SPEED_SUPER)
1590 			status |= usb3_feature_get_un_enabled(usb3);
1591 		break;
1592 	case USB_RECIP_INTERFACE:
1593 		break;
1594 	case USB_RECIP_ENDPOINT:
1595 		num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1596 		usb3_ep = usb3_get_ep(usb3, num);
1597 		if (usb3_ep->halt)
1598 			status |= 1 << USB_ENDPOINT_HALT;
1599 		break;
1600 	default:
1601 		stall = true;
1602 		break;
1603 	}
1604 
1605 	if (!stall) {
1606 		status = cpu_to_le16(status);
1607 		dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1608 			usb_req_to_usb3_req(usb3->ep0_req));
1609 		usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
1610 					 usb3_pipe0_get_status_completion);
1611 	}
1612 
1613 	return stall;
1614 }
1615 
1616 static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1617 					struct usb_ctrlrequest *ctrl, bool set)
1618 {
1619 	bool stall = true;
1620 	u16 w_value = le16_to_cpu(ctrl->wValue);
1621 
1622 	switch (w_value) {
1623 	case USB_DEVICE_TEST_MODE:
1624 		if (!set)
1625 			break;
1626 		usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1627 		stall = false;
1628 		break;
1629 	case USB_DEVICE_U1_ENABLE:
1630 	case USB_DEVICE_U2_ENABLE:
1631 		if (usb3->gadget.speed != USB_SPEED_SUPER)
1632 			break;
1633 		if (w_value == USB_DEVICE_U1_ENABLE)
1634 			usb3_feature_u1_enable(usb3, set);
1635 		if (w_value == USB_DEVICE_U2_ENABLE)
1636 			usb3_feature_u2_enable(usb3, set);
1637 		stall = false;
1638 		break;
1639 	default:
1640 		break;
1641 	}
1642 
1643 	return stall;
1644 }
1645 
1646 static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1647 {
1648 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1649 
1650 	if (unlikely(usb3_ep->num))
1651 		return -EINVAL;
1652 
1653 	usb3_ep->halt = halt;
1654 	if (halt)
1655 		usb3_set_p0_con_stall(usb3);
1656 	else
1657 		usb3_set_p0_con_stop(usb3);
1658 
1659 	return 0;
1660 }
1661 
1662 static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1663 			    bool is_clear_feature)
1664 {
1665 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1666 	unsigned long flags;
1667 
1668 	spin_lock_irqsave(&usb3->lock, flags);
1669 	if (!usb3_pn_change(usb3, usb3_ep->num)) {
1670 		usb3_ep->halt = halt;
1671 		if (halt) {
1672 			usb3_pn_stall(usb3);
1673 		} else if (!is_clear_feature || !usb3_ep->wedge) {
1674 			usb3_pn_con_clear(usb3);
1675 			usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1676 			usb3_pn_stop(usb3);
1677 		}
1678 	}
1679 	spin_unlock_irqrestore(&usb3->lock, flags);
1680 
1681 	return 0;
1682 }
1683 
1684 static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1685 			 bool is_clear_feature)
1686 {
1687 	int ret = 0;
1688 
1689 	if (halt && usb3_ep->started)
1690 		return -EAGAIN;
1691 
1692 	if (usb3_ep->num)
1693 		ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1694 	else
1695 		ret = usb3_set_halt_p0(usb3_ep, halt);
1696 
1697 	return ret;
1698 }
1699 
1700 static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1701 					  struct usb_ctrlrequest *ctrl,
1702 					  bool set)
1703 {
1704 	int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1705 	struct renesas_usb3_ep *usb3_ep;
1706 	struct renesas_usb3_request *usb3_req;
1707 
1708 	if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1709 		return true;	/* stall */
1710 
1711 	usb3_ep = usb3_get_ep(usb3, num);
1712 	usb3_set_halt(usb3_ep, set, true);
1713 
1714 	/* Restarts a queue if clear feature */
1715 	if (!set) {
1716 		usb3_ep->started = false;
1717 		usb3_req = usb3_get_request(usb3_ep);
1718 		if (usb3_req)
1719 			usb3_start_pipen(usb3_ep, usb3_req);
1720 	}
1721 
1722 	return false;
1723 }
1724 
1725 static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1726 				 struct usb_ctrlrequest *ctrl, bool set)
1727 {
1728 	bool stall = false;
1729 
1730 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
1731 	case USB_RECIP_DEVICE:
1732 		stall = usb3_std_req_feature_device(usb3, ctrl, set);
1733 		break;
1734 	case USB_RECIP_INTERFACE:
1735 		break;
1736 	case USB_RECIP_ENDPOINT:
1737 		stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1738 		break;
1739 	default:
1740 		stall = true;
1741 		break;
1742 	}
1743 
1744 	if (!stall)
1745 		usb3_set_p0_con_for_no_data(usb3);
1746 
1747 	return stall;
1748 }
1749 
1750 static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1751 					  struct usb_request *req)
1752 {
1753 	/* TODO */
1754 }
1755 
1756 static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1757 				 struct usb_ctrlrequest *ctrl)
1758 {
1759 	u16 w_length = le16_to_cpu(ctrl->wLength);
1760 
1761 	if (w_length != 6)
1762 		return true;	/* stall */
1763 
1764 	dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1765 		usb_req_to_usb3_req(usb3->ep0_req));
1766 	usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1767 
1768 	return false;
1769 }
1770 
1771 static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1772 					   struct usb_ctrlrequest *ctrl)
1773 {
1774 	if (ctrl->wValue > 0)
1775 		usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1776 	else
1777 		usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1778 
1779 	return false;
1780 }
1781 
1782 /**
1783  * usb3_handle_standard_request - handle some standard requests
1784  * @usb3: the renesas_usb3 pointer
1785  * @ctrl: a pointer of setup data
1786  *
1787  * Returns true if this function handled a standard request
1788  */
1789 static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1790 					 struct usb_ctrlrequest *ctrl)
1791 {
1792 	bool ret = false;
1793 	bool stall = false;
1794 
1795 	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1796 		switch (ctrl->bRequest) {
1797 		case USB_REQ_SET_ADDRESS:
1798 			stall = usb3_std_req_set_address(usb3, ctrl);
1799 			ret = true;
1800 			break;
1801 		case USB_REQ_GET_STATUS:
1802 			stall = usb3_std_req_get_status(usb3, ctrl);
1803 			ret = true;
1804 			break;
1805 		case USB_REQ_CLEAR_FEATURE:
1806 			stall = usb3_std_req_feature(usb3, ctrl, false);
1807 			ret = true;
1808 			break;
1809 		case USB_REQ_SET_FEATURE:
1810 			stall = usb3_std_req_feature(usb3, ctrl, true);
1811 			ret = true;
1812 			break;
1813 		case USB_REQ_SET_SEL:
1814 			stall = usb3_std_req_set_sel(usb3, ctrl);
1815 			ret = true;
1816 			break;
1817 		case USB_REQ_SET_ISOCH_DELAY:
1818 			/* This hardware doesn't support Isochronous xfer */
1819 			stall = true;
1820 			ret = true;
1821 			break;
1822 		case USB_REQ_SET_CONFIGURATION:
1823 			usb3_std_req_set_configuration(usb3, ctrl);
1824 			break;
1825 		default:
1826 			break;
1827 		}
1828 	}
1829 
1830 	if (stall)
1831 		usb3_set_p0_con_stall(usb3);
1832 
1833 	return ret;
1834 }
1835 
1836 static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1837 {
1838 	usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1839 
1840 	return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1841 }
1842 
1843 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1844 {
1845 	struct usb_ctrlrequest ctrl;
1846 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1847 
1848 	/* Call giveback function if previous transfer is not completed */
1849 	if (usb3_ep->started)
1850 		usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1851 				  -ECONNRESET);
1852 
1853 	usb3_p0_con_clear_buffer(usb3);
1854 	usb3_get_setup_data(usb3, &ctrl);
1855 	if (!usb3_handle_standard_request(usb3, &ctrl))
1856 		if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1857 			usb3_set_p0_con_stall(usb3);
1858 }
1859 
1860 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1861 {
1862 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1863 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1864 
1865 	if (!usb3_req)
1866 		return;
1867 
1868 	usb3_p0_xfer(usb3_ep, usb3_req);
1869 }
1870 
1871 static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1872 {
1873 	u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1874 
1875 	p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1876 	usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1877 	if (p0_int_sta & P0_INT_STSED)
1878 		usb3_irq_epc_pipe0_status_end(usb3);
1879 	if (p0_int_sta & P0_INT_SETUP)
1880 		usb3_irq_epc_pipe0_setup(usb3);
1881 	if (p0_int_sta & P0_INT_BFRDY)
1882 		usb3_irq_epc_pipe0_bfrdy(usb3);
1883 }
1884 
1885 static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1886 				    struct renesas_usb3_ep *usb3_ep,
1887 				    struct renesas_usb3_request *usb3_req,
1888 				    int status)
1889 {
1890 	unsigned long flags;
1891 
1892 	spin_lock_irqsave(&usb3->lock, flags);
1893 	if (usb3_pn_change(usb3, usb3_ep->num))
1894 		usb3_pn_stop(usb3);
1895 	spin_unlock_irqrestore(&usb3->lock, flags);
1896 
1897 	usb3_disable_pipe_irq(usb3, usb3_ep->num);
1898 	usb3_request_done(usb3_ep, usb3_req, status);
1899 
1900 	/* get next usb3_req */
1901 	usb3_req = usb3_get_request(usb3_ep);
1902 	if (usb3_req)
1903 		usb3_start_pipen(usb3_ep, usb3_req);
1904 }
1905 
1906 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1907 {
1908 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1909 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1910 
1911 	if (!usb3_req)
1912 		return;
1913 
1914 	if (usb3_ep->dir_in) {
1915 		dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1916 			__func__, usb3_req->req.length, usb3_req->req.actual);
1917 		usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1918 	}
1919 }
1920 
1921 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1922 {
1923 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1924 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1925 	bool done = false;
1926 
1927 	if (!usb3_req)
1928 		return;
1929 
1930 	spin_lock(&usb3->lock);
1931 	if (usb3_pn_change(usb3, num))
1932 		goto out;
1933 
1934 	if (usb3_ep->dir_in) {
1935 		/* Do not stop the IN pipe here to detect LSTTR interrupt */
1936 		if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1937 			usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1938 	} else {
1939 		if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1940 			done = true;
1941 	}
1942 
1943 out:
1944 	/* need to unlock because usb3_request_done_pipen() locks it */
1945 	spin_unlock(&usb3->lock);
1946 
1947 	if (done)
1948 		usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1949 }
1950 
1951 static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1952 {
1953 	u32 pn_int_sta;
1954 
1955 	spin_lock(&usb3->lock);
1956 	if (usb3_pn_change(usb3, num) < 0) {
1957 		spin_unlock(&usb3->lock);
1958 		return;
1959 	}
1960 
1961 	pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1962 	pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1963 	usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1964 	spin_unlock(&usb3->lock);
1965 	if (pn_int_sta & PN_INT_LSTTR)
1966 		usb3_irq_epc_pipen_lsttr(usb3, num);
1967 	if (pn_int_sta & PN_INT_BFRDY)
1968 		usb3_irq_epc_pipen_bfrdy(usb3, num);
1969 }
1970 
1971 static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1972 {
1973 	int i;
1974 
1975 	for (i = 0; i < usb3->num_usb3_eps; i++) {
1976 		if (int_sta_2 & USB_INT_2_PIPE(i)) {
1977 			if (!i)
1978 				usb3_irq_epc_pipe0(usb3);
1979 			else
1980 				usb3_irq_epc_pipen(usb3, i);
1981 		}
1982 	}
1983 }
1984 
1985 static void usb3_irq_idmon_change(struct renesas_usb3 *usb3)
1986 {
1987 	usb3_check_id(usb3);
1988 }
1989 
1990 static void usb3_irq_otg_int(struct renesas_usb3 *usb3, u32 otg_int_sta)
1991 {
1992 	if (otg_int_sta & USB_OTG_IDMON)
1993 		usb3_irq_idmon_change(usb3);
1994 }
1995 
1996 static void usb3_irq_epc(struct renesas_usb3 *usb3)
1997 {
1998 	u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
1999 	u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
2000 	u32 otg_int_sta = usb3_read(usb3, USB3_USB_OTG_INT_STA);
2001 
2002 	int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
2003 	if (int_sta_1) {
2004 		usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
2005 		usb3_irq_epc_int_1(usb3, int_sta_1);
2006 	}
2007 
2008 	int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
2009 	if (int_sta_2)
2010 		usb3_irq_epc_int_2(usb3, int_sta_2);
2011 
2012 	otg_int_sta &= usb3_read(usb3, USB3_USB_OTG_INT_ENA);
2013 	if (otg_int_sta) {
2014 		usb3_write(usb3, otg_int_sta, USB3_USB_OTG_INT_STA);
2015 		usb3_irq_otg_int(usb3, otg_int_sta);
2016 	}
2017 }
2018 
2019 static void usb3_irq_dma_int(struct renesas_usb3 *usb3, u32 dma_sta)
2020 {
2021 	struct renesas_usb3_ep *usb3_ep;
2022 	struct renesas_usb3_request *usb3_req;
2023 	int i, status;
2024 
2025 	for (i = 0; i < usb3->num_usb3_eps; i++) {
2026 		if (!(dma_sta & DMA_INT(i)))
2027 			continue;
2028 
2029 		usb3_ep = usb3_get_ep(usb3, i);
2030 		if (!(usb3_read(usb3, USB3_AXI_INT_STA) &
2031 		    AXI_INT_PRDEN_CLR_STA(usb3_ep->dma->num)))
2032 			continue;
2033 
2034 		usb3_req = usb3_get_request(usb3_ep);
2035 		status = usb3_dma_try_stop(usb3_ep, usb3_req);
2036 		usb3_request_done_pipen(usb3, usb3_ep, usb3_req, status);
2037 	}
2038 }
2039 
2040 static void usb3_irq_dma(struct renesas_usb3 *usb3)
2041 {
2042 	u32 dma_sta = usb3_read(usb3, USB3_DMA_INT_STA);
2043 
2044 	dma_sta &= usb3_read(usb3, USB3_DMA_INT_ENA);
2045 	if (dma_sta) {
2046 		usb3_write(usb3, dma_sta, USB3_DMA_INT_STA);
2047 		usb3_irq_dma_int(usb3, dma_sta);
2048 	}
2049 }
2050 
2051 static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
2052 {
2053 	struct renesas_usb3 *usb3 = _usb3;
2054 	irqreturn_t ret = IRQ_NONE;
2055 	u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
2056 
2057 	if (axi_int_sta & AXI_INT_DMAINT) {
2058 		usb3_irq_dma(usb3);
2059 		ret = IRQ_HANDLED;
2060 	}
2061 
2062 	if (axi_int_sta & AXI_INT_EPCINT) {
2063 		usb3_irq_epc(usb3);
2064 		ret = IRQ_HANDLED;
2065 	}
2066 
2067 	return ret;
2068 }
2069 
2070 static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
2071 			      const struct usb_endpoint_descriptor *desc)
2072 {
2073 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2074 	u32 val = 0;
2075 
2076 	val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
2077 	val |= PN_MOD_TYPE(usb_endpoint_type(desc));
2078 	val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
2079 	usb3_write(usb3, val, USB3_PN_MOD);
2080 }
2081 
2082 static u32 usb3_calc_ramarea(int ram_size)
2083 {
2084 	WARN_ON(ram_size > SZ_16K);
2085 
2086 	if (ram_size <= SZ_1K)
2087 		return PN_RAMMAP_RAMAREA_1KB;
2088 	else if (ram_size <= SZ_2K)
2089 		return PN_RAMMAP_RAMAREA_2KB;
2090 	else if (ram_size <= SZ_4K)
2091 		return PN_RAMMAP_RAMAREA_4KB;
2092 	else if (ram_size <= SZ_8K)
2093 		return PN_RAMMAP_RAMAREA_8KB;
2094 	else
2095 		return PN_RAMMAP_RAMAREA_16KB;
2096 }
2097 
2098 static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
2099 				const struct usb_endpoint_descriptor *desc)
2100 {
2101 	int i;
2102 	static const u32 max_packet_array[] = {8, 16, 32, 64, 512};
2103 	u32 mpkt = PN_RAMMAP_MPKT(1024);
2104 
2105 	for (i = 0; i < ARRAY_SIZE(max_packet_array); i++) {
2106 		if (usb_endpoint_maxp(desc) <= max_packet_array[i])
2107 			mpkt = PN_RAMMAP_MPKT(max_packet_array[i]);
2108 	}
2109 
2110 	return usb3_ep->rammap_val | mpkt;
2111 }
2112 
2113 static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
2114 			      const struct usb_endpoint_descriptor *desc)
2115 {
2116 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2117 	unsigned long flags;
2118 
2119 	usb3_ep->dir_in = usb_endpoint_dir_in(desc);
2120 
2121 	spin_lock_irqsave(&usb3->lock, flags);
2122 	if (!usb3_pn_change(usb3, usb3_ep->num)) {
2123 		usb3_write_pn_mod(usb3_ep, desc);
2124 		usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
2125 			   USB3_PN_RAMMAP);
2126 		usb3_pn_con_clear(usb3);
2127 		usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
2128 	}
2129 	spin_unlock_irqrestore(&usb3->lock, flags);
2130 
2131 	return 0;
2132 }
2133 
2134 static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
2135 {
2136 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2137 	unsigned long flags;
2138 
2139 	usb3_ep->halt = false;
2140 
2141 	spin_lock_irqsave(&usb3->lock, flags);
2142 	if (!usb3_pn_change(usb3, usb3_ep->num)) {
2143 		usb3_write(usb3, 0, USB3_PN_INT_ENA);
2144 		usb3_write(usb3, 0, USB3_PN_RAMMAP);
2145 		usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
2146 	}
2147 	spin_unlock_irqrestore(&usb3->lock, flags);
2148 
2149 	return 0;
2150 }
2151 
2152 /*------- usb_ep_ops -----------------------------------------------------*/
2153 static int renesas_usb3_ep_enable(struct usb_ep *_ep,
2154 				  const struct usb_endpoint_descriptor *desc)
2155 {
2156 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2157 
2158 	return usb3_enable_pipe_n(usb3_ep, desc);
2159 }
2160 
2161 static int renesas_usb3_ep_disable(struct usb_ep *_ep)
2162 {
2163 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2164 	struct renesas_usb3_request *usb3_req;
2165 
2166 	do {
2167 		usb3_req = usb3_get_request(usb3_ep);
2168 		if (!usb3_req)
2169 			break;
2170 		usb3_dma_try_stop(usb3_ep, usb3_req);
2171 		usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
2172 	} while (1);
2173 
2174 	return usb3_disable_pipe_n(usb3_ep);
2175 }
2176 
2177 static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
2178 {
2179 	struct renesas_usb3_request *usb3_req;
2180 
2181 	usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
2182 	if (!usb3_req)
2183 		return NULL;
2184 
2185 	INIT_LIST_HEAD(&usb3_req->queue);
2186 
2187 	return &usb3_req->req;
2188 }
2189 
2190 static void __renesas_usb3_ep_free_request(struct usb_request *_req)
2191 {
2192 	struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
2193 
2194 	kfree(usb3_req);
2195 }
2196 
2197 static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
2198 							 gfp_t gfp_flags)
2199 {
2200 	return __renesas_usb3_ep_alloc_request(gfp_flags);
2201 }
2202 
2203 static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
2204 					 struct usb_request *_req)
2205 {
2206 	__renesas_usb3_ep_free_request(_req);
2207 }
2208 
2209 static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2210 {
2211 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2212 	struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
2213 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2214 
2215 	dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
2216 		_req->length);
2217 
2218 	usb3_dma_try_stop(usb3_ep, usb3_req);
2219 	usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
2220 
2221 	return 0;
2222 }
2223 
2224 static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
2225 {
2226 	return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
2227 }
2228 
2229 static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
2230 {
2231 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2232 
2233 	usb3_ep->wedge = true;
2234 	return usb3_set_halt(usb3_ep, true, false);
2235 }
2236 
2237 static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
2238 {
2239 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
2240 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
2241 	unsigned long flags;
2242 
2243 	if (usb3_ep->num) {
2244 		spin_lock_irqsave(&usb3->lock, flags);
2245 		if (!usb3_pn_change(usb3, usb3_ep->num)) {
2246 			usb3_pn_con_clear(usb3);
2247 			usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
2248 		}
2249 		spin_unlock_irqrestore(&usb3->lock, flags);
2250 	} else {
2251 		usb3_p0_con_clear_buffer(usb3);
2252 	}
2253 }
2254 
2255 static const struct usb_ep_ops renesas_usb3_ep_ops = {
2256 	.enable		= renesas_usb3_ep_enable,
2257 	.disable	= renesas_usb3_ep_disable,
2258 
2259 	.alloc_request	= renesas_usb3_ep_alloc_request,
2260 	.free_request	= renesas_usb3_ep_free_request,
2261 
2262 	.queue		= renesas_usb3_ep_queue,
2263 	.dequeue	= renesas_usb3_ep_dequeue,
2264 
2265 	.set_halt	= renesas_usb3_ep_set_halt,
2266 	.set_wedge	= renesas_usb3_ep_set_wedge,
2267 	.fifo_flush	= renesas_usb3_ep_fifo_flush,
2268 };
2269 
2270 /*------- usb_gadget_ops -------------------------------------------------*/
2271 static int renesas_usb3_start(struct usb_gadget *gadget,
2272 			      struct usb_gadget_driver *driver)
2273 {
2274 	struct renesas_usb3 *usb3;
2275 
2276 	if (!driver || driver->max_speed < USB_SPEED_FULL ||
2277 	    !driver->setup)
2278 		return -EINVAL;
2279 
2280 	usb3 = gadget_to_renesas_usb3(gadget);
2281 
2282 	/* hook up the driver */
2283 	usb3->driver = driver;
2284 
2285 	if (usb3->phy)
2286 		phy_init(usb3->phy);
2287 
2288 	pm_runtime_get_sync(usb3_to_dev(usb3));
2289 
2290 	renesas_usb3_init_controller(usb3);
2291 
2292 	return 0;
2293 }
2294 
2295 static int renesas_usb3_stop(struct usb_gadget *gadget)
2296 {
2297 	struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
2298 
2299 	usb3->softconnect = false;
2300 	usb3->gadget.speed = USB_SPEED_UNKNOWN;
2301 	usb3->driver = NULL;
2302 	renesas_usb3_stop_controller(usb3);
2303 
2304 	if (usb3->phy)
2305 		phy_exit(usb3->phy);
2306 
2307 	pm_runtime_put(usb3_to_dev(usb3));
2308 
2309 	return 0;
2310 }
2311 
2312 static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
2313 {
2314 	return -EOPNOTSUPP;
2315 }
2316 
2317 static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
2318 {
2319 	struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
2320 
2321 	usb3->softconnect = !!is_on;
2322 
2323 	return 0;
2324 }
2325 
2326 static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
2327 {
2328 	gadget->is_selfpowered = !!is_self;
2329 
2330 	return 0;
2331 }
2332 
2333 static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
2334 	.get_frame		= renesas_usb3_get_frame,
2335 	.udc_start		= renesas_usb3_start,
2336 	.udc_stop		= renesas_usb3_stop,
2337 	.pullup			= renesas_usb3_pullup,
2338 	.set_selfpowered	= renesas_usb3_set_selfpowered,
2339 };
2340 
2341 static enum usb_role renesas_usb3_role_switch_get(struct device *dev)
2342 {
2343 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2344 	enum usb_role cur_role;
2345 
2346 	pm_runtime_get_sync(dev);
2347 	cur_role = usb3_is_host(usb3) ? USB_ROLE_HOST : USB_ROLE_DEVICE;
2348 	pm_runtime_put(dev);
2349 
2350 	return cur_role;
2351 }
2352 
2353 static void handle_ext_role_switch_states(struct device *dev,
2354 					    enum usb_role role)
2355 {
2356 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2357 	struct device *host = usb3->host_dev;
2358 	enum usb_role cur_role = renesas_usb3_role_switch_get(dev);
2359 
2360 	switch (role) {
2361 	case USB_ROLE_NONE:
2362 		usb3->connection_state = USB_ROLE_NONE;
2363 		if (usb3->driver)
2364 			usb3_disconnect(usb3);
2365 		usb3_vbus_out(usb3, false);
2366 		break;
2367 	case USB_ROLE_DEVICE:
2368 		if (usb3->connection_state == USB_ROLE_NONE) {
2369 			usb3->connection_state = USB_ROLE_DEVICE;
2370 			usb3_set_mode(usb3, false);
2371 			if (usb3->driver)
2372 				usb3_connect(usb3);
2373 		} else if (cur_role == USB_ROLE_HOST)  {
2374 			device_release_driver(host);
2375 			usb3_set_mode(usb3, false);
2376 			if (usb3->driver)
2377 				usb3_connect(usb3);
2378 		}
2379 		usb3_vbus_out(usb3, false);
2380 		break;
2381 	case USB_ROLE_HOST:
2382 		if (usb3->connection_state == USB_ROLE_NONE) {
2383 			if (usb3->driver)
2384 				usb3_disconnect(usb3);
2385 
2386 			usb3->connection_state = USB_ROLE_HOST;
2387 			usb3_set_mode(usb3, true);
2388 			usb3_vbus_out(usb3, true);
2389 			if (device_attach(host) < 0)
2390 				dev_err(dev, "device_attach(host) failed\n");
2391 		} else if (cur_role == USB_ROLE_DEVICE) {
2392 			usb3_disconnect(usb3);
2393 			/* Must set the mode before device_attach of the host */
2394 			usb3_set_mode(usb3, true);
2395 			/* This device_attach() might sleep */
2396 			if (device_attach(host) < 0)
2397 				dev_err(dev, "device_attach(host) failed\n");
2398 		}
2399 		break;
2400 	default:
2401 		break;
2402 	}
2403 }
2404 
2405 static void handle_role_switch_states(struct device *dev,
2406 					    enum usb_role role)
2407 {
2408 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2409 	struct device *host = usb3->host_dev;
2410 	enum usb_role cur_role = renesas_usb3_role_switch_get(dev);
2411 
2412 	if (cur_role == USB_ROLE_HOST && role == USB_ROLE_DEVICE) {
2413 		device_release_driver(host);
2414 		usb3_set_mode(usb3, false);
2415 	} else if (cur_role == USB_ROLE_DEVICE && role == USB_ROLE_HOST) {
2416 		/* Must set the mode before device_attach of the host */
2417 		usb3_set_mode(usb3, true);
2418 		/* This device_attach() might sleep */
2419 		if (device_attach(host) < 0)
2420 			dev_err(dev, "device_attach(host) failed\n");
2421 	}
2422 }
2423 
2424 static int renesas_usb3_role_switch_set(struct device *dev,
2425 					enum usb_role role)
2426 {
2427 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2428 
2429 	pm_runtime_get_sync(dev);
2430 
2431 	if (usb3->role_sw_by_connector)
2432 		handle_ext_role_switch_states(dev, role);
2433 	else
2434 		handle_role_switch_states(dev, role);
2435 
2436 	pm_runtime_put(dev);
2437 
2438 	return 0;
2439 }
2440 
2441 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
2442 			  const char *buf, size_t count)
2443 {
2444 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2445 	bool new_mode_is_host;
2446 
2447 	if (!usb3->driver)
2448 		return -ENODEV;
2449 
2450 	if (usb3->forced_b_device)
2451 		return -EBUSY;
2452 
2453 	if (!strncmp(buf, "host", strlen("host")))
2454 		new_mode_is_host = true;
2455 	else if (!strncmp(buf, "peripheral", strlen("peripheral")))
2456 		new_mode_is_host = false;
2457 	else
2458 		return -EINVAL;
2459 
2460 	if (new_mode_is_host == usb3_is_host(usb3))
2461 		return -EINVAL;
2462 
2463 	usb3_mode_config(usb3, new_mode_is_host, usb3_is_a_device(usb3));
2464 
2465 	return count;
2466 }
2467 
2468 static ssize_t role_show(struct device *dev, struct device_attribute *attr,
2469 			 char *buf)
2470 {
2471 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2472 
2473 	if (!usb3->driver)
2474 		return -ENODEV;
2475 
2476 	return sprintf(buf, "%s\n", usb3_is_host(usb3) ? "host" : "peripheral");
2477 }
2478 static DEVICE_ATTR_RW(role);
2479 
2480 static int renesas_usb3_b_device_show(struct seq_file *s, void *unused)
2481 {
2482 	struct renesas_usb3 *usb3 = s->private;
2483 
2484 	seq_printf(s, "%d\n", usb3->forced_b_device);
2485 
2486 	return 0;
2487 }
2488 
2489 static int renesas_usb3_b_device_open(struct inode *inode, struct file *file)
2490 {
2491 	return single_open(file, renesas_usb3_b_device_show, inode->i_private);
2492 }
2493 
2494 static ssize_t renesas_usb3_b_device_write(struct file *file,
2495 					   const char __user *ubuf,
2496 					   size_t count, loff_t *ppos)
2497 {
2498 	struct seq_file *s = file->private_data;
2499 	struct renesas_usb3 *usb3 = s->private;
2500 	char buf[32];
2501 
2502 	if (!usb3->driver)
2503 		return -ENODEV;
2504 
2505 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2506 		return -EFAULT;
2507 
2508 	usb3->start_to_connect = false;
2509 	if (usb3->workaround_for_vbus && usb3->forced_b_device &&
2510 	    !strncmp(buf, "2", 1))
2511 		usb3->start_to_connect = true;
2512 	else if (!strncmp(buf, "1", 1))
2513 		usb3->forced_b_device = true;
2514 	else
2515 		usb3->forced_b_device = false;
2516 
2517 	if (usb3->workaround_for_vbus)
2518 		usb3_disconnect(usb3);
2519 
2520 	/* Let this driver call usb3_connect() if needed */
2521 	usb3_check_id(usb3);
2522 
2523 	return count;
2524 }
2525 
2526 static const struct file_operations renesas_usb3_b_device_fops = {
2527 	.open = renesas_usb3_b_device_open,
2528 	.write = renesas_usb3_b_device_write,
2529 	.read = seq_read,
2530 	.llseek = seq_lseek,
2531 	.release = single_release,
2532 };
2533 
2534 static void renesas_usb3_debugfs_init(struct renesas_usb3 *usb3,
2535 				      struct device *dev)
2536 {
2537 	usb3->dentry = debugfs_create_dir(dev_name(dev), NULL);
2538 
2539 	debugfs_create_file("b_device", 0644, usb3->dentry, usb3,
2540 			    &renesas_usb3_b_device_fops);
2541 }
2542 
2543 /*------- platform_driver ------------------------------------------------*/
2544 static int renesas_usb3_remove(struct platform_device *pdev)
2545 {
2546 	struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
2547 
2548 	debugfs_remove_recursive(usb3->dentry);
2549 	device_remove_file(&pdev->dev, &dev_attr_role);
2550 
2551 	usb_role_switch_unregister(usb3->role_sw);
2552 
2553 	usb_del_gadget_udc(&usb3->gadget);
2554 	renesas_usb3_dma_free_prd(usb3, &pdev->dev);
2555 
2556 	__renesas_usb3_ep_free_request(usb3->ep0_req);
2557 	pm_runtime_disable(&pdev->dev);
2558 
2559 	return 0;
2560 }
2561 
2562 static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
2563 				const struct renesas_usb3_priv *priv)
2564 {
2565 	struct renesas_usb3_ep *usb3_ep;
2566 	int i;
2567 
2568 	/* calculate num_usb3_eps from renesas_usb3_priv */
2569 	usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
2570 			     priv->ramsize_per_pipe + 1;
2571 
2572 	if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
2573 		usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
2574 
2575 	usb3->usb3_ep = devm_kcalloc(dev,
2576 				     usb3->num_usb3_eps, sizeof(*usb3_ep),
2577 				     GFP_KERNEL);
2578 	if (!usb3->usb3_ep)
2579 		return -ENOMEM;
2580 
2581 	dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
2582 	/*
2583 	 * This driver prepares pipes as follows:
2584 	 *  - odd pipes = IN pipe
2585 	 *  - even pipes = OUT pipe (except pipe 0)
2586 	 */
2587 	usb3_for_each_ep(usb3_ep, usb3, i) {
2588 		snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
2589 		usb3_ep->usb3 = usb3;
2590 		usb3_ep->num = i;
2591 		usb3_ep->ep.name = usb3_ep->ep_name;
2592 		usb3_ep->ep.ops = &renesas_usb3_ep_ops;
2593 		INIT_LIST_HEAD(&usb3_ep->queue);
2594 		INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
2595 		if (!i) {
2596 			/* for control pipe */
2597 			usb3->gadget.ep0 = &usb3_ep->ep;
2598 			usb_ep_set_maxpacket_limit(&usb3_ep->ep,
2599 						USB3_EP0_SS_MAX_PACKET_SIZE);
2600 			usb3_ep->ep.caps.type_control = true;
2601 			usb3_ep->ep.caps.dir_in = true;
2602 			usb3_ep->ep.caps.dir_out = true;
2603 			continue;
2604 		}
2605 
2606 		/* for bulk or interrupt pipe */
2607 		usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
2608 		list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
2609 		usb3_ep->ep.caps.type_bulk = true;
2610 		usb3_ep->ep.caps.type_int = true;
2611 		if (i & 1)
2612 			usb3_ep->ep.caps.dir_in = true;
2613 		else
2614 			usb3_ep->ep.caps.dir_out = true;
2615 	}
2616 
2617 	return 0;
2618 }
2619 
2620 static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
2621 				  const struct renesas_usb3_priv *priv)
2622 {
2623 	struct renesas_usb3_ep *usb3_ep;
2624 	int i;
2625 	u32 ramif[2], basead[2];	/* index 0 = for IN pipes */
2626 	u32 *cur_ramif, *cur_basead;
2627 	u32 val;
2628 
2629 	memset(ramif, 0, sizeof(ramif));
2630 	memset(basead, 0, sizeof(basead));
2631 
2632 	/*
2633 	 * This driver prepares pipes as follows:
2634 	 *  - all pipes = the same size as "ramsize_per_pipe"
2635 	 * Please refer to the "Method of Specifying RAM Mapping"
2636 	 */
2637 	usb3_for_each_ep(usb3_ep, usb3, i) {
2638 		if (!i)
2639 			continue;	/* out of scope if ep num = 0 */
2640 		if (usb3_ep->ep.caps.dir_in) {
2641 			cur_ramif = &ramif[0];
2642 			cur_basead = &basead[0];
2643 		} else {
2644 			cur_ramif = &ramif[1];
2645 			cur_basead = &basead[1];
2646 		}
2647 
2648 		if (*cur_basead > priv->ramsize_per_ramif)
2649 			continue;	/* out of memory for IN or OUT pipe */
2650 
2651 		/* calculate rammap_val */
2652 		val = PN_RAMMAP_RAMIF(*cur_ramif);
2653 		val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
2654 		val |= PN_RAMMAP_BASEAD(*cur_basead);
2655 		usb3_ep->rammap_val = val;
2656 
2657 		dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
2658 			i, val, *cur_ramif, *cur_basead);
2659 
2660 		/* update current ramif */
2661 		if (*cur_ramif + 1 == priv->num_ramif) {
2662 			*cur_ramif = 0;
2663 			*cur_basead += priv->ramsize_per_pipe;
2664 		} else {
2665 			(*cur_ramif)++;
2666 		}
2667 	}
2668 }
2669 
2670 static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795_es1 = {
2671 	.ramsize_per_ramif = SZ_16K,
2672 	.num_ramif = 2,
2673 	.ramsize_per_pipe = SZ_4K,
2674 	.workaround_for_vbus = true,
2675 };
2676 
2677 static const struct renesas_usb3_priv renesas_usb3_priv_gen3 = {
2678 	.ramsize_per_ramif = SZ_16K,
2679 	.num_ramif = 4,
2680 	.ramsize_per_pipe = SZ_4K,
2681 };
2682 
2683 static const struct renesas_usb3_priv renesas_usb3_priv_r8a77990 = {
2684 	.ramsize_per_ramif = SZ_16K,
2685 	.num_ramif = 4,
2686 	.ramsize_per_pipe = SZ_4K,
2687 	.workaround_for_vbus = true,
2688 };
2689 
2690 static const struct of_device_id usb3_of_match[] = {
2691 	{
2692 		.compatible = "renesas,r8a7795-usb3-peri",
2693 		.data = &renesas_usb3_priv_gen3,
2694 	},
2695 	{
2696 		.compatible = "renesas,rcar-gen3-usb3-peri",
2697 		.data = &renesas_usb3_priv_gen3,
2698 	},
2699 	{ },
2700 };
2701 MODULE_DEVICE_TABLE(of, usb3_of_match);
2702 
2703 static const struct soc_device_attribute renesas_usb3_quirks_match[] = {
2704 	{
2705 		.soc_id = "r8a774c0",
2706 		.data = &renesas_usb3_priv_r8a77990,
2707 	},
2708 	{
2709 		.soc_id = "r8a7795", .revision = "ES1.*",
2710 		.data = &renesas_usb3_priv_r8a7795_es1,
2711 	},
2712 	{
2713 		.soc_id = "r8a77990",
2714 		.data = &renesas_usb3_priv_r8a77990,
2715 	},
2716 	{ /* sentinel */ },
2717 };
2718 
2719 static const unsigned int renesas_usb3_cable[] = {
2720 	EXTCON_USB,
2721 	EXTCON_USB_HOST,
2722 	EXTCON_NONE,
2723 };
2724 
2725 static struct usb_role_switch_desc renesas_usb3_role_switch_desc = {
2726 	.set = renesas_usb3_role_switch_set,
2727 	.get = renesas_usb3_role_switch_get,
2728 	.allow_userspace_control = true,
2729 };
2730 
2731 static int renesas_usb3_probe(struct platform_device *pdev)
2732 {
2733 	struct renesas_usb3 *usb3;
2734 	struct resource *res;
2735 	int irq, ret;
2736 	const struct renesas_usb3_priv *priv;
2737 	const struct soc_device_attribute *attr;
2738 
2739 	attr = soc_device_match(renesas_usb3_quirks_match);
2740 	if (attr)
2741 		priv = attr->data;
2742 	else
2743 		priv = of_device_get_match_data(&pdev->dev);
2744 
2745 	irq = platform_get_irq(pdev, 0);
2746 	if (irq < 0) {
2747 		dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
2748 		return irq;
2749 	}
2750 
2751 	usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
2752 	if (!usb3)
2753 		return -ENOMEM;
2754 
2755 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2756 	usb3->reg = devm_ioremap_resource(&pdev->dev, res);
2757 	if (IS_ERR(usb3->reg))
2758 		return PTR_ERR(usb3->reg);
2759 
2760 	platform_set_drvdata(pdev, usb3);
2761 	spin_lock_init(&usb3->lock);
2762 
2763 	usb3->gadget.ops = &renesas_usb3_gadget_ops;
2764 	usb3->gadget.name = udc_name;
2765 	usb3->gadget.max_speed = USB_SPEED_SUPER;
2766 	INIT_LIST_HEAD(&usb3->gadget.ep_list);
2767 	ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
2768 	if (ret < 0)
2769 		return ret;
2770 	renesas_usb3_init_ram(usb3, &pdev->dev, priv);
2771 
2772 	ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
2773 			       dev_name(&pdev->dev), usb3);
2774 	if (ret < 0)
2775 		return ret;
2776 
2777 	INIT_WORK(&usb3->extcon_work, renesas_usb3_extcon_work);
2778 	usb3->extcon = devm_extcon_dev_allocate(&pdev->dev, renesas_usb3_cable);
2779 	if (IS_ERR(usb3->extcon))
2780 		return PTR_ERR(usb3->extcon);
2781 
2782 	ret = devm_extcon_dev_register(&pdev->dev, usb3->extcon);
2783 	if (ret < 0) {
2784 		dev_err(&pdev->dev, "Failed to register extcon\n");
2785 		return ret;
2786 	}
2787 
2788 	/* for ep0 handling */
2789 	usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
2790 	if (!usb3->ep0_req)
2791 		return -ENOMEM;
2792 
2793 	ret = renesas_usb3_dma_alloc_prd(usb3, &pdev->dev);
2794 	if (ret < 0)
2795 		goto err_alloc_prd;
2796 
2797 	/*
2798 	 * This is optional. So, if this driver cannot get a phy,
2799 	 * this driver will not handle a phy anymore.
2800 	 */
2801 	usb3->phy = devm_phy_optional_get(&pdev->dev, "usb");
2802 	if (IS_ERR(usb3->phy)) {
2803 		ret = PTR_ERR(usb3->phy);
2804 		goto err_add_udc;
2805 	}
2806 
2807 	pm_runtime_enable(&pdev->dev);
2808 	ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
2809 	if (ret < 0)
2810 		goto err_add_udc;
2811 
2812 	ret = device_create_file(&pdev->dev, &dev_attr_role);
2813 	if (ret < 0)
2814 		goto err_dev_create;
2815 
2816 	if (device_property_read_bool(&pdev->dev, "usb-role-switch")) {
2817 		usb3->role_sw_by_connector = true;
2818 		renesas_usb3_role_switch_desc.fwnode = dev_fwnode(&pdev->dev);
2819 	}
2820 
2821 	INIT_WORK(&usb3->role_work, renesas_usb3_role_work);
2822 	usb3->role_sw = usb_role_switch_register(&pdev->dev,
2823 					&renesas_usb3_role_switch_desc);
2824 	if (!IS_ERR(usb3->role_sw)) {
2825 		usb3->host_dev = usb_of_get_companion_dev(&pdev->dev);
2826 		if (!usb3->host_dev) {
2827 			/* If not found, this driver will not use a role sw */
2828 			usb_role_switch_unregister(usb3->role_sw);
2829 			usb3->role_sw = NULL;
2830 		}
2831 	} else {
2832 		usb3->role_sw = NULL;
2833 	}
2834 
2835 	usb3->workaround_for_vbus = priv->workaround_for_vbus;
2836 
2837 	renesas_usb3_debugfs_init(usb3, &pdev->dev);
2838 
2839 	dev_info(&pdev->dev, "probed%s\n", usb3->phy ? " with phy" : "");
2840 
2841 	return 0;
2842 
2843 err_dev_create:
2844 	usb_del_gadget_udc(&usb3->gadget);
2845 
2846 err_add_udc:
2847 	renesas_usb3_dma_free_prd(usb3, &pdev->dev);
2848 
2849 err_alloc_prd:
2850 	__renesas_usb3_ep_free_request(usb3->ep0_req);
2851 
2852 	return ret;
2853 }
2854 
2855 #ifdef CONFIG_PM_SLEEP
2856 static int renesas_usb3_suspend(struct device *dev)
2857 {
2858 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2859 
2860 	/* Not started */
2861 	if (!usb3->driver)
2862 		return 0;
2863 
2864 	renesas_usb3_stop_controller(usb3);
2865 	if (usb3->phy)
2866 		phy_exit(usb3->phy);
2867 	pm_runtime_put(dev);
2868 
2869 	return 0;
2870 }
2871 
2872 static int renesas_usb3_resume(struct device *dev)
2873 {
2874 	struct renesas_usb3 *usb3 = dev_get_drvdata(dev);
2875 
2876 	/* Not started */
2877 	if (!usb3->driver)
2878 		return 0;
2879 
2880 	if (usb3->phy)
2881 		phy_init(usb3->phy);
2882 	pm_runtime_get_sync(dev);
2883 	renesas_usb3_init_controller(usb3);
2884 
2885 	return 0;
2886 }
2887 #endif
2888 
2889 static SIMPLE_DEV_PM_OPS(renesas_usb3_pm_ops, renesas_usb3_suspend,
2890 			renesas_usb3_resume);
2891 
2892 static struct platform_driver renesas_usb3_driver = {
2893 	.probe		= renesas_usb3_probe,
2894 	.remove		= renesas_usb3_remove,
2895 	.driver		= {
2896 		.name =	(char *)udc_name,
2897 		.pm		= &renesas_usb3_pm_ops,
2898 		.of_match_table = of_match_ptr(usb3_of_match),
2899 	},
2900 };
2901 module_platform_driver(renesas_usb3_driver);
2902 
2903 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
2904 MODULE_LICENSE("GPL v2");
2905 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
2906 MODULE_ALIAS("platform:renesas_usb3");
2907