1 /*
2  * Renesas USB3.0 Peripheral driver (USB gadget)
3  *
4  * Copyright (C) 2015  Renesas Electronics Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/err.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/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 
24 /* register definitions */
25 #define USB3_AXI_INT_STA	0x008
26 #define USB3_AXI_INT_ENA	0x00c
27 #define USB3_DMA_INT_STA	0x010
28 #define USB3_DMA_INT_ENA	0x014
29 #define USB3_USB_COM_CON	0x200
30 #define USB3_USB20_CON		0x204
31 #define USB3_USB30_CON		0x208
32 #define USB3_USB_STA		0x210
33 #define USB3_DRD_CON		0x218
34 #define USB3_USB_INT_STA_1	0x220
35 #define USB3_USB_INT_STA_2	0x224
36 #define USB3_USB_INT_ENA_1	0x228
37 #define USB3_USB_INT_ENA_2	0x22c
38 #define USB3_STUP_DAT_0		0x230
39 #define USB3_STUP_DAT_1		0x234
40 #define USB3_P0_MOD		0x280
41 #define USB3_P0_CON		0x288
42 #define USB3_P0_STA		0x28c
43 #define USB3_P0_INT_STA		0x290
44 #define USB3_P0_INT_ENA		0x294
45 #define USB3_P0_LNG		0x2a0
46 #define USB3_P0_READ		0x2a4
47 #define USB3_P0_WRITE		0x2a8
48 #define USB3_PIPE_COM		0x2b0
49 #define USB3_PN_MOD		0x2c0
50 #define USB3_PN_RAMMAP		0x2c4
51 #define USB3_PN_CON		0x2c8
52 #define USB3_PN_STA		0x2cc
53 #define USB3_PN_INT_STA		0x2d0
54 #define USB3_PN_INT_ENA		0x2d4
55 #define USB3_PN_LNG		0x2e0
56 #define USB3_PN_READ		0x2e4
57 #define USB3_PN_WRITE		0x2e8
58 #define USB3_SSIFCMD		0x340
59 
60 /* AXI_INT_ENA and AXI_INT_STA */
61 #define AXI_INT_DMAINT		BIT(31)
62 #define AXI_INT_EPCINT		BIT(30)
63 
64 /* LCLKSEL */
65 #define LCLKSEL_LSEL		BIT(18)
66 
67 /* USB_COM_CON */
68 #define USB_COM_CON_CONF		BIT(24)
69 #define USB_COM_CON_SPD_MODE		BIT(17)
70 #define USB_COM_CON_EP0_EN		BIT(16)
71 #define USB_COM_CON_DEV_ADDR_SHIFT	8
72 #define USB_COM_CON_DEV_ADDR_MASK	GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
73 #define USB_COM_CON_DEV_ADDR(n)		(((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
74 					 USB_COM_CON_DEV_ADDR_MASK)
75 #define USB_COM_CON_RX_DETECTION	BIT(1)
76 #define USB_COM_CON_PIPE_CLR		BIT(0)
77 
78 /* USB20_CON */
79 #define USB20_CON_B2_PUE		BIT(31)
80 #define USB20_CON_B2_SUSPEND		BIT(24)
81 #define USB20_CON_B2_CONNECT		BIT(17)
82 #define USB20_CON_B2_TSTMOD_SHIFT	8
83 #define USB20_CON_B2_TSTMOD_MASK	GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
84 #define USB20_CON_B2_TSTMOD(n)		(((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
85 					 USB20_CON_B2_TSTMOD_MASK)
86 #define USB20_CON_B2_TSTMOD_EN		BIT(0)
87 
88 /* USB30_CON */
89 #define USB30_CON_POW_SEL_SHIFT		24
90 #define USB30_CON_POW_SEL_MASK		GENMASK(26, USB30_CON_POW_SEL_SHIFT)
91 #define USB30_CON_POW_SEL_IN_U3		BIT(26)
92 #define USB30_CON_POW_SEL_IN_DISCON	0
93 #define USB30_CON_POW_SEL_P2_TO_P0	BIT(25)
94 #define USB30_CON_POW_SEL_P0_TO_P3	BIT(24)
95 #define USB30_CON_POW_SEL_P0_TO_P2	0
96 #define USB30_CON_B3_PLLWAKE		BIT(23)
97 #define USB30_CON_B3_CONNECT		BIT(17)
98 #define USB30_CON_B3_HOTRST_CMP		BIT(1)
99 
100 /* USB_STA */
101 #define USB_STA_SPEED_MASK	(BIT(2) | BIT(1))
102 #define USB_STA_SPEED_HS	BIT(2)
103 #define USB_STA_SPEED_FS	BIT(1)
104 #define USB_STA_SPEED_SS	0
105 #define USB_STA_VBUS_STA	BIT(0)
106 
107 /* DRD_CON */
108 #define DRD_CON_PERI_CON	BIT(24)
109 
110 /* USB_INT_ENA_1 and USB_INT_STA_1 */
111 #define USB_INT_1_B3_PLLWKUP	BIT(31)
112 #define USB_INT_1_B3_LUPSUCS	BIT(30)
113 #define USB_INT_1_B3_DISABLE	BIT(27)
114 #define USB_INT_1_B3_WRMRST	BIT(21)
115 #define USB_INT_1_B3_HOTRST	BIT(20)
116 #define USB_INT_1_B2_USBRST	BIT(12)
117 #define USB_INT_1_B2_L1SPND	BIT(11)
118 #define USB_INT_1_B2_SPND	BIT(9)
119 #define USB_INT_1_B2_RSUM	BIT(8)
120 #define USB_INT_1_SPEED		BIT(1)
121 #define USB_INT_1_VBUS_CNG	BIT(0)
122 
123 /* USB_INT_ENA_2 and USB_INT_STA_2 */
124 #define USB_INT_2_PIPE(n)	BIT(n)
125 
126 /* P0_MOD */
127 #define P0_MOD_DIR		BIT(6)
128 
129 /* P0_CON and PN_CON */
130 #define PX_CON_BYTE_EN_MASK		(BIT(10) | BIT(9))
131 #define PX_CON_BYTE_EN_SHIFT		9
132 #define PX_CON_BYTE_EN_BYTES(n)		(((n) << PX_CON_BYTE_EN_SHIFT) & \
133 					 PX_CON_BYTE_EN_MASK)
134 #define PX_CON_SEND			BIT(8)
135 
136 /* P0_CON */
137 #define P0_CON_ST_RES_MASK		(BIT(27) | BIT(26))
138 #define P0_CON_ST_RES_FORCE_STALL	BIT(27)
139 #define P0_CON_ST_RES_NORMAL		BIT(26)
140 #define P0_CON_ST_RES_FORCE_NRDY	0
141 #define P0_CON_OT_RES_MASK		(BIT(25) | BIT(24))
142 #define P0_CON_OT_RES_FORCE_STALL	BIT(25)
143 #define P0_CON_OT_RES_NORMAL		BIT(24)
144 #define P0_CON_OT_RES_FORCE_NRDY	0
145 #define P0_CON_IN_RES_MASK		(BIT(17) | BIT(16))
146 #define P0_CON_IN_RES_FORCE_STALL	BIT(17)
147 #define P0_CON_IN_RES_NORMAL		BIT(16)
148 #define P0_CON_IN_RES_FORCE_NRDY	0
149 #define P0_CON_RES_WEN			BIT(7)
150 #define P0_CON_BCLR			BIT(1)
151 
152 /* P0_STA and PN_STA */
153 #define PX_STA_BUFSTS		BIT(0)
154 
155 /* P0_INT_ENA and P0_INT_STA */
156 #define P0_INT_STSED		BIT(18)
157 #define P0_INT_STSST		BIT(17)
158 #define P0_INT_SETUP		BIT(16)
159 #define P0_INT_RCVNL		BIT(8)
160 #define P0_INT_ERDY		BIT(7)
161 #define P0_INT_FLOW		BIT(6)
162 #define P0_INT_STALL		BIT(2)
163 #define P0_INT_NRDY		BIT(1)
164 #define P0_INT_BFRDY		BIT(0)
165 #define P0_INT_ALL_BITS		(P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
166 
167 /* PN_MOD */
168 #define PN_MOD_DIR		BIT(6)
169 #define PN_MOD_TYPE_SHIFT	4
170 #define PN_MOD_TYPE_MASK	GENMASK(5, PN_MOD_TYPE_SHIFT)
171 #define PN_MOD_TYPE(n)		(((n) << PN_MOD_TYPE_SHIFT) & \
172 				 PN_MOD_TYPE_MASK)
173 #define PN_MOD_EPNUM_MASK	GENMASK(3, 0)
174 #define PN_MOD_EPNUM(n)		((n) & PN_MOD_EPNUM_MASK)
175 
176 /* PN_RAMMAP */
177 #define PN_RAMMAP_RAMAREA_SHIFT	29
178 #define PN_RAMMAP_RAMAREA_MASK	GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
179 #define PN_RAMMAP_RAMAREA_16KB	BIT(31)
180 #define PN_RAMMAP_RAMAREA_8KB	(BIT(30) | BIT(29))
181 #define PN_RAMMAP_RAMAREA_4KB	BIT(30)
182 #define PN_RAMMAP_RAMAREA_2KB	BIT(29)
183 #define PN_RAMMAP_RAMAREA_1KB	0
184 #define PN_RAMMAP_MPKT_SHIFT	16
185 #define PN_RAMMAP_MPKT_MASK	GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
186 #define PN_RAMMAP_MPKT(n)	(((n) << PN_RAMMAP_MPKT_SHIFT) & \
187 				 PN_RAMMAP_MPKT_MASK)
188 #define PN_RAMMAP_RAMIF_SHIFT	14
189 #define PN_RAMMAP_RAMIF_MASK	GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
190 #define PN_RAMMAP_RAMIF(n)	(((n) << PN_RAMMAP_RAMIF_SHIFT) & \
191 				 PN_RAMMAP_RAMIF_MASK)
192 #define PN_RAMMAP_BASEAD_MASK	GENMASK(13, 0)
193 #define PN_RAMMAP_BASEAD(offs)	(((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
194 #define PN_RAMMAP_DATA(area, ramif, basead)	((PN_RAMMAP_##area) | \
195 						 (PN_RAMMAP_RAMIF(ramif)) | \
196 						 (PN_RAMMAP_BASEAD(basead)))
197 
198 /* PN_CON */
199 #define PN_CON_EN		BIT(31)
200 #define PN_CON_DATAIF_EN	BIT(30)
201 #define PN_CON_RES_MASK		(BIT(17) | BIT(16))
202 #define PN_CON_RES_FORCE_STALL	BIT(17)
203 #define PN_CON_RES_NORMAL	BIT(16)
204 #define PN_CON_RES_FORCE_NRDY	0
205 #define PN_CON_LAST		BIT(11)
206 #define PN_CON_RES_WEN		BIT(7)
207 #define PN_CON_CLR		BIT(0)
208 
209 /* PN_INT_STA and PN_INT_ENA */
210 #define PN_INT_LSTTR	BIT(4)
211 #define PN_INT_BFRDY	BIT(0)
212 
213 /* USB3_SSIFCMD */
214 #define SSIFCMD_URES_U2		BIT(9)
215 #define SSIFCMD_URES_U1		BIT(8)
216 #define SSIFCMD_UDIR_U2		BIT(7)
217 #define SSIFCMD_UDIR_U1		BIT(6)
218 #define SSIFCMD_UREQ_U2		BIT(5)
219 #define SSIFCMD_UREQ_U1		BIT(4)
220 
221 #define USB3_EP0_SS_MAX_PACKET_SIZE	512
222 #define USB3_EP0_HSFS_MAX_PACKET_SIZE	64
223 #define USB3_EP0_BUF_SIZE		8
224 #define USB3_MAX_NUM_PIPES		30
225 #define USB3_WAIT_US			3
226 
227 struct renesas_usb3;
228 struct renesas_usb3_request {
229 	struct usb_request	req;
230 	struct list_head	queue;
231 };
232 
233 #define USB3_EP_NAME_SIZE	8
234 struct renesas_usb3_ep {
235 	struct usb_ep ep;
236 	struct renesas_usb3 *usb3;
237 	int num;
238 	char ep_name[USB3_EP_NAME_SIZE];
239 	struct list_head queue;
240 	u32 rammap_val;
241 	bool dir_in;
242 	bool halt;
243 	bool wedge;
244 	bool started;
245 };
246 
247 struct renesas_usb3_priv {
248 	int ramsize_per_ramif;		/* unit = bytes */
249 	int num_ramif;
250 	int ramsize_per_pipe;		/* unit = bytes */
251 	bool workaround_for_vbus;	/* if true, don't check vbus signal */
252 };
253 
254 struct renesas_usb3 {
255 	void __iomem *reg;
256 
257 	struct usb_gadget gadget;
258 	struct usb_gadget_driver *driver;
259 
260 	struct renesas_usb3_ep *usb3_ep;
261 	int num_usb3_eps;
262 
263 	spinlock_t lock;
264 	int disabled_count;
265 
266 	struct usb_request *ep0_req;
267 	u16 test_mode;
268 	u8 ep0_buf[USB3_EP0_BUF_SIZE];
269 	bool softconnect;
270 	bool workaround_for_vbus;
271 };
272 
273 #define gadget_to_renesas_usb3(_gadget)	\
274 		container_of(_gadget, struct renesas_usb3, gadget)
275 #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
276 #define usb3_to_dev(_usb3)	(_usb3->gadget.dev.parent)
277 
278 #define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
279 #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
280 #define usb_req_to_usb3_req(_req) container_of(_req, \
281 					    struct renesas_usb3_request, req)
282 
283 #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
284 #define usb3_for_each_ep(usb3_ep, usb3, i)			\
285 		for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i));	\
286 		     (i) < (usb3)->num_usb3_eps;		\
287 		     (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
288 
289 static const char udc_name[] = "renesas_usb3";
290 
291 static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
292 {
293 	iowrite32(data, usb3->reg + offs);
294 }
295 
296 static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
297 {
298 	return ioread32(usb3->reg + offs);
299 }
300 
301 static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
302 {
303 	u32 val = usb3_read(usb3, offs);
304 
305 	val |= bits;
306 	usb3_write(usb3, val, offs);
307 }
308 
309 static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
310 {
311 	u32 val = usb3_read(usb3, offs);
312 
313 	val &= ~bits;
314 	usb3_write(usb3, val, offs);
315 }
316 
317 static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
318 		     u32 expected)
319 {
320 	int i;
321 
322 	for (i = 0; i < USB3_WAIT_US; i++) {
323 		if ((usb3_read(usb3, reg) & mask) == expected)
324 			return 0;
325 		udelay(1);
326 	}
327 
328 	dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
329 		__func__, reg, mask, expected);
330 
331 	return -EBUSY;
332 }
333 
334 static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
335 {
336 	usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
337 }
338 
339 static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
340 {
341 	usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
342 }
343 
344 static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
345 {
346 	usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
347 }
348 
349 static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
350 {
351 	usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
352 }
353 
354 static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
355 {
356 	/* Set AXI_INT */
357 	usb3_write(usb3, ~0, USB3_DMA_INT_STA);
358 	usb3_write(usb3, 0, USB3_DMA_INT_ENA);
359 	usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
360 }
361 
362 static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
363 {
364 	/* FIXME: How to change host / peripheral mode as well? */
365 	usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
366 
367 	usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
368 	usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
369 }
370 
371 static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
372 {
373 	if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
374 		return true;	/* already waked it up */
375 
376 	usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
377 	usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
378 
379 	return false;
380 }
381 
382 static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
383 {
384 	u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
385 
386 	if (usb3->softconnect && pullup)
387 		usb3_set_bit(usb3, bits, USB3_USB20_CON);
388 	else
389 		usb3_clear_bit(usb3, bits, USB3_USB20_CON);
390 }
391 
392 static void usb3_set_test_mode(struct renesas_usb3 *usb3)
393 {
394 	u32 val = usb3_read(usb3, USB3_USB20_CON);
395 
396 	val &= ~USB20_CON_B2_TSTMOD_MASK;
397 	val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
398 	usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
399 	if (!usb3->test_mode)
400 		usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
401 }
402 
403 static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
404 {
405 	usb3->disabled_count++;
406 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
407 	usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
408 	usb3_usb2_pullup(usb3, 1);
409 }
410 
411 static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
412 {
413 	return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
414 }
415 
416 static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
417 {
418 	if (!usb3_is_usb3_phy_in_u3(usb3))
419 		return true;	/* already waked it up */
420 
421 	usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
422 	usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
423 
424 	return false;
425 }
426 
427 static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
428 {
429 	u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
430 	u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
431 	u32 val = usb3_read(usb3, USB3_SSIFCMD);
432 	u16 ret = 0;
433 
434 	/* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
435 	if (!(val & mask_u2))
436 		ret |= 1 << USB_DEV_STAT_U2_ENABLED;
437 	if (!(val & mask_u1))
438 		ret |= 1 << USB_DEV_STAT_U1_ENABLED;
439 
440 	return ret;
441 }
442 
443 static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
444 {
445 	u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
446 
447 	/* Enables U2 if the bits of UDIR and UREQ are set to 0 */
448 	if (enable)
449 		usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
450 	else
451 		usb3_set_bit(usb3, bits, USB3_SSIFCMD);
452 }
453 
454 static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
455 {
456 	u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
457 
458 	/* Enables U1 if the bits of UDIR and UREQ are set to 0 */
459 	if (enable)
460 		usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
461 	else
462 		usb3_set_bit(usb3, bits, USB3_SSIFCMD);
463 }
464 
465 static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
466 {
467 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
468 	usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
469 	usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
470 }
471 
472 static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
473 {
474 	usb3_start_operation_for_usb3(usb3);
475 	usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
476 
477 	usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
478 			  USB_INT_1_SPEED);
479 }
480 
481 static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
482 {
483 	usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
484 }
485 
486 static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
487 					     bool is_usb3)
488 {
489 	usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
490 	usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
491 	usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
492 
493 	if (is_usb3)
494 		usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
495 				  USB_INT_1_B3_HOTRST);
496 	else
497 		usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
498 				  USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
499 }
500 
501 static void usb3_connect(struct renesas_usb3 *usb3)
502 {
503 	if (usb3_wakeup_usb3_phy(usb3))
504 		usb3_start_usb3_connection(usb3);
505 }
506 
507 static void usb3_reset_epc(struct renesas_usb3 *usb3)
508 {
509 	usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
510 	usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
511 	usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
512 	usb3->test_mode = 0;
513 	usb3_set_test_mode(usb3);
514 }
515 
516 static void usb3_disconnect(struct renesas_usb3 *usb3)
517 {
518 	usb3->disabled_count = 0;
519 	usb3_usb2_pullup(usb3, 0);
520 	usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
521 	usb3_reset_epc(usb3);
522 
523 	if (usb3->driver)
524 		usb3->driver->disconnect(&usb3->gadget);
525 }
526 
527 static void usb3_check_vbus(struct renesas_usb3 *usb3)
528 {
529 	if (usb3->workaround_for_vbus) {
530 		usb3_connect(usb3);
531 	} else {
532 		if (usb3_read(usb3, USB3_USB_STA) & USB_STA_VBUS_STA)
533 			usb3_connect(usb3);
534 		else
535 			usb3_disconnect(usb3);
536 	}
537 }
538 
539 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
540 {
541 	usb3_init_axi_bridge(usb3);
542 	usb3_init_epc_registers(usb3);
543 
544 	usb3_check_vbus(usb3);
545 }
546 
547 static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
548 {
549 	usb3_disconnect(usb3);
550 	usb3_write(usb3, 0, USB3_P0_INT_ENA);
551 	usb3_write(usb3, 0, USB3_PN_INT_ENA);
552 	usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
553 	usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
554 	usb3_write(usb3, 0, USB3_AXI_INT_ENA);
555 }
556 
557 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
558 {
559 	usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
560 	usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
561 	usb3_start_usb3_connection(usb3);
562 }
563 
564 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
565 {
566 	usb3_transition_to_default_state(usb3, true);
567 }
568 
569 static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
570 {
571 	usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
572 	usb3_start_usb2_connection(usb3);
573 	usb3_transition_to_default_state(usb3, false);
574 }
575 
576 static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
577 {
578 	usb3_stop_usb3_connection(usb3);
579 	if (usb3_wakeup_usb2_phy(usb3))
580 		usb3_irq_epc_int_1_resume(usb3);
581 }
582 
583 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
584 {
585 	usb3_reset_epc(usb3);
586 	if (usb3->disabled_count < 3)
587 		usb3_start_usb3_connection(usb3);
588 	else
589 		usb3_start_usb2_connection(usb3);
590 }
591 
592 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
593 {
594 	usb3_check_vbus(usb3);
595 }
596 
597 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
598 {
599 	usb3_reset_epc(usb3);
600 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
601 
602 	/* This bit shall be set within 12ms from the start of HotReset */
603 	usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
604 }
605 
606 static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
607 {
608 	usb3_reset_epc(usb3);
609 	usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
610 
611 	usb3_start_operation_for_usb3(usb3);
612 	usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
613 }
614 
615 static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
616 {
617 	u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
618 
619 	switch (speed) {
620 	case USB_STA_SPEED_SS:
621 		usb3->gadget.speed = USB_SPEED_SUPER;
622 		break;
623 	case USB_STA_SPEED_HS:
624 		usb3->gadget.speed = USB_SPEED_HIGH;
625 		break;
626 	case USB_STA_SPEED_FS:
627 		usb3->gadget.speed = USB_SPEED_FULL;
628 		break;
629 	default:
630 		usb3->gadget.speed = USB_SPEED_UNKNOWN;
631 		break;
632 	}
633 }
634 
635 static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
636 {
637 	if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
638 		usb3_irq_epc_int_1_pll_wakeup(usb3);
639 
640 	if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
641 		usb3_irq_epc_int_1_linkup_success(usb3);
642 
643 	if (int_sta_1 & USB_INT_1_B3_HOTRST)
644 		usb3_irq_epc_int_1_hot_reset(usb3);
645 
646 	if (int_sta_1 & USB_INT_1_B3_WRMRST)
647 		usb3_irq_epc_int_1_warm_reset(usb3);
648 
649 	if (int_sta_1 & USB_INT_1_B3_DISABLE)
650 		usb3_irq_epc_int_1_disable(usb3);
651 
652 	if (int_sta_1 & USB_INT_1_B2_USBRST)
653 		usb3_irq_epc_int_1_bus_reset(usb3);
654 
655 	if (int_sta_1 & USB_INT_1_B2_RSUM)
656 		usb3_irq_epc_int_1_resume(usb3);
657 
658 	if (int_sta_1 & USB_INT_1_SPEED)
659 		usb3_irq_epc_int_1_speed(usb3);
660 
661 	if (int_sta_1 & USB_INT_1_VBUS_CNG)
662 		usb3_irq_epc_int_1_vbus_change(usb3);
663 }
664 
665 static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
666 						       *usb3_ep)
667 {
668 	return list_first_entry_or_null(&usb3_ep->queue,
669 					struct renesas_usb3_request, queue);
670 }
671 
672 static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
673 						     *usb3_ep)
674 {
675 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
676 	struct renesas_usb3_request *usb3_req;
677 	unsigned long flags;
678 
679 	spin_lock_irqsave(&usb3->lock, flags);
680 	usb3_req = __usb3_get_request(usb3_ep);
681 	spin_unlock_irqrestore(&usb3->lock, flags);
682 
683 	return usb3_req;
684 }
685 
686 static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
687 			      struct renesas_usb3_request *usb3_req, int status)
688 {
689 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
690 	unsigned long flags;
691 
692 	dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
693 		usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
694 		status);
695 	usb3_req->req.status = status;
696 	spin_lock_irqsave(&usb3->lock, flags);
697 	usb3_ep->started = false;
698 	list_del_init(&usb3_req->queue);
699 	spin_unlock_irqrestore(&usb3->lock, flags);
700 	usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
701 }
702 
703 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
704 {
705 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
706 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
707 
708 	if (usb3_req)
709 		usb3_request_done(usb3_ep, usb3_req, 0);
710 	if (usb3->test_mode)
711 		usb3_set_test_mode(usb3);
712 }
713 
714 static void usb3_get_setup_data(struct renesas_usb3 *usb3,
715 				struct usb_ctrlrequest *ctrl)
716 {
717 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
718 	u32 *data = (u32 *)ctrl;
719 
720 	*data++ = usb3_read(usb3, USB3_STUP_DAT_0);
721 	*data = usb3_read(usb3, USB3_STUP_DAT_1);
722 
723 	/* update this driver's flag */
724 	usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
725 }
726 
727 static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
728 {
729 	u32 val = usb3_read(usb3, USB3_P0_CON);
730 
731 	val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
732 	val |= res | P0_CON_RES_WEN;
733 	usb3_write(usb3, val, USB3_P0_CON);
734 }
735 
736 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
737 {
738 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
739 				   P0_CON_OT_RES_FORCE_STALL |
740 				   P0_CON_IN_RES_NORMAL);
741 }
742 
743 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
744 {
745 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
746 				   P0_CON_OT_RES_FORCE_STALL |
747 				   P0_CON_IN_RES_NORMAL);
748 }
749 
750 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
751 {
752 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
753 				   P0_CON_OT_RES_NORMAL |
754 				   P0_CON_IN_RES_FORCE_STALL);
755 }
756 
757 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
758 {
759 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
760 				   P0_CON_OT_RES_NORMAL |
761 				   P0_CON_IN_RES_FORCE_STALL);
762 }
763 
764 static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
765 {
766 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
767 				   P0_CON_OT_RES_FORCE_STALL |
768 				   P0_CON_IN_RES_FORCE_STALL);
769 }
770 
771 static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
772 {
773 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
774 				   P0_CON_OT_RES_FORCE_STALL |
775 				   P0_CON_IN_RES_FORCE_STALL);
776 }
777 
778 static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
779 {
780 	usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
781 				   P0_CON_OT_RES_FORCE_NRDY |
782 				   P0_CON_IN_RES_FORCE_NRDY);
783 }
784 
785 static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
786 {
787 	if (num == 0 || num > usb3->num_usb3_eps)
788 		return -ENXIO;
789 
790 	usb3_write(usb3, num, USB3_PIPE_COM);
791 
792 	return 0;
793 }
794 
795 static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
796 {
797 	u32 val = usb3_read(usb3, USB3_PN_CON);
798 
799 	val &= ~PN_CON_RES_MASK;
800 	val |= res & PN_CON_RES_MASK;
801 	val |= PN_CON_RES_WEN;
802 	usb3_write(usb3, val, USB3_PN_CON);
803 }
804 
805 static void usb3_pn_start(struct renesas_usb3 *usb3)
806 {
807 	usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
808 }
809 
810 static void usb3_pn_stop(struct renesas_usb3 *usb3)
811 {
812 	usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
813 }
814 
815 static void usb3_pn_stall(struct renesas_usb3 *usb3)
816 {
817 	usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
818 }
819 
820 static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
821 {
822 	usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
823 
824 	return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
825 }
826 
827 static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
828 				      struct renesas_usb3_request *usb3_req)
829 {
830 	struct usb_request *req = &usb3_req->req;
831 
832 	if ((!req->zero && req->actual == req->length) ||
833 	    (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
834 		return true;
835 	else
836 		return false;
837 }
838 
839 static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
840 {
841 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
842 	u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
843 
844 	return usb3_wait(usb3, sta_reg, mask, mask);
845 }
846 
847 static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
848 				 bool last)
849 {
850 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
851 	u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
852 	u32 val = usb3_read(usb3, con_reg);
853 
854 	val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
855 	val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
856 	usb3_write(usb3, val, con_reg);
857 }
858 
859 static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
860 			   struct renesas_usb3_request *usb3_req,
861 			   u32 fifo_reg)
862 {
863 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
864 	int i;
865 	int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
866 			usb3_ep->ep.maxpacket);
867 	u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
868 	u32 tmp = 0;
869 	bool is_last;
870 
871 	if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
872 		return -EBUSY;
873 
874 	/* Update gadget driver parameter */
875 	usb3_req->req.actual += len;
876 
877 	/* Write data to the register */
878 	if (len >= 4) {
879 		iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
880 		buf += (len / 4) * 4;
881 		len %= 4;	/* update len to use usb3_set_pX_con_send() */
882 	}
883 
884 	if (len) {
885 		for (i = 0; i < len; i++)
886 			tmp |= buf[i] << (8 * i);
887 		usb3_write(usb3, tmp, fifo_reg);
888 	}
889 
890 	is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
891 	/* Send the data */
892 	usb3_set_px_con_send(usb3_ep, len, is_last);
893 
894 	return is_last ? 0 : -EAGAIN;
895 }
896 
897 static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
898 {
899 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
900 	u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
901 
902 	return usb3_read(usb3, lng_reg);
903 }
904 
905 static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
906 			  struct renesas_usb3_request *usb3_req, u32 fifo_reg)
907 {
908 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
909 	int i;
910 	int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
911 			usb3_get_received_length(usb3_ep));
912 	u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
913 	u32 tmp = 0;
914 
915 	if (!len)
916 		return 0;
917 
918 	/* Update gadget driver parameter */
919 	usb3_req->req.actual += len;
920 
921 	/* Read data from the register */
922 	if (len >= 4) {
923 		ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
924 		buf += (len / 4) * 4;
925 		len %= 4;
926 	}
927 
928 	if (len) {
929 		tmp = usb3_read(usb3, fifo_reg);
930 		for (i = 0; i < len; i++)
931 			buf[i] = (tmp >> (8 * i)) & 0xff;
932 	}
933 
934 	return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
935 }
936 
937 static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
938 				  struct renesas_usb3_request *usb3_req)
939 {
940 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
941 
942 	if (usb3_ep->dir_in) {
943 		usb3_set_p0_con_for_ctrl_read_status(usb3);
944 	} else {
945 		if (!usb3_req->req.length)
946 			usb3_set_p0_con_for_no_data(usb3);
947 		else
948 			usb3_set_p0_con_for_ctrl_write_status(usb3);
949 	}
950 }
951 
952 static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
953 			 struct renesas_usb3_request *usb3_req)
954 {
955 	int ret = -EAGAIN;
956 
957 	if (usb3_ep->dir_in)
958 		ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
959 	else
960 		ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
961 
962 	if (!ret)
963 		usb3_set_status_stage(usb3_ep, usb3_req);
964 }
965 
966 static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
967 			     struct renesas_usb3_request *usb3_req)
968 {
969 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
970 
971 	if (usb3_ep->started)
972 		return;
973 
974 	usb3_ep->started = true;
975 
976 	if (usb3_ep->dir_in) {
977 		usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
978 		usb3_set_p0_con_for_ctrl_read_data(usb3);
979 	} else {
980 		usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
981 		usb3_set_p0_con_for_ctrl_write_data(usb3);
982 	}
983 
984 	usb3_p0_xfer(usb3_ep, usb3_req);
985 }
986 
987 static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
988 			     struct renesas_usb3_request *usb3_req)
989 {
990 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
991 	struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
992 	unsigned long flags;
993 	int ret = -EAGAIN;
994 	u32 enable_bits = 0;
995 
996 	if (usb3_ep->halt || usb3_ep->started)
997 		return;
998 	if (usb3_req != usb3_req_first)
999 		return;
1000 
1001 	spin_lock_irqsave(&usb3->lock, flags);
1002 	if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1003 		goto out;
1004 
1005 	usb3_ep->started = true;
1006 	usb3_pn_start(usb3);
1007 
1008 	if (usb3_ep->dir_in) {
1009 		ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1010 		enable_bits |= PN_INT_LSTTR;
1011 	}
1012 
1013 	if (ret < 0)
1014 		enable_bits |= PN_INT_BFRDY;
1015 
1016 	if (enable_bits) {
1017 		usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1018 		usb3_enable_pipe_irq(usb3, usb3_ep->num);
1019 	}
1020 out:
1021 	spin_unlock_irqrestore(&usb3->lock, flags);
1022 }
1023 
1024 static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1025 				 gfp_t gfp_flags)
1026 {
1027 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1028 	struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1029 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1030 	unsigned long flags;
1031 
1032 	dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1033 		_req->length);
1034 
1035 	_req->status = -EINPROGRESS;
1036 	_req->actual = 0;
1037 	spin_lock_irqsave(&usb3->lock, flags);
1038 	list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1039 	spin_unlock_irqrestore(&usb3->lock, flags);
1040 
1041 	if (!usb3_ep->num)
1042 		usb3_start_pipe0(usb3_ep, usb3_req);
1043 	else
1044 		usb3_start_pipen(usb3_ep, usb3_req);
1045 
1046 	return 0;
1047 }
1048 
1049 static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1050 {
1051 	/* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1052 	usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1053 }
1054 
1055 static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1056 				     struct usb_ctrlrequest *ctrl)
1057 {
1058 	if (ctrl->wValue >= 128)
1059 		return true;	/* stall */
1060 
1061 	usb3_set_device_address(usb3, ctrl->wValue);
1062 	usb3_set_p0_con_for_no_data(usb3);
1063 
1064 	return false;
1065 }
1066 
1067 static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1068 				     void *tx_data, size_t len,
1069 				     void (*complete)(struct usb_ep *ep,
1070 						      struct usb_request *req))
1071 {
1072 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1073 
1074 	if (tx_data)
1075 		memcpy(usb3->ep0_buf, tx_data,
1076 		       min_t(size_t, len, USB3_EP0_BUF_SIZE));
1077 
1078 	usb3->ep0_req->buf = &usb3->ep0_buf;
1079 	usb3->ep0_req->length = len;
1080 	usb3->ep0_req->complete = complete;
1081 	renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1082 }
1083 
1084 static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1085 					     struct usb_request *req)
1086 {
1087 }
1088 
1089 static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1090 				    struct usb_ctrlrequest *ctrl)
1091 {
1092 	bool stall = false;
1093 	struct renesas_usb3_ep *usb3_ep;
1094 	int num;
1095 	u16 status = 0;
1096 
1097 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
1098 	case USB_RECIP_DEVICE:
1099 		if (usb3->gadget.is_selfpowered)
1100 			status |= 1 << USB_DEVICE_SELF_POWERED;
1101 		if (usb3->gadget.speed == USB_SPEED_SUPER)
1102 			status |= usb3_feature_get_un_enabled(usb3);
1103 		break;
1104 	case USB_RECIP_INTERFACE:
1105 		break;
1106 	case USB_RECIP_ENDPOINT:
1107 		num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1108 		usb3_ep = usb3_get_ep(usb3, num);
1109 		if (usb3_ep->halt)
1110 			status |= 1 << USB_ENDPOINT_HALT;
1111 		break;
1112 	default:
1113 		stall = true;
1114 		break;
1115 	}
1116 
1117 	if (!stall) {
1118 		status = cpu_to_le16(status);
1119 		dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1120 			usb_req_to_usb3_req(usb3->ep0_req));
1121 		usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
1122 					 usb3_pipe0_get_status_completion);
1123 	}
1124 
1125 	return stall;
1126 }
1127 
1128 static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1129 					struct usb_ctrlrequest *ctrl, bool set)
1130 {
1131 	bool stall = true;
1132 	u16 w_value = le16_to_cpu(ctrl->wValue);
1133 
1134 	switch (w_value) {
1135 	case USB_DEVICE_TEST_MODE:
1136 		if (!set)
1137 			break;
1138 		usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1139 		stall = false;
1140 		break;
1141 	case USB_DEVICE_U1_ENABLE:
1142 	case USB_DEVICE_U2_ENABLE:
1143 		if (usb3->gadget.speed != USB_SPEED_SUPER)
1144 			break;
1145 		if (w_value == USB_DEVICE_U1_ENABLE)
1146 			usb3_feature_u1_enable(usb3, set);
1147 		if (w_value == USB_DEVICE_U2_ENABLE)
1148 			usb3_feature_u2_enable(usb3, set);
1149 		stall = false;
1150 		break;
1151 	default:
1152 		break;
1153 	}
1154 
1155 	return stall;
1156 }
1157 
1158 static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1159 {
1160 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1161 
1162 	if (unlikely(usb3_ep->num))
1163 		return -EINVAL;
1164 
1165 	usb3_ep->halt = halt;
1166 	if (halt)
1167 		usb3_set_p0_con_stall(usb3);
1168 	else
1169 		usb3_set_p0_con_stop(usb3);
1170 
1171 	return 0;
1172 }
1173 
1174 static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1175 			    bool is_clear_feature)
1176 {
1177 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1178 	unsigned long flags;
1179 
1180 	spin_lock_irqsave(&usb3->lock, flags);
1181 	if (!usb3_pn_change(usb3, usb3_ep->num)) {
1182 		usb3_ep->halt = halt;
1183 		if (halt) {
1184 			usb3_pn_stall(usb3);
1185 		} else if (!is_clear_feature || !usb3_ep->wedge) {
1186 			usb3_pn_con_clear(usb3);
1187 			usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1188 			usb3_pn_stop(usb3);
1189 		}
1190 	}
1191 	spin_unlock_irqrestore(&usb3->lock, flags);
1192 
1193 	return 0;
1194 }
1195 
1196 static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1197 			 bool is_clear_feature)
1198 {
1199 	int ret = 0;
1200 
1201 	if (halt && usb3_ep->started)
1202 		return -EAGAIN;
1203 
1204 	if (usb3_ep->num)
1205 		ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1206 	else
1207 		ret = usb3_set_halt_p0(usb3_ep, halt);
1208 
1209 	return ret;
1210 }
1211 
1212 static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1213 					  struct usb_ctrlrequest *ctrl,
1214 					  bool set)
1215 {
1216 	int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1217 	struct renesas_usb3_ep *usb3_ep;
1218 	struct renesas_usb3_request *usb3_req;
1219 
1220 	if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1221 		return true;	/* stall */
1222 
1223 	usb3_ep = usb3_get_ep(usb3, num);
1224 	usb3_set_halt(usb3_ep, set, true);
1225 
1226 	/* Restarts a queue if clear feature */
1227 	if (!set) {
1228 		usb3_ep->started = false;
1229 		usb3_req = usb3_get_request(usb3_ep);
1230 		if (usb3_req)
1231 			usb3_start_pipen(usb3_ep, usb3_req);
1232 	}
1233 
1234 	return false;
1235 }
1236 
1237 static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1238 				 struct usb_ctrlrequest *ctrl, bool set)
1239 {
1240 	bool stall = false;
1241 
1242 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
1243 	case USB_RECIP_DEVICE:
1244 		stall = usb3_std_req_feature_device(usb3, ctrl, set);
1245 		break;
1246 	case USB_RECIP_INTERFACE:
1247 		break;
1248 	case USB_RECIP_ENDPOINT:
1249 		stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1250 		break;
1251 	default:
1252 		stall = true;
1253 		break;
1254 	}
1255 
1256 	if (!stall)
1257 		usb3_set_p0_con_for_no_data(usb3);
1258 
1259 	return stall;
1260 }
1261 
1262 static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1263 					  struct usb_request *req)
1264 {
1265 	/* TODO */
1266 }
1267 
1268 static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1269 				 struct usb_ctrlrequest *ctrl)
1270 {
1271 	u16 w_length = le16_to_cpu(ctrl->wLength);
1272 
1273 	if (w_length != 6)
1274 		return true;	/* stall */
1275 
1276 	dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1277 		usb_req_to_usb3_req(usb3->ep0_req));
1278 	usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1279 
1280 	return false;
1281 }
1282 
1283 static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1284 					   struct usb_ctrlrequest *ctrl)
1285 {
1286 	if (ctrl->wValue > 0)
1287 		usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1288 	else
1289 		usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1290 
1291 	return false;
1292 }
1293 
1294 /**
1295  * usb3_handle_standard_request - handle some standard requests
1296  * @usb3: the renesas_usb3 pointer
1297  * @ctrl: a pointer of setup data
1298  *
1299  * Returns true if this function handled a standard request
1300  */
1301 static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1302 					 struct usb_ctrlrequest *ctrl)
1303 {
1304 	bool ret = false;
1305 	bool stall = false;
1306 
1307 	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1308 		switch (ctrl->bRequest) {
1309 		case USB_REQ_SET_ADDRESS:
1310 			stall = usb3_std_req_set_address(usb3, ctrl);
1311 			ret = true;
1312 			break;
1313 		case USB_REQ_GET_STATUS:
1314 			stall = usb3_std_req_get_status(usb3, ctrl);
1315 			ret = true;
1316 			break;
1317 		case USB_REQ_CLEAR_FEATURE:
1318 			stall = usb3_std_req_feature(usb3, ctrl, false);
1319 			ret = true;
1320 			break;
1321 		case USB_REQ_SET_FEATURE:
1322 			stall = usb3_std_req_feature(usb3, ctrl, true);
1323 			ret = true;
1324 			break;
1325 		case USB_REQ_SET_SEL:
1326 			stall = usb3_std_req_set_sel(usb3, ctrl);
1327 			ret = true;
1328 			break;
1329 		case USB_REQ_SET_ISOCH_DELAY:
1330 			/* This hardware doesn't support Isochronous xfer */
1331 			stall = true;
1332 			ret = true;
1333 			break;
1334 		case USB_REQ_SET_CONFIGURATION:
1335 			usb3_std_req_set_configuration(usb3, ctrl);
1336 			break;
1337 		default:
1338 			break;
1339 		}
1340 	}
1341 
1342 	if (stall)
1343 		usb3_set_p0_con_stall(usb3);
1344 
1345 	return ret;
1346 }
1347 
1348 static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1349 {
1350 	usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1351 
1352 	return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1353 }
1354 
1355 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1356 {
1357 	struct usb_ctrlrequest ctrl;
1358 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1359 
1360 	/* Call giveback function if previous transfer is not completed */
1361 	if (usb3_ep->started)
1362 		usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1363 				  -ECONNRESET);
1364 
1365 	usb3_p0_con_clear_buffer(usb3);
1366 	usb3_get_setup_data(usb3, &ctrl);
1367 	if (!usb3_handle_standard_request(usb3, &ctrl))
1368 		if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1369 			usb3_set_p0_con_stall(usb3);
1370 }
1371 
1372 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1373 {
1374 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1375 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1376 
1377 	if (!usb3_req)
1378 		return;
1379 
1380 	usb3_p0_xfer(usb3_ep, usb3_req);
1381 }
1382 
1383 static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1384 {
1385 	u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1386 
1387 	p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1388 	usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1389 	if (p0_int_sta & P0_INT_STSED)
1390 		usb3_irq_epc_pipe0_status_end(usb3);
1391 	if (p0_int_sta & P0_INT_SETUP)
1392 		usb3_irq_epc_pipe0_setup(usb3);
1393 	if (p0_int_sta & P0_INT_BFRDY)
1394 		usb3_irq_epc_pipe0_bfrdy(usb3);
1395 }
1396 
1397 static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1398 				    struct renesas_usb3_ep *usb3_ep,
1399 				    struct renesas_usb3_request *usb3_req,
1400 				    int status)
1401 {
1402 	usb3_pn_stop(usb3);
1403 	usb3_disable_pipe_irq(usb3, usb3_ep->num);
1404 	usb3_request_done(usb3_ep, usb3_req, status);
1405 
1406 	/* get next usb3_req */
1407 	usb3_req = usb3_get_request(usb3_ep);
1408 	if (usb3_req)
1409 		usb3_start_pipen(usb3_ep, usb3_req);
1410 }
1411 
1412 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1413 {
1414 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1415 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1416 
1417 	if (!usb3_req)
1418 		return;
1419 
1420 	if (usb3_ep->dir_in) {
1421 		dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1422 			__func__, usb3_req->req.length, usb3_req->req.actual);
1423 		usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1424 	}
1425 }
1426 
1427 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1428 {
1429 	struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1430 	struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1431 
1432 	if (!usb3_req)
1433 		return;
1434 
1435 	if (usb3_ep->dir_in) {
1436 		/* Do not stop the IN pipe here to detect LSTTR interrupt */
1437 		if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1438 			usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1439 	} else {
1440 		if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1441 			usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1442 	}
1443 }
1444 
1445 static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1446 {
1447 	u32 pn_int_sta;
1448 
1449 	if (usb3_pn_change(usb3, num) < 0)
1450 		return;
1451 
1452 	pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1453 	pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1454 	usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1455 	if (pn_int_sta & PN_INT_LSTTR)
1456 		usb3_irq_epc_pipen_lsttr(usb3, num);
1457 	if (pn_int_sta & PN_INT_BFRDY)
1458 		usb3_irq_epc_pipen_bfrdy(usb3, num);
1459 }
1460 
1461 static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1462 {
1463 	int i;
1464 
1465 	for (i = 0; i < usb3->num_usb3_eps; i++) {
1466 		if (int_sta_2 & USB_INT_2_PIPE(i)) {
1467 			if (!i)
1468 				usb3_irq_epc_pipe0(usb3);
1469 			else
1470 				usb3_irq_epc_pipen(usb3, i);
1471 		}
1472 	}
1473 }
1474 
1475 static void usb3_irq_epc(struct renesas_usb3 *usb3)
1476 {
1477 	u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
1478 	u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
1479 
1480 	int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
1481 	if (int_sta_1) {
1482 		usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
1483 		usb3_irq_epc_int_1(usb3, int_sta_1);
1484 	}
1485 
1486 	int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
1487 	if (int_sta_2)
1488 		usb3_irq_epc_int_2(usb3, int_sta_2);
1489 }
1490 
1491 static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
1492 {
1493 	struct renesas_usb3 *usb3 = _usb3;
1494 	irqreturn_t ret = IRQ_NONE;
1495 	u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
1496 
1497 	if (axi_int_sta & AXI_INT_EPCINT) {
1498 		usb3_irq_epc(usb3);
1499 		ret = IRQ_HANDLED;
1500 	}
1501 
1502 	return ret;
1503 }
1504 
1505 static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
1506 			      const struct usb_endpoint_descriptor *desc)
1507 {
1508 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1509 	u32 val = 0;
1510 
1511 	val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
1512 	val |= PN_MOD_TYPE(usb_endpoint_type(desc));
1513 	val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
1514 	usb3_write(usb3, val, USB3_PN_MOD);
1515 }
1516 
1517 static u32 usb3_calc_ramarea(int ram_size)
1518 {
1519 	WARN_ON(ram_size > SZ_16K);
1520 
1521 	if (ram_size <= SZ_1K)
1522 		return PN_RAMMAP_RAMAREA_1KB;
1523 	else if (ram_size <= SZ_2K)
1524 		return PN_RAMMAP_RAMAREA_2KB;
1525 	else if (ram_size <= SZ_4K)
1526 		return PN_RAMMAP_RAMAREA_4KB;
1527 	else if (ram_size <= SZ_8K)
1528 		return PN_RAMMAP_RAMAREA_8KB;
1529 	else
1530 		return PN_RAMMAP_RAMAREA_16KB;
1531 }
1532 
1533 static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
1534 				const struct usb_endpoint_descriptor *desc)
1535 {
1536 	return usb3_ep->rammap_val | PN_RAMMAP_MPKT(usb_endpoint_maxp(desc));
1537 }
1538 
1539 static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
1540 			      const struct usb_endpoint_descriptor *desc)
1541 {
1542 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1543 	unsigned long flags;
1544 
1545 	usb3_ep->dir_in = usb_endpoint_dir_in(desc);
1546 
1547 	spin_lock_irqsave(&usb3->lock, flags);
1548 	if (!usb3_pn_change(usb3, usb3_ep->num)) {
1549 		usb3_write_pn_mod(usb3_ep, desc);
1550 		usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
1551 			   USB3_PN_RAMMAP);
1552 		usb3_pn_con_clear(usb3);
1553 		usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1554 	}
1555 	spin_unlock_irqrestore(&usb3->lock, flags);
1556 
1557 	return 0;
1558 }
1559 
1560 static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
1561 {
1562 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1563 	unsigned long flags;
1564 
1565 	usb3_ep->halt = false;
1566 
1567 	spin_lock_irqsave(&usb3->lock, flags);
1568 	if (!usb3_pn_change(usb3, usb3_ep->num)) {
1569 		usb3_write(usb3, 0, USB3_PN_RAMMAP);
1570 		usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
1571 	}
1572 	spin_unlock_irqrestore(&usb3->lock, flags);
1573 
1574 	return 0;
1575 }
1576 
1577 /*------- usb_ep_ops -----------------------------------------------------*/
1578 static int renesas_usb3_ep_enable(struct usb_ep *_ep,
1579 				  const struct usb_endpoint_descriptor *desc)
1580 {
1581 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1582 
1583 	return usb3_enable_pipe_n(usb3_ep, desc);
1584 }
1585 
1586 static int renesas_usb3_ep_disable(struct usb_ep *_ep)
1587 {
1588 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1589 	struct renesas_usb3_request *usb3_req;
1590 
1591 	do {
1592 		usb3_req = usb3_get_request(usb3_ep);
1593 		if (!usb3_req)
1594 			break;
1595 		usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
1596 	} while (1);
1597 
1598 	return usb3_disable_pipe_n(usb3_ep);
1599 }
1600 
1601 static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
1602 {
1603 	struct renesas_usb3_request *usb3_req;
1604 
1605 	usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
1606 	if (!usb3_req)
1607 		return NULL;
1608 
1609 	INIT_LIST_HEAD(&usb3_req->queue);
1610 
1611 	return &usb3_req->req;
1612 }
1613 
1614 static void __renesas_usb3_ep_free_request(struct usb_request *_req)
1615 {
1616 	struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1617 
1618 	kfree(usb3_req);
1619 }
1620 
1621 static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
1622 							 gfp_t gfp_flags)
1623 {
1624 	return __renesas_usb3_ep_alloc_request(gfp_flags);
1625 }
1626 
1627 static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
1628 					 struct usb_request *_req)
1629 {
1630 	__renesas_usb3_ep_free_request(_req);
1631 }
1632 
1633 static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1634 {
1635 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1636 	struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1637 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1638 
1639 	dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
1640 		_req->length);
1641 
1642 	usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
1643 
1644 	return 0;
1645 }
1646 
1647 static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
1648 {
1649 	return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
1650 }
1651 
1652 static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
1653 {
1654 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1655 
1656 	usb3_ep->wedge = true;
1657 	return usb3_set_halt(usb3_ep, true, false);
1658 }
1659 
1660 static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
1661 {
1662 	struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1663 	struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1664 	unsigned long flags;
1665 
1666 	if (usb3_ep->num) {
1667 		spin_lock_irqsave(&usb3->lock, flags);
1668 		if (!usb3_pn_change(usb3, usb3_ep->num)) {
1669 			usb3_pn_con_clear(usb3);
1670 			usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1671 		}
1672 		spin_unlock_irqrestore(&usb3->lock, flags);
1673 	} else {
1674 		usb3_p0_con_clear_buffer(usb3);
1675 	}
1676 }
1677 
1678 static struct usb_ep_ops renesas_usb3_ep_ops = {
1679 	.enable		= renesas_usb3_ep_enable,
1680 	.disable	= renesas_usb3_ep_disable,
1681 
1682 	.alloc_request	= renesas_usb3_ep_alloc_request,
1683 	.free_request	= renesas_usb3_ep_free_request,
1684 
1685 	.queue		= renesas_usb3_ep_queue,
1686 	.dequeue	= renesas_usb3_ep_dequeue,
1687 
1688 	.set_halt	= renesas_usb3_ep_set_halt,
1689 	.set_wedge	= renesas_usb3_ep_set_wedge,
1690 	.fifo_flush	= renesas_usb3_ep_fifo_flush,
1691 };
1692 
1693 /*------- usb_gadget_ops -------------------------------------------------*/
1694 static int renesas_usb3_start(struct usb_gadget *gadget,
1695 			      struct usb_gadget_driver *driver)
1696 {
1697 	struct renesas_usb3 *usb3;
1698 
1699 	if (!driver || driver->max_speed < USB_SPEED_FULL ||
1700 	    !driver->setup)
1701 		return -EINVAL;
1702 
1703 	usb3 = gadget_to_renesas_usb3(gadget);
1704 
1705 	/* hook up the driver */
1706 	usb3->driver = driver;
1707 
1708 	renesas_usb3_init_controller(usb3);
1709 
1710 	return 0;
1711 }
1712 
1713 static int renesas_usb3_stop(struct usb_gadget *gadget)
1714 {
1715 	struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1716 	unsigned long flags;
1717 
1718 	spin_lock_irqsave(&usb3->lock, flags);
1719 	usb3->softconnect = false;
1720 	usb3->gadget.speed = USB_SPEED_UNKNOWN;
1721 	usb3->driver = NULL;
1722 	renesas_usb3_stop_controller(usb3);
1723 	spin_unlock_irqrestore(&usb3->lock, flags);
1724 
1725 	return 0;
1726 }
1727 
1728 static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
1729 {
1730 	return -EOPNOTSUPP;
1731 }
1732 
1733 static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
1734 {
1735 	struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1736 
1737 	usb3->softconnect = !!is_on;
1738 
1739 	return 0;
1740 }
1741 
1742 static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
1743 {
1744 	gadget->is_selfpowered = !!is_self;
1745 
1746 	return 0;
1747 }
1748 
1749 static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
1750 	.get_frame		= renesas_usb3_get_frame,
1751 	.udc_start		= renesas_usb3_start,
1752 	.udc_stop		= renesas_usb3_stop,
1753 	.pullup			= renesas_usb3_pullup,
1754 	.set_selfpowered	= renesas_usb3_set_selfpowered,
1755 };
1756 
1757 /*------- platform_driver ------------------------------------------------*/
1758 static int renesas_usb3_remove(struct platform_device *pdev)
1759 {
1760 	struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
1761 
1762 	pm_runtime_put(&pdev->dev);
1763 	pm_runtime_disable(&pdev->dev);
1764 
1765 	usb_del_gadget_udc(&usb3->gadget);
1766 
1767 	__renesas_usb3_ep_free_request(usb3->ep0_req);
1768 
1769 	return 0;
1770 }
1771 
1772 static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
1773 				const struct renesas_usb3_priv *priv)
1774 {
1775 	struct renesas_usb3_ep *usb3_ep;
1776 	int i;
1777 
1778 	/* calculate num_usb3_eps from renesas_usb3_priv */
1779 	usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
1780 			     priv->ramsize_per_pipe + 1;
1781 
1782 	if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
1783 		usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
1784 
1785 	usb3->usb3_ep = devm_kzalloc(dev, sizeof(*usb3_ep) * usb3->num_usb3_eps,
1786 				     GFP_KERNEL);
1787 	if (!usb3->usb3_ep)
1788 		return -ENOMEM;
1789 
1790 	dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
1791 	/*
1792 	 * This driver prepares pipes as the followings:
1793 	 *  - odd pipes = IN pipe
1794 	 *  - even pipes = OUT pipe (except pipe 0)
1795 	 */
1796 	usb3_for_each_ep(usb3_ep, usb3, i) {
1797 		snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
1798 		usb3_ep->usb3 = usb3;
1799 		usb3_ep->num = i;
1800 		usb3_ep->ep.name = usb3_ep->ep_name;
1801 		usb3_ep->ep.ops = &renesas_usb3_ep_ops;
1802 		INIT_LIST_HEAD(&usb3_ep->queue);
1803 		INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
1804 		if (!i) {
1805 			/* for control pipe */
1806 			usb3->gadget.ep0 = &usb3_ep->ep;
1807 			usb_ep_set_maxpacket_limit(&usb3_ep->ep,
1808 						USB3_EP0_HSFS_MAX_PACKET_SIZE);
1809 			usb3_ep->ep.caps.type_control = true;
1810 			usb3_ep->ep.caps.dir_in = true;
1811 			usb3_ep->ep.caps.dir_out = true;
1812 			continue;
1813 		}
1814 
1815 		/* for bulk or interrupt pipe */
1816 		usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
1817 		list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
1818 		usb3_ep->ep.caps.type_bulk = true;
1819 		usb3_ep->ep.caps.type_int = true;
1820 		if (i & 1)
1821 			usb3_ep->ep.caps.dir_in = true;
1822 		else
1823 			usb3_ep->ep.caps.dir_out = true;
1824 	}
1825 
1826 	return 0;
1827 }
1828 
1829 static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
1830 				  const struct renesas_usb3_priv *priv)
1831 {
1832 	struct renesas_usb3_ep *usb3_ep;
1833 	int i;
1834 	u32 ramif[2], basead[2];	/* index 0 = for IN pipes */
1835 	u32 *cur_ramif, *cur_basead;
1836 	u32 val;
1837 
1838 	memset(ramif, 0, sizeof(ramif));
1839 	memset(basead, 0, sizeof(basead));
1840 
1841 	/*
1842 	 * This driver prepares pipes as the followings:
1843 	 *  - all pipes = the same size as "ramsize_per_pipe"
1844 	 * Please refer to the "Method of Specifying RAM Mapping"
1845 	 */
1846 	usb3_for_each_ep(usb3_ep, usb3, i) {
1847 		if (!i)
1848 			continue;	/* out of scope if ep num = 0 */
1849 		if (usb3_ep->ep.caps.dir_in) {
1850 			cur_ramif = &ramif[0];
1851 			cur_basead = &basead[0];
1852 		} else {
1853 			cur_ramif = &ramif[1];
1854 			cur_basead = &basead[1];
1855 		}
1856 
1857 		if (*cur_basead > priv->ramsize_per_ramif)
1858 			continue;	/* out of memory for IN or OUT pipe */
1859 
1860 		/* calculate rammap_val */
1861 		val = PN_RAMMAP_RAMIF(*cur_ramif);
1862 		val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
1863 		val |= PN_RAMMAP_BASEAD(*cur_basead);
1864 		usb3_ep->rammap_val = val;
1865 
1866 		dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
1867 			i, val, *cur_ramif, *cur_basead);
1868 
1869 		/* update current ramif */
1870 		if (*cur_ramif + 1 == priv->num_ramif) {
1871 			*cur_ramif = 0;
1872 			*cur_basead += priv->ramsize_per_pipe;
1873 		} else {
1874 			(*cur_ramif)++;
1875 		}
1876 	}
1877 }
1878 
1879 static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795 = {
1880 	.ramsize_per_ramif = SZ_16K,
1881 	.num_ramif = 2,
1882 	.ramsize_per_pipe = SZ_4K,
1883 	.workaround_for_vbus = true,
1884 };
1885 
1886 static const struct of_device_id usb3_of_match[] = {
1887 	{
1888 		.compatible = "renesas,r8a7795-usb3-peri",
1889 		.data = &renesas_usb3_priv_r8a7795,
1890 	},
1891 	{ },
1892 };
1893 MODULE_DEVICE_TABLE(of, usb3_of_match);
1894 
1895 static int renesas_usb3_probe(struct platform_device *pdev)
1896 {
1897 	struct renesas_usb3 *usb3;
1898 	struct resource *res;
1899 	const struct of_device_id *match;
1900 	int irq, ret;
1901 	const struct renesas_usb3_priv *priv;
1902 
1903 	match = of_match_node(usb3_of_match, pdev->dev.of_node);
1904 	if (!match)
1905 		return -ENODEV;
1906 	priv = match->data;
1907 
1908 	irq = platform_get_irq(pdev, 0);
1909 	if (irq < 0)
1910 		return -ENODEV;
1911 
1912 	usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
1913 	if (!usb3)
1914 		return -ENOMEM;
1915 
1916 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1917 	usb3->reg = devm_ioremap_resource(&pdev->dev, res);
1918 	if (IS_ERR(usb3->reg))
1919 		return PTR_ERR(usb3->reg);
1920 
1921 	platform_set_drvdata(pdev, usb3);
1922 	spin_lock_init(&usb3->lock);
1923 
1924 	usb3->gadget.ops = &renesas_usb3_gadget_ops;
1925 	usb3->gadget.name = udc_name;
1926 	usb3->gadget.max_speed = USB_SPEED_SUPER;
1927 	INIT_LIST_HEAD(&usb3->gadget.ep_list);
1928 	ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
1929 	if (ret < 0)
1930 		return ret;
1931 	renesas_usb3_init_ram(usb3, &pdev->dev, priv);
1932 
1933 	ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
1934 			       dev_name(&pdev->dev), usb3);
1935 	if (ret < 0)
1936 		return ret;
1937 
1938 	/* for ep0 handling */
1939 	usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
1940 	if (!usb3->ep0_req)
1941 		return -ENOMEM;
1942 
1943 	ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
1944 	if (ret < 0)
1945 		goto err_add_udc;
1946 
1947 	usb3->workaround_for_vbus = priv->workaround_for_vbus;
1948 
1949 	pm_runtime_enable(&pdev->dev);
1950 	pm_runtime_get_sync(&pdev->dev);
1951 
1952 	dev_info(&pdev->dev, "probed\n");
1953 
1954 	return 0;
1955 
1956 err_add_udc:
1957 	__renesas_usb3_ep_free_request(usb3->ep0_req);
1958 
1959 	return ret;
1960 }
1961 
1962 static struct platform_driver renesas_usb3_driver = {
1963 	.probe		= renesas_usb3_probe,
1964 	.remove		= renesas_usb3_remove,
1965 	.driver		= {
1966 		.name =	(char *)udc_name,
1967 		.of_match_table = of_match_ptr(usb3_of_match),
1968 	},
1969 };
1970 module_platform_driver(renesas_usb3_driver);
1971 
1972 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
1973 MODULE_LICENSE("GPL v2");
1974 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
1975 MODULE_ALIAS("platform:renesas_usb3");
1976