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