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