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