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