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