doc: Update rcu_assign_pointer() definition in whatisRCU.txt
[linux/fpc-iii.git] / drivers / usb / gadget / udc / renesas_usb3.c
blob2218f91e92a61cd8078afe857ba347463aa8ff63
1 /*
2 * Renesas USB3.0 Peripheral driver (USB gadget)
4 * Copyright (C) 2015 Renesas Electronics Corporation
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 */
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
24 /* register definitions */
25 #define USB3_AXI_INT_STA 0x008
26 #define USB3_AXI_INT_ENA 0x00c
27 #define USB3_DMA_INT_STA 0x010
28 #define USB3_DMA_INT_ENA 0x014
29 #define USB3_USB_COM_CON 0x200
30 #define USB3_USB20_CON 0x204
31 #define USB3_USB30_CON 0x208
32 #define USB3_USB_STA 0x210
33 #define USB3_DRD_CON 0x218
34 #define USB3_USB_INT_STA_1 0x220
35 #define USB3_USB_INT_STA_2 0x224
36 #define USB3_USB_INT_ENA_1 0x228
37 #define USB3_USB_INT_ENA_2 0x22c
38 #define USB3_STUP_DAT_0 0x230
39 #define USB3_STUP_DAT_1 0x234
40 #define USB3_P0_MOD 0x280
41 #define USB3_P0_CON 0x288
42 #define USB3_P0_STA 0x28c
43 #define USB3_P0_INT_STA 0x290
44 #define USB3_P0_INT_ENA 0x294
45 #define USB3_P0_LNG 0x2a0
46 #define USB3_P0_READ 0x2a4
47 #define USB3_P0_WRITE 0x2a8
48 #define USB3_PIPE_COM 0x2b0
49 #define USB3_PN_MOD 0x2c0
50 #define USB3_PN_RAMMAP 0x2c4
51 #define USB3_PN_CON 0x2c8
52 #define USB3_PN_STA 0x2cc
53 #define USB3_PN_INT_STA 0x2d0
54 #define USB3_PN_INT_ENA 0x2d4
55 #define USB3_PN_LNG 0x2e0
56 #define USB3_PN_READ 0x2e4
57 #define USB3_PN_WRITE 0x2e8
58 #define USB3_SSIFCMD 0x340
60 /* AXI_INT_ENA and AXI_INT_STA */
61 #define AXI_INT_DMAINT BIT(31)
62 #define AXI_INT_EPCINT BIT(30)
64 /* LCLKSEL */
65 #define LCLKSEL_LSEL BIT(18)
67 /* USB_COM_CON */
68 #define USB_COM_CON_CONF BIT(24)
69 #define USB_COM_CON_SPD_MODE BIT(17)
70 #define USB_COM_CON_EP0_EN BIT(16)
71 #define USB_COM_CON_DEV_ADDR_SHIFT 8
72 #define USB_COM_CON_DEV_ADDR_MASK GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
73 #define USB_COM_CON_DEV_ADDR(n) (((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
74 USB_COM_CON_DEV_ADDR_MASK)
75 #define USB_COM_CON_RX_DETECTION BIT(1)
76 #define USB_COM_CON_PIPE_CLR BIT(0)
78 /* USB20_CON */
79 #define USB20_CON_B2_PUE BIT(31)
80 #define USB20_CON_B2_SUSPEND BIT(24)
81 #define USB20_CON_B2_CONNECT BIT(17)
82 #define USB20_CON_B2_TSTMOD_SHIFT 8
83 #define USB20_CON_B2_TSTMOD_MASK GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
84 #define USB20_CON_B2_TSTMOD(n) (((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
85 USB20_CON_B2_TSTMOD_MASK)
86 #define USB20_CON_B2_TSTMOD_EN BIT(0)
88 /* USB30_CON */
89 #define USB30_CON_POW_SEL_SHIFT 24
90 #define USB30_CON_POW_SEL_MASK GENMASK(26, USB30_CON_POW_SEL_SHIFT)
91 #define USB30_CON_POW_SEL_IN_U3 BIT(26)
92 #define USB30_CON_POW_SEL_IN_DISCON 0
93 #define USB30_CON_POW_SEL_P2_TO_P0 BIT(25)
94 #define USB30_CON_POW_SEL_P0_TO_P3 BIT(24)
95 #define USB30_CON_POW_SEL_P0_TO_P2 0
96 #define USB30_CON_B3_PLLWAKE BIT(23)
97 #define USB30_CON_B3_CONNECT BIT(17)
98 #define USB30_CON_B3_HOTRST_CMP BIT(1)
100 /* USB_STA */
101 #define USB_STA_SPEED_MASK (BIT(2) | BIT(1))
102 #define USB_STA_SPEED_HS BIT(2)
103 #define USB_STA_SPEED_FS BIT(1)
104 #define USB_STA_SPEED_SS 0
105 #define USB_STA_VBUS_STA BIT(0)
107 /* DRD_CON */
108 #define DRD_CON_PERI_CON BIT(24)
109 #define DRD_CON_VBOUT BIT(0)
111 /* USB_INT_ENA_1 and USB_INT_STA_1 */
112 #define USB_INT_1_B3_PLLWKUP BIT(31)
113 #define USB_INT_1_B3_LUPSUCS BIT(30)
114 #define USB_INT_1_B3_DISABLE BIT(27)
115 #define USB_INT_1_B3_WRMRST BIT(21)
116 #define USB_INT_1_B3_HOTRST BIT(20)
117 #define USB_INT_1_B2_USBRST BIT(12)
118 #define USB_INT_1_B2_L1SPND BIT(11)
119 #define USB_INT_1_B2_SPND BIT(9)
120 #define USB_INT_1_B2_RSUM BIT(8)
121 #define USB_INT_1_SPEED BIT(1)
122 #define USB_INT_1_VBUS_CNG BIT(0)
124 /* USB_INT_ENA_2 and USB_INT_STA_2 */
125 #define USB_INT_2_PIPE(n) BIT(n)
127 /* P0_MOD */
128 #define P0_MOD_DIR BIT(6)
130 /* P0_CON and PN_CON */
131 #define PX_CON_BYTE_EN_MASK (BIT(10) | BIT(9))
132 #define PX_CON_BYTE_EN_SHIFT 9
133 #define PX_CON_BYTE_EN_BYTES(n) (((n) << PX_CON_BYTE_EN_SHIFT) & \
134 PX_CON_BYTE_EN_MASK)
135 #define PX_CON_SEND BIT(8)
137 /* P0_CON */
138 #define P0_CON_ST_RES_MASK (BIT(27) | BIT(26))
139 #define P0_CON_ST_RES_FORCE_STALL BIT(27)
140 #define P0_CON_ST_RES_NORMAL BIT(26)
141 #define P0_CON_ST_RES_FORCE_NRDY 0
142 #define P0_CON_OT_RES_MASK (BIT(25) | BIT(24))
143 #define P0_CON_OT_RES_FORCE_STALL BIT(25)
144 #define P0_CON_OT_RES_NORMAL BIT(24)
145 #define P0_CON_OT_RES_FORCE_NRDY 0
146 #define P0_CON_IN_RES_MASK (BIT(17) | BIT(16))
147 #define P0_CON_IN_RES_FORCE_STALL BIT(17)
148 #define P0_CON_IN_RES_NORMAL BIT(16)
149 #define P0_CON_IN_RES_FORCE_NRDY 0
150 #define P0_CON_RES_WEN BIT(7)
151 #define P0_CON_BCLR BIT(1)
153 /* P0_STA and PN_STA */
154 #define PX_STA_BUFSTS BIT(0)
156 /* P0_INT_ENA and P0_INT_STA */
157 #define P0_INT_STSED BIT(18)
158 #define P0_INT_STSST BIT(17)
159 #define P0_INT_SETUP BIT(16)
160 #define P0_INT_RCVNL BIT(8)
161 #define P0_INT_ERDY BIT(7)
162 #define P0_INT_FLOW BIT(6)
163 #define P0_INT_STALL BIT(2)
164 #define P0_INT_NRDY BIT(1)
165 #define P0_INT_BFRDY BIT(0)
166 #define P0_INT_ALL_BITS (P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
168 /* PN_MOD */
169 #define PN_MOD_DIR BIT(6)
170 #define PN_MOD_TYPE_SHIFT 4
171 #define PN_MOD_TYPE_MASK GENMASK(5, PN_MOD_TYPE_SHIFT)
172 #define PN_MOD_TYPE(n) (((n) << PN_MOD_TYPE_SHIFT) & \
173 PN_MOD_TYPE_MASK)
174 #define PN_MOD_EPNUM_MASK GENMASK(3, 0)
175 #define PN_MOD_EPNUM(n) ((n) & PN_MOD_EPNUM_MASK)
177 /* PN_RAMMAP */
178 #define PN_RAMMAP_RAMAREA_SHIFT 29
179 #define PN_RAMMAP_RAMAREA_MASK GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
180 #define PN_RAMMAP_RAMAREA_16KB BIT(31)
181 #define PN_RAMMAP_RAMAREA_8KB (BIT(30) | BIT(29))
182 #define PN_RAMMAP_RAMAREA_4KB BIT(30)
183 #define PN_RAMMAP_RAMAREA_2KB BIT(29)
184 #define PN_RAMMAP_RAMAREA_1KB 0
185 #define PN_RAMMAP_MPKT_SHIFT 16
186 #define PN_RAMMAP_MPKT_MASK GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
187 #define PN_RAMMAP_MPKT(n) (((n) << PN_RAMMAP_MPKT_SHIFT) & \
188 PN_RAMMAP_MPKT_MASK)
189 #define PN_RAMMAP_RAMIF_SHIFT 14
190 #define PN_RAMMAP_RAMIF_MASK GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
191 #define PN_RAMMAP_RAMIF(n) (((n) << PN_RAMMAP_RAMIF_SHIFT) & \
192 PN_RAMMAP_RAMIF_MASK)
193 #define PN_RAMMAP_BASEAD_MASK GENMASK(13, 0)
194 #define PN_RAMMAP_BASEAD(offs) (((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
195 #define PN_RAMMAP_DATA(area, ramif, basead) ((PN_RAMMAP_##area) | \
196 (PN_RAMMAP_RAMIF(ramif)) | \
197 (PN_RAMMAP_BASEAD(basead)))
199 /* PN_CON */
200 #define PN_CON_EN BIT(31)
201 #define PN_CON_DATAIF_EN BIT(30)
202 #define PN_CON_RES_MASK (BIT(17) | BIT(16))
203 #define PN_CON_RES_FORCE_STALL BIT(17)
204 #define PN_CON_RES_NORMAL BIT(16)
205 #define PN_CON_RES_FORCE_NRDY 0
206 #define PN_CON_LAST BIT(11)
207 #define PN_CON_RES_WEN BIT(7)
208 #define PN_CON_CLR BIT(0)
210 /* PN_INT_STA and PN_INT_ENA */
211 #define PN_INT_LSTTR BIT(4)
212 #define PN_INT_BFRDY BIT(0)
214 /* USB3_SSIFCMD */
215 #define SSIFCMD_URES_U2 BIT(9)
216 #define SSIFCMD_URES_U1 BIT(8)
217 #define SSIFCMD_UDIR_U2 BIT(7)
218 #define SSIFCMD_UDIR_U1 BIT(6)
219 #define SSIFCMD_UREQ_U2 BIT(5)
220 #define SSIFCMD_UREQ_U1 BIT(4)
222 #define USB3_EP0_SS_MAX_PACKET_SIZE 512
223 #define USB3_EP0_HSFS_MAX_PACKET_SIZE 64
224 #define USB3_EP0_BUF_SIZE 8
225 #define USB3_MAX_NUM_PIPES 30
226 #define USB3_WAIT_US 3
228 struct renesas_usb3;
229 struct renesas_usb3_request {
230 struct usb_request req;
231 struct list_head queue;
234 #define USB3_EP_NAME_SIZE 8
235 struct renesas_usb3_ep {
236 struct usb_ep ep;
237 struct renesas_usb3 *usb3;
238 int num;
239 char ep_name[USB3_EP_NAME_SIZE];
240 struct list_head queue;
241 u32 rammap_val;
242 bool dir_in;
243 bool halt;
244 bool wedge;
245 bool started;
248 struct renesas_usb3_priv {
249 int ramsize_per_ramif; /* unit = bytes */
250 int num_ramif;
251 int ramsize_per_pipe; /* unit = bytes */
252 bool workaround_for_vbus; /* if true, don't check vbus signal */
255 struct renesas_usb3 {
256 void __iomem *reg;
258 struct usb_gadget gadget;
259 struct usb_gadget_driver *driver;
261 struct renesas_usb3_ep *usb3_ep;
262 int num_usb3_eps;
264 spinlock_t lock;
265 int disabled_count;
267 struct usb_request *ep0_req;
268 u16 test_mode;
269 u8 ep0_buf[USB3_EP0_BUF_SIZE];
270 bool softconnect;
271 bool workaround_for_vbus;
274 #define gadget_to_renesas_usb3(_gadget) \
275 container_of(_gadget, struct renesas_usb3, gadget)
276 #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
277 #define usb3_to_dev(_usb3) (_usb3->gadget.dev.parent)
279 #define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
280 #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
281 #define usb_req_to_usb3_req(_req) container_of(_req, \
282 struct renesas_usb3_request, req)
284 #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
285 #define usb3_for_each_ep(usb3_ep, usb3, i) \
286 for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
287 (i) < (usb3)->num_usb3_eps; \
288 (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
290 static const char udc_name[] = "renesas_usb3";
292 static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
294 iowrite32(data, usb3->reg + offs);
297 static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
299 return ioread32(usb3->reg + offs);
302 static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
304 u32 val = usb3_read(usb3, offs);
306 val |= bits;
307 usb3_write(usb3, val, offs);
310 static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
312 u32 val = usb3_read(usb3, offs);
314 val &= ~bits;
315 usb3_write(usb3, val, offs);
318 static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
319 u32 expected)
321 int i;
323 for (i = 0; i < USB3_WAIT_US; i++) {
324 if ((usb3_read(usb3, reg) & mask) == expected)
325 return 0;
326 udelay(1);
329 dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
330 __func__, reg, mask, expected);
332 return -EBUSY;
335 static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
337 usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
340 static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
342 usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
345 static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
347 usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
350 static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
352 usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
355 static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
357 /* Set AXI_INT */
358 usb3_write(usb3, ~0, USB3_DMA_INT_STA);
359 usb3_write(usb3, 0, USB3_DMA_INT_ENA);
360 usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
363 static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
365 /* FIXME: How to change host / peripheral mode as well? */
366 usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
367 usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
369 usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
370 usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
373 static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
375 if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
376 return true; /* already waked it up */
378 usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
379 usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
381 return false;
384 static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
386 u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
388 if (usb3->softconnect && pullup)
389 usb3_set_bit(usb3, bits, USB3_USB20_CON);
390 else
391 usb3_clear_bit(usb3, bits, USB3_USB20_CON);
394 static void usb3_set_test_mode(struct renesas_usb3 *usb3)
396 u32 val = usb3_read(usb3, USB3_USB20_CON);
398 val &= ~USB20_CON_B2_TSTMOD_MASK;
399 val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
400 usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
401 if (!usb3->test_mode)
402 usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
405 static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
407 usb3->disabled_count++;
408 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
409 usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
410 usb3_usb2_pullup(usb3, 1);
413 static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
415 return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
418 static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
420 if (!usb3_is_usb3_phy_in_u3(usb3))
421 return true; /* already waked it up */
423 usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
424 usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
426 return false;
429 static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
431 u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
432 u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
433 u32 val = usb3_read(usb3, USB3_SSIFCMD);
434 u16 ret = 0;
436 /* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
437 if (!(val & mask_u2))
438 ret |= 1 << USB_DEV_STAT_U2_ENABLED;
439 if (!(val & mask_u1))
440 ret |= 1 << USB_DEV_STAT_U1_ENABLED;
442 return ret;
445 static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
447 u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
449 /* Enables U2 if the bits of UDIR and UREQ are set to 0 */
450 if (enable)
451 usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
452 else
453 usb3_set_bit(usb3, bits, USB3_SSIFCMD);
456 static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
458 u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
460 /* Enables U1 if the bits of UDIR and UREQ are set to 0 */
461 if (enable)
462 usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
463 else
464 usb3_set_bit(usb3, bits, USB3_SSIFCMD);
467 static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
469 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
470 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
471 usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
474 static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
476 usb3_start_operation_for_usb3(usb3);
477 usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
479 usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
480 USB_INT_1_SPEED);
483 static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
485 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
488 static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
489 bool is_usb3)
491 usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
492 usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
493 usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
495 if (is_usb3)
496 usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
497 USB_INT_1_B3_HOTRST);
498 else
499 usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
500 USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
503 static void usb3_connect(struct renesas_usb3 *usb3)
505 if (usb3_wakeup_usb3_phy(usb3))
506 usb3_start_usb3_connection(usb3);
509 static void usb3_reset_epc(struct renesas_usb3 *usb3)
511 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
512 usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
513 usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
514 usb3->test_mode = 0;
515 usb3_set_test_mode(usb3);
518 static void usb3_disconnect(struct renesas_usb3 *usb3)
520 usb3->disabled_count = 0;
521 usb3_usb2_pullup(usb3, 0);
522 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
523 usb3_reset_epc(usb3);
525 if (usb3->driver)
526 usb3->driver->disconnect(&usb3->gadget);
529 static void usb3_check_vbus(struct renesas_usb3 *usb3)
531 if (usb3->workaround_for_vbus) {
532 usb3_connect(usb3);
533 } else {
534 if (usb3_read(usb3, USB3_USB_STA) & USB_STA_VBUS_STA)
535 usb3_connect(usb3);
536 else
537 usb3_disconnect(usb3);
541 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
543 usb3_init_axi_bridge(usb3);
544 usb3_init_epc_registers(usb3);
546 usb3_check_vbus(usb3);
549 static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
551 usb3_disconnect(usb3);
552 usb3_write(usb3, 0, USB3_P0_INT_ENA);
553 usb3_write(usb3, 0, USB3_PN_INT_ENA);
554 usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
555 usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
556 usb3_write(usb3, 0, USB3_AXI_INT_ENA);
559 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
561 usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
562 usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
563 usb3_start_usb3_connection(usb3);
566 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
568 usb3_transition_to_default_state(usb3, true);
571 static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
573 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
574 usb3_start_usb2_connection(usb3);
575 usb3_transition_to_default_state(usb3, false);
578 static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
580 usb3_stop_usb3_connection(usb3);
581 if (usb3_wakeup_usb2_phy(usb3))
582 usb3_irq_epc_int_1_resume(usb3);
585 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
587 usb3_reset_epc(usb3);
588 if (usb3->disabled_count < 3)
589 usb3_start_usb3_connection(usb3);
590 else
591 usb3_start_usb2_connection(usb3);
594 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
596 usb3_check_vbus(usb3);
599 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
601 usb3_reset_epc(usb3);
602 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
604 /* This bit shall be set within 12ms from the start of HotReset */
605 usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
608 static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
610 usb3_reset_epc(usb3);
611 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
613 usb3_start_operation_for_usb3(usb3);
614 usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
617 static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
619 u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
621 switch (speed) {
622 case USB_STA_SPEED_SS:
623 usb3->gadget.speed = USB_SPEED_SUPER;
624 break;
625 case USB_STA_SPEED_HS:
626 usb3->gadget.speed = USB_SPEED_HIGH;
627 break;
628 case USB_STA_SPEED_FS:
629 usb3->gadget.speed = USB_SPEED_FULL;
630 break;
631 default:
632 usb3->gadget.speed = USB_SPEED_UNKNOWN;
633 break;
637 static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
639 if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
640 usb3_irq_epc_int_1_pll_wakeup(usb3);
642 if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
643 usb3_irq_epc_int_1_linkup_success(usb3);
645 if (int_sta_1 & USB_INT_1_B3_HOTRST)
646 usb3_irq_epc_int_1_hot_reset(usb3);
648 if (int_sta_1 & USB_INT_1_B3_WRMRST)
649 usb3_irq_epc_int_1_warm_reset(usb3);
651 if (int_sta_1 & USB_INT_1_B3_DISABLE)
652 usb3_irq_epc_int_1_disable(usb3);
654 if (int_sta_1 & USB_INT_1_B2_USBRST)
655 usb3_irq_epc_int_1_bus_reset(usb3);
657 if (int_sta_1 & USB_INT_1_B2_RSUM)
658 usb3_irq_epc_int_1_resume(usb3);
660 if (int_sta_1 & USB_INT_1_SPEED)
661 usb3_irq_epc_int_1_speed(usb3);
663 if (int_sta_1 & USB_INT_1_VBUS_CNG)
664 usb3_irq_epc_int_1_vbus_change(usb3);
667 static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
668 *usb3_ep)
670 return list_first_entry_or_null(&usb3_ep->queue,
671 struct renesas_usb3_request, queue);
674 static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
675 *usb3_ep)
677 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
678 struct renesas_usb3_request *usb3_req;
679 unsigned long flags;
681 spin_lock_irqsave(&usb3->lock, flags);
682 usb3_req = __usb3_get_request(usb3_ep);
683 spin_unlock_irqrestore(&usb3->lock, flags);
685 return usb3_req;
688 static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
689 struct renesas_usb3_request *usb3_req, int status)
691 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
692 unsigned long flags;
694 dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
695 usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
696 status);
697 usb3_req->req.status = status;
698 spin_lock_irqsave(&usb3->lock, flags);
699 usb3_ep->started = false;
700 list_del_init(&usb3_req->queue);
701 spin_unlock_irqrestore(&usb3->lock, flags);
702 usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
705 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
707 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
708 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
710 if (usb3_req)
711 usb3_request_done(usb3_ep, usb3_req, 0);
712 if (usb3->test_mode)
713 usb3_set_test_mode(usb3);
716 static void usb3_get_setup_data(struct renesas_usb3 *usb3,
717 struct usb_ctrlrequest *ctrl)
719 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
720 u32 *data = (u32 *)ctrl;
722 *data++ = usb3_read(usb3, USB3_STUP_DAT_0);
723 *data = usb3_read(usb3, USB3_STUP_DAT_1);
725 /* update this driver's flag */
726 usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
729 static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
731 u32 val = usb3_read(usb3, USB3_P0_CON);
733 val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
734 val |= res | P0_CON_RES_WEN;
735 usb3_write(usb3, val, USB3_P0_CON);
738 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
740 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
741 P0_CON_OT_RES_FORCE_STALL |
742 P0_CON_IN_RES_NORMAL);
745 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
747 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
748 P0_CON_OT_RES_FORCE_STALL |
749 P0_CON_IN_RES_NORMAL);
752 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
754 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
755 P0_CON_OT_RES_NORMAL |
756 P0_CON_IN_RES_FORCE_STALL);
759 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
761 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
762 P0_CON_OT_RES_NORMAL |
763 P0_CON_IN_RES_FORCE_STALL);
766 static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
768 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
769 P0_CON_OT_RES_FORCE_STALL |
770 P0_CON_IN_RES_FORCE_STALL);
773 static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
775 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
776 P0_CON_OT_RES_FORCE_STALL |
777 P0_CON_IN_RES_FORCE_STALL);
780 static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
782 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
783 P0_CON_OT_RES_FORCE_NRDY |
784 P0_CON_IN_RES_FORCE_NRDY);
787 static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
789 if (num == 0 || num > usb3->num_usb3_eps)
790 return -ENXIO;
792 usb3_write(usb3, num, USB3_PIPE_COM);
794 return 0;
797 static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
799 u32 val = usb3_read(usb3, USB3_PN_CON);
801 val &= ~PN_CON_RES_MASK;
802 val |= res & PN_CON_RES_MASK;
803 val |= PN_CON_RES_WEN;
804 usb3_write(usb3, val, USB3_PN_CON);
807 static void usb3_pn_start(struct renesas_usb3 *usb3)
809 usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
812 static void usb3_pn_stop(struct renesas_usb3 *usb3)
814 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
817 static void usb3_pn_stall(struct renesas_usb3 *usb3)
819 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
822 static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
824 usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
826 return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
829 static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
830 struct renesas_usb3_request *usb3_req)
832 struct usb_request *req = &usb3_req->req;
834 if ((!req->zero && req->actual == req->length) ||
835 (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
836 return true;
837 else
838 return false;
841 static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
843 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
844 u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
846 return usb3_wait(usb3, sta_reg, mask, mask);
849 static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
850 bool last)
852 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
853 u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
854 u32 val = usb3_read(usb3, con_reg);
856 val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
857 val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
858 usb3_write(usb3, val, con_reg);
861 static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
862 struct renesas_usb3_request *usb3_req,
863 u32 fifo_reg)
865 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
866 int i;
867 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
868 usb3_ep->ep.maxpacket);
869 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
870 u32 tmp = 0;
871 bool is_last;
873 if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
874 return -EBUSY;
876 /* Update gadget driver parameter */
877 usb3_req->req.actual += len;
879 /* Write data to the register */
880 if (len >= 4) {
881 iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
882 buf += (len / 4) * 4;
883 len %= 4; /* update len to use usb3_set_pX_con_send() */
886 if (len) {
887 for (i = 0; i < len; i++)
888 tmp |= buf[i] << (8 * i);
889 usb3_write(usb3, tmp, fifo_reg);
892 is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
893 /* Send the data */
894 usb3_set_px_con_send(usb3_ep, len, is_last);
896 return is_last ? 0 : -EAGAIN;
899 static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
901 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
902 u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
904 return usb3_read(usb3, lng_reg);
907 static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
908 struct renesas_usb3_request *usb3_req, u32 fifo_reg)
910 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
911 int i;
912 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
913 usb3_get_received_length(usb3_ep));
914 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
915 u32 tmp = 0;
917 if (!len)
918 return 0;
920 /* Update gadget driver parameter */
921 usb3_req->req.actual += len;
923 /* Read data from the register */
924 if (len >= 4) {
925 ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
926 buf += (len / 4) * 4;
927 len %= 4;
930 if (len) {
931 tmp = usb3_read(usb3, fifo_reg);
932 for (i = 0; i < len; i++)
933 buf[i] = (tmp >> (8 * i)) & 0xff;
936 return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
939 static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
940 struct renesas_usb3_request *usb3_req)
942 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
944 if (usb3_ep->dir_in) {
945 usb3_set_p0_con_for_ctrl_read_status(usb3);
946 } else {
947 if (!usb3_req->req.length)
948 usb3_set_p0_con_for_no_data(usb3);
949 else
950 usb3_set_p0_con_for_ctrl_write_status(usb3);
954 static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
955 struct renesas_usb3_request *usb3_req)
957 int ret = -EAGAIN;
959 if (usb3_ep->dir_in)
960 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
961 else
962 ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
964 if (!ret)
965 usb3_set_status_stage(usb3_ep, usb3_req);
968 static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
969 struct renesas_usb3_request *usb3_req)
971 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
973 if (usb3_ep->started)
974 return;
976 usb3_ep->started = true;
978 if (usb3_ep->dir_in) {
979 usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
980 usb3_set_p0_con_for_ctrl_read_data(usb3);
981 } else {
982 usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
983 usb3_set_p0_con_for_ctrl_write_data(usb3);
986 usb3_p0_xfer(usb3_ep, usb3_req);
989 static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
990 struct renesas_usb3_request *usb3_req)
992 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
993 struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
994 unsigned long flags;
995 int ret = -EAGAIN;
996 u32 enable_bits = 0;
998 if (usb3_ep->halt || usb3_ep->started)
999 return;
1000 if (usb3_req != usb3_req_first)
1001 return;
1003 spin_lock_irqsave(&usb3->lock, flags);
1004 if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1005 goto out;
1007 usb3_ep->started = true;
1008 usb3_pn_start(usb3);
1010 if (usb3_ep->dir_in) {
1011 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1012 enable_bits |= PN_INT_LSTTR;
1015 if (ret < 0)
1016 enable_bits |= PN_INT_BFRDY;
1018 if (enable_bits) {
1019 usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1020 usb3_enable_pipe_irq(usb3, usb3_ep->num);
1022 out:
1023 spin_unlock_irqrestore(&usb3->lock, flags);
1026 static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1027 gfp_t gfp_flags)
1029 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1030 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1031 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1032 unsigned long flags;
1034 dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1035 _req->length);
1037 _req->status = -EINPROGRESS;
1038 _req->actual = 0;
1039 spin_lock_irqsave(&usb3->lock, flags);
1040 list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1041 spin_unlock_irqrestore(&usb3->lock, flags);
1043 if (!usb3_ep->num)
1044 usb3_start_pipe0(usb3_ep, usb3_req);
1045 else
1046 usb3_start_pipen(usb3_ep, usb3_req);
1048 return 0;
1051 static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1053 /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1054 usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1057 static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1058 struct usb_ctrlrequest *ctrl)
1060 if (ctrl->wValue >= 128)
1061 return true; /* stall */
1063 usb3_set_device_address(usb3, ctrl->wValue);
1064 usb3_set_p0_con_for_no_data(usb3);
1066 return false;
1069 static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1070 void *tx_data, size_t len,
1071 void (*complete)(struct usb_ep *ep,
1072 struct usb_request *req))
1074 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1076 if (tx_data)
1077 memcpy(usb3->ep0_buf, tx_data,
1078 min_t(size_t, len, USB3_EP0_BUF_SIZE));
1080 usb3->ep0_req->buf = &usb3->ep0_buf;
1081 usb3->ep0_req->length = len;
1082 usb3->ep0_req->complete = complete;
1083 renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1086 static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1087 struct usb_request *req)
1091 static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1092 struct usb_ctrlrequest *ctrl)
1094 bool stall = false;
1095 struct renesas_usb3_ep *usb3_ep;
1096 int num;
1097 u16 status = 0;
1099 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1100 case USB_RECIP_DEVICE:
1101 if (usb3->gadget.is_selfpowered)
1102 status |= 1 << USB_DEVICE_SELF_POWERED;
1103 if (usb3->gadget.speed == USB_SPEED_SUPER)
1104 status |= usb3_feature_get_un_enabled(usb3);
1105 break;
1106 case USB_RECIP_INTERFACE:
1107 break;
1108 case USB_RECIP_ENDPOINT:
1109 num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1110 usb3_ep = usb3_get_ep(usb3, num);
1111 if (usb3_ep->halt)
1112 status |= 1 << USB_ENDPOINT_HALT;
1113 break;
1114 default:
1115 stall = true;
1116 break;
1119 if (!stall) {
1120 status = cpu_to_le16(status);
1121 dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1122 usb_req_to_usb3_req(usb3->ep0_req));
1123 usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
1124 usb3_pipe0_get_status_completion);
1127 return stall;
1130 static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1131 struct usb_ctrlrequest *ctrl, bool set)
1133 bool stall = true;
1134 u16 w_value = le16_to_cpu(ctrl->wValue);
1136 switch (w_value) {
1137 case USB_DEVICE_TEST_MODE:
1138 if (!set)
1139 break;
1140 usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1141 stall = false;
1142 break;
1143 case USB_DEVICE_U1_ENABLE:
1144 case USB_DEVICE_U2_ENABLE:
1145 if (usb3->gadget.speed != USB_SPEED_SUPER)
1146 break;
1147 if (w_value == USB_DEVICE_U1_ENABLE)
1148 usb3_feature_u1_enable(usb3, set);
1149 if (w_value == USB_DEVICE_U2_ENABLE)
1150 usb3_feature_u2_enable(usb3, set);
1151 stall = false;
1152 break;
1153 default:
1154 break;
1157 return stall;
1160 static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1162 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1164 if (unlikely(usb3_ep->num))
1165 return -EINVAL;
1167 usb3_ep->halt = halt;
1168 if (halt)
1169 usb3_set_p0_con_stall(usb3);
1170 else
1171 usb3_set_p0_con_stop(usb3);
1173 return 0;
1176 static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1177 bool is_clear_feature)
1179 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1180 unsigned long flags;
1182 spin_lock_irqsave(&usb3->lock, flags);
1183 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1184 usb3_ep->halt = halt;
1185 if (halt) {
1186 usb3_pn_stall(usb3);
1187 } else if (!is_clear_feature || !usb3_ep->wedge) {
1188 usb3_pn_con_clear(usb3);
1189 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1190 usb3_pn_stop(usb3);
1193 spin_unlock_irqrestore(&usb3->lock, flags);
1195 return 0;
1198 static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1199 bool is_clear_feature)
1201 int ret = 0;
1203 if (halt && usb3_ep->started)
1204 return -EAGAIN;
1206 if (usb3_ep->num)
1207 ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1208 else
1209 ret = usb3_set_halt_p0(usb3_ep, halt);
1211 return ret;
1214 static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1215 struct usb_ctrlrequest *ctrl,
1216 bool set)
1218 int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1219 struct renesas_usb3_ep *usb3_ep;
1220 struct renesas_usb3_request *usb3_req;
1222 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1223 return true; /* stall */
1225 usb3_ep = usb3_get_ep(usb3, num);
1226 usb3_set_halt(usb3_ep, set, true);
1228 /* Restarts a queue if clear feature */
1229 if (!set) {
1230 usb3_ep->started = false;
1231 usb3_req = usb3_get_request(usb3_ep);
1232 if (usb3_req)
1233 usb3_start_pipen(usb3_ep, usb3_req);
1236 return false;
1239 static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1240 struct usb_ctrlrequest *ctrl, bool set)
1242 bool stall = false;
1244 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1245 case USB_RECIP_DEVICE:
1246 stall = usb3_std_req_feature_device(usb3, ctrl, set);
1247 break;
1248 case USB_RECIP_INTERFACE:
1249 break;
1250 case USB_RECIP_ENDPOINT:
1251 stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1252 break;
1253 default:
1254 stall = true;
1255 break;
1258 if (!stall)
1259 usb3_set_p0_con_for_no_data(usb3);
1261 return stall;
1264 static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1265 struct usb_request *req)
1267 /* TODO */
1270 static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1271 struct usb_ctrlrequest *ctrl)
1273 u16 w_length = le16_to_cpu(ctrl->wLength);
1275 if (w_length != 6)
1276 return true; /* stall */
1278 dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1279 usb_req_to_usb3_req(usb3->ep0_req));
1280 usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1282 return false;
1285 static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1286 struct usb_ctrlrequest *ctrl)
1288 if (ctrl->wValue > 0)
1289 usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1290 else
1291 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1293 return false;
1297 * usb3_handle_standard_request - handle some standard requests
1298 * @usb3: the renesas_usb3 pointer
1299 * @ctrl: a pointer of setup data
1301 * Returns true if this function handled a standard request
1303 static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1304 struct usb_ctrlrequest *ctrl)
1306 bool ret = false;
1307 bool stall = false;
1309 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1310 switch (ctrl->bRequest) {
1311 case USB_REQ_SET_ADDRESS:
1312 stall = usb3_std_req_set_address(usb3, ctrl);
1313 ret = true;
1314 break;
1315 case USB_REQ_GET_STATUS:
1316 stall = usb3_std_req_get_status(usb3, ctrl);
1317 ret = true;
1318 break;
1319 case USB_REQ_CLEAR_FEATURE:
1320 stall = usb3_std_req_feature(usb3, ctrl, false);
1321 ret = true;
1322 break;
1323 case USB_REQ_SET_FEATURE:
1324 stall = usb3_std_req_feature(usb3, ctrl, true);
1325 ret = true;
1326 break;
1327 case USB_REQ_SET_SEL:
1328 stall = usb3_std_req_set_sel(usb3, ctrl);
1329 ret = true;
1330 break;
1331 case USB_REQ_SET_ISOCH_DELAY:
1332 /* This hardware doesn't support Isochronous xfer */
1333 stall = true;
1334 ret = true;
1335 break;
1336 case USB_REQ_SET_CONFIGURATION:
1337 usb3_std_req_set_configuration(usb3, ctrl);
1338 break;
1339 default:
1340 break;
1344 if (stall)
1345 usb3_set_p0_con_stall(usb3);
1347 return ret;
1350 static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1352 usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1354 return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1357 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1359 struct usb_ctrlrequest ctrl;
1360 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1362 /* Call giveback function if previous transfer is not completed */
1363 if (usb3_ep->started)
1364 usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1365 -ECONNRESET);
1367 usb3_p0_con_clear_buffer(usb3);
1368 usb3_get_setup_data(usb3, &ctrl);
1369 if (!usb3_handle_standard_request(usb3, &ctrl))
1370 if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1371 usb3_set_p0_con_stall(usb3);
1374 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1376 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1377 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1379 if (!usb3_req)
1380 return;
1382 usb3_p0_xfer(usb3_ep, usb3_req);
1385 static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1387 u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1389 p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1390 usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1391 if (p0_int_sta & P0_INT_STSED)
1392 usb3_irq_epc_pipe0_status_end(usb3);
1393 if (p0_int_sta & P0_INT_SETUP)
1394 usb3_irq_epc_pipe0_setup(usb3);
1395 if (p0_int_sta & P0_INT_BFRDY)
1396 usb3_irq_epc_pipe0_bfrdy(usb3);
1399 static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1400 struct renesas_usb3_ep *usb3_ep,
1401 struct renesas_usb3_request *usb3_req,
1402 int status)
1404 usb3_pn_stop(usb3);
1405 usb3_disable_pipe_irq(usb3, usb3_ep->num);
1406 usb3_request_done(usb3_ep, usb3_req, status);
1408 /* get next usb3_req */
1409 usb3_req = usb3_get_request(usb3_ep);
1410 if (usb3_req)
1411 usb3_start_pipen(usb3_ep, usb3_req);
1414 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1416 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1417 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1419 if (!usb3_req)
1420 return;
1422 if (usb3_ep->dir_in) {
1423 dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1424 __func__, usb3_req->req.length, usb3_req->req.actual);
1425 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1429 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1431 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1432 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1434 if (!usb3_req)
1435 return;
1437 if (usb3_ep->dir_in) {
1438 /* Do not stop the IN pipe here to detect LSTTR interrupt */
1439 if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1440 usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1441 } else {
1442 if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1443 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1447 static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1449 u32 pn_int_sta;
1451 if (usb3_pn_change(usb3, num) < 0)
1452 return;
1454 pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1455 pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1456 usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1457 if (pn_int_sta & PN_INT_LSTTR)
1458 usb3_irq_epc_pipen_lsttr(usb3, num);
1459 if (pn_int_sta & PN_INT_BFRDY)
1460 usb3_irq_epc_pipen_bfrdy(usb3, num);
1463 static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1465 int i;
1467 for (i = 0; i < usb3->num_usb3_eps; i++) {
1468 if (int_sta_2 & USB_INT_2_PIPE(i)) {
1469 if (!i)
1470 usb3_irq_epc_pipe0(usb3);
1471 else
1472 usb3_irq_epc_pipen(usb3, i);
1477 static void usb3_irq_epc(struct renesas_usb3 *usb3)
1479 u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
1480 u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
1482 int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
1483 if (int_sta_1) {
1484 usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
1485 usb3_irq_epc_int_1(usb3, int_sta_1);
1488 int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
1489 if (int_sta_2)
1490 usb3_irq_epc_int_2(usb3, int_sta_2);
1493 static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
1495 struct renesas_usb3 *usb3 = _usb3;
1496 irqreturn_t ret = IRQ_NONE;
1497 u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
1499 if (axi_int_sta & AXI_INT_EPCINT) {
1500 usb3_irq_epc(usb3);
1501 ret = IRQ_HANDLED;
1504 return ret;
1507 static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
1508 const struct usb_endpoint_descriptor *desc)
1510 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1511 u32 val = 0;
1513 val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
1514 val |= PN_MOD_TYPE(usb_endpoint_type(desc));
1515 val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
1516 usb3_write(usb3, val, USB3_PN_MOD);
1519 static u32 usb3_calc_ramarea(int ram_size)
1521 WARN_ON(ram_size > SZ_16K);
1523 if (ram_size <= SZ_1K)
1524 return PN_RAMMAP_RAMAREA_1KB;
1525 else if (ram_size <= SZ_2K)
1526 return PN_RAMMAP_RAMAREA_2KB;
1527 else if (ram_size <= SZ_4K)
1528 return PN_RAMMAP_RAMAREA_4KB;
1529 else if (ram_size <= SZ_8K)
1530 return PN_RAMMAP_RAMAREA_8KB;
1531 else
1532 return PN_RAMMAP_RAMAREA_16KB;
1535 static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
1536 const struct usb_endpoint_descriptor *desc)
1538 return usb3_ep->rammap_val | PN_RAMMAP_MPKT(usb_endpoint_maxp(desc));
1541 static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
1542 const struct usb_endpoint_descriptor *desc)
1544 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1545 unsigned long flags;
1547 usb3_ep->dir_in = usb_endpoint_dir_in(desc);
1549 spin_lock_irqsave(&usb3->lock, flags);
1550 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1551 usb3_write_pn_mod(usb3_ep, desc);
1552 usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
1553 USB3_PN_RAMMAP);
1554 usb3_pn_con_clear(usb3);
1555 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1557 spin_unlock_irqrestore(&usb3->lock, flags);
1559 return 0;
1562 static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
1564 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1565 unsigned long flags;
1567 usb3_ep->halt = false;
1569 spin_lock_irqsave(&usb3->lock, flags);
1570 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1571 usb3_write(usb3, 0, USB3_PN_RAMMAP);
1572 usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
1574 spin_unlock_irqrestore(&usb3->lock, flags);
1576 return 0;
1579 /*------- usb_ep_ops -----------------------------------------------------*/
1580 static int renesas_usb3_ep_enable(struct usb_ep *_ep,
1581 const struct usb_endpoint_descriptor *desc)
1583 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1585 return usb3_enable_pipe_n(usb3_ep, desc);
1588 static int renesas_usb3_ep_disable(struct usb_ep *_ep)
1590 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1591 struct renesas_usb3_request *usb3_req;
1593 do {
1594 usb3_req = usb3_get_request(usb3_ep);
1595 if (!usb3_req)
1596 break;
1597 usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
1598 } while (1);
1600 return usb3_disable_pipe_n(usb3_ep);
1603 static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
1605 struct renesas_usb3_request *usb3_req;
1607 usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
1608 if (!usb3_req)
1609 return NULL;
1611 INIT_LIST_HEAD(&usb3_req->queue);
1613 return &usb3_req->req;
1616 static void __renesas_usb3_ep_free_request(struct usb_request *_req)
1618 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1620 kfree(usb3_req);
1623 static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
1624 gfp_t gfp_flags)
1626 return __renesas_usb3_ep_alloc_request(gfp_flags);
1629 static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
1630 struct usb_request *_req)
1632 __renesas_usb3_ep_free_request(_req);
1635 static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1637 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1638 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1639 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1641 dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
1642 _req->length);
1644 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
1646 return 0;
1649 static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
1651 return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
1654 static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
1656 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1658 usb3_ep->wedge = true;
1659 return usb3_set_halt(usb3_ep, true, false);
1662 static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
1664 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1665 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1666 unsigned long flags;
1668 if (usb3_ep->num) {
1669 spin_lock_irqsave(&usb3->lock, flags);
1670 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1671 usb3_pn_con_clear(usb3);
1672 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1674 spin_unlock_irqrestore(&usb3->lock, flags);
1675 } else {
1676 usb3_p0_con_clear_buffer(usb3);
1680 static struct usb_ep_ops renesas_usb3_ep_ops = {
1681 .enable = renesas_usb3_ep_enable,
1682 .disable = renesas_usb3_ep_disable,
1684 .alloc_request = renesas_usb3_ep_alloc_request,
1685 .free_request = renesas_usb3_ep_free_request,
1687 .queue = renesas_usb3_ep_queue,
1688 .dequeue = renesas_usb3_ep_dequeue,
1690 .set_halt = renesas_usb3_ep_set_halt,
1691 .set_wedge = renesas_usb3_ep_set_wedge,
1692 .fifo_flush = renesas_usb3_ep_fifo_flush,
1695 /*------- usb_gadget_ops -------------------------------------------------*/
1696 static int renesas_usb3_start(struct usb_gadget *gadget,
1697 struct usb_gadget_driver *driver)
1699 struct renesas_usb3 *usb3;
1701 if (!driver || driver->max_speed < USB_SPEED_FULL ||
1702 !driver->setup)
1703 return -EINVAL;
1705 usb3 = gadget_to_renesas_usb3(gadget);
1707 /* hook up the driver */
1708 usb3->driver = driver;
1710 renesas_usb3_init_controller(usb3);
1712 return 0;
1715 static int renesas_usb3_stop(struct usb_gadget *gadget)
1717 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1718 unsigned long flags;
1720 spin_lock_irqsave(&usb3->lock, flags);
1721 usb3->softconnect = false;
1722 usb3->gadget.speed = USB_SPEED_UNKNOWN;
1723 usb3->driver = NULL;
1724 renesas_usb3_stop_controller(usb3);
1725 spin_unlock_irqrestore(&usb3->lock, flags);
1727 return 0;
1730 static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
1732 return -EOPNOTSUPP;
1735 static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
1737 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1739 usb3->softconnect = !!is_on;
1741 return 0;
1744 static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
1746 gadget->is_selfpowered = !!is_self;
1748 return 0;
1751 static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
1752 .get_frame = renesas_usb3_get_frame,
1753 .udc_start = renesas_usb3_start,
1754 .udc_stop = renesas_usb3_stop,
1755 .pullup = renesas_usb3_pullup,
1756 .set_selfpowered = renesas_usb3_set_selfpowered,
1759 /*------- platform_driver ------------------------------------------------*/
1760 static int renesas_usb3_remove(struct platform_device *pdev)
1762 struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
1764 pm_runtime_put(&pdev->dev);
1765 pm_runtime_disable(&pdev->dev);
1767 usb_del_gadget_udc(&usb3->gadget);
1769 __renesas_usb3_ep_free_request(usb3->ep0_req);
1771 return 0;
1774 static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
1775 const struct renesas_usb3_priv *priv)
1777 struct renesas_usb3_ep *usb3_ep;
1778 int i;
1780 /* calculate num_usb3_eps from renesas_usb3_priv */
1781 usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
1782 priv->ramsize_per_pipe + 1;
1784 if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
1785 usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
1787 usb3->usb3_ep = devm_kzalloc(dev, sizeof(*usb3_ep) * usb3->num_usb3_eps,
1788 GFP_KERNEL);
1789 if (!usb3->usb3_ep)
1790 return -ENOMEM;
1792 dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
1794 * This driver prepares pipes as follows:
1795 * - odd pipes = IN pipe
1796 * - even pipes = OUT pipe (except pipe 0)
1798 usb3_for_each_ep(usb3_ep, usb3, i) {
1799 snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
1800 usb3_ep->usb3 = usb3;
1801 usb3_ep->num = i;
1802 usb3_ep->ep.name = usb3_ep->ep_name;
1803 usb3_ep->ep.ops = &renesas_usb3_ep_ops;
1804 INIT_LIST_HEAD(&usb3_ep->queue);
1805 INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
1806 if (!i) {
1807 /* for control pipe */
1808 usb3->gadget.ep0 = &usb3_ep->ep;
1809 usb_ep_set_maxpacket_limit(&usb3_ep->ep,
1810 USB3_EP0_HSFS_MAX_PACKET_SIZE);
1811 usb3_ep->ep.caps.type_control = true;
1812 usb3_ep->ep.caps.dir_in = true;
1813 usb3_ep->ep.caps.dir_out = true;
1814 continue;
1817 /* for bulk or interrupt pipe */
1818 usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
1819 list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
1820 usb3_ep->ep.caps.type_bulk = true;
1821 usb3_ep->ep.caps.type_int = true;
1822 if (i & 1)
1823 usb3_ep->ep.caps.dir_in = true;
1824 else
1825 usb3_ep->ep.caps.dir_out = true;
1828 return 0;
1831 static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
1832 const struct renesas_usb3_priv *priv)
1834 struct renesas_usb3_ep *usb3_ep;
1835 int i;
1836 u32 ramif[2], basead[2]; /* index 0 = for IN pipes */
1837 u32 *cur_ramif, *cur_basead;
1838 u32 val;
1840 memset(ramif, 0, sizeof(ramif));
1841 memset(basead, 0, sizeof(basead));
1844 * This driver prepares pipes as follows:
1845 * - all pipes = the same size as "ramsize_per_pipe"
1846 * Please refer to the "Method of Specifying RAM Mapping"
1848 usb3_for_each_ep(usb3_ep, usb3, i) {
1849 if (!i)
1850 continue; /* out of scope if ep num = 0 */
1851 if (usb3_ep->ep.caps.dir_in) {
1852 cur_ramif = &ramif[0];
1853 cur_basead = &basead[0];
1854 } else {
1855 cur_ramif = &ramif[1];
1856 cur_basead = &basead[1];
1859 if (*cur_basead > priv->ramsize_per_ramif)
1860 continue; /* out of memory for IN or OUT pipe */
1862 /* calculate rammap_val */
1863 val = PN_RAMMAP_RAMIF(*cur_ramif);
1864 val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
1865 val |= PN_RAMMAP_BASEAD(*cur_basead);
1866 usb3_ep->rammap_val = val;
1868 dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
1869 i, val, *cur_ramif, *cur_basead);
1871 /* update current ramif */
1872 if (*cur_ramif + 1 == priv->num_ramif) {
1873 *cur_ramif = 0;
1874 *cur_basead += priv->ramsize_per_pipe;
1875 } else {
1876 (*cur_ramif)++;
1881 static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795 = {
1882 .ramsize_per_ramif = SZ_16K,
1883 .num_ramif = 2,
1884 .ramsize_per_pipe = SZ_4K,
1885 .workaround_for_vbus = true,
1888 static const struct of_device_id usb3_of_match[] = {
1890 .compatible = "renesas,r8a7795-usb3-peri",
1891 .data = &renesas_usb3_priv_r8a7795,
1893 { },
1895 MODULE_DEVICE_TABLE(of, usb3_of_match);
1897 static int renesas_usb3_probe(struct platform_device *pdev)
1899 struct renesas_usb3 *usb3;
1900 struct resource *res;
1901 const struct of_device_id *match;
1902 int irq, ret;
1903 const struct renesas_usb3_priv *priv;
1905 match = of_match_node(usb3_of_match, pdev->dev.of_node);
1906 if (!match)
1907 return -ENODEV;
1908 priv = match->data;
1910 irq = platform_get_irq(pdev, 0);
1911 if (irq < 0)
1912 return -ENODEV;
1914 usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
1915 if (!usb3)
1916 return -ENOMEM;
1918 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1919 usb3->reg = devm_ioremap_resource(&pdev->dev, res);
1920 if (IS_ERR(usb3->reg))
1921 return PTR_ERR(usb3->reg);
1923 platform_set_drvdata(pdev, usb3);
1924 spin_lock_init(&usb3->lock);
1926 usb3->gadget.ops = &renesas_usb3_gadget_ops;
1927 usb3->gadget.name = udc_name;
1928 usb3->gadget.max_speed = USB_SPEED_SUPER;
1929 INIT_LIST_HEAD(&usb3->gadget.ep_list);
1930 ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
1931 if (ret < 0)
1932 return ret;
1933 renesas_usb3_init_ram(usb3, &pdev->dev, priv);
1935 ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
1936 dev_name(&pdev->dev), usb3);
1937 if (ret < 0)
1938 return ret;
1940 /* for ep0 handling */
1941 usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
1942 if (!usb3->ep0_req)
1943 return -ENOMEM;
1945 ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
1946 if (ret < 0)
1947 goto err_add_udc;
1949 usb3->workaround_for_vbus = priv->workaround_for_vbus;
1951 pm_runtime_enable(&pdev->dev);
1952 pm_runtime_get_sync(&pdev->dev);
1954 dev_info(&pdev->dev, "probed\n");
1956 return 0;
1958 err_add_udc:
1959 __renesas_usb3_ep_free_request(usb3->ep0_req);
1961 return ret;
1964 static struct platform_driver renesas_usb3_driver = {
1965 .probe = renesas_usb3_probe,
1966 .remove = renesas_usb3_remove,
1967 .driver = {
1968 .name = (char *)udc_name,
1969 .of_match_table = of_match_ptr(usb3_of_match),
1972 module_platform_driver(renesas_usb3_driver);
1974 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
1975 MODULE_LICENSE("GPL v2");
1976 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
1977 MODULE_ALIAS("platform:renesas_usb3");