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