inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / usb / gadget / udc / renesas_usb3.c
blobb1ae944c83a939a380e9785db4666924056c5bbd
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 6 /* This includes PIPE 0 */
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);
524 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM | USB_INT_1_B3_PLLWKUP |
525 USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
526 USB_INT_1_SPEED | USB_INT_1_B3_WRMRST |
527 USB_INT_1_B3_HOTRST | USB_INT_1_B2_SPND |
528 USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
529 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
530 usb3_init_epc_registers(usb3);
532 if (usb3->driver)
533 usb3->driver->disconnect(&usb3->gadget);
536 static void usb3_check_vbus(struct renesas_usb3 *usb3)
538 if (usb3->workaround_for_vbus) {
539 usb3_connect(usb3);
540 } else {
541 if (usb3_read(usb3, USB3_USB_STA) & USB_STA_VBUS_STA)
542 usb3_connect(usb3);
543 else
544 usb3_disconnect(usb3);
548 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
550 usb3_init_axi_bridge(usb3);
551 usb3_init_epc_registers(usb3);
553 usb3_check_vbus(usb3);
556 static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
558 usb3_disconnect(usb3);
559 usb3_write(usb3, 0, USB3_P0_INT_ENA);
560 usb3_write(usb3, 0, USB3_PN_INT_ENA);
561 usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
562 usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
563 usb3_write(usb3, 0, USB3_AXI_INT_ENA);
566 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
568 usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
569 usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
570 usb3_start_usb3_connection(usb3);
573 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
575 usb3_transition_to_default_state(usb3, true);
578 static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
580 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
581 usb3_start_usb2_connection(usb3);
582 usb3_transition_to_default_state(usb3, false);
585 static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
587 usb3_stop_usb3_connection(usb3);
588 if (usb3_wakeup_usb2_phy(usb3))
589 usb3_irq_epc_int_1_resume(usb3);
592 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
594 usb3_reset_epc(usb3);
595 if (usb3->disabled_count < 3)
596 usb3_start_usb3_connection(usb3);
597 else
598 usb3_start_usb2_connection(usb3);
601 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
603 usb3_check_vbus(usb3);
606 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
608 usb3_reset_epc(usb3);
609 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
611 /* This bit shall be set within 12ms from the start of HotReset */
612 usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
615 static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
617 usb3_reset_epc(usb3);
618 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
620 usb3_start_operation_for_usb3(usb3);
621 usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
624 static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
626 u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
628 switch (speed) {
629 case USB_STA_SPEED_SS:
630 usb3->gadget.speed = USB_SPEED_SUPER;
631 break;
632 case USB_STA_SPEED_HS:
633 usb3->gadget.speed = USB_SPEED_HIGH;
634 break;
635 case USB_STA_SPEED_FS:
636 usb3->gadget.speed = USB_SPEED_FULL;
637 break;
638 default:
639 usb3->gadget.speed = USB_SPEED_UNKNOWN;
640 break;
644 static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
646 if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
647 usb3_irq_epc_int_1_pll_wakeup(usb3);
649 if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
650 usb3_irq_epc_int_1_linkup_success(usb3);
652 if (int_sta_1 & USB_INT_1_B3_HOTRST)
653 usb3_irq_epc_int_1_hot_reset(usb3);
655 if (int_sta_1 & USB_INT_1_B3_WRMRST)
656 usb3_irq_epc_int_1_warm_reset(usb3);
658 if (int_sta_1 & USB_INT_1_B3_DISABLE)
659 usb3_irq_epc_int_1_disable(usb3);
661 if (int_sta_1 & USB_INT_1_B2_USBRST)
662 usb3_irq_epc_int_1_bus_reset(usb3);
664 if (int_sta_1 & USB_INT_1_B2_RSUM)
665 usb3_irq_epc_int_1_resume(usb3);
667 if (int_sta_1 & USB_INT_1_SPEED)
668 usb3_irq_epc_int_1_speed(usb3);
670 if (int_sta_1 & USB_INT_1_VBUS_CNG)
671 usb3_irq_epc_int_1_vbus_change(usb3);
674 static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
675 *usb3_ep)
677 return list_first_entry_or_null(&usb3_ep->queue,
678 struct renesas_usb3_request, queue);
681 static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
682 *usb3_ep)
684 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
685 struct renesas_usb3_request *usb3_req;
686 unsigned long flags;
688 spin_lock_irqsave(&usb3->lock, flags);
689 usb3_req = __usb3_get_request(usb3_ep);
690 spin_unlock_irqrestore(&usb3->lock, flags);
692 return usb3_req;
695 static void __usb3_request_done(struct renesas_usb3_ep *usb3_ep,
696 struct renesas_usb3_request *usb3_req,
697 int status)
699 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
701 dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
702 usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
703 status);
704 usb3_req->req.status = status;
705 usb3_ep->started = false;
706 list_del_init(&usb3_req->queue);
707 spin_unlock(&usb3->lock);
708 usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
709 spin_lock(&usb3->lock);
712 static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
713 struct renesas_usb3_request *usb3_req, int status)
715 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
716 unsigned long flags;
718 spin_lock_irqsave(&usb3->lock, flags);
719 __usb3_request_done(usb3_ep, usb3_req, status);
720 spin_unlock_irqrestore(&usb3->lock, flags);
723 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
725 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
726 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
728 if (usb3_req)
729 usb3_request_done(usb3_ep, usb3_req, 0);
730 if (usb3->test_mode)
731 usb3_set_test_mode(usb3);
734 static void usb3_get_setup_data(struct renesas_usb3 *usb3,
735 struct usb_ctrlrequest *ctrl)
737 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
738 u32 *data = (u32 *)ctrl;
740 *data++ = usb3_read(usb3, USB3_STUP_DAT_0);
741 *data = usb3_read(usb3, USB3_STUP_DAT_1);
743 /* update this driver's flag */
744 usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
747 static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
749 u32 val = usb3_read(usb3, USB3_P0_CON);
751 val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
752 val |= res | P0_CON_RES_WEN;
753 usb3_write(usb3, val, USB3_P0_CON);
756 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
758 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
759 P0_CON_OT_RES_FORCE_STALL |
760 P0_CON_IN_RES_NORMAL);
763 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
765 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
766 P0_CON_OT_RES_FORCE_STALL |
767 P0_CON_IN_RES_NORMAL);
770 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
772 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
773 P0_CON_OT_RES_NORMAL |
774 P0_CON_IN_RES_FORCE_STALL);
777 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
779 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
780 P0_CON_OT_RES_NORMAL |
781 P0_CON_IN_RES_FORCE_STALL);
784 static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
786 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
787 P0_CON_OT_RES_FORCE_STALL |
788 P0_CON_IN_RES_FORCE_STALL);
791 static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
793 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
794 P0_CON_OT_RES_FORCE_STALL |
795 P0_CON_IN_RES_FORCE_STALL);
798 static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
800 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
801 P0_CON_OT_RES_FORCE_NRDY |
802 P0_CON_IN_RES_FORCE_NRDY);
805 static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
807 if (num == 0 || num > usb3->num_usb3_eps)
808 return -ENXIO;
810 usb3_write(usb3, num, USB3_PIPE_COM);
812 return 0;
815 static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
817 u32 val = usb3_read(usb3, USB3_PN_CON);
819 val &= ~PN_CON_RES_MASK;
820 val |= res & PN_CON_RES_MASK;
821 val |= PN_CON_RES_WEN;
822 usb3_write(usb3, val, USB3_PN_CON);
825 static void usb3_pn_start(struct renesas_usb3 *usb3)
827 usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
830 static void usb3_pn_stop(struct renesas_usb3 *usb3)
832 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
835 static void usb3_pn_stall(struct renesas_usb3 *usb3)
837 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
840 static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
842 usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
844 return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
847 static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
848 struct renesas_usb3_request *usb3_req)
850 struct usb_request *req = &usb3_req->req;
852 if ((!req->zero && req->actual == req->length) ||
853 (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
854 return true;
855 else
856 return false;
859 static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
861 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
862 u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
864 return usb3_wait(usb3, sta_reg, mask, mask);
867 static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
868 bool last)
870 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
871 u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
872 u32 val = usb3_read(usb3, con_reg);
874 val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
875 val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
876 usb3_write(usb3, val, con_reg);
879 static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
880 struct renesas_usb3_request *usb3_req,
881 u32 fifo_reg)
883 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
884 int i;
885 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
886 usb3_ep->ep.maxpacket);
887 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
888 u32 tmp = 0;
889 bool is_last = !len ? true : false;
891 if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
892 return -EBUSY;
894 /* Update gadget driver parameter */
895 usb3_req->req.actual += len;
897 /* Write data to the register */
898 if (len >= 4) {
899 iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
900 buf += (len / 4) * 4;
901 len %= 4; /* update len to use usb3_set_pX_con_send() */
904 if (len) {
905 for (i = 0; i < len; i++)
906 tmp |= buf[i] << (8 * i);
907 usb3_write(usb3, tmp, fifo_reg);
910 if (!is_last)
911 is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
912 /* Send the data */
913 usb3_set_px_con_send(usb3_ep, len, is_last);
915 return is_last ? 0 : -EAGAIN;
918 static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
920 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
921 u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
923 return usb3_read(usb3, lng_reg);
926 static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
927 struct renesas_usb3_request *usb3_req, u32 fifo_reg)
929 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
930 int i;
931 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
932 usb3_get_received_length(usb3_ep));
933 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
934 u32 tmp = 0;
936 if (!len)
937 return 0;
939 /* Update gadget driver parameter */
940 usb3_req->req.actual += len;
942 /* Read data from the register */
943 if (len >= 4) {
944 ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
945 buf += (len / 4) * 4;
946 len %= 4;
949 if (len) {
950 tmp = usb3_read(usb3, fifo_reg);
951 for (i = 0; i < len; i++)
952 buf[i] = (tmp >> (8 * i)) & 0xff;
955 return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
958 static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
959 struct renesas_usb3_request *usb3_req)
961 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
963 if (usb3_ep->dir_in) {
964 usb3_set_p0_con_for_ctrl_read_status(usb3);
965 } else {
966 if (!usb3_req->req.length)
967 usb3_set_p0_con_for_no_data(usb3);
968 else
969 usb3_set_p0_con_for_ctrl_write_status(usb3);
973 static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
974 struct renesas_usb3_request *usb3_req)
976 int ret = -EAGAIN;
978 if (usb3_ep->dir_in)
979 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
980 else
981 ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
983 if (!ret)
984 usb3_set_status_stage(usb3_ep, usb3_req);
987 static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
988 struct renesas_usb3_request *usb3_req)
990 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
992 if (usb3_ep->started)
993 return;
995 usb3_ep->started = true;
997 if (usb3_ep->dir_in) {
998 usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
999 usb3_set_p0_con_for_ctrl_read_data(usb3);
1000 } else {
1001 usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
1002 if (usb3_req->req.length)
1003 usb3_set_p0_con_for_ctrl_write_data(usb3);
1006 usb3_p0_xfer(usb3_ep, usb3_req);
1009 static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
1010 struct renesas_usb3_request *usb3_req)
1012 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1013 struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
1014 unsigned long flags;
1015 int ret = -EAGAIN;
1016 u32 enable_bits = 0;
1018 if (usb3_ep->halt || usb3_ep->started)
1019 return;
1020 if (usb3_req != usb3_req_first)
1021 return;
1023 spin_lock_irqsave(&usb3->lock, flags);
1024 if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1025 goto out;
1027 usb3_ep->started = true;
1028 usb3_pn_start(usb3);
1030 if (usb3_ep->dir_in) {
1031 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1032 enable_bits |= PN_INT_LSTTR;
1035 if (ret < 0)
1036 enable_bits |= PN_INT_BFRDY;
1038 if (enable_bits) {
1039 usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1040 usb3_enable_pipe_irq(usb3, usb3_ep->num);
1042 out:
1043 spin_unlock_irqrestore(&usb3->lock, flags);
1046 static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1047 gfp_t gfp_flags)
1049 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1050 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1051 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1052 unsigned long flags;
1054 dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1055 _req->length);
1057 _req->status = -EINPROGRESS;
1058 _req->actual = 0;
1059 spin_lock_irqsave(&usb3->lock, flags);
1060 list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1061 spin_unlock_irqrestore(&usb3->lock, flags);
1063 if (!usb3_ep->num)
1064 usb3_start_pipe0(usb3_ep, usb3_req);
1065 else
1066 usb3_start_pipen(usb3_ep, usb3_req);
1068 return 0;
1071 static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1073 /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1074 usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1077 static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1078 struct usb_ctrlrequest *ctrl)
1080 if (ctrl->wValue >= 128)
1081 return true; /* stall */
1083 usb3_set_device_address(usb3, ctrl->wValue);
1084 usb3_set_p0_con_for_no_data(usb3);
1086 return false;
1089 static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1090 void *tx_data, size_t len,
1091 void (*complete)(struct usb_ep *ep,
1092 struct usb_request *req))
1094 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1096 if (tx_data)
1097 memcpy(usb3->ep0_buf, tx_data,
1098 min_t(size_t, len, USB3_EP0_BUF_SIZE));
1100 usb3->ep0_req->buf = &usb3->ep0_buf;
1101 usb3->ep0_req->length = len;
1102 usb3->ep0_req->complete = complete;
1103 renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1106 static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1107 struct usb_request *req)
1111 static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1112 struct usb_ctrlrequest *ctrl)
1114 bool stall = false;
1115 struct renesas_usb3_ep *usb3_ep;
1116 int num;
1117 u16 status = 0;
1119 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1120 case USB_RECIP_DEVICE:
1121 if (usb3->gadget.is_selfpowered)
1122 status |= 1 << USB_DEVICE_SELF_POWERED;
1123 if (usb3->gadget.speed == USB_SPEED_SUPER)
1124 status |= usb3_feature_get_un_enabled(usb3);
1125 break;
1126 case USB_RECIP_INTERFACE:
1127 break;
1128 case USB_RECIP_ENDPOINT:
1129 num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1130 usb3_ep = usb3_get_ep(usb3, num);
1131 if (usb3_ep->halt)
1132 status |= 1 << USB_ENDPOINT_HALT;
1133 break;
1134 default:
1135 stall = true;
1136 break;
1139 if (!stall) {
1140 status = cpu_to_le16(status);
1141 dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1142 usb_req_to_usb3_req(usb3->ep0_req));
1143 usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
1144 usb3_pipe0_get_status_completion);
1147 return stall;
1150 static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1151 struct usb_ctrlrequest *ctrl, bool set)
1153 bool stall = true;
1154 u16 w_value = le16_to_cpu(ctrl->wValue);
1156 switch (w_value) {
1157 case USB_DEVICE_TEST_MODE:
1158 if (!set)
1159 break;
1160 usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1161 stall = false;
1162 break;
1163 case USB_DEVICE_U1_ENABLE:
1164 case USB_DEVICE_U2_ENABLE:
1165 if (usb3->gadget.speed != USB_SPEED_SUPER)
1166 break;
1167 if (w_value == USB_DEVICE_U1_ENABLE)
1168 usb3_feature_u1_enable(usb3, set);
1169 if (w_value == USB_DEVICE_U2_ENABLE)
1170 usb3_feature_u2_enable(usb3, set);
1171 stall = false;
1172 break;
1173 default:
1174 break;
1177 return stall;
1180 static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1182 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1184 if (unlikely(usb3_ep->num))
1185 return -EINVAL;
1187 usb3_ep->halt = halt;
1188 if (halt)
1189 usb3_set_p0_con_stall(usb3);
1190 else
1191 usb3_set_p0_con_stop(usb3);
1193 return 0;
1196 static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1197 bool is_clear_feature)
1199 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1200 unsigned long flags;
1202 spin_lock_irqsave(&usb3->lock, flags);
1203 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1204 usb3_ep->halt = halt;
1205 if (halt) {
1206 usb3_pn_stall(usb3);
1207 } else if (!is_clear_feature || !usb3_ep->wedge) {
1208 usb3_pn_con_clear(usb3);
1209 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1210 usb3_pn_stop(usb3);
1213 spin_unlock_irqrestore(&usb3->lock, flags);
1215 return 0;
1218 static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1219 bool is_clear_feature)
1221 int ret = 0;
1223 if (halt && usb3_ep->started)
1224 return -EAGAIN;
1226 if (usb3_ep->num)
1227 ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1228 else
1229 ret = usb3_set_halt_p0(usb3_ep, halt);
1231 return ret;
1234 static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1235 struct usb_ctrlrequest *ctrl,
1236 bool set)
1238 int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1239 struct renesas_usb3_ep *usb3_ep;
1240 struct renesas_usb3_request *usb3_req;
1242 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1243 return true; /* stall */
1245 usb3_ep = usb3_get_ep(usb3, num);
1246 usb3_set_halt(usb3_ep, set, true);
1248 /* Restarts a queue if clear feature */
1249 if (!set) {
1250 usb3_ep->started = false;
1251 usb3_req = usb3_get_request(usb3_ep);
1252 if (usb3_req)
1253 usb3_start_pipen(usb3_ep, usb3_req);
1256 return false;
1259 static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1260 struct usb_ctrlrequest *ctrl, bool set)
1262 bool stall = false;
1264 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1265 case USB_RECIP_DEVICE:
1266 stall = usb3_std_req_feature_device(usb3, ctrl, set);
1267 break;
1268 case USB_RECIP_INTERFACE:
1269 break;
1270 case USB_RECIP_ENDPOINT:
1271 stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1272 break;
1273 default:
1274 stall = true;
1275 break;
1278 if (!stall)
1279 usb3_set_p0_con_for_no_data(usb3);
1281 return stall;
1284 static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1285 struct usb_request *req)
1287 /* TODO */
1290 static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1291 struct usb_ctrlrequest *ctrl)
1293 u16 w_length = le16_to_cpu(ctrl->wLength);
1295 if (w_length != 6)
1296 return true; /* stall */
1298 dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1299 usb_req_to_usb3_req(usb3->ep0_req));
1300 usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1302 return false;
1305 static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1306 struct usb_ctrlrequest *ctrl)
1308 if (ctrl->wValue > 0)
1309 usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1310 else
1311 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1313 return false;
1317 * usb3_handle_standard_request - handle some standard requests
1318 * @usb3: the renesas_usb3 pointer
1319 * @ctrl: a pointer of setup data
1321 * Returns true if this function handled a standard request
1323 static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1324 struct usb_ctrlrequest *ctrl)
1326 bool ret = false;
1327 bool stall = false;
1329 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1330 switch (ctrl->bRequest) {
1331 case USB_REQ_SET_ADDRESS:
1332 stall = usb3_std_req_set_address(usb3, ctrl);
1333 ret = true;
1334 break;
1335 case USB_REQ_GET_STATUS:
1336 stall = usb3_std_req_get_status(usb3, ctrl);
1337 ret = true;
1338 break;
1339 case USB_REQ_CLEAR_FEATURE:
1340 stall = usb3_std_req_feature(usb3, ctrl, false);
1341 ret = true;
1342 break;
1343 case USB_REQ_SET_FEATURE:
1344 stall = usb3_std_req_feature(usb3, ctrl, true);
1345 ret = true;
1346 break;
1347 case USB_REQ_SET_SEL:
1348 stall = usb3_std_req_set_sel(usb3, ctrl);
1349 ret = true;
1350 break;
1351 case USB_REQ_SET_ISOCH_DELAY:
1352 /* This hardware doesn't support Isochronous xfer */
1353 stall = true;
1354 ret = true;
1355 break;
1356 case USB_REQ_SET_CONFIGURATION:
1357 usb3_std_req_set_configuration(usb3, ctrl);
1358 break;
1359 default:
1360 break;
1364 if (stall)
1365 usb3_set_p0_con_stall(usb3);
1367 return ret;
1370 static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1372 usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1374 return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1377 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1379 struct usb_ctrlrequest ctrl;
1380 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1382 /* Call giveback function if previous transfer is not completed */
1383 if (usb3_ep->started)
1384 usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1385 -ECONNRESET);
1387 usb3_p0_con_clear_buffer(usb3);
1388 usb3_get_setup_data(usb3, &ctrl);
1389 if (!usb3_handle_standard_request(usb3, &ctrl))
1390 if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1391 usb3_set_p0_con_stall(usb3);
1394 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1396 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1397 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1399 if (!usb3_req)
1400 return;
1402 usb3_p0_xfer(usb3_ep, usb3_req);
1405 static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1407 u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1409 p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1410 usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1411 if (p0_int_sta & P0_INT_STSED)
1412 usb3_irq_epc_pipe0_status_end(usb3);
1413 if (p0_int_sta & P0_INT_SETUP)
1414 usb3_irq_epc_pipe0_setup(usb3);
1415 if (p0_int_sta & P0_INT_BFRDY)
1416 usb3_irq_epc_pipe0_bfrdy(usb3);
1419 static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1420 struct renesas_usb3_ep *usb3_ep,
1421 struct renesas_usb3_request *usb3_req,
1422 int status)
1424 unsigned long flags;
1426 spin_lock_irqsave(&usb3->lock, flags);
1427 if (usb3_pn_change(usb3, usb3_ep->num))
1428 usb3_pn_stop(usb3);
1429 spin_unlock_irqrestore(&usb3->lock, flags);
1431 usb3_disable_pipe_irq(usb3, usb3_ep->num);
1432 usb3_request_done(usb3_ep, usb3_req, status);
1434 /* get next usb3_req */
1435 usb3_req = usb3_get_request(usb3_ep);
1436 if (usb3_req)
1437 usb3_start_pipen(usb3_ep, usb3_req);
1440 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1442 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1443 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1445 if (!usb3_req)
1446 return;
1448 if (usb3_ep->dir_in) {
1449 dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1450 __func__, usb3_req->req.length, usb3_req->req.actual);
1451 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1455 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1457 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1458 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1459 bool done = false;
1461 if (!usb3_req)
1462 return;
1464 spin_lock(&usb3->lock);
1465 if (usb3_pn_change(usb3, num))
1466 goto out;
1468 if (usb3_ep->dir_in) {
1469 /* Do not stop the IN pipe here to detect LSTTR interrupt */
1470 if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1471 usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1472 } else {
1473 if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1474 done = true;
1477 out:
1478 /* need to unlock because usb3_request_done_pipen() locks it */
1479 spin_unlock(&usb3->lock);
1481 if (done)
1482 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1485 static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1487 u32 pn_int_sta;
1489 spin_lock(&usb3->lock);
1490 if (usb3_pn_change(usb3, num) < 0) {
1491 spin_unlock(&usb3->lock);
1492 return;
1495 pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1496 pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1497 usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1498 spin_unlock(&usb3->lock);
1499 if (pn_int_sta & PN_INT_LSTTR)
1500 usb3_irq_epc_pipen_lsttr(usb3, num);
1501 if (pn_int_sta & PN_INT_BFRDY)
1502 usb3_irq_epc_pipen_bfrdy(usb3, num);
1505 static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1507 int i;
1509 for (i = 0; i < usb3->num_usb3_eps; i++) {
1510 if (int_sta_2 & USB_INT_2_PIPE(i)) {
1511 if (!i)
1512 usb3_irq_epc_pipe0(usb3);
1513 else
1514 usb3_irq_epc_pipen(usb3, i);
1519 static void usb3_irq_epc(struct renesas_usb3 *usb3)
1521 u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
1522 u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
1524 int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
1525 if (int_sta_1) {
1526 usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
1527 usb3_irq_epc_int_1(usb3, int_sta_1);
1530 int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
1531 if (int_sta_2)
1532 usb3_irq_epc_int_2(usb3, int_sta_2);
1535 static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
1537 struct renesas_usb3 *usb3 = _usb3;
1538 irqreturn_t ret = IRQ_NONE;
1539 u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
1541 if (axi_int_sta & AXI_INT_EPCINT) {
1542 usb3_irq_epc(usb3);
1543 ret = IRQ_HANDLED;
1546 return ret;
1549 static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
1550 const struct usb_endpoint_descriptor *desc)
1552 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1553 u32 val = 0;
1555 val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
1556 val |= PN_MOD_TYPE(usb_endpoint_type(desc));
1557 val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
1558 usb3_write(usb3, val, USB3_PN_MOD);
1561 static u32 usb3_calc_ramarea(int ram_size)
1563 WARN_ON(ram_size > SZ_16K);
1565 if (ram_size <= SZ_1K)
1566 return PN_RAMMAP_RAMAREA_1KB;
1567 else if (ram_size <= SZ_2K)
1568 return PN_RAMMAP_RAMAREA_2KB;
1569 else if (ram_size <= SZ_4K)
1570 return PN_RAMMAP_RAMAREA_4KB;
1571 else if (ram_size <= SZ_8K)
1572 return PN_RAMMAP_RAMAREA_8KB;
1573 else
1574 return PN_RAMMAP_RAMAREA_16KB;
1577 static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
1578 const struct usb_endpoint_descriptor *desc)
1580 int i;
1581 const u32 max_packet_array[] = {8, 16, 32, 64, 512};
1582 u32 mpkt = PN_RAMMAP_MPKT(1024);
1584 for (i = 0; i < ARRAY_SIZE(max_packet_array); i++) {
1585 if (usb_endpoint_maxp(desc) <= max_packet_array[i])
1586 mpkt = PN_RAMMAP_MPKT(max_packet_array[i]);
1589 return usb3_ep->rammap_val | mpkt;
1592 static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
1593 const struct usb_endpoint_descriptor *desc)
1595 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1596 unsigned long flags;
1598 usb3_ep->dir_in = usb_endpoint_dir_in(desc);
1600 spin_lock_irqsave(&usb3->lock, flags);
1601 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1602 usb3_write_pn_mod(usb3_ep, desc);
1603 usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
1604 USB3_PN_RAMMAP);
1605 usb3_pn_con_clear(usb3);
1606 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1608 spin_unlock_irqrestore(&usb3->lock, flags);
1610 return 0;
1613 static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
1615 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1616 unsigned long flags;
1618 usb3_ep->halt = false;
1620 spin_lock_irqsave(&usb3->lock, flags);
1621 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1622 usb3_write(usb3, 0, USB3_PN_RAMMAP);
1623 usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
1625 spin_unlock_irqrestore(&usb3->lock, flags);
1627 return 0;
1630 /*------- usb_ep_ops -----------------------------------------------------*/
1631 static int renesas_usb3_ep_enable(struct usb_ep *_ep,
1632 const struct usb_endpoint_descriptor *desc)
1634 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1636 return usb3_enable_pipe_n(usb3_ep, desc);
1639 static int renesas_usb3_ep_disable(struct usb_ep *_ep)
1641 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1642 struct renesas_usb3_request *usb3_req;
1644 do {
1645 usb3_req = usb3_get_request(usb3_ep);
1646 if (!usb3_req)
1647 break;
1648 usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
1649 } while (1);
1651 return usb3_disable_pipe_n(usb3_ep);
1654 static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
1656 struct renesas_usb3_request *usb3_req;
1658 usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
1659 if (!usb3_req)
1660 return NULL;
1662 INIT_LIST_HEAD(&usb3_req->queue);
1664 return &usb3_req->req;
1667 static void __renesas_usb3_ep_free_request(struct usb_request *_req)
1669 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1671 kfree(usb3_req);
1674 static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
1675 gfp_t gfp_flags)
1677 return __renesas_usb3_ep_alloc_request(gfp_flags);
1680 static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
1681 struct usb_request *_req)
1683 __renesas_usb3_ep_free_request(_req);
1686 static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1688 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1689 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1690 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1692 dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
1693 _req->length);
1695 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
1697 return 0;
1700 static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
1702 return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
1705 static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
1707 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1709 usb3_ep->wedge = true;
1710 return usb3_set_halt(usb3_ep, true, false);
1713 static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
1715 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1716 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1717 unsigned long flags;
1719 if (usb3_ep->num) {
1720 spin_lock_irqsave(&usb3->lock, flags);
1721 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1722 usb3_pn_con_clear(usb3);
1723 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1725 spin_unlock_irqrestore(&usb3->lock, flags);
1726 } else {
1727 usb3_p0_con_clear_buffer(usb3);
1731 static struct usb_ep_ops renesas_usb3_ep_ops = {
1732 .enable = renesas_usb3_ep_enable,
1733 .disable = renesas_usb3_ep_disable,
1735 .alloc_request = renesas_usb3_ep_alloc_request,
1736 .free_request = renesas_usb3_ep_free_request,
1738 .queue = renesas_usb3_ep_queue,
1739 .dequeue = renesas_usb3_ep_dequeue,
1741 .set_halt = renesas_usb3_ep_set_halt,
1742 .set_wedge = renesas_usb3_ep_set_wedge,
1743 .fifo_flush = renesas_usb3_ep_fifo_flush,
1746 /*------- usb_gadget_ops -------------------------------------------------*/
1747 static int renesas_usb3_start(struct usb_gadget *gadget,
1748 struct usb_gadget_driver *driver)
1750 struct renesas_usb3 *usb3;
1752 if (!driver || driver->max_speed < USB_SPEED_FULL ||
1753 !driver->setup)
1754 return -EINVAL;
1756 usb3 = gadget_to_renesas_usb3(gadget);
1758 /* hook up the driver */
1759 usb3->driver = driver;
1761 pm_runtime_enable(usb3_to_dev(usb3));
1762 pm_runtime_get_sync(usb3_to_dev(usb3));
1764 renesas_usb3_init_controller(usb3);
1766 return 0;
1769 static int renesas_usb3_stop(struct usb_gadget *gadget)
1771 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1773 usb3->softconnect = false;
1774 usb3->gadget.speed = USB_SPEED_UNKNOWN;
1775 usb3->driver = NULL;
1776 renesas_usb3_stop_controller(usb3);
1778 pm_runtime_put(usb3_to_dev(usb3));
1779 pm_runtime_disable(usb3_to_dev(usb3));
1781 return 0;
1784 static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
1786 return -EOPNOTSUPP;
1789 static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
1791 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1793 usb3->softconnect = !!is_on;
1795 return 0;
1798 static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
1800 gadget->is_selfpowered = !!is_self;
1802 return 0;
1805 static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
1806 .get_frame = renesas_usb3_get_frame,
1807 .udc_start = renesas_usb3_start,
1808 .udc_stop = renesas_usb3_stop,
1809 .pullup = renesas_usb3_pullup,
1810 .set_selfpowered = renesas_usb3_set_selfpowered,
1813 /*------- platform_driver ------------------------------------------------*/
1814 static int renesas_usb3_remove(struct platform_device *pdev)
1816 struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
1818 usb_del_gadget_udc(&usb3->gadget);
1820 __renesas_usb3_ep_free_request(usb3->ep0_req);
1822 return 0;
1825 static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
1826 const struct renesas_usb3_priv *priv)
1828 struct renesas_usb3_ep *usb3_ep;
1829 int i;
1831 /* calculate num_usb3_eps from renesas_usb3_priv */
1832 usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
1833 priv->ramsize_per_pipe + 1;
1835 if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
1836 usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
1838 usb3->usb3_ep = devm_kzalloc(dev, sizeof(*usb3_ep) * usb3->num_usb3_eps,
1839 GFP_KERNEL);
1840 if (!usb3->usb3_ep)
1841 return -ENOMEM;
1843 dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
1845 * This driver prepares pipes as the followings:
1846 * - odd pipes = IN pipe
1847 * - even pipes = OUT pipe (except pipe 0)
1849 usb3_for_each_ep(usb3_ep, usb3, i) {
1850 snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
1851 usb3_ep->usb3 = usb3;
1852 usb3_ep->num = i;
1853 usb3_ep->ep.name = usb3_ep->ep_name;
1854 usb3_ep->ep.ops = &renesas_usb3_ep_ops;
1855 INIT_LIST_HEAD(&usb3_ep->queue);
1856 INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
1857 if (!i) {
1858 /* for control pipe */
1859 usb3->gadget.ep0 = &usb3_ep->ep;
1860 usb_ep_set_maxpacket_limit(&usb3_ep->ep,
1861 USB3_EP0_HSFS_MAX_PACKET_SIZE);
1862 usb3_ep->ep.caps.type_control = true;
1863 usb3_ep->ep.caps.dir_in = true;
1864 usb3_ep->ep.caps.dir_out = true;
1865 continue;
1868 /* for bulk or interrupt pipe */
1869 usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
1870 list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
1871 usb3_ep->ep.caps.type_bulk = true;
1872 usb3_ep->ep.caps.type_int = true;
1873 if (i & 1)
1874 usb3_ep->ep.caps.dir_in = true;
1875 else
1876 usb3_ep->ep.caps.dir_out = true;
1879 return 0;
1882 static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
1883 const struct renesas_usb3_priv *priv)
1885 struct renesas_usb3_ep *usb3_ep;
1886 int i;
1887 u32 ramif[2], basead[2]; /* index 0 = for IN pipes */
1888 u32 *cur_ramif, *cur_basead;
1889 u32 val;
1891 memset(ramif, 0, sizeof(ramif));
1892 memset(basead, 0, sizeof(basead));
1895 * This driver prepares pipes as the followings:
1896 * - all pipes = the same size as "ramsize_per_pipe"
1897 * Please refer to the "Method of Specifying RAM Mapping"
1899 usb3_for_each_ep(usb3_ep, usb3, i) {
1900 if (!i)
1901 continue; /* out of scope if ep num = 0 */
1902 if (usb3_ep->ep.caps.dir_in) {
1903 cur_ramif = &ramif[0];
1904 cur_basead = &basead[0];
1905 } else {
1906 cur_ramif = &ramif[1];
1907 cur_basead = &basead[1];
1910 if (*cur_basead > priv->ramsize_per_ramif)
1911 continue; /* out of memory for IN or OUT pipe */
1913 /* calculate rammap_val */
1914 val = PN_RAMMAP_RAMIF(*cur_ramif);
1915 val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
1916 val |= PN_RAMMAP_BASEAD(*cur_basead);
1917 usb3_ep->rammap_val = val;
1919 dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
1920 i, val, *cur_ramif, *cur_basead);
1922 /* update current ramif */
1923 if (*cur_ramif + 1 == priv->num_ramif) {
1924 *cur_ramif = 0;
1925 *cur_basead += priv->ramsize_per_pipe;
1926 } else {
1927 (*cur_ramif)++;
1932 static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795 = {
1933 .ramsize_per_ramif = SZ_16K,
1934 .num_ramif = 2,
1935 .ramsize_per_pipe = SZ_4K,
1936 .workaround_for_vbus = true,
1939 static const struct of_device_id usb3_of_match[] = {
1941 .compatible = "renesas,r8a7795-usb3-peri",
1942 .data = &renesas_usb3_priv_r8a7795,
1944 { },
1946 MODULE_DEVICE_TABLE(of, usb3_of_match);
1948 static int renesas_usb3_probe(struct platform_device *pdev)
1950 struct renesas_usb3 *usb3;
1951 struct resource *res;
1952 const struct of_device_id *match;
1953 int irq, ret;
1954 const struct renesas_usb3_priv *priv;
1956 match = of_match_node(usb3_of_match, pdev->dev.of_node);
1957 if (!match)
1958 return -ENODEV;
1959 priv = match->data;
1961 irq = platform_get_irq(pdev, 0);
1962 if (irq < 0)
1963 return -ENODEV;
1965 usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
1966 if (!usb3)
1967 return -ENOMEM;
1969 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1970 usb3->reg = devm_ioremap_resource(&pdev->dev, res);
1971 if (IS_ERR(usb3->reg))
1972 return PTR_ERR(usb3->reg);
1974 platform_set_drvdata(pdev, usb3);
1975 spin_lock_init(&usb3->lock);
1977 usb3->gadget.ops = &renesas_usb3_gadget_ops;
1978 usb3->gadget.name = udc_name;
1979 usb3->gadget.max_speed = USB_SPEED_SUPER;
1980 INIT_LIST_HEAD(&usb3->gadget.ep_list);
1981 ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
1982 if (ret < 0)
1983 return ret;
1984 renesas_usb3_init_ram(usb3, &pdev->dev, priv);
1986 ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
1987 dev_name(&pdev->dev), usb3);
1988 if (ret < 0)
1989 return ret;
1991 /* for ep0 handling */
1992 usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
1993 if (!usb3->ep0_req)
1994 return -ENOMEM;
1996 ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
1997 if (ret < 0)
1998 goto err_add_udc;
2000 usb3->workaround_for_vbus = priv->workaround_for_vbus;
2002 dev_info(&pdev->dev, "probed\n");
2004 return 0;
2006 err_add_udc:
2007 __renesas_usb3_ep_free_request(usb3->ep0_req);
2009 return ret;
2012 static struct platform_driver renesas_usb3_driver = {
2013 .probe = renesas_usb3_probe,
2014 .remove = renesas_usb3_remove,
2015 .driver = {
2016 .name = (char *)udc_name,
2017 .of_match_table = of_match_ptr(usb3_of_match),
2020 module_platform_driver(renesas_usb3_driver);
2022 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
2023 MODULE_LICENSE("GPL v2");
2024 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
2025 MODULE_ALIAS("platform:renesas_usb3");