1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas USB3.0 Peripheral driver (USB gadget)
5 * Copyright (C) 2015-2017 Renesas Electronics Corporation
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>
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/sys_soc.h>
23 #include <linux/uaccess.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/usb/gadget.h>
27 /* register definitions */
28 #define USB3_AXI_INT_STA 0x008
29 #define USB3_AXI_INT_ENA 0x00c
30 #define USB3_DMA_INT_STA 0x010
31 #define USB3_DMA_INT_ENA 0x014
32 #define USB3_DMA_CH0_CON(n) (0x030 + ((n) - 1) * 0x10) /* n = 1 to 4 */
33 #define USB3_DMA_CH0_PRD_ADR(n) (0x034 + ((n) - 1) * 0x10) /* n = 1 to 4 */
34 #define USB3_USB_COM_CON 0x200
35 #define USB3_USB20_CON 0x204
36 #define USB3_USB30_CON 0x208
37 #define USB3_USB_STA 0x210
38 #define USB3_DRD_CON 0x218
39 #define USB3_USB_INT_STA_1 0x220
40 #define USB3_USB_INT_STA_2 0x224
41 #define USB3_USB_INT_ENA_1 0x228
42 #define USB3_USB_INT_ENA_2 0x22c
43 #define USB3_STUP_DAT_0 0x230
44 #define USB3_STUP_DAT_1 0x234
45 #define USB3_USB_OTG_STA 0x268
46 #define USB3_USB_OTG_INT_STA 0x26c
47 #define USB3_USB_OTG_INT_ENA 0x270
48 #define USB3_P0_MOD 0x280
49 #define USB3_P0_CON 0x288
50 #define USB3_P0_STA 0x28c
51 #define USB3_P0_INT_STA 0x290
52 #define USB3_P0_INT_ENA 0x294
53 #define USB3_P0_LNG 0x2a0
54 #define USB3_P0_READ 0x2a4
55 #define USB3_P0_WRITE 0x2a8
56 #define USB3_PIPE_COM 0x2b0
57 #define USB3_PN_MOD 0x2c0
58 #define USB3_PN_RAMMAP 0x2c4
59 #define USB3_PN_CON 0x2c8
60 #define USB3_PN_STA 0x2cc
61 #define USB3_PN_INT_STA 0x2d0
62 #define USB3_PN_INT_ENA 0x2d4
63 #define USB3_PN_LNG 0x2e0
64 #define USB3_PN_READ 0x2e4
65 #define USB3_PN_WRITE 0x2e8
66 #define USB3_SSIFCMD 0x340
68 /* AXI_INT_ENA and AXI_INT_STA */
69 #define AXI_INT_DMAINT BIT(31)
70 #define AXI_INT_EPCINT BIT(30)
71 /* PRD's n = from 1 to 4 */
72 #define AXI_INT_PRDEN_CLR_STA_SHIFT(n) (16 + (n) - 1)
73 #define AXI_INT_PRDERR_STA_SHIFT(n) (0 + (n) - 1)
74 #define AXI_INT_PRDEN_CLR_STA(n) (1 << AXI_INT_PRDEN_CLR_STA_SHIFT(n))
75 #define AXI_INT_PRDERR_STA(n) (1 << AXI_INT_PRDERR_STA_SHIFT(n))
77 /* DMA_INT_ENA and DMA_INT_STA */
78 #define DMA_INT(n) BIT(n)
81 #define DMA_CON_PIPE_DIR BIT(15) /* 1: In Transfer */
82 #define DMA_CON_PIPE_NO_SHIFT 8
83 #define DMA_CON_PIPE_NO_MASK GENMASK(12, DMA_CON_PIPE_NO_SHIFT)
84 #define DMA_COM_PIPE_NO(n) (((n) << DMA_CON_PIPE_NO_SHIFT) & \
86 #define DMA_CON_PRD_EN BIT(0)
89 #define LCLKSEL_LSEL BIT(18)
92 #define USB_COM_CON_CONF BIT(24)
93 #define USB_COM_CON_PN_WDATAIF_NL BIT(23)
94 #define USB_COM_CON_PN_RDATAIF_NL BIT(22)
95 #define USB_COM_CON_PN_LSTTR_PP BIT(21)
96 #define USB_COM_CON_SPD_MODE BIT(17)
97 #define USB_COM_CON_EP0_EN BIT(16)
98 #define USB_COM_CON_DEV_ADDR_SHIFT 8
99 #define USB_COM_CON_DEV_ADDR_MASK GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
100 #define USB_COM_CON_DEV_ADDR(n) (((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
101 USB_COM_CON_DEV_ADDR_MASK)
102 #define USB_COM_CON_RX_DETECTION BIT(1)
103 #define USB_COM_CON_PIPE_CLR BIT(0)
106 #define USB20_CON_B2_PUE BIT(31)
107 #define USB20_CON_B2_SUSPEND BIT(24)
108 #define USB20_CON_B2_CONNECT BIT(17)
109 #define USB20_CON_B2_TSTMOD_SHIFT 8
110 #define USB20_CON_B2_TSTMOD_MASK GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
111 #define USB20_CON_B2_TSTMOD(n) (((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
112 USB20_CON_B2_TSTMOD_MASK)
113 #define USB20_CON_B2_TSTMOD_EN BIT(0)
116 #define USB30_CON_POW_SEL_SHIFT 24
117 #define USB30_CON_POW_SEL_MASK GENMASK(26, USB30_CON_POW_SEL_SHIFT)
118 #define USB30_CON_POW_SEL_IN_U3 BIT(26)
119 #define USB30_CON_POW_SEL_IN_DISCON 0
120 #define USB30_CON_POW_SEL_P2_TO_P0 BIT(25)
121 #define USB30_CON_POW_SEL_P0_TO_P3 BIT(24)
122 #define USB30_CON_POW_SEL_P0_TO_P2 0
123 #define USB30_CON_B3_PLLWAKE BIT(23)
124 #define USB30_CON_B3_CONNECT BIT(17)
125 #define USB30_CON_B3_HOTRST_CMP BIT(1)
128 #define USB_STA_SPEED_MASK (BIT(2) | BIT(1))
129 #define USB_STA_SPEED_HS BIT(2)
130 #define USB_STA_SPEED_FS BIT(1)
131 #define USB_STA_SPEED_SS 0
132 #define USB_STA_VBUS_STA BIT(0)
135 #define DRD_CON_PERI_CON BIT(24)
136 #define DRD_CON_VBOUT BIT(0)
138 /* USB_INT_ENA_1 and USB_INT_STA_1 */
139 #define USB_INT_1_B3_PLLWKUP BIT(31)
140 #define USB_INT_1_B3_LUPSUCS BIT(30)
141 #define USB_INT_1_B3_DISABLE BIT(27)
142 #define USB_INT_1_B3_WRMRST BIT(21)
143 #define USB_INT_1_B3_HOTRST BIT(20)
144 #define USB_INT_1_B2_USBRST BIT(12)
145 #define USB_INT_1_B2_L1SPND BIT(11)
146 #define USB_INT_1_B2_SPND BIT(9)
147 #define USB_INT_1_B2_RSUM BIT(8)
148 #define USB_INT_1_SPEED BIT(1)
149 #define USB_INT_1_VBUS_CNG BIT(0)
151 /* USB_INT_ENA_2 and USB_INT_STA_2 */
152 #define USB_INT_2_PIPE(n) BIT(n)
154 /* USB_OTG_STA, USB_OTG_INT_STA and USB_OTG_INT_ENA */
155 #define USB_OTG_IDMON BIT(4)
158 #define P0_MOD_DIR BIT(6)
160 /* P0_CON and PN_CON */
161 #define PX_CON_BYTE_EN_MASK (BIT(10) | BIT(9))
162 #define PX_CON_BYTE_EN_SHIFT 9
163 #define PX_CON_BYTE_EN_BYTES(n) (((n) << PX_CON_BYTE_EN_SHIFT) & \
165 #define PX_CON_SEND BIT(8)
168 #define P0_CON_ST_RES_MASK (BIT(27) | BIT(26))
169 #define P0_CON_ST_RES_FORCE_STALL BIT(27)
170 #define P0_CON_ST_RES_NORMAL BIT(26)
171 #define P0_CON_ST_RES_FORCE_NRDY 0
172 #define P0_CON_OT_RES_MASK (BIT(25) | BIT(24))
173 #define P0_CON_OT_RES_FORCE_STALL BIT(25)
174 #define P0_CON_OT_RES_NORMAL BIT(24)
175 #define P0_CON_OT_RES_FORCE_NRDY 0
176 #define P0_CON_IN_RES_MASK (BIT(17) | BIT(16))
177 #define P0_CON_IN_RES_FORCE_STALL BIT(17)
178 #define P0_CON_IN_RES_NORMAL BIT(16)
179 #define P0_CON_IN_RES_FORCE_NRDY 0
180 #define P0_CON_RES_WEN BIT(7)
181 #define P0_CON_BCLR BIT(1)
183 /* P0_STA and PN_STA */
184 #define PX_STA_BUFSTS BIT(0)
186 /* P0_INT_ENA and P0_INT_STA */
187 #define P0_INT_STSED BIT(18)
188 #define P0_INT_STSST BIT(17)
189 #define P0_INT_SETUP BIT(16)
190 #define P0_INT_RCVNL BIT(8)
191 #define P0_INT_ERDY BIT(7)
192 #define P0_INT_FLOW BIT(6)
193 #define P0_INT_STALL BIT(2)
194 #define P0_INT_NRDY BIT(1)
195 #define P0_INT_BFRDY BIT(0)
196 #define P0_INT_ALL_BITS (P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
199 #define PN_MOD_DIR BIT(6)
200 #define PN_MOD_TYPE_SHIFT 4
201 #define PN_MOD_TYPE_MASK GENMASK(5, PN_MOD_TYPE_SHIFT)
202 #define PN_MOD_TYPE(n) (((n) << PN_MOD_TYPE_SHIFT) & \
204 #define PN_MOD_EPNUM_MASK GENMASK(3, 0)
205 #define PN_MOD_EPNUM(n) ((n) & PN_MOD_EPNUM_MASK)
208 #define PN_RAMMAP_RAMAREA_SHIFT 29
209 #define PN_RAMMAP_RAMAREA_MASK GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
210 #define PN_RAMMAP_RAMAREA_16KB BIT(31)
211 #define PN_RAMMAP_RAMAREA_8KB (BIT(30) | BIT(29))
212 #define PN_RAMMAP_RAMAREA_4KB BIT(30)
213 #define PN_RAMMAP_RAMAREA_2KB BIT(29)
214 #define PN_RAMMAP_RAMAREA_1KB 0
215 #define PN_RAMMAP_MPKT_SHIFT 16
216 #define PN_RAMMAP_MPKT_MASK GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
217 #define PN_RAMMAP_MPKT(n) (((n) << PN_RAMMAP_MPKT_SHIFT) & \
219 #define PN_RAMMAP_RAMIF_SHIFT 14
220 #define PN_RAMMAP_RAMIF_MASK GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
221 #define PN_RAMMAP_RAMIF(n) (((n) << PN_RAMMAP_RAMIF_SHIFT) & \
222 PN_RAMMAP_RAMIF_MASK)
223 #define PN_RAMMAP_BASEAD_MASK GENMASK(13, 0)
224 #define PN_RAMMAP_BASEAD(offs) (((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
225 #define PN_RAMMAP_DATA(area, ramif, basead) ((PN_RAMMAP_##area) | \
226 (PN_RAMMAP_RAMIF(ramif)) | \
227 (PN_RAMMAP_BASEAD(basead)))
230 #define PN_CON_EN BIT(31)
231 #define PN_CON_DATAIF_EN BIT(30)
232 #define PN_CON_RES_MASK (BIT(17) | BIT(16))
233 #define PN_CON_RES_FORCE_STALL BIT(17)
234 #define PN_CON_RES_NORMAL BIT(16)
235 #define PN_CON_RES_FORCE_NRDY 0
236 #define PN_CON_LAST BIT(11)
237 #define PN_CON_RES_WEN BIT(7)
238 #define PN_CON_CLR BIT(0)
240 /* PN_INT_STA and PN_INT_ENA */
241 #define PN_INT_LSTTR BIT(4)
242 #define PN_INT_BFRDY BIT(0)
245 #define SSIFCMD_URES_U2 BIT(9)
246 #define SSIFCMD_URES_U1 BIT(8)
247 #define SSIFCMD_UDIR_U2 BIT(7)
248 #define SSIFCMD_UDIR_U1 BIT(6)
249 #define SSIFCMD_UREQ_U2 BIT(5)
250 #define SSIFCMD_UREQ_U1 BIT(4)
252 #define USB3_EP0_SS_MAX_PACKET_SIZE 512
253 #define USB3_EP0_HSFS_MAX_PACKET_SIZE 64
254 #define USB3_EP0_BUF_SIZE 8
255 #define USB3_MAX_NUM_PIPES 6 /* This includes PIPE 0 */
256 #define USB3_WAIT_US 3
257 #define USB3_DMA_NUM_SETTING_AREA 4
259 * To avoid double-meaning of "0" (xferred 65536 bytes or received zlp if
260 * buffer size is 65536), this driver uses the maximum size per a entry is
263 #define USB3_DMA_MAX_XFER_SIZE 32768
264 #define USB3_DMA_PRD_SIZE 4096
268 /* Physical Region Descriptor Table */
269 struct renesas_usb3_prd
{
271 #define USB3_PRD1_E BIT(30) /* the end of chain */
272 #define USB3_PRD1_U BIT(29) /* completion of transfer */
273 #define USB3_PRD1_D BIT(28) /* Error occurred */
274 #define USB3_PRD1_INT BIT(27) /* Interrupt occurred */
275 #define USB3_PRD1_LST BIT(26) /* Last Packet */
276 #define USB3_PRD1_B_INC BIT(24)
277 #define USB3_PRD1_MPS_8 0
278 #define USB3_PRD1_MPS_16 BIT(21)
279 #define USB3_PRD1_MPS_32 BIT(22)
280 #define USB3_PRD1_MPS_64 (BIT(22) | BIT(21))
281 #define USB3_PRD1_MPS_512 BIT(23)
282 #define USB3_PRD1_MPS_1024 (BIT(23) | BIT(21))
283 #define USB3_PRD1_MPS_RESERVED (BIT(23) | BIT(22) | BIT(21))
284 #define USB3_PRD1_SIZE_MASK GENMASK(15, 0)
288 #define USB3_DMA_NUM_PRD_ENTRIES (USB3_DMA_PRD_SIZE / \
289 sizeof(struct renesas_usb3_prd))
290 #define USB3_DMA_MAX_XFER_SIZE_ALL_PRDS (USB3_DMA_PRD_SIZE / \
291 sizeof(struct renesas_usb3_prd) * \
292 USB3_DMA_MAX_XFER_SIZE)
294 struct renesas_usb3_dma
{
295 struct renesas_usb3_prd
*prd
;
297 int num
; /* Setting area number (from 1 to 4) */
301 struct renesas_usb3_request
{
302 struct usb_request req
;
303 struct list_head queue
;
306 #define USB3_EP_NAME_SIZE 8
307 struct renesas_usb3_ep
{
309 struct renesas_usb3
*usb3
;
310 struct renesas_usb3_dma
*dma
;
312 char ep_name
[USB3_EP_NAME_SIZE
];
313 struct list_head queue
;
321 struct renesas_usb3_priv
{
322 int ramsize_per_ramif
; /* unit = bytes */
324 int ramsize_per_pipe
; /* unit = bytes */
325 bool workaround_for_vbus
; /* if true, don't check vbus signal */
328 struct renesas_usb3
{
331 struct usb_gadget gadget
;
332 struct usb_gadget_driver
*driver
;
333 struct extcon_dev
*extcon
;
334 struct work_struct extcon_work
;
337 struct renesas_usb3_ep
*usb3_ep
;
340 struct renesas_usb3_dma dma
[USB3_DMA_NUM_SETTING_AREA
];
345 struct usb_request
*ep0_req
;
347 u8 ep0_buf
[USB3_EP0_BUF_SIZE
];
349 bool workaround_for_vbus
;
350 bool extcon_host
; /* check id and set EXTCON_USB_HOST */
351 bool extcon_usb
; /* check vbus and set EXTCON_USB */
352 bool forced_b_device
;
355 #define gadget_to_renesas_usb3(_gadget) \
356 container_of(_gadget, struct renesas_usb3, gadget)
357 #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
358 #define usb3_to_dev(_usb3) (_usb3->gadget.dev.parent)
360 #define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
361 #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
362 #define usb_req_to_usb3_req(_req) container_of(_req, \
363 struct renesas_usb3_request, req)
365 #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
366 #define usb3_for_each_ep(usb3_ep, usb3, i) \
367 for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
368 (i) < (usb3)->num_usb3_eps; \
369 (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
371 #define usb3_get_dma(usb3, i) (&(usb3)->dma[i])
372 #define usb3_for_each_dma(usb3, dma, i) \
373 for ((i) = 0, dma = usb3_get_dma((usb3), (i)); \
374 (i) < USB3_DMA_NUM_SETTING_AREA; \
375 (i)++, dma = usb3_get_dma((usb3), (i)))
377 static const char udc_name
[] = "renesas_usb3";
379 static bool use_dma
= 1;
380 module_param(use_dma
, bool, 0644);
381 MODULE_PARM_DESC(use_dma
, "use dedicated DMAC");
383 static void usb3_write(struct renesas_usb3
*usb3
, u32 data
, u32 offs
)
385 iowrite32(data
, usb3
->reg
+ offs
);
388 static u32
usb3_read(struct renesas_usb3
*usb3
, u32 offs
)
390 return ioread32(usb3
->reg
+ offs
);
393 static void usb3_set_bit(struct renesas_usb3
*usb3
, u32 bits
, u32 offs
)
395 u32 val
= usb3_read(usb3
, offs
);
398 usb3_write(usb3
, val
, offs
);
401 static void usb3_clear_bit(struct renesas_usb3
*usb3
, u32 bits
, u32 offs
)
403 u32 val
= usb3_read(usb3
, offs
);
406 usb3_write(usb3
, val
, offs
);
409 static int usb3_wait(struct renesas_usb3
*usb3
, u32 reg
, u32 mask
,
414 for (i
= 0; i
< USB3_WAIT_US
; i
++) {
415 if ((usb3_read(usb3
, reg
) & mask
) == expected
)
420 dev_dbg(usb3_to_dev(usb3
), "%s: timed out (%8x, %08x, %08x)\n",
421 __func__
, reg
, mask
, expected
);
426 static void renesas_usb3_extcon_work(struct work_struct
*work
)
428 struct renesas_usb3
*usb3
= container_of(work
, struct renesas_usb3
,
431 extcon_set_state_sync(usb3
->extcon
, EXTCON_USB_HOST
, usb3
->extcon_host
);
432 extcon_set_state_sync(usb3
->extcon
, EXTCON_USB
, usb3
->extcon_usb
);
435 static void usb3_enable_irq_1(struct renesas_usb3
*usb3
, u32 bits
)
437 usb3_set_bit(usb3
, bits
, USB3_USB_INT_ENA_1
);
440 static void usb3_disable_irq_1(struct renesas_usb3
*usb3
, u32 bits
)
442 usb3_clear_bit(usb3
, bits
, USB3_USB_INT_ENA_1
);
445 static void usb3_enable_pipe_irq(struct renesas_usb3
*usb3
, int num
)
447 usb3_set_bit(usb3
, USB_INT_2_PIPE(num
), USB3_USB_INT_ENA_2
);
450 static void usb3_disable_pipe_irq(struct renesas_usb3
*usb3
, int num
)
452 usb3_clear_bit(usb3
, USB_INT_2_PIPE(num
), USB3_USB_INT_ENA_2
);
455 static bool usb3_is_host(struct renesas_usb3
*usb3
)
457 return !(usb3_read(usb3
, USB3_DRD_CON
) & DRD_CON_PERI_CON
);
460 static void usb3_init_axi_bridge(struct renesas_usb3
*usb3
)
463 usb3_write(usb3
, ~0, USB3_DMA_INT_STA
);
464 usb3_write(usb3
, 0, USB3_DMA_INT_ENA
);
465 usb3_set_bit(usb3
, AXI_INT_DMAINT
| AXI_INT_EPCINT
, USB3_AXI_INT_ENA
);
468 static void usb3_init_epc_registers(struct renesas_usb3
*usb3
)
470 usb3_write(usb3
, ~0, USB3_USB_INT_STA_1
);
471 usb3_enable_irq_1(usb3
, USB_INT_1_VBUS_CNG
);
474 static bool usb3_wakeup_usb2_phy(struct renesas_usb3
*usb3
)
476 if (!(usb3_read(usb3
, USB3_USB20_CON
) & USB20_CON_B2_SUSPEND
))
477 return true; /* already waked it up */
479 usb3_clear_bit(usb3
, USB20_CON_B2_SUSPEND
, USB3_USB20_CON
);
480 usb3_enable_irq_1(usb3
, USB_INT_1_B2_RSUM
);
485 static void usb3_usb2_pullup(struct renesas_usb3
*usb3
, int pullup
)
487 u32 bits
= USB20_CON_B2_PUE
| USB20_CON_B2_CONNECT
;
489 if (usb3
->softconnect
&& pullup
)
490 usb3_set_bit(usb3
, bits
, USB3_USB20_CON
);
492 usb3_clear_bit(usb3
, bits
, USB3_USB20_CON
);
495 static void usb3_set_test_mode(struct renesas_usb3
*usb3
)
497 u32 val
= usb3_read(usb3
, USB3_USB20_CON
);
499 val
&= ~USB20_CON_B2_TSTMOD_MASK
;
500 val
|= USB20_CON_B2_TSTMOD(usb3
->test_mode
);
501 usb3_write(usb3
, val
| USB20_CON_B2_TSTMOD_EN
, USB3_USB20_CON
);
502 if (!usb3
->test_mode
)
503 usb3_clear_bit(usb3
, USB20_CON_B2_TSTMOD_EN
, USB3_USB20_CON
);
506 static void usb3_start_usb2_connection(struct renesas_usb3
*usb3
)
508 usb3
->disabled_count
++;
509 usb3_set_bit(usb3
, USB_COM_CON_EP0_EN
, USB3_USB_COM_CON
);
510 usb3_set_bit(usb3
, USB_COM_CON_SPD_MODE
, USB3_USB_COM_CON
);
511 usb3_usb2_pullup(usb3
, 1);
514 static int usb3_is_usb3_phy_in_u3(struct renesas_usb3
*usb3
)
516 return usb3_read(usb3
, USB3_USB30_CON
) & USB30_CON_POW_SEL_IN_U3
;
519 static bool usb3_wakeup_usb3_phy(struct renesas_usb3
*usb3
)
521 if (!usb3_is_usb3_phy_in_u3(usb3
))
522 return true; /* already waked it up */
524 usb3_set_bit(usb3
, USB30_CON_B3_PLLWAKE
, USB3_USB30_CON
);
525 usb3_enable_irq_1(usb3
, USB_INT_1_B3_PLLWKUP
);
530 static u16
usb3_feature_get_un_enabled(struct renesas_usb3
*usb3
)
532 u32 mask_u2
= SSIFCMD_UDIR_U2
| SSIFCMD_UREQ_U2
;
533 u32 mask_u1
= SSIFCMD_UDIR_U1
| SSIFCMD_UREQ_U1
;
534 u32 val
= usb3_read(usb3
, USB3_SSIFCMD
);
537 /* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
538 if (!(val
& mask_u2
))
539 ret
|= 1 << USB_DEV_STAT_U2_ENABLED
;
540 if (!(val
& mask_u1
))
541 ret
|= 1 << USB_DEV_STAT_U1_ENABLED
;
546 static void usb3_feature_u2_enable(struct renesas_usb3
*usb3
, bool enable
)
548 u32 bits
= SSIFCMD_UDIR_U2
| SSIFCMD_UREQ_U2
;
550 /* Enables U2 if the bits of UDIR and UREQ are set to 0 */
552 usb3_clear_bit(usb3
, bits
, USB3_SSIFCMD
);
554 usb3_set_bit(usb3
, bits
, USB3_SSIFCMD
);
557 static void usb3_feature_u1_enable(struct renesas_usb3
*usb3
, bool enable
)
559 u32 bits
= SSIFCMD_UDIR_U1
| SSIFCMD_UREQ_U1
;
561 /* Enables U1 if the bits of UDIR and UREQ are set to 0 */
563 usb3_clear_bit(usb3
, bits
, USB3_SSIFCMD
);
565 usb3_set_bit(usb3
, bits
, USB3_SSIFCMD
);
568 static void usb3_start_operation_for_usb3(struct renesas_usb3
*usb3
)
570 usb3_set_bit(usb3
, USB_COM_CON_EP0_EN
, USB3_USB_COM_CON
);
571 usb3_clear_bit(usb3
, USB_COM_CON_SPD_MODE
, USB3_USB_COM_CON
);
572 usb3_set_bit(usb3
, USB30_CON_B3_CONNECT
, USB3_USB30_CON
);
575 static void usb3_start_usb3_connection(struct renesas_usb3
*usb3
)
577 usb3_start_operation_for_usb3(usb3
);
578 usb3_set_bit(usb3
, USB_COM_CON_RX_DETECTION
, USB3_USB_COM_CON
);
580 usb3_enable_irq_1(usb3
, USB_INT_1_B3_LUPSUCS
| USB_INT_1_B3_DISABLE
|
584 static void usb3_stop_usb3_connection(struct renesas_usb3
*usb3
)
586 usb3_clear_bit(usb3
, USB30_CON_B3_CONNECT
, USB3_USB30_CON
);
589 static void usb3_transition_to_default_state(struct renesas_usb3
*usb3
,
592 usb3_set_bit(usb3
, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2
);
593 usb3_write(usb3
, P0_INT_ALL_BITS
, USB3_P0_INT_STA
);
594 usb3_set_bit(usb3
, P0_INT_ALL_BITS
, USB3_P0_INT_ENA
);
597 usb3_enable_irq_1(usb3
, USB_INT_1_B3_WRMRST
|
598 USB_INT_1_B3_HOTRST
);
600 usb3_enable_irq_1(usb3
, USB_INT_1_B2_SPND
|
601 USB_INT_1_B2_L1SPND
| USB_INT_1_B2_USBRST
);
604 static void usb3_connect(struct renesas_usb3
*usb3
)
606 if (usb3_wakeup_usb3_phy(usb3
))
607 usb3_start_usb3_connection(usb3
);
610 static void usb3_reset_epc(struct renesas_usb3
*usb3
)
612 usb3_clear_bit(usb3
, USB_COM_CON_CONF
, USB3_USB_COM_CON
);
613 usb3_clear_bit(usb3
, USB_COM_CON_EP0_EN
, USB3_USB_COM_CON
);
614 usb3_set_bit(usb3
, USB_COM_CON_PIPE_CLR
, USB3_USB_COM_CON
);
616 usb3_set_test_mode(usb3
);
619 static void usb3_disconnect(struct renesas_usb3
*usb3
)
621 usb3
->disabled_count
= 0;
622 usb3_usb2_pullup(usb3
, 0);
623 usb3_clear_bit(usb3
, USB30_CON_B3_CONNECT
, USB3_USB30_CON
);
624 usb3_reset_epc(usb3
);
627 usb3
->driver
->disconnect(&usb3
->gadget
);
630 static void usb3_check_vbus(struct renesas_usb3
*usb3
)
632 if (usb3
->workaround_for_vbus
) {
635 usb3
->extcon_usb
= !!(usb3_read(usb3
, USB3_USB_STA
) &
637 if (usb3
->extcon_usb
)
640 usb3_disconnect(usb3
);
642 schedule_work(&usb3
->extcon_work
);
646 static void usb3_set_mode(struct renesas_usb3
*usb3
, bool host
)
649 usb3_clear_bit(usb3
, DRD_CON_PERI_CON
, USB3_DRD_CON
);
651 usb3_set_bit(usb3
, DRD_CON_PERI_CON
, USB3_DRD_CON
);
654 static void usb3_vbus_out(struct renesas_usb3
*usb3
, bool enable
)
657 usb3_set_bit(usb3
, DRD_CON_VBOUT
, USB3_DRD_CON
);
659 usb3_clear_bit(usb3
, DRD_CON_VBOUT
, USB3_DRD_CON
);
662 static void usb3_mode_config(struct renesas_usb3
*usb3
, bool host
, bool a_dev
)
666 spin_lock_irqsave(&usb3
->lock
, flags
);
667 usb3_set_mode(usb3
, host
);
668 usb3_vbus_out(usb3
, a_dev
);
669 /* for A-Peripheral or forced B-device mode */
670 if ((!host
&& a_dev
) ||
671 (usb3
->workaround_for_vbus
&& usb3
->forced_b_device
))
673 spin_unlock_irqrestore(&usb3
->lock
, flags
);
676 static bool usb3_is_a_device(struct renesas_usb3
*usb3
)
678 return !(usb3_read(usb3
, USB3_USB_OTG_STA
) & USB_OTG_IDMON
);
681 static void usb3_check_id(struct renesas_usb3
*usb3
)
683 usb3
->extcon_host
= usb3_is_a_device(usb3
);
685 if (usb3
->extcon_host
&& !usb3
->forced_b_device
)
686 usb3_mode_config(usb3
, true, true);
688 usb3_mode_config(usb3
, false, false);
690 schedule_work(&usb3
->extcon_work
);
693 static void renesas_usb3_init_controller(struct renesas_usb3
*usb3
)
695 usb3_init_axi_bridge(usb3
);
696 usb3_init_epc_registers(usb3
);
697 usb3_set_bit(usb3
, USB_COM_CON_PN_WDATAIF_NL
|
698 USB_COM_CON_PN_RDATAIF_NL
| USB_COM_CON_PN_LSTTR_PP
,
700 usb3_write(usb3
, USB_OTG_IDMON
, USB3_USB_OTG_INT_STA
);
701 usb3_write(usb3
, USB_OTG_IDMON
, USB3_USB_OTG_INT_ENA
);
704 usb3_check_vbus(usb3
);
707 static void renesas_usb3_stop_controller(struct renesas_usb3
*usb3
)
709 usb3_disconnect(usb3
);
710 usb3_write(usb3
, 0, USB3_P0_INT_ENA
);
711 usb3_write(usb3
, 0, USB3_USB_OTG_INT_ENA
);
712 usb3_write(usb3
, 0, USB3_USB_INT_ENA_1
);
713 usb3_write(usb3
, 0, USB3_USB_INT_ENA_2
);
714 usb3_write(usb3
, 0, USB3_AXI_INT_ENA
);
717 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3
*usb3
)
719 usb3_disable_irq_1(usb3
, USB_INT_1_B3_PLLWKUP
);
720 usb3_clear_bit(usb3
, USB30_CON_B3_PLLWAKE
, USB3_USB30_CON
);
721 usb3_start_usb3_connection(usb3
);
724 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3
*usb3
)
726 usb3_transition_to_default_state(usb3
, true);
729 static void usb3_irq_epc_int_1_resume(struct renesas_usb3
*usb3
)
731 usb3_disable_irq_1(usb3
, USB_INT_1_B2_RSUM
);
732 usb3_start_usb2_connection(usb3
);
733 usb3_transition_to_default_state(usb3
, false);
736 static void usb3_irq_epc_int_1_disable(struct renesas_usb3
*usb3
)
738 usb3_stop_usb3_connection(usb3
);
739 if (usb3_wakeup_usb2_phy(usb3
))
740 usb3_irq_epc_int_1_resume(usb3
);
743 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3
*usb3
)
745 usb3_reset_epc(usb3
);
746 if (usb3
->disabled_count
< 3)
747 usb3_start_usb3_connection(usb3
);
749 usb3_start_usb2_connection(usb3
);
752 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3
*usb3
)
754 usb3_check_vbus(usb3
);
757 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3
*usb3
)
759 usb3_reset_epc(usb3
);
760 usb3_set_bit(usb3
, USB_COM_CON_EP0_EN
, USB3_USB_COM_CON
);
762 /* This bit shall be set within 12ms from the start of HotReset */
763 usb3_set_bit(usb3
, USB30_CON_B3_HOTRST_CMP
, USB3_USB30_CON
);
766 static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3
*usb3
)
768 usb3_reset_epc(usb3
);
769 usb3_set_bit(usb3
, USB_COM_CON_EP0_EN
, USB3_USB_COM_CON
);
771 usb3_start_operation_for_usb3(usb3
);
772 usb3_enable_irq_1(usb3
, USB_INT_1_SPEED
);
775 static void usb3_irq_epc_int_1_speed(struct renesas_usb3
*usb3
)
777 u32 speed
= usb3_read(usb3
, USB3_USB_STA
) & USB_STA_SPEED_MASK
;
780 case USB_STA_SPEED_SS
:
781 usb3
->gadget
.speed
= USB_SPEED_SUPER
;
783 case USB_STA_SPEED_HS
:
784 usb3
->gadget
.speed
= USB_SPEED_HIGH
;
786 case USB_STA_SPEED_FS
:
787 usb3
->gadget
.speed
= USB_SPEED_FULL
;
790 usb3
->gadget
.speed
= USB_SPEED_UNKNOWN
;
795 static void usb3_irq_epc_int_1(struct renesas_usb3
*usb3
, u32 int_sta_1
)
797 if (int_sta_1
& USB_INT_1_B3_PLLWKUP
)
798 usb3_irq_epc_int_1_pll_wakeup(usb3
);
800 if (int_sta_1
& USB_INT_1_B3_LUPSUCS
)
801 usb3_irq_epc_int_1_linkup_success(usb3
);
803 if (int_sta_1
& USB_INT_1_B3_HOTRST
)
804 usb3_irq_epc_int_1_hot_reset(usb3
);
806 if (int_sta_1
& USB_INT_1_B3_WRMRST
)
807 usb3_irq_epc_int_1_warm_reset(usb3
);
809 if (int_sta_1
& USB_INT_1_B3_DISABLE
)
810 usb3_irq_epc_int_1_disable(usb3
);
812 if (int_sta_1
& USB_INT_1_B2_USBRST
)
813 usb3_irq_epc_int_1_bus_reset(usb3
);
815 if (int_sta_1
& USB_INT_1_B2_RSUM
)
816 usb3_irq_epc_int_1_resume(usb3
);
818 if (int_sta_1
& USB_INT_1_SPEED
)
819 usb3_irq_epc_int_1_speed(usb3
);
821 if (int_sta_1
& USB_INT_1_VBUS_CNG
)
822 usb3_irq_epc_int_1_vbus_change(usb3
);
825 static struct renesas_usb3_request
*__usb3_get_request(struct renesas_usb3_ep
828 return list_first_entry_or_null(&usb3_ep
->queue
,
829 struct renesas_usb3_request
, queue
);
832 static struct renesas_usb3_request
*usb3_get_request(struct renesas_usb3_ep
835 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
836 struct renesas_usb3_request
*usb3_req
;
839 spin_lock_irqsave(&usb3
->lock
, flags
);
840 usb3_req
= __usb3_get_request(usb3_ep
);
841 spin_unlock_irqrestore(&usb3
->lock
, flags
);
846 static void __usb3_request_done(struct renesas_usb3_ep
*usb3_ep
,
847 struct renesas_usb3_request
*usb3_req
,
850 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
852 dev_dbg(usb3_to_dev(usb3
), "giveback: ep%2d, %u, %u, %d\n",
853 usb3_ep
->num
, usb3_req
->req
.length
, usb3_req
->req
.actual
,
855 usb3_req
->req
.status
= status
;
856 usb3_ep
->started
= false;
857 list_del_init(&usb3_req
->queue
);
858 spin_unlock(&usb3
->lock
);
859 usb_gadget_giveback_request(&usb3_ep
->ep
, &usb3_req
->req
);
860 spin_lock(&usb3
->lock
);
863 static void usb3_request_done(struct renesas_usb3_ep
*usb3_ep
,
864 struct renesas_usb3_request
*usb3_req
, int status
)
866 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
869 spin_lock_irqsave(&usb3
->lock
, flags
);
870 __usb3_request_done(usb3_ep
, usb3_req
, status
);
871 spin_unlock_irqrestore(&usb3
->lock
, flags
);
874 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3
*usb3
)
876 struct renesas_usb3_ep
*usb3_ep
= usb3_get_ep(usb3
, 0);
877 struct renesas_usb3_request
*usb3_req
= usb3_get_request(usb3_ep
);
880 usb3_request_done(usb3_ep
, usb3_req
, 0);
882 usb3_set_test_mode(usb3
);
885 static void usb3_get_setup_data(struct renesas_usb3
*usb3
,
886 struct usb_ctrlrequest
*ctrl
)
888 struct renesas_usb3_ep
*usb3_ep
= usb3_get_ep(usb3
, 0);
889 u32
*data
= (u32
*)ctrl
;
891 *data
++ = usb3_read(usb3
, USB3_STUP_DAT_0
);
892 *data
= usb3_read(usb3
, USB3_STUP_DAT_1
);
894 /* update this driver's flag */
895 usb3_ep
->dir_in
= !!(ctrl
->bRequestType
& USB_DIR_IN
);
898 static void usb3_set_p0_con_update_res(struct renesas_usb3
*usb3
, u32 res
)
900 u32 val
= usb3_read(usb3
, USB3_P0_CON
);
902 val
&= ~(P0_CON_ST_RES_MASK
| P0_CON_OT_RES_MASK
| P0_CON_IN_RES_MASK
);
903 val
|= res
| P0_CON_RES_WEN
;
904 usb3_write(usb3
, val
, USB3_P0_CON
);
907 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3
*usb3
)
909 usb3_set_p0_con_update_res(usb3
, P0_CON_ST_RES_FORCE_NRDY
|
910 P0_CON_OT_RES_FORCE_STALL
|
911 P0_CON_IN_RES_NORMAL
);
914 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3
*usb3
)
916 usb3_set_p0_con_update_res(usb3
, P0_CON_ST_RES_NORMAL
|
917 P0_CON_OT_RES_FORCE_STALL
|
918 P0_CON_IN_RES_NORMAL
);
921 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3
*usb3
)
923 usb3_set_p0_con_update_res(usb3
, P0_CON_ST_RES_FORCE_NRDY
|
924 P0_CON_OT_RES_NORMAL
|
925 P0_CON_IN_RES_FORCE_STALL
);
928 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3
*usb3
)
930 usb3_set_p0_con_update_res(usb3
, P0_CON_ST_RES_NORMAL
|
931 P0_CON_OT_RES_NORMAL
|
932 P0_CON_IN_RES_FORCE_STALL
);
935 static void usb3_set_p0_con_for_no_data(struct renesas_usb3
*usb3
)
937 usb3_set_p0_con_update_res(usb3
, P0_CON_ST_RES_NORMAL
|
938 P0_CON_OT_RES_FORCE_STALL
|
939 P0_CON_IN_RES_FORCE_STALL
);
942 static void usb3_set_p0_con_stall(struct renesas_usb3
*usb3
)
944 usb3_set_p0_con_update_res(usb3
, P0_CON_ST_RES_FORCE_STALL
|
945 P0_CON_OT_RES_FORCE_STALL
|
946 P0_CON_IN_RES_FORCE_STALL
);
949 static void usb3_set_p0_con_stop(struct renesas_usb3
*usb3
)
951 usb3_set_p0_con_update_res(usb3
, P0_CON_ST_RES_FORCE_NRDY
|
952 P0_CON_OT_RES_FORCE_NRDY
|
953 P0_CON_IN_RES_FORCE_NRDY
);
956 static int usb3_pn_change(struct renesas_usb3
*usb3
, int num
)
958 if (num
== 0 || num
> usb3
->num_usb3_eps
)
961 usb3_write(usb3
, num
, USB3_PIPE_COM
);
966 static void usb3_set_pn_con_update_res(struct renesas_usb3
*usb3
, u32 res
)
968 u32 val
= usb3_read(usb3
, USB3_PN_CON
);
970 val
&= ~PN_CON_RES_MASK
;
971 val
|= res
& PN_CON_RES_MASK
;
972 val
|= PN_CON_RES_WEN
;
973 usb3_write(usb3
, val
, USB3_PN_CON
);
976 static void usb3_pn_start(struct renesas_usb3
*usb3
)
978 usb3_set_pn_con_update_res(usb3
, PN_CON_RES_NORMAL
);
981 static void usb3_pn_stop(struct renesas_usb3
*usb3
)
983 usb3_set_pn_con_update_res(usb3
, PN_CON_RES_FORCE_NRDY
);
986 static void usb3_pn_stall(struct renesas_usb3
*usb3
)
988 usb3_set_pn_con_update_res(usb3
, PN_CON_RES_FORCE_STALL
);
991 static int usb3_pn_con_clear(struct renesas_usb3
*usb3
)
993 usb3_set_bit(usb3
, PN_CON_CLR
, USB3_PN_CON
);
995 return usb3_wait(usb3
, USB3_PN_CON
, PN_CON_CLR
, 0);
998 static bool usb3_is_transfer_complete(struct renesas_usb3_ep
*usb3_ep
,
999 struct renesas_usb3_request
*usb3_req
)
1001 struct usb_request
*req
= &usb3_req
->req
;
1003 if ((!req
->zero
&& req
->actual
== req
->length
) ||
1004 (req
->actual
% usb3_ep
->ep
.maxpacket
) || (req
->length
== 0))
1010 static int usb3_wait_pipe_status(struct renesas_usb3_ep
*usb3_ep
, u32 mask
)
1012 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1013 u32 sta_reg
= usb3_ep
->num
? USB3_PN_STA
: USB3_P0_STA
;
1015 return usb3_wait(usb3
, sta_reg
, mask
, mask
);
1018 static void usb3_set_px_con_send(struct renesas_usb3_ep
*usb3_ep
, int bytes
,
1021 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1022 u32 con_reg
= usb3_ep
->num
? USB3_PN_CON
: USB3_P0_CON
;
1023 u32 val
= usb3_read(usb3
, con_reg
);
1025 val
|= PX_CON_SEND
| PX_CON_BYTE_EN_BYTES(bytes
);
1026 val
|= (usb3_ep
->num
&& last
) ? PN_CON_LAST
: 0;
1027 usb3_write(usb3
, val
, con_reg
);
1030 static int usb3_write_pipe(struct renesas_usb3_ep
*usb3_ep
,
1031 struct renesas_usb3_request
*usb3_req
,
1034 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1036 int len
= min_t(unsigned, usb3_req
->req
.length
- usb3_req
->req
.actual
,
1037 usb3_ep
->ep
.maxpacket
);
1038 u8
*buf
= usb3_req
->req
.buf
+ usb3_req
->req
.actual
;
1040 bool is_last
= !len
? true : false;
1042 if (usb3_wait_pipe_status(usb3_ep
, PX_STA_BUFSTS
) < 0)
1045 /* Update gadget driver parameter */
1046 usb3_req
->req
.actual
+= len
;
1048 /* Write data to the register */
1050 iowrite32_rep(usb3
->reg
+ fifo_reg
, buf
, len
/ 4);
1051 buf
+= (len
/ 4) * 4;
1052 len
%= 4; /* update len to use usb3_set_pX_con_send() */
1056 for (i
= 0; i
< len
; i
++)
1057 tmp
|= buf
[i
] << (8 * i
);
1058 usb3_write(usb3
, tmp
, fifo_reg
);
1062 is_last
= usb3_is_transfer_complete(usb3_ep
, usb3_req
);
1064 usb3_set_px_con_send(usb3_ep
, len
, is_last
);
1066 return is_last
? 0 : -EAGAIN
;
1069 static u32
usb3_get_received_length(struct renesas_usb3_ep
*usb3_ep
)
1071 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1072 u32 lng_reg
= usb3_ep
->num
? USB3_PN_LNG
: USB3_P0_LNG
;
1074 return usb3_read(usb3
, lng_reg
);
1077 static int usb3_read_pipe(struct renesas_usb3_ep
*usb3_ep
,
1078 struct renesas_usb3_request
*usb3_req
, u32 fifo_reg
)
1080 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1082 int len
= min_t(unsigned, usb3_req
->req
.length
- usb3_req
->req
.actual
,
1083 usb3_get_received_length(usb3_ep
));
1084 u8
*buf
= usb3_req
->req
.buf
+ usb3_req
->req
.actual
;
1090 /* Update gadget driver parameter */
1091 usb3_req
->req
.actual
+= len
;
1093 /* Read data from the register */
1095 ioread32_rep(usb3
->reg
+ fifo_reg
, buf
, len
/ 4);
1096 buf
+= (len
/ 4) * 4;
1101 tmp
= usb3_read(usb3
, fifo_reg
);
1102 for (i
= 0; i
< len
; i
++)
1103 buf
[i
] = (tmp
>> (8 * i
)) & 0xff;
1106 return usb3_is_transfer_complete(usb3_ep
, usb3_req
) ? 0 : -EAGAIN
;
1109 static void usb3_set_status_stage(struct renesas_usb3_ep
*usb3_ep
,
1110 struct renesas_usb3_request
*usb3_req
)
1112 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1114 if (usb3_ep
->dir_in
) {
1115 usb3_set_p0_con_for_ctrl_read_status(usb3
);
1117 if (!usb3_req
->req
.length
)
1118 usb3_set_p0_con_for_no_data(usb3
);
1120 usb3_set_p0_con_for_ctrl_write_status(usb3
);
1124 static void usb3_p0_xfer(struct renesas_usb3_ep
*usb3_ep
,
1125 struct renesas_usb3_request
*usb3_req
)
1129 if (usb3_ep
->dir_in
)
1130 ret
= usb3_write_pipe(usb3_ep
, usb3_req
, USB3_P0_WRITE
);
1132 ret
= usb3_read_pipe(usb3_ep
, usb3_req
, USB3_P0_READ
);
1135 usb3_set_status_stage(usb3_ep
, usb3_req
);
1138 static void usb3_start_pipe0(struct renesas_usb3_ep
*usb3_ep
,
1139 struct renesas_usb3_request
*usb3_req
)
1141 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1143 if (usb3_ep
->started
)
1146 usb3_ep
->started
= true;
1148 if (usb3_ep
->dir_in
) {
1149 usb3_set_bit(usb3
, P0_MOD_DIR
, USB3_P0_MOD
);
1150 usb3_set_p0_con_for_ctrl_read_data(usb3
);
1152 usb3_clear_bit(usb3
, P0_MOD_DIR
, USB3_P0_MOD
);
1153 if (usb3_req
->req
.length
)
1154 usb3_set_p0_con_for_ctrl_write_data(usb3
);
1157 usb3_p0_xfer(usb3_ep
, usb3_req
);
1160 static void usb3_enable_dma_pipen(struct renesas_usb3
*usb3
)
1162 usb3_set_bit(usb3
, PN_CON_DATAIF_EN
, USB3_PN_CON
);
1165 static void usb3_disable_dma_pipen(struct renesas_usb3
*usb3
)
1167 usb3_clear_bit(usb3
, PN_CON_DATAIF_EN
, USB3_PN_CON
);
1170 static void usb3_enable_dma_irq(struct renesas_usb3
*usb3
, int num
)
1172 usb3_set_bit(usb3
, DMA_INT(num
), USB3_DMA_INT_ENA
);
1175 static void usb3_disable_dma_irq(struct renesas_usb3
*usb3
, int num
)
1177 usb3_clear_bit(usb3
, DMA_INT(num
), USB3_DMA_INT_ENA
);
1180 static u32
usb3_dma_mps_to_prd_word1(struct renesas_usb3_ep
*usb3_ep
)
1182 switch (usb3_ep
->ep
.maxpacket
) {
1184 return USB3_PRD1_MPS_8
;
1186 return USB3_PRD1_MPS_16
;
1188 return USB3_PRD1_MPS_32
;
1190 return USB3_PRD1_MPS_64
;
1192 return USB3_PRD1_MPS_512
;
1194 return USB3_PRD1_MPS_1024
;
1196 return USB3_PRD1_MPS_RESERVED
;
1200 static bool usb3_dma_get_setting_area(struct renesas_usb3_ep
*usb3_ep
,
1201 struct renesas_usb3_request
*usb3_req
)
1203 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1204 struct renesas_usb3_dma
*dma
;
1208 if (usb3_req
->req
.length
> USB3_DMA_MAX_XFER_SIZE_ALL_PRDS
) {
1209 dev_dbg(usb3_to_dev(usb3
), "%s: the length is too big (%d)\n",
1210 __func__
, usb3_req
->req
.length
);
1214 /* The driver doesn't handle zero-length packet via dmac */
1215 if (!usb3_req
->req
.length
)
1218 if (usb3_dma_mps_to_prd_word1(usb3_ep
) == USB3_PRD1_MPS_RESERVED
)
1221 usb3_for_each_dma(usb3
, dma
, i
) {
1225 if (usb_gadget_map_request(&usb3
->gadget
, &usb3_req
->req
,
1226 usb3_ep
->dir_in
) < 0)
1238 static void usb3_dma_put_setting_area(struct renesas_usb3_ep
*usb3_ep
,
1239 struct renesas_usb3_request
*usb3_req
)
1241 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1243 struct renesas_usb3_dma
*dma
;
1245 usb3_for_each_dma(usb3
, dma
, i
) {
1246 if (usb3_ep
->dma
== dma
) {
1247 usb_gadget_unmap_request(&usb3
->gadget
, &usb3_req
->req
,
1250 usb3_ep
->dma
= NULL
;
1256 static void usb3_dma_fill_prd(struct renesas_usb3_ep
*usb3_ep
,
1257 struct renesas_usb3_request
*usb3_req
)
1259 struct renesas_usb3_prd
*cur_prd
= usb3_ep
->dma
->prd
;
1260 u32 remain
= usb3_req
->req
.length
;
1261 u32 dma
= usb3_req
->req
.dma
;
1266 len
= min_t(u32
, remain
, USB3_DMA_MAX_XFER_SIZE
) &
1267 USB3_PRD1_SIZE_MASK
;
1268 cur_prd
->word1
= usb3_dma_mps_to_prd_word1(usb3_ep
) |
1269 USB3_PRD1_B_INC
| len
;
1273 if (!remain
|| (i
+ 1) < USB3_DMA_NUM_PRD_ENTRIES
)
1280 cur_prd
->word1
|= USB3_PRD1_E
| USB3_PRD1_INT
;
1281 if (usb3_ep
->dir_in
)
1282 cur_prd
->word1
|= USB3_PRD1_LST
;
1285 static void usb3_dma_kick_prd(struct renesas_usb3_ep
*usb3_ep
)
1287 struct renesas_usb3_dma
*dma
= usb3_ep
->dma
;
1288 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1289 u32 dma_con
= DMA_COM_PIPE_NO(usb3_ep
->num
) | DMA_CON_PRD_EN
;
1291 if (usb3_ep
->dir_in
)
1292 dma_con
|= DMA_CON_PIPE_DIR
;
1294 wmb(); /* prd entries should be in system memory here */
1296 usb3_write(usb3
, 1 << usb3_ep
->num
, USB3_DMA_INT_STA
);
1297 usb3_write(usb3
, AXI_INT_PRDEN_CLR_STA(dma
->num
) |
1298 AXI_INT_PRDERR_STA(dma
->num
), USB3_AXI_INT_STA
);
1300 usb3_write(usb3
, dma
->prd_dma
, USB3_DMA_CH0_PRD_ADR(dma
->num
));
1301 usb3_write(usb3
, dma_con
, USB3_DMA_CH0_CON(dma
->num
));
1302 usb3_enable_dma_irq(usb3
, usb3_ep
->num
);
1305 static void usb3_dma_stop_prd(struct renesas_usb3_ep
*usb3_ep
)
1307 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1308 struct renesas_usb3_dma
*dma
= usb3_ep
->dma
;
1310 usb3_disable_dma_irq(usb3
, usb3_ep
->num
);
1311 usb3_write(usb3
, 0, USB3_DMA_CH0_CON(dma
->num
));
1314 static int usb3_dma_update_status(struct renesas_usb3_ep
*usb3_ep
,
1315 struct renesas_usb3_request
*usb3_req
)
1317 struct renesas_usb3_prd
*cur_prd
= usb3_ep
->dma
->prd
;
1318 struct usb_request
*req
= &usb3_req
->req
;
1323 rmb(); /* The controller updated prd entries */
1326 if (cur_prd
->word1
& USB3_PRD1_D
)
1328 if (cur_prd
->word1
& USB3_PRD1_E
)
1329 len
= req
->length
% USB3_DMA_MAX_XFER_SIZE
;
1331 len
= USB3_DMA_MAX_XFER_SIZE
;
1332 remain
= cur_prd
->word1
& USB3_PRD1_SIZE_MASK
;
1333 req
->actual
+= len
- remain
;
1335 if (cur_prd
->word1
& USB3_PRD1_E
||
1336 (i
+ 1) < USB3_DMA_NUM_PRD_ENTRIES
)
1346 static bool usb3_dma_try_start(struct renesas_usb3_ep
*usb3_ep
,
1347 struct renesas_usb3_request
*usb3_req
)
1349 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1354 if (usb3_dma_get_setting_area(usb3_ep
, usb3_req
)) {
1356 usb3_enable_dma_pipen(usb3
);
1357 usb3_dma_fill_prd(usb3_ep
, usb3_req
);
1358 usb3_dma_kick_prd(usb3_ep
);
1359 usb3_pn_start(usb3
);
1366 static int usb3_dma_try_stop(struct renesas_usb3_ep
*usb3_ep
,
1367 struct renesas_usb3_request
*usb3_req
)
1369 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1370 unsigned long flags
;
1373 spin_lock_irqsave(&usb3
->lock
, flags
);
1377 if (!usb3_pn_change(usb3
, usb3_ep
->num
))
1378 usb3_disable_dma_pipen(usb3
);
1379 usb3_dma_stop_prd(usb3_ep
);
1380 status
= usb3_dma_update_status(usb3_ep
, usb3_req
);
1381 usb3_dma_put_setting_area(usb3_ep
, usb3_req
);
1384 spin_unlock_irqrestore(&usb3
->lock
, flags
);
1388 static int renesas_usb3_dma_free_prd(struct renesas_usb3
*usb3
,
1392 struct renesas_usb3_dma
*dma
;
1394 usb3_for_each_dma(usb3
, dma
, i
) {
1396 dma_free_coherent(dev
, USB3_DMA_PRD_SIZE
,
1397 dma
->prd
, dma
->prd_dma
);
1405 static int renesas_usb3_dma_alloc_prd(struct renesas_usb3
*usb3
,
1409 struct renesas_usb3_dma
*dma
;
1414 usb3_for_each_dma(usb3
, dma
, i
) {
1415 dma
->prd
= dma_alloc_coherent(dev
, USB3_DMA_PRD_SIZE
,
1416 &dma
->prd_dma
, GFP_KERNEL
);
1418 renesas_usb3_dma_free_prd(usb3
, dev
);
1427 static void usb3_start_pipen(struct renesas_usb3_ep
*usb3_ep
,
1428 struct renesas_usb3_request
*usb3_req
)
1430 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1431 struct renesas_usb3_request
*usb3_req_first
= usb3_get_request(usb3_ep
);
1432 unsigned long flags
;
1434 u32 enable_bits
= 0;
1436 spin_lock_irqsave(&usb3
->lock
, flags
);
1437 if (usb3_ep
->halt
|| usb3_ep
->started
)
1439 if (usb3_req
!= usb3_req_first
)
1442 if (usb3_pn_change(usb3
, usb3_ep
->num
) < 0)
1445 usb3_ep
->started
= true;
1447 if (usb3_dma_try_start(usb3_ep
, usb3_req
))
1450 usb3_pn_start(usb3
);
1452 if (usb3_ep
->dir_in
) {
1453 ret
= usb3_write_pipe(usb3_ep
, usb3_req
, USB3_PN_WRITE
);
1454 enable_bits
|= PN_INT_LSTTR
;
1458 enable_bits
|= PN_INT_BFRDY
;
1461 usb3_set_bit(usb3
, enable_bits
, USB3_PN_INT_ENA
);
1462 usb3_enable_pipe_irq(usb3
, usb3_ep
->num
);
1465 spin_unlock_irqrestore(&usb3
->lock
, flags
);
1468 static int renesas_usb3_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1471 struct renesas_usb3_ep
*usb3_ep
= usb_ep_to_usb3_ep(_ep
);
1472 struct renesas_usb3_request
*usb3_req
= usb_req_to_usb3_req(_req
);
1473 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1474 unsigned long flags
;
1476 dev_dbg(usb3_to_dev(usb3
), "ep_queue: ep%2d, %u\n", usb3_ep
->num
,
1479 _req
->status
= -EINPROGRESS
;
1481 spin_lock_irqsave(&usb3
->lock
, flags
);
1482 list_add_tail(&usb3_req
->queue
, &usb3_ep
->queue
);
1483 spin_unlock_irqrestore(&usb3
->lock
, flags
);
1486 usb3_start_pipe0(usb3_ep
, usb3_req
);
1488 usb3_start_pipen(usb3_ep
, usb3_req
);
1493 static void usb3_set_device_address(struct renesas_usb3
*usb3
, u16 addr
)
1495 /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1496 usb3_set_bit(usb3
, USB_COM_CON_DEV_ADDR(addr
), USB3_USB_COM_CON
);
1499 static bool usb3_std_req_set_address(struct renesas_usb3
*usb3
,
1500 struct usb_ctrlrequest
*ctrl
)
1502 if (ctrl
->wValue
>= 128)
1503 return true; /* stall */
1505 usb3_set_device_address(usb3
, ctrl
->wValue
);
1506 usb3_set_p0_con_for_no_data(usb3
);
1511 static void usb3_pipe0_internal_xfer(struct renesas_usb3
*usb3
,
1512 void *tx_data
, size_t len
,
1513 void (*complete
)(struct usb_ep
*ep
,
1514 struct usb_request
*req
))
1516 struct renesas_usb3_ep
*usb3_ep
= usb3_get_ep(usb3
, 0);
1519 memcpy(usb3
->ep0_buf
, tx_data
,
1520 min_t(size_t, len
, USB3_EP0_BUF_SIZE
));
1522 usb3
->ep0_req
->buf
= &usb3
->ep0_buf
;
1523 usb3
->ep0_req
->length
= len
;
1524 usb3
->ep0_req
->complete
= complete
;
1525 renesas_usb3_ep_queue(&usb3_ep
->ep
, usb3
->ep0_req
, GFP_ATOMIC
);
1528 static void usb3_pipe0_get_status_completion(struct usb_ep
*ep
,
1529 struct usb_request
*req
)
1533 static bool usb3_std_req_get_status(struct renesas_usb3
*usb3
,
1534 struct usb_ctrlrequest
*ctrl
)
1537 struct renesas_usb3_ep
*usb3_ep
;
1541 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1542 case USB_RECIP_DEVICE
:
1543 if (usb3
->gadget
.is_selfpowered
)
1544 status
|= 1 << USB_DEVICE_SELF_POWERED
;
1545 if (usb3
->gadget
.speed
== USB_SPEED_SUPER
)
1546 status
|= usb3_feature_get_un_enabled(usb3
);
1548 case USB_RECIP_INTERFACE
:
1550 case USB_RECIP_ENDPOINT
:
1551 num
= le16_to_cpu(ctrl
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
1552 usb3_ep
= usb3_get_ep(usb3
, num
);
1554 status
|= 1 << USB_ENDPOINT_HALT
;
1562 status
= cpu_to_le16(status
);
1563 dev_dbg(usb3_to_dev(usb3
), "get_status: req = %p\n",
1564 usb_req_to_usb3_req(usb3
->ep0_req
));
1565 usb3_pipe0_internal_xfer(usb3
, &status
, sizeof(status
),
1566 usb3_pipe0_get_status_completion
);
1572 static bool usb3_std_req_feature_device(struct renesas_usb3
*usb3
,
1573 struct usb_ctrlrequest
*ctrl
, bool set
)
1576 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1579 case USB_DEVICE_TEST_MODE
:
1582 usb3
->test_mode
= le16_to_cpu(ctrl
->wIndex
) >> 8;
1585 case USB_DEVICE_U1_ENABLE
:
1586 case USB_DEVICE_U2_ENABLE
:
1587 if (usb3
->gadget
.speed
!= USB_SPEED_SUPER
)
1589 if (w_value
== USB_DEVICE_U1_ENABLE
)
1590 usb3_feature_u1_enable(usb3
, set
);
1591 if (w_value
== USB_DEVICE_U2_ENABLE
)
1592 usb3_feature_u2_enable(usb3
, set
);
1602 static int usb3_set_halt_p0(struct renesas_usb3_ep
*usb3_ep
, bool halt
)
1604 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1606 if (unlikely(usb3_ep
->num
))
1609 usb3_ep
->halt
= halt
;
1611 usb3_set_p0_con_stall(usb3
);
1613 usb3_set_p0_con_stop(usb3
);
1618 static int usb3_set_halt_pn(struct renesas_usb3_ep
*usb3_ep
, bool halt
,
1619 bool is_clear_feature
)
1621 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
1622 unsigned long flags
;
1624 spin_lock_irqsave(&usb3
->lock
, flags
);
1625 if (!usb3_pn_change(usb3
, usb3_ep
->num
)) {
1626 usb3_ep
->halt
= halt
;
1628 usb3_pn_stall(usb3
);
1629 } else if (!is_clear_feature
|| !usb3_ep
->wedge
) {
1630 usb3_pn_con_clear(usb3
);
1631 usb3_set_bit(usb3
, PN_CON_EN
, USB3_PN_CON
);
1635 spin_unlock_irqrestore(&usb3
->lock
, flags
);
1640 static int usb3_set_halt(struct renesas_usb3_ep
*usb3_ep
, bool halt
,
1641 bool is_clear_feature
)
1645 if (halt
&& usb3_ep
->started
)
1649 ret
= usb3_set_halt_pn(usb3_ep
, halt
, is_clear_feature
);
1651 ret
= usb3_set_halt_p0(usb3_ep
, halt
);
1656 static bool usb3_std_req_feature_endpoint(struct renesas_usb3
*usb3
,
1657 struct usb_ctrlrequest
*ctrl
,
1660 int num
= le16_to_cpu(ctrl
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
1661 struct renesas_usb3_ep
*usb3_ep
;
1662 struct renesas_usb3_request
*usb3_req
;
1664 if (le16_to_cpu(ctrl
->wValue
) != USB_ENDPOINT_HALT
)
1665 return true; /* stall */
1667 usb3_ep
= usb3_get_ep(usb3
, num
);
1668 usb3_set_halt(usb3_ep
, set
, true);
1670 /* Restarts a queue if clear feature */
1672 usb3_ep
->started
= false;
1673 usb3_req
= usb3_get_request(usb3_ep
);
1675 usb3_start_pipen(usb3_ep
, usb3_req
);
1681 static bool usb3_std_req_feature(struct renesas_usb3
*usb3
,
1682 struct usb_ctrlrequest
*ctrl
, bool set
)
1686 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1687 case USB_RECIP_DEVICE
:
1688 stall
= usb3_std_req_feature_device(usb3
, ctrl
, set
);
1690 case USB_RECIP_INTERFACE
:
1692 case USB_RECIP_ENDPOINT
:
1693 stall
= usb3_std_req_feature_endpoint(usb3
, ctrl
, set
);
1701 usb3_set_p0_con_for_no_data(usb3
);
1706 static void usb3_pipe0_set_sel_completion(struct usb_ep
*ep
,
1707 struct usb_request
*req
)
1712 static bool usb3_std_req_set_sel(struct renesas_usb3
*usb3
,
1713 struct usb_ctrlrequest
*ctrl
)
1715 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1718 return true; /* stall */
1720 dev_dbg(usb3_to_dev(usb3
), "set_sel: req = %p\n",
1721 usb_req_to_usb3_req(usb3
->ep0_req
));
1722 usb3_pipe0_internal_xfer(usb3
, NULL
, 6, usb3_pipe0_set_sel_completion
);
1727 static bool usb3_std_req_set_configuration(struct renesas_usb3
*usb3
,
1728 struct usb_ctrlrequest
*ctrl
)
1730 if (ctrl
->wValue
> 0)
1731 usb3_set_bit(usb3
, USB_COM_CON_CONF
, USB3_USB_COM_CON
);
1733 usb3_clear_bit(usb3
, USB_COM_CON_CONF
, USB3_USB_COM_CON
);
1739 * usb3_handle_standard_request - handle some standard requests
1740 * @usb3: the renesas_usb3 pointer
1741 * @ctrl: a pointer of setup data
1743 * Returns true if this function handled a standard request
1745 static bool usb3_handle_standard_request(struct renesas_usb3
*usb3
,
1746 struct usb_ctrlrequest
*ctrl
)
1751 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1752 switch (ctrl
->bRequest
) {
1753 case USB_REQ_SET_ADDRESS
:
1754 stall
= usb3_std_req_set_address(usb3
, ctrl
);
1757 case USB_REQ_GET_STATUS
:
1758 stall
= usb3_std_req_get_status(usb3
, ctrl
);
1761 case USB_REQ_CLEAR_FEATURE
:
1762 stall
= usb3_std_req_feature(usb3
, ctrl
, false);
1765 case USB_REQ_SET_FEATURE
:
1766 stall
= usb3_std_req_feature(usb3
, ctrl
, true);
1769 case USB_REQ_SET_SEL
:
1770 stall
= usb3_std_req_set_sel(usb3
, ctrl
);
1773 case USB_REQ_SET_ISOCH_DELAY
:
1774 /* This hardware doesn't support Isochronous xfer */
1778 case USB_REQ_SET_CONFIGURATION
:
1779 usb3_std_req_set_configuration(usb3
, ctrl
);
1787 usb3_set_p0_con_stall(usb3
);
1792 static int usb3_p0_con_clear_buffer(struct renesas_usb3
*usb3
)
1794 usb3_set_bit(usb3
, P0_CON_BCLR
, USB3_P0_CON
);
1796 return usb3_wait(usb3
, USB3_P0_CON
, P0_CON_BCLR
, 0);
1799 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3
*usb3
)
1801 struct usb_ctrlrequest ctrl
;
1802 struct renesas_usb3_ep
*usb3_ep
= usb3_get_ep(usb3
, 0);
1804 /* Call giveback function if previous transfer is not completed */
1805 if (usb3_ep
->started
)
1806 usb3_request_done(usb3_ep
, usb3_get_request(usb3_ep
),
1809 usb3_p0_con_clear_buffer(usb3
);
1810 usb3_get_setup_data(usb3
, &ctrl
);
1811 if (!usb3_handle_standard_request(usb3
, &ctrl
))
1812 if (usb3
->driver
->setup(&usb3
->gadget
, &ctrl
) < 0)
1813 usb3_set_p0_con_stall(usb3
);
1816 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3
*usb3
)
1818 struct renesas_usb3_ep
*usb3_ep
= usb3_get_ep(usb3
, 0);
1819 struct renesas_usb3_request
*usb3_req
= usb3_get_request(usb3_ep
);
1824 usb3_p0_xfer(usb3_ep
, usb3_req
);
1827 static void usb3_irq_epc_pipe0(struct renesas_usb3
*usb3
)
1829 u32 p0_int_sta
= usb3_read(usb3
, USB3_P0_INT_STA
);
1831 p0_int_sta
&= usb3_read(usb3
, USB3_P0_INT_ENA
);
1832 usb3_write(usb3
, p0_int_sta
, USB3_P0_INT_STA
);
1833 if (p0_int_sta
& P0_INT_STSED
)
1834 usb3_irq_epc_pipe0_status_end(usb3
);
1835 if (p0_int_sta
& P0_INT_SETUP
)
1836 usb3_irq_epc_pipe0_setup(usb3
);
1837 if (p0_int_sta
& P0_INT_BFRDY
)
1838 usb3_irq_epc_pipe0_bfrdy(usb3
);
1841 static void usb3_request_done_pipen(struct renesas_usb3
*usb3
,
1842 struct renesas_usb3_ep
*usb3_ep
,
1843 struct renesas_usb3_request
*usb3_req
,
1846 unsigned long flags
;
1848 spin_lock_irqsave(&usb3
->lock
, flags
);
1849 if (usb3_pn_change(usb3
, usb3_ep
->num
))
1851 spin_unlock_irqrestore(&usb3
->lock
, flags
);
1853 usb3_disable_pipe_irq(usb3
, usb3_ep
->num
);
1854 usb3_request_done(usb3_ep
, usb3_req
, status
);
1856 /* get next usb3_req */
1857 usb3_req
= usb3_get_request(usb3_ep
);
1859 usb3_start_pipen(usb3_ep
, usb3_req
);
1862 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3
*usb3
, int num
)
1864 struct renesas_usb3_ep
*usb3_ep
= usb3_get_ep(usb3
, num
);
1865 struct renesas_usb3_request
*usb3_req
= usb3_get_request(usb3_ep
);
1870 if (usb3_ep
->dir_in
) {
1871 dev_dbg(usb3_to_dev(usb3
), "%s: len = %u, actual = %u\n",
1872 __func__
, usb3_req
->req
.length
, usb3_req
->req
.actual
);
1873 usb3_request_done_pipen(usb3
, usb3_ep
, usb3_req
, 0);
1877 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3
*usb3
, int num
)
1879 struct renesas_usb3_ep
*usb3_ep
= usb3_get_ep(usb3
, num
);
1880 struct renesas_usb3_request
*usb3_req
= usb3_get_request(usb3_ep
);
1886 spin_lock(&usb3
->lock
);
1887 if (usb3_pn_change(usb3
, num
))
1890 if (usb3_ep
->dir_in
) {
1891 /* Do not stop the IN pipe here to detect LSTTR interrupt */
1892 if (!usb3_write_pipe(usb3_ep
, usb3_req
, USB3_PN_WRITE
))
1893 usb3_clear_bit(usb3
, PN_INT_BFRDY
, USB3_PN_INT_ENA
);
1895 if (!usb3_read_pipe(usb3_ep
, usb3_req
, USB3_PN_READ
))
1900 /* need to unlock because usb3_request_done_pipen() locks it */
1901 spin_unlock(&usb3
->lock
);
1904 usb3_request_done_pipen(usb3
, usb3_ep
, usb3_req
, 0);
1907 static void usb3_irq_epc_pipen(struct renesas_usb3
*usb3
, int num
)
1911 spin_lock(&usb3
->lock
);
1912 if (usb3_pn_change(usb3
, num
) < 0) {
1913 spin_unlock(&usb3
->lock
);
1917 pn_int_sta
= usb3_read(usb3
, USB3_PN_INT_STA
);
1918 pn_int_sta
&= usb3_read(usb3
, USB3_PN_INT_ENA
);
1919 usb3_write(usb3
, pn_int_sta
, USB3_PN_INT_STA
);
1920 spin_unlock(&usb3
->lock
);
1921 if (pn_int_sta
& PN_INT_LSTTR
)
1922 usb3_irq_epc_pipen_lsttr(usb3
, num
);
1923 if (pn_int_sta
& PN_INT_BFRDY
)
1924 usb3_irq_epc_pipen_bfrdy(usb3
, num
);
1927 static void usb3_irq_epc_int_2(struct renesas_usb3
*usb3
, u32 int_sta_2
)
1931 for (i
= 0; i
< usb3
->num_usb3_eps
; i
++) {
1932 if (int_sta_2
& USB_INT_2_PIPE(i
)) {
1934 usb3_irq_epc_pipe0(usb3
);
1936 usb3_irq_epc_pipen(usb3
, i
);
1941 static void usb3_irq_idmon_change(struct renesas_usb3
*usb3
)
1943 usb3_check_id(usb3
);
1946 static void usb3_irq_otg_int(struct renesas_usb3
*usb3
, u32 otg_int_sta
)
1948 if (otg_int_sta
& USB_OTG_IDMON
)
1949 usb3_irq_idmon_change(usb3
);
1952 static void usb3_irq_epc(struct renesas_usb3
*usb3
)
1954 u32 int_sta_1
= usb3_read(usb3
, USB3_USB_INT_STA_1
);
1955 u32 int_sta_2
= usb3_read(usb3
, USB3_USB_INT_STA_2
);
1956 u32 otg_int_sta
= usb3_read(usb3
, USB3_USB_OTG_INT_STA
);
1958 int_sta_1
&= usb3_read(usb3
, USB3_USB_INT_ENA_1
);
1960 usb3_write(usb3
, int_sta_1
, USB3_USB_INT_STA_1
);
1961 usb3_irq_epc_int_1(usb3
, int_sta_1
);
1964 int_sta_2
&= usb3_read(usb3
, USB3_USB_INT_ENA_2
);
1966 usb3_irq_epc_int_2(usb3
, int_sta_2
);
1968 otg_int_sta
&= usb3_read(usb3
, USB3_USB_OTG_INT_ENA
);
1970 usb3_write(usb3
, otg_int_sta
, USB3_USB_OTG_INT_STA
);
1971 usb3_irq_otg_int(usb3
, otg_int_sta
);
1975 static void usb3_irq_dma_int(struct renesas_usb3
*usb3
, u32 dma_sta
)
1977 struct renesas_usb3_ep
*usb3_ep
;
1978 struct renesas_usb3_request
*usb3_req
;
1981 for (i
= 0; i
< usb3
->num_usb3_eps
; i
++) {
1982 if (!(dma_sta
& DMA_INT(i
)))
1985 usb3_ep
= usb3_get_ep(usb3
, i
);
1986 if (!(usb3_read(usb3
, USB3_AXI_INT_STA
) &
1987 AXI_INT_PRDEN_CLR_STA(usb3_ep
->dma
->num
)))
1990 usb3_req
= usb3_get_request(usb3_ep
);
1991 status
= usb3_dma_try_stop(usb3_ep
, usb3_req
);
1992 usb3_request_done_pipen(usb3
, usb3_ep
, usb3_req
, status
);
1996 static void usb3_irq_dma(struct renesas_usb3
*usb3
)
1998 u32 dma_sta
= usb3_read(usb3
, USB3_DMA_INT_STA
);
2000 dma_sta
&= usb3_read(usb3
, USB3_DMA_INT_ENA
);
2002 usb3_write(usb3
, dma_sta
, USB3_DMA_INT_STA
);
2003 usb3_irq_dma_int(usb3
, dma_sta
);
2007 static irqreturn_t
renesas_usb3_irq(int irq
, void *_usb3
)
2009 struct renesas_usb3
*usb3
= _usb3
;
2010 irqreturn_t ret
= IRQ_NONE
;
2011 u32 axi_int_sta
= usb3_read(usb3
, USB3_AXI_INT_STA
);
2013 if (axi_int_sta
& AXI_INT_DMAINT
) {
2018 if (axi_int_sta
& AXI_INT_EPCINT
) {
2026 static void usb3_write_pn_mod(struct renesas_usb3_ep
*usb3_ep
,
2027 const struct usb_endpoint_descriptor
*desc
)
2029 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
2032 val
|= usb3_ep
->dir_in
? PN_MOD_DIR
: 0;
2033 val
|= PN_MOD_TYPE(usb_endpoint_type(desc
));
2034 val
|= PN_MOD_EPNUM(usb_endpoint_num(desc
));
2035 usb3_write(usb3
, val
, USB3_PN_MOD
);
2038 static u32
usb3_calc_ramarea(int ram_size
)
2040 WARN_ON(ram_size
> SZ_16K
);
2042 if (ram_size
<= SZ_1K
)
2043 return PN_RAMMAP_RAMAREA_1KB
;
2044 else if (ram_size
<= SZ_2K
)
2045 return PN_RAMMAP_RAMAREA_2KB
;
2046 else if (ram_size
<= SZ_4K
)
2047 return PN_RAMMAP_RAMAREA_4KB
;
2048 else if (ram_size
<= SZ_8K
)
2049 return PN_RAMMAP_RAMAREA_8KB
;
2051 return PN_RAMMAP_RAMAREA_16KB
;
2054 static u32
usb3_calc_rammap_val(struct renesas_usb3_ep
*usb3_ep
,
2055 const struct usb_endpoint_descriptor
*desc
)
2058 static const u32 max_packet_array
[] = {8, 16, 32, 64, 512};
2059 u32 mpkt
= PN_RAMMAP_MPKT(1024);
2061 for (i
= 0; i
< ARRAY_SIZE(max_packet_array
); i
++) {
2062 if (usb_endpoint_maxp(desc
) <= max_packet_array
[i
])
2063 mpkt
= PN_RAMMAP_MPKT(max_packet_array
[i
]);
2066 return usb3_ep
->rammap_val
| mpkt
;
2069 static int usb3_enable_pipe_n(struct renesas_usb3_ep
*usb3_ep
,
2070 const struct usb_endpoint_descriptor
*desc
)
2072 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
2073 unsigned long flags
;
2075 usb3_ep
->dir_in
= usb_endpoint_dir_in(desc
);
2077 spin_lock_irqsave(&usb3
->lock
, flags
);
2078 if (!usb3_pn_change(usb3
, usb3_ep
->num
)) {
2079 usb3_write_pn_mod(usb3_ep
, desc
);
2080 usb3_write(usb3
, usb3_calc_rammap_val(usb3_ep
, desc
),
2082 usb3_pn_con_clear(usb3
);
2083 usb3_set_bit(usb3
, PN_CON_EN
, USB3_PN_CON
);
2085 spin_unlock_irqrestore(&usb3
->lock
, flags
);
2090 static int usb3_disable_pipe_n(struct renesas_usb3_ep
*usb3_ep
)
2092 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
2093 unsigned long flags
;
2095 usb3_ep
->halt
= false;
2097 spin_lock_irqsave(&usb3
->lock
, flags
);
2098 if (!usb3_pn_change(usb3
, usb3_ep
->num
)) {
2099 usb3_write(usb3
, 0, USB3_PN_INT_ENA
);
2100 usb3_write(usb3
, 0, USB3_PN_RAMMAP
);
2101 usb3_clear_bit(usb3
, PN_CON_EN
, USB3_PN_CON
);
2103 spin_unlock_irqrestore(&usb3
->lock
, flags
);
2108 /*------- usb_ep_ops -----------------------------------------------------*/
2109 static int renesas_usb3_ep_enable(struct usb_ep
*_ep
,
2110 const struct usb_endpoint_descriptor
*desc
)
2112 struct renesas_usb3_ep
*usb3_ep
= usb_ep_to_usb3_ep(_ep
);
2114 return usb3_enable_pipe_n(usb3_ep
, desc
);
2117 static int renesas_usb3_ep_disable(struct usb_ep
*_ep
)
2119 struct renesas_usb3_ep
*usb3_ep
= usb_ep_to_usb3_ep(_ep
);
2120 struct renesas_usb3_request
*usb3_req
;
2123 usb3_req
= usb3_get_request(usb3_ep
);
2126 usb3_dma_try_stop(usb3_ep
, usb3_req
);
2127 usb3_request_done(usb3_ep
, usb3_req
, -ESHUTDOWN
);
2130 return usb3_disable_pipe_n(usb3_ep
);
2133 static struct usb_request
*__renesas_usb3_ep_alloc_request(gfp_t gfp_flags
)
2135 struct renesas_usb3_request
*usb3_req
;
2137 usb3_req
= kzalloc(sizeof(struct renesas_usb3_request
), gfp_flags
);
2141 INIT_LIST_HEAD(&usb3_req
->queue
);
2143 return &usb3_req
->req
;
2146 static void __renesas_usb3_ep_free_request(struct usb_request
*_req
)
2148 struct renesas_usb3_request
*usb3_req
= usb_req_to_usb3_req(_req
);
2153 static struct usb_request
*renesas_usb3_ep_alloc_request(struct usb_ep
*_ep
,
2156 return __renesas_usb3_ep_alloc_request(gfp_flags
);
2159 static void renesas_usb3_ep_free_request(struct usb_ep
*_ep
,
2160 struct usb_request
*_req
)
2162 __renesas_usb3_ep_free_request(_req
);
2165 static int renesas_usb3_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
2167 struct renesas_usb3_ep
*usb3_ep
= usb_ep_to_usb3_ep(_ep
);
2168 struct renesas_usb3_request
*usb3_req
= usb_req_to_usb3_req(_req
);
2169 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
2171 dev_dbg(usb3_to_dev(usb3
), "ep_dequeue: ep%2d, %u\n", usb3_ep
->num
,
2174 usb3_dma_try_stop(usb3_ep
, usb3_req
);
2175 usb3_request_done_pipen(usb3
, usb3_ep
, usb3_req
, -ECONNRESET
);
2180 static int renesas_usb3_ep_set_halt(struct usb_ep
*_ep
, int value
)
2182 return usb3_set_halt(usb_ep_to_usb3_ep(_ep
), !!value
, false);
2185 static int renesas_usb3_ep_set_wedge(struct usb_ep
*_ep
)
2187 struct renesas_usb3_ep
*usb3_ep
= usb_ep_to_usb3_ep(_ep
);
2189 usb3_ep
->wedge
= true;
2190 return usb3_set_halt(usb3_ep
, true, false);
2193 static void renesas_usb3_ep_fifo_flush(struct usb_ep
*_ep
)
2195 struct renesas_usb3_ep
*usb3_ep
= usb_ep_to_usb3_ep(_ep
);
2196 struct renesas_usb3
*usb3
= usb3_ep_to_usb3(usb3_ep
);
2197 unsigned long flags
;
2200 spin_lock_irqsave(&usb3
->lock
, flags
);
2201 if (!usb3_pn_change(usb3
, usb3_ep
->num
)) {
2202 usb3_pn_con_clear(usb3
);
2203 usb3_set_bit(usb3
, PN_CON_EN
, USB3_PN_CON
);
2205 spin_unlock_irqrestore(&usb3
->lock
, flags
);
2207 usb3_p0_con_clear_buffer(usb3
);
2211 static const struct usb_ep_ops renesas_usb3_ep_ops
= {
2212 .enable
= renesas_usb3_ep_enable
,
2213 .disable
= renesas_usb3_ep_disable
,
2215 .alloc_request
= renesas_usb3_ep_alloc_request
,
2216 .free_request
= renesas_usb3_ep_free_request
,
2218 .queue
= renesas_usb3_ep_queue
,
2219 .dequeue
= renesas_usb3_ep_dequeue
,
2221 .set_halt
= renesas_usb3_ep_set_halt
,
2222 .set_wedge
= renesas_usb3_ep_set_wedge
,
2223 .fifo_flush
= renesas_usb3_ep_fifo_flush
,
2226 /*------- usb_gadget_ops -------------------------------------------------*/
2227 static int renesas_usb3_start(struct usb_gadget
*gadget
,
2228 struct usb_gadget_driver
*driver
)
2230 struct renesas_usb3
*usb3
;
2232 if (!driver
|| driver
->max_speed
< USB_SPEED_FULL
||
2236 usb3
= gadget_to_renesas_usb3(gadget
);
2238 /* hook up the driver */
2239 usb3
->driver
= driver
;
2242 phy_init(usb3
->phy
);
2244 pm_runtime_get_sync(usb3_to_dev(usb3
));
2246 renesas_usb3_init_controller(usb3
);
2251 static int renesas_usb3_stop(struct usb_gadget
*gadget
)
2253 struct renesas_usb3
*usb3
= gadget_to_renesas_usb3(gadget
);
2255 usb3
->softconnect
= false;
2256 usb3
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2257 usb3
->driver
= NULL
;
2258 renesas_usb3_stop_controller(usb3
);
2261 phy_exit(usb3
->phy
);
2263 pm_runtime_put(usb3_to_dev(usb3
));
2268 static int renesas_usb3_get_frame(struct usb_gadget
*_gadget
)
2273 static int renesas_usb3_pullup(struct usb_gadget
*gadget
, int is_on
)
2275 struct renesas_usb3
*usb3
= gadget_to_renesas_usb3(gadget
);
2277 usb3
->softconnect
= !!is_on
;
2282 static int renesas_usb3_set_selfpowered(struct usb_gadget
*gadget
, int is_self
)
2284 gadget
->is_selfpowered
= !!is_self
;
2289 static const struct usb_gadget_ops renesas_usb3_gadget_ops
= {
2290 .get_frame
= renesas_usb3_get_frame
,
2291 .udc_start
= renesas_usb3_start
,
2292 .udc_stop
= renesas_usb3_stop
,
2293 .pullup
= renesas_usb3_pullup
,
2294 .set_selfpowered
= renesas_usb3_set_selfpowered
,
2297 static ssize_t
role_store(struct device
*dev
, struct device_attribute
*attr
,
2298 const char *buf
, size_t count
)
2300 struct renesas_usb3
*usb3
= dev_get_drvdata(dev
);
2301 bool new_mode_is_host
;
2306 if (usb3
->forced_b_device
)
2309 if (!strncmp(buf
, "host", strlen("host")))
2310 new_mode_is_host
= true;
2311 else if (!strncmp(buf
, "peripheral", strlen("peripheral")))
2312 new_mode_is_host
= false;
2316 if (new_mode_is_host
== usb3_is_host(usb3
))
2319 usb3_mode_config(usb3
, new_mode_is_host
, usb3_is_a_device(usb3
));
2324 static ssize_t
role_show(struct device
*dev
, struct device_attribute
*attr
,
2327 struct renesas_usb3
*usb3
= dev_get_drvdata(dev
);
2332 return sprintf(buf
, "%s\n", usb3_is_host(usb3
) ? "host" : "peripheral");
2334 static DEVICE_ATTR_RW(role
);
2336 static int renesas_usb3_b_device_show(struct seq_file
*s
, void *unused
)
2338 struct renesas_usb3
*usb3
= s
->private;
2340 seq_printf(s
, "%d\n", usb3
->forced_b_device
);
2345 static int renesas_usb3_b_device_open(struct inode
*inode
, struct file
*file
)
2347 return single_open(file
, renesas_usb3_b_device_show
, inode
->i_private
);
2350 static ssize_t
renesas_usb3_b_device_write(struct file
*file
,
2351 const char __user
*ubuf
,
2352 size_t count
, loff_t
*ppos
)
2354 struct seq_file
*s
= file
->private_data
;
2355 struct renesas_usb3
*usb3
= s
->private;
2361 if (copy_from_user(&buf
, ubuf
, min_t(size_t, sizeof(buf
) - 1, count
)))
2364 if (!strncmp(buf
, "1", 1))
2365 usb3
->forced_b_device
= true;
2367 usb3
->forced_b_device
= false;
2369 /* Let this driver call usb3_connect() anyway */
2370 usb3_check_id(usb3
);
2375 static const struct file_operations renesas_usb3_b_device_fops
= {
2376 .open
= renesas_usb3_b_device_open
,
2377 .write
= renesas_usb3_b_device_write
,
2379 .llseek
= seq_lseek
,
2380 .release
= single_release
,
2383 static void renesas_usb3_debugfs_init(struct renesas_usb3
*usb3
,
2386 struct dentry
*root
, *file
;
2388 root
= debugfs_create_dir(dev_name(dev
), NULL
);
2389 if (IS_ERR_OR_NULL(root
)) {
2390 dev_info(dev
, "%s: Can't create the root\n", __func__
);
2394 file
= debugfs_create_file("b_device", 0644, root
, usb3
,
2395 &renesas_usb3_b_device_fops
);
2397 dev_info(dev
, "%s: Can't create debugfs mode\n", __func__
);
2400 /*------- platform_driver ------------------------------------------------*/
2401 static int renesas_usb3_remove(struct platform_device
*pdev
)
2403 struct renesas_usb3
*usb3
= platform_get_drvdata(pdev
);
2405 device_remove_file(&pdev
->dev
, &dev_attr_role
);
2407 usb_del_gadget_udc(&usb3
->gadget
);
2408 renesas_usb3_dma_free_prd(usb3
, &pdev
->dev
);
2410 __renesas_usb3_ep_free_request(usb3
->ep0_req
);
2413 pm_runtime_disable(usb3_to_dev(usb3
));
2418 static int renesas_usb3_init_ep(struct renesas_usb3
*usb3
, struct device
*dev
,
2419 const struct renesas_usb3_priv
*priv
)
2421 struct renesas_usb3_ep
*usb3_ep
;
2424 /* calculate num_usb3_eps from renesas_usb3_priv */
2425 usb3
->num_usb3_eps
= priv
->ramsize_per_ramif
* priv
->num_ramif
* 2 /
2426 priv
->ramsize_per_pipe
+ 1;
2428 if (usb3
->num_usb3_eps
> USB3_MAX_NUM_PIPES
)
2429 usb3
->num_usb3_eps
= USB3_MAX_NUM_PIPES
;
2431 usb3
->usb3_ep
= devm_kzalloc(dev
, sizeof(*usb3_ep
) * usb3
->num_usb3_eps
,
2436 dev_dbg(dev
, "%s: num_usb3_eps = %d\n", __func__
, usb3
->num_usb3_eps
);
2438 * This driver prepares pipes as follows:
2439 * - odd pipes = IN pipe
2440 * - even pipes = OUT pipe (except pipe 0)
2442 usb3_for_each_ep(usb3_ep
, usb3
, i
) {
2443 snprintf(usb3_ep
->ep_name
, sizeof(usb3_ep
->ep_name
), "ep%d", i
);
2444 usb3_ep
->usb3
= usb3
;
2446 usb3_ep
->ep
.name
= usb3_ep
->ep_name
;
2447 usb3_ep
->ep
.ops
= &renesas_usb3_ep_ops
;
2448 INIT_LIST_HEAD(&usb3_ep
->queue
);
2449 INIT_LIST_HEAD(&usb3_ep
->ep
.ep_list
);
2451 /* for control pipe */
2452 usb3
->gadget
.ep0
= &usb3_ep
->ep
;
2453 usb_ep_set_maxpacket_limit(&usb3_ep
->ep
,
2454 USB3_EP0_HSFS_MAX_PACKET_SIZE
);
2455 usb3_ep
->ep
.caps
.type_control
= true;
2456 usb3_ep
->ep
.caps
.dir_in
= true;
2457 usb3_ep
->ep
.caps
.dir_out
= true;
2461 /* for bulk or interrupt pipe */
2462 usb_ep_set_maxpacket_limit(&usb3_ep
->ep
, ~0);
2463 list_add_tail(&usb3_ep
->ep
.ep_list
, &usb3
->gadget
.ep_list
);
2464 usb3_ep
->ep
.caps
.type_bulk
= true;
2465 usb3_ep
->ep
.caps
.type_int
= true;
2467 usb3_ep
->ep
.caps
.dir_in
= true;
2469 usb3_ep
->ep
.caps
.dir_out
= true;
2475 static void renesas_usb3_init_ram(struct renesas_usb3
*usb3
, struct device
*dev
,
2476 const struct renesas_usb3_priv
*priv
)
2478 struct renesas_usb3_ep
*usb3_ep
;
2480 u32 ramif
[2], basead
[2]; /* index 0 = for IN pipes */
2481 u32
*cur_ramif
, *cur_basead
;
2484 memset(ramif
, 0, sizeof(ramif
));
2485 memset(basead
, 0, sizeof(basead
));
2488 * This driver prepares pipes as follows:
2489 * - all pipes = the same size as "ramsize_per_pipe"
2490 * Please refer to the "Method of Specifying RAM Mapping"
2492 usb3_for_each_ep(usb3_ep
, usb3
, i
) {
2494 continue; /* out of scope if ep num = 0 */
2495 if (usb3_ep
->ep
.caps
.dir_in
) {
2496 cur_ramif
= &ramif
[0];
2497 cur_basead
= &basead
[0];
2499 cur_ramif
= &ramif
[1];
2500 cur_basead
= &basead
[1];
2503 if (*cur_basead
> priv
->ramsize_per_ramif
)
2504 continue; /* out of memory for IN or OUT pipe */
2506 /* calculate rammap_val */
2507 val
= PN_RAMMAP_RAMIF(*cur_ramif
);
2508 val
|= usb3_calc_ramarea(priv
->ramsize_per_pipe
);
2509 val
|= PN_RAMMAP_BASEAD(*cur_basead
);
2510 usb3_ep
->rammap_val
= val
;
2512 dev_dbg(dev
, "ep%2d: val = %08x, ramif = %d, base = %x\n",
2513 i
, val
, *cur_ramif
, *cur_basead
);
2515 /* update current ramif */
2516 if (*cur_ramif
+ 1 == priv
->num_ramif
) {
2518 *cur_basead
+= priv
->ramsize_per_pipe
;
2525 static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795_es1
= {
2526 .ramsize_per_ramif
= SZ_16K
,
2528 .ramsize_per_pipe
= SZ_4K
,
2529 .workaround_for_vbus
= true,
2532 static const struct renesas_usb3_priv renesas_usb3_priv_gen3
= {
2533 .ramsize_per_ramif
= SZ_16K
,
2535 .ramsize_per_pipe
= SZ_4K
,
2538 static const struct of_device_id usb3_of_match
[] = {
2540 .compatible
= "renesas,r8a7795-usb3-peri",
2541 .data
= &renesas_usb3_priv_gen3
,
2544 .compatible
= "renesas,rcar-gen3-usb3-peri",
2545 .data
= &renesas_usb3_priv_gen3
,
2549 MODULE_DEVICE_TABLE(of
, usb3_of_match
);
2551 static const struct soc_device_attribute renesas_usb3_quirks_match
[] = {
2553 .soc_id
= "r8a7795", .revision
= "ES1.*",
2554 .data
= &renesas_usb3_priv_r8a7795_es1
,
2559 static const unsigned int renesas_usb3_cable
[] = {
2565 static int renesas_usb3_probe(struct platform_device
*pdev
)
2567 struct renesas_usb3
*usb3
;
2568 struct resource
*res
;
2570 const struct renesas_usb3_priv
*priv
;
2571 const struct soc_device_attribute
*attr
;
2573 attr
= soc_device_match(renesas_usb3_quirks_match
);
2577 priv
= of_device_get_match_data(&pdev
->dev
);
2579 irq
= platform_get_irq(pdev
, 0);
2581 dev_err(&pdev
->dev
, "Failed to get IRQ: %d\n", irq
);
2585 usb3
= devm_kzalloc(&pdev
->dev
, sizeof(*usb3
), GFP_KERNEL
);
2589 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2590 usb3
->reg
= devm_ioremap_resource(&pdev
->dev
, res
);
2591 if (IS_ERR(usb3
->reg
))
2592 return PTR_ERR(usb3
->reg
);
2594 platform_set_drvdata(pdev
, usb3
);
2595 spin_lock_init(&usb3
->lock
);
2597 usb3
->gadget
.ops
= &renesas_usb3_gadget_ops
;
2598 usb3
->gadget
.name
= udc_name
;
2599 usb3
->gadget
.max_speed
= USB_SPEED_SUPER
;
2600 INIT_LIST_HEAD(&usb3
->gadget
.ep_list
);
2601 ret
= renesas_usb3_init_ep(usb3
, &pdev
->dev
, priv
);
2604 renesas_usb3_init_ram(usb3
, &pdev
->dev
, priv
);
2606 ret
= devm_request_irq(&pdev
->dev
, irq
, renesas_usb3_irq
, 0,
2607 dev_name(&pdev
->dev
), usb3
);
2611 INIT_WORK(&usb3
->extcon_work
, renesas_usb3_extcon_work
);
2612 usb3
->extcon
= devm_extcon_dev_allocate(&pdev
->dev
, renesas_usb3_cable
);
2613 if (IS_ERR(usb3
->extcon
))
2614 return PTR_ERR(usb3
->extcon
);
2616 ret
= devm_extcon_dev_register(&pdev
->dev
, usb3
->extcon
);
2618 dev_err(&pdev
->dev
, "Failed to register extcon\n");
2622 /* for ep0 handling */
2623 usb3
->ep0_req
= __renesas_usb3_ep_alloc_request(GFP_KERNEL
);
2627 ret
= renesas_usb3_dma_alloc_prd(usb3
, &pdev
->dev
);
2631 ret
= usb_add_gadget_udc(&pdev
->dev
, &usb3
->gadget
);
2635 ret
= device_create_file(&pdev
->dev
, &dev_attr_role
);
2637 goto err_dev_create
;
2640 * This is an optional. So, if this driver cannot get a phy,
2641 * this driver will not handle a phy anymore.
2643 usb3
->phy
= devm_phy_get(&pdev
->dev
, "usb");
2644 if (IS_ERR(usb3
->phy
))
2647 usb3
->workaround_for_vbus
= priv
->workaround_for_vbus
;
2649 renesas_usb3_debugfs_init(usb3
, &pdev
->dev
);
2651 dev_info(&pdev
->dev
, "probed%s\n", usb3
->phy
? " with phy" : "");
2652 pm_runtime_enable(usb3_to_dev(usb3
));
2657 usb_del_gadget_udc(&usb3
->gadget
);
2660 renesas_usb3_dma_free_prd(usb3
, &pdev
->dev
);
2663 __renesas_usb3_ep_free_request(usb3
->ep0_req
);
2668 #ifdef CONFIG_PM_SLEEP
2669 static int renesas_usb3_suspend(struct device
*dev
)
2671 struct renesas_usb3
*usb3
= dev_get_drvdata(dev
);
2677 renesas_usb3_stop_controller(usb3
);
2679 phy_exit(usb3
->phy
);
2680 pm_runtime_put(dev
);
2685 static int renesas_usb3_resume(struct device
*dev
)
2687 struct renesas_usb3
*usb3
= dev_get_drvdata(dev
);
2694 phy_init(usb3
->phy
);
2695 pm_runtime_get_sync(dev
);
2696 renesas_usb3_init_controller(usb3
);
2702 static SIMPLE_DEV_PM_OPS(renesas_usb3_pm_ops
, renesas_usb3_suspend
,
2703 renesas_usb3_resume
);
2705 static struct platform_driver renesas_usb3_driver
= {
2706 .probe
= renesas_usb3_probe
,
2707 .remove
= renesas_usb3_remove
,
2709 .name
= (char *)udc_name
,
2710 .pm
= &renesas_usb3_pm_ops
,
2711 .of_match_table
= of_match_ptr(usb3_of_match
),
2714 module_platform_driver(renesas_usb3_driver
);
2716 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
2717 MODULE_LICENSE("GPL v2");
2718 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
2719 MODULE_ALIAS("platform:renesas_usb3");