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