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