mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / usb / gadget / udc / renesas_usb3.c
blob93a3bec81df78006f1ba3871aece717fe69e05b3
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)
110 /* USB_INT_ENA_1 and USB_INT_STA_1 */
111 #define USB_INT_1_B3_PLLWKUP BIT(31)
112 #define USB_INT_1_B3_LUPSUCS BIT(30)
113 #define USB_INT_1_B3_DISABLE BIT(27)
114 #define USB_INT_1_B3_WRMRST BIT(21)
115 #define USB_INT_1_B3_HOTRST BIT(20)
116 #define USB_INT_1_B2_USBRST BIT(12)
117 #define USB_INT_1_B2_L1SPND BIT(11)
118 #define USB_INT_1_B2_SPND BIT(9)
119 #define USB_INT_1_B2_RSUM BIT(8)
120 #define USB_INT_1_SPEED BIT(1)
121 #define USB_INT_1_VBUS_CNG BIT(0)
123 /* USB_INT_ENA_2 and USB_INT_STA_2 */
124 #define USB_INT_2_PIPE(n) BIT(n)
126 /* P0_MOD */
127 #define P0_MOD_DIR BIT(6)
129 /* P0_CON and PN_CON */
130 #define PX_CON_BYTE_EN_MASK (BIT(10) | BIT(9))
131 #define PX_CON_BYTE_EN_SHIFT 9
132 #define PX_CON_BYTE_EN_BYTES(n) (((n) << PX_CON_BYTE_EN_SHIFT) & \
133 PX_CON_BYTE_EN_MASK)
134 #define PX_CON_SEND BIT(8)
136 /* P0_CON */
137 #define P0_CON_ST_RES_MASK (BIT(27) | BIT(26))
138 #define P0_CON_ST_RES_FORCE_STALL BIT(27)
139 #define P0_CON_ST_RES_NORMAL BIT(26)
140 #define P0_CON_ST_RES_FORCE_NRDY 0
141 #define P0_CON_OT_RES_MASK (BIT(25) | BIT(24))
142 #define P0_CON_OT_RES_FORCE_STALL BIT(25)
143 #define P0_CON_OT_RES_NORMAL BIT(24)
144 #define P0_CON_OT_RES_FORCE_NRDY 0
145 #define P0_CON_IN_RES_MASK (BIT(17) | BIT(16))
146 #define P0_CON_IN_RES_FORCE_STALL BIT(17)
147 #define P0_CON_IN_RES_NORMAL BIT(16)
148 #define P0_CON_IN_RES_FORCE_NRDY 0
149 #define P0_CON_RES_WEN BIT(7)
150 #define P0_CON_BCLR BIT(1)
152 /* P0_STA and PN_STA */
153 #define PX_STA_BUFSTS BIT(0)
155 /* P0_INT_ENA and P0_INT_STA */
156 #define P0_INT_STSED BIT(18)
157 #define P0_INT_STSST BIT(17)
158 #define P0_INT_SETUP BIT(16)
159 #define P0_INT_RCVNL BIT(8)
160 #define P0_INT_ERDY BIT(7)
161 #define P0_INT_FLOW BIT(6)
162 #define P0_INT_STALL BIT(2)
163 #define P0_INT_NRDY BIT(1)
164 #define P0_INT_BFRDY BIT(0)
165 #define P0_INT_ALL_BITS (P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
167 /* PN_MOD */
168 #define PN_MOD_DIR BIT(6)
169 #define PN_MOD_TYPE_SHIFT 4
170 #define PN_MOD_TYPE_MASK GENMASK(5, PN_MOD_TYPE_SHIFT)
171 #define PN_MOD_TYPE(n) (((n) << PN_MOD_TYPE_SHIFT) & \
172 PN_MOD_TYPE_MASK)
173 #define PN_MOD_EPNUM_MASK GENMASK(3, 0)
174 #define PN_MOD_EPNUM(n) ((n) & PN_MOD_EPNUM_MASK)
176 /* PN_RAMMAP */
177 #define PN_RAMMAP_RAMAREA_SHIFT 29
178 #define PN_RAMMAP_RAMAREA_MASK GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
179 #define PN_RAMMAP_RAMAREA_16KB BIT(31)
180 #define PN_RAMMAP_RAMAREA_8KB (BIT(30) | BIT(29))
181 #define PN_RAMMAP_RAMAREA_4KB BIT(30)
182 #define PN_RAMMAP_RAMAREA_2KB BIT(29)
183 #define PN_RAMMAP_RAMAREA_1KB 0
184 #define PN_RAMMAP_MPKT_SHIFT 16
185 #define PN_RAMMAP_MPKT_MASK GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
186 #define PN_RAMMAP_MPKT(n) (((n) << PN_RAMMAP_MPKT_SHIFT) & \
187 PN_RAMMAP_MPKT_MASK)
188 #define PN_RAMMAP_RAMIF_SHIFT 14
189 #define PN_RAMMAP_RAMIF_MASK GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
190 #define PN_RAMMAP_RAMIF(n) (((n) << PN_RAMMAP_RAMIF_SHIFT) & \
191 PN_RAMMAP_RAMIF_MASK)
192 #define PN_RAMMAP_BASEAD_MASK GENMASK(13, 0)
193 #define PN_RAMMAP_BASEAD(offs) (((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
194 #define PN_RAMMAP_DATA(area, ramif, basead) ((PN_RAMMAP_##area) | \
195 (PN_RAMMAP_RAMIF(ramif)) | \
196 (PN_RAMMAP_BASEAD(basead)))
198 /* PN_CON */
199 #define PN_CON_EN BIT(31)
200 #define PN_CON_DATAIF_EN BIT(30)
201 #define PN_CON_RES_MASK (BIT(17) | BIT(16))
202 #define PN_CON_RES_FORCE_STALL BIT(17)
203 #define PN_CON_RES_NORMAL BIT(16)
204 #define PN_CON_RES_FORCE_NRDY 0
205 #define PN_CON_LAST BIT(11)
206 #define PN_CON_RES_WEN BIT(7)
207 #define PN_CON_CLR BIT(0)
209 /* PN_INT_STA and PN_INT_ENA */
210 #define PN_INT_LSTTR BIT(4)
211 #define PN_INT_BFRDY BIT(0)
213 /* USB3_SSIFCMD */
214 #define SSIFCMD_URES_U2 BIT(9)
215 #define SSIFCMD_URES_U1 BIT(8)
216 #define SSIFCMD_UDIR_U2 BIT(7)
217 #define SSIFCMD_UDIR_U1 BIT(6)
218 #define SSIFCMD_UREQ_U2 BIT(5)
219 #define SSIFCMD_UREQ_U1 BIT(4)
221 #define USB3_EP0_SS_MAX_PACKET_SIZE 512
222 #define USB3_EP0_HSFS_MAX_PACKET_SIZE 64
223 #define USB3_EP0_BUF_SIZE 8
224 #define USB3_MAX_NUM_PIPES 30
225 #define USB3_WAIT_US 3
227 struct renesas_usb3;
228 struct renesas_usb3_request {
229 struct usb_request req;
230 struct list_head queue;
233 #define USB3_EP_NAME_SIZE 8
234 struct renesas_usb3_ep {
235 struct usb_ep ep;
236 struct renesas_usb3 *usb3;
237 int num;
238 char ep_name[USB3_EP_NAME_SIZE];
239 struct list_head queue;
240 u32 rammap_val;
241 bool dir_in;
242 bool halt;
243 bool wedge;
244 bool started;
247 struct renesas_usb3_priv {
248 int ramsize_per_ramif; /* unit = bytes */
249 int num_ramif;
250 int ramsize_per_pipe; /* unit = bytes */
251 bool workaround_for_vbus; /* if true, don't check vbus signal */
254 struct renesas_usb3 {
255 void __iomem *reg;
257 struct usb_gadget gadget;
258 struct usb_gadget_driver *driver;
260 struct renesas_usb3_ep *usb3_ep;
261 int num_usb3_eps;
263 spinlock_t lock;
264 int disabled_count;
266 struct usb_request *ep0_req;
267 u16 test_mode;
268 u8 ep0_buf[USB3_EP0_BUF_SIZE];
269 bool softconnect;
270 bool workaround_for_vbus;
273 #define gadget_to_renesas_usb3(_gadget) \
274 container_of(_gadget, struct renesas_usb3, gadget)
275 #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
276 #define usb3_to_dev(_usb3) (_usb3->gadget.dev.parent)
278 #define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
279 #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
280 #define usb_req_to_usb3_req(_req) container_of(_req, \
281 struct renesas_usb3_request, req)
283 #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
284 #define usb3_for_each_ep(usb3_ep, usb3, i) \
285 for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
286 (i) < (usb3)->num_usb3_eps; \
287 (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
289 static const char udc_name[] = "renesas_usb3";
291 static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
293 iowrite32(data, usb3->reg + offs);
296 static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
298 return ioread32(usb3->reg + offs);
301 static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
303 u32 val = usb3_read(usb3, offs);
305 val |= bits;
306 usb3_write(usb3, val, offs);
309 static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
311 u32 val = usb3_read(usb3, offs);
313 val &= ~bits;
314 usb3_write(usb3, val, offs);
317 static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
318 u32 expected)
320 int i;
322 for (i = 0; i < USB3_WAIT_US; i++) {
323 if ((usb3_read(usb3, reg) & mask) == expected)
324 return 0;
325 udelay(1);
328 dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
329 __func__, reg, mask, expected);
331 return -EBUSY;
334 static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
336 usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
339 static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
341 usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
344 static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
346 usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
349 static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
351 usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
354 static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
356 /* Set AXI_INT */
357 usb3_write(usb3, ~0, USB3_DMA_INT_STA);
358 usb3_write(usb3, 0, USB3_DMA_INT_ENA);
359 usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
362 static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
364 /* FIXME: How to change host / peripheral mode as well? */
365 usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
367 usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
368 usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
371 static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
373 if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
374 return true; /* already waked it up */
376 usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
377 usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
379 return false;
382 static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
384 u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
386 if (usb3->softconnect && pullup)
387 usb3_set_bit(usb3, bits, USB3_USB20_CON);
388 else
389 usb3_clear_bit(usb3, bits, USB3_USB20_CON);
392 static void usb3_set_test_mode(struct renesas_usb3 *usb3)
394 u32 val = usb3_read(usb3, USB3_USB20_CON);
396 val &= ~USB20_CON_B2_TSTMOD_MASK;
397 val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
398 usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
399 if (!usb3->test_mode)
400 usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
403 static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
405 usb3->disabled_count++;
406 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
407 usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
408 usb3_usb2_pullup(usb3, 1);
411 static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
413 return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
416 static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
418 if (!usb3_is_usb3_phy_in_u3(usb3))
419 return true; /* already waked it up */
421 usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
422 usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
424 return false;
427 static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
429 u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
430 u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
431 u32 val = usb3_read(usb3, USB3_SSIFCMD);
432 u16 ret = 0;
434 /* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
435 if (!(val & mask_u2))
436 ret |= 1 << USB_DEV_STAT_U2_ENABLED;
437 if (!(val & mask_u1))
438 ret |= 1 << USB_DEV_STAT_U1_ENABLED;
440 return ret;
443 static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
445 u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
447 /* Enables U2 if the bits of UDIR and UREQ are set to 0 */
448 if (enable)
449 usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
450 else
451 usb3_set_bit(usb3, bits, USB3_SSIFCMD);
454 static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
456 u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
458 /* Enables U1 if the bits of UDIR and UREQ are set to 0 */
459 if (enable)
460 usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
461 else
462 usb3_set_bit(usb3, bits, USB3_SSIFCMD);
465 static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
467 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
468 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
469 usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
472 static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
474 usb3_start_operation_for_usb3(usb3);
475 usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
477 usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
478 USB_INT_1_SPEED);
481 static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
483 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
486 static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
487 bool is_usb3)
489 usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
490 usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
491 usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
493 if (is_usb3)
494 usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
495 USB_INT_1_B3_HOTRST);
496 else
497 usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
498 USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
501 static void usb3_connect(struct renesas_usb3 *usb3)
503 if (usb3_wakeup_usb3_phy(usb3))
504 usb3_start_usb3_connection(usb3);
507 static void usb3_reset_epc(struct renesas_usb3 *usb3)
509 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
510 usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
511 usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
512 usb3->test_mode = 0;
513 usb3_set_test_mode(usb3);
516 static void usb3_disconnect(struct renesas_usb3 *usb3)
518 usb3->disabled_count = 0;
519 usb3_usb2_pullup(usb3, 0);
520 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
521 usb3_reset_epc(usb3);
523 if (usb3->driver)
524 usb3->driver->disconnect(&usb3->gadget);
527 static void usb3_check_vbus(struct renesas_usb3 *usb3)
529 if (usb3->workaround_for_vbus) {
530 usb3_connect(usb3);
531 } else {
532 if (usb3_read(usb3, USB3_USB_STA) & USB_STA_VBUS_STA)
533 usb3_connect(usb3);
534 else
535 usb3_disconnect(usb3);
539 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
541 usb3_init_axi_bridge(usb3);
542 usb3_init_epc_registers(usb3);
544 usb3_check_vbus(usb3);
547 static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
549 usb3_disconnect(usb3);
550 usb3_write(usb3, 0, USB3_P0_INT_ENA);
551 usb3_write(usb3, 0, USB3_PN_INT_ENA);
552 usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
553 usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
554 usb3_write(usb3, 0, USB3_AXI_INT_ENA);
557 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
559 usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
560 usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
561 usb3_start_usb3_connection(usb3);
564 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
566 usb3_transition_to_default_state(usb3, true);
569 static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
571 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
572 usb3_start_usb2_connection(usb3);
573 usb3_transition_to_default_state(usb3, false);
576 static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
578 usb3_stop_usb3_connection(usb3);
579 if (usb3_wakeup_usb2_phy(usb3))
580 usb3_irq_epc_int_1_resume(usb3);
583 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
585 usb3_reset_epc(usb3);
586 if (usb3->disabled_count < 3)
587 usb3_start_usb3_connection(usb3);
588 else
589 usb3_start_usb2_connection(usb3);
592 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
594 usb3_check_vbus(usb3);
597 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
599 usb3_reset_epc(usb3);
600 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
602 /* This bit shall be set within 12ms from the start of HotReset */
603 usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
606 static void usb3_irq_epc_int_1_warm_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 usb3_start_operation_for_usb3(usb3);
612 usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
615 static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
617 u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
619 switch (speed) {
620 case USB_STA_SPEED_SS:
621 usb3->gadget.speed = USB_SPEED_SUPER;
622 break;
623 case USB_STA_SPEED_HS:
624 usb3->gadget.speed = USB_SPEED_HIGH;
625 break;
626 case USB_STA_SPEED_FS:
627 usb3->gadget.speed = USB_SPEED_FULL;
628 break;
629 default:
630 usb3->gadget.speed = USB_SPEED_UNKNOWN;
631 break;
635 static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
637 if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
638 usb3_irq_epc_int_1_pll_wakeup(usb3);
640 if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
641 usb3_irq_epc_int_1_linkup_success(usb3);
643 if (int_sta_1 & USB_INT_1_B3_HOTRST)
644 usb3_irq_epc_int_1_hot_reset(usb3);
646 if (int_sta_1 & USB_INT_1_B3_WRMRST)
647 usb3_irq_epc_int_1_warm_reset(usb3);
649 if (int_sta_1 & USB_INT_1_B3_DISABLE)
650 usb3_irq_epc_int_1_disable(usb3);
652 if (int_sta_1 & USB_INT_1_B2_USBRST)
653 usb3_irq_epc_int_1_bus_reset(usb3);
655 if (int_sta_1 & USB_INT_1_B2_RSUM)
656 usb3_irq_epc_int_1_resume(usb3);
658 if (int_sta_1 & USB_INT_1_SPEED)
659 usb3_irq_epc_int_1_speed(usb3);
661 if (int_sta_1 & USB_INT_1_VBUS_CNG)
662 usb3_irq_epc_int_1_vbus_change(usb3);
665 static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
666 *usb3_ep)
668 return list_first_entry_or_null(&usb3_ep->queue,
669 struct renesas_usb3_request, queue);
672 static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
673 *usb3_ep)
675 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
676 struct renesas_usb3_request *usb3_req;
677 unsigned long flags;
679 spin_lock_irqsave(&usb3->lock, flags);
680 usb3_req = __usb3_get_request(usb3_ep);
681 spin_unlock_irqrestore(&usb3->lock, flags);
683 return usb3_req;
686 static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
687 struct renesas_usb3_request *usb3_req, int status)
689 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
690 unsigned long flags;
692 dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
693 usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
694 status);
695 usb3_req->req.status = status;
696 spin_lock_irqsave(&usb3->lock, flags);
697 usb3_ep->started = false;
698 list_del_init(&usb3_req->queue);
699 spin_unlock_irqrestore(&usb3->lock, flags);
700 usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
703 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
705 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
706 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
708 if (usb3_req)
709 usb3_request_done(usb3_ep, usb3_req, 0);
710 if (usb3->test_mode)
711 usb3_set_test_mode(usb3);
714 static void usb3_get_setup_data(struct renesas_usb3 *usb3,
715 struct usb_ctrlrequest *ctrl)
717 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
718 u32 *data = (u32 *)ctrl;
720 *data++ = usb3_read(usb3, USB3_STUP_DAT_0);
721 *data = usb3_read(usb3, USB3_STUP_DAT_1);
723 /* update this driver's flag */
724 usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
727 static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
729 u32 val = usb3_read(usb3, USB3_P0_CON);
731 val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
732 val |= res | P0_CON_RES_WEN;
733 usb3_write(usb3, val, USB3_P0_CON);
736 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
738 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
739 P0_CON_OT_RES_FORCE_STALL |
740 P0_CON_IN_RES_NORMAL);
743 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
745 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
746 P0_CON_OT_RES_FORCE_STALL |
747 P0_CON_IN_RES_NORMAL);
750 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
752 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
753 P0_CON_OT_RES_NORMAL |
754 P0_CON_IN_RES_FORCE_STALL);
757 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
759 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
760 P0_CON_OT_RES_NORMAL |
761 P0_CON_IN_RES_FORCE_STALL);
764 static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
766 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
767 P0_CON_OT_RES_FORCE_STALL |
768 P0_CON_IN_RES_FORCE_STALL);
771 static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
773 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
774 P0_CON_OT_RES_FORCE_STALL |
775 P0_CON_IN_RES_FORCE_STALL);
778 static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
780 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
781 P0_CON_OT_RES_FORCE_NRDY |
782 P0_CON_IN_RES_FORCE_NRDY);
785 static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
787 if (num == 0 || num > usb3->num_usb3_eps)
788 return -ENXIO;
790 usb3_write(usb3, num, USB3_PIPE_COM);
792 return 0;
795 static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
797 u32 val = usb3_read(usb3, USB3_PN_CON);
799 val &= ~PN_CON_RES_MASK;
800 val |= res & PN_CON_RES_MASK;
801 val |= PN_CON_RES_WEN;
802 usb3_write(usb3, val, USB3_PN_CON);
805 static void usb3_pn_start(struct renesas_usb3 *usb3)
807 usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
810 static void usb3_pn_stop(struct renesas_usb3 *usb3)
812 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
815 static void usb3_pn_stall(struct renesas_usb3 *usb3)
817 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
820 static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
822 usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
824 return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
827 static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
828 struct renesas_usb3_request *usb3_req)
830 struct usb_request *req = &usb3_req->req;
832 if ((!req->zero && req->actual == req->length) ||
833 (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
834 return true;
835 else
836 return false;
839 static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
841 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
842 u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
844 return usb3_wait(usb3, sta_reg, mask, mask);
847 static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
848 bool last)
850 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
851 u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
852 u32 val = usb3_read(usb3, con_reg);
854 val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
855 val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
856 usb3_write(usb3, val, con_reg);
859 static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
860 struct renesas_usb3_request *usb3_req,
861 u32 fifo_reg)
863 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
864 int i;
865 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
866 usb3_ep->ep.maxpacket);
867 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
868 u32 tmp = 0;
869 bool is_last;
871 if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
872 return -EBUSY;
874 /* Update gadget driver parameter */
875 usb3_req->req.actual += len;
877 /* Write data to the register */
878 if (len >= 4) {
879 iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
880 buf += (len / 4) * 4;
881 len %= 4; /* update len to use usb3_set_pX_con_send() */
884 if (len) {
885 for (i = 0; i < len; i++)
886 tmp |= buf[i] << (8 * i);
887 usb3_write(usb3, tmp, fifo_reg);
890 is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
891 /* Send the data */
892 usb3_set_px_con_send(usb3_ep, len, is_last);
894 return is_last ? 0 : -EAGAIN;
897 static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
899 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
900 u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
902 return usb3_read(usb3, lng_reg);
905 static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
906 struct renesas_usb3_request *usb3_req, u32 fifo_reg)
908 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
909 int i;
910 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
911 usb3_get_received_length(usb3_ep));
912 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
913 u32 tmp = 0;
915 if (!len)
916 return 0;
918 /* Update gadget driver parameter */
919 usb3_req->req.actual += len;
921 /* Read data from the register */
922 if (len >= 4) {
923 ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
924 buf += (len / 4) * 4;
925 len %= 4;
928 if (len) {
929 tmp = usb3_read(usb3, fifo_reg);
930 for (i = 0; i < len; i++)
931 buf[i] = (tmp >> (8 * i)) & 0xff;
934 return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
937 static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
938 struct renesas_usb3_request *usb3_req)
940 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
942 if (usb3_ep->dir_in) {
943 usb3_set_p0_con_for_ctrl_read_status(usb3);
944 } else {
945 if (!usb3_req->req.length)
946 usb3_set_p0_con_for_no_data(usb3);
947 else
948 usb3_set_p0_con_for_ctrl_write_status(usb3);
952 static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
953 struct renesas_usb3_request *usb3_req)
955 int ret = -EAGAIN;
957 if (usb3_ep->dir_in)
958 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
959 else
960 ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
962 if (!ret)
963 usb3_set_status_stage(usb3_ep, usb3_req);
966 static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
967 struct renesas_usb3_request *usb3_req)
969 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
971 if (usb3_ep->started)
972 return;
974 usb3_ep->started = true;
976 if (usb3_ep->dir_in) {
977 usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
978 usb3_set_p0_con_for_ctrl_read_data(usb3);
979 } else {
980 usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
981 usb3_set_p0_con_for_ctrl_write_data(usb3);
984 usb3_p0_xfer(usb3_ep, usb3_req);
987 static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
988 struct renesas_usb3_request *usb3_req)
990 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
991 struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
992 unsigned long flags;
993 int ret = -EAGAIN;
994 u32 enable_bits = 0;
996 if (usb3_ep->halt || usb3_ep->started)
997 return;
998 if (usb3_req != usb3_req_first)
999 return;
1001 spin_lock_irqsave(&usb3->lock, flags);
1002 if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1003 goto out;
1005 usb3_ep->started = true;
1006 usb3_pn_start(usb3);
1008 if (usb3_ep->dir_in) {
1009 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1010 enable_bits |= PN_INT_LSTTR;
1013 if (ret < 0)
1014 enable_bits |= PN_INT_BFRDY;
1016 if (enable_bits) {
1017 usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1018 usb3_enable_pipe_irq(usb3, usb3_ep->num);
1020 out:
1021 spin_unlock_irqrestore(&usb3->lock, flags);
1024 static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1025 gfp_t gfp_flags)
1027 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1028 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1029 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1030 unsigned long flags;
1032 dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1033 _req->length);
1035 _req->status = -EINPROGRESS;
1036 _req->actual = 0;
1037 spin_lock_irqsave(&usb3->lock, flags);
1038 list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1039 spin_unlock_irqrestore(&usb3->lock, flags);
1041 if (!usb3_ep->num)
1042 usb3_start_pipe0(usb3_ep, usb3_req);
1043 else
1044 usb3_start_pipen(usb3_ep, usb3_req);
1046 return 0;
1049 static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1051 /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1052 usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1055 static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1056 struct usb_ctrlrequest *ctrl)
1058 if (ctrl->wValue >= 128)
1059 return true; /* stall */
1061 usb3_set_device_address(usb3, ctrl->wValue);
1062 usb3_set_p0_con_for_no_data(usb3);
1064 return false;
1067 static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1068 void *tx_data, size_t len,
1069 void (*complete)(struct usb_ep *ep,
1070 struct usb_request *req))
1072 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1074 if (tx_data)
1075 memcpy(usb3->ep0_buf, tx_data,
1076 min_t(size_t, len, USB3_EP0_BUF_SIZE));
1078 usb3->ep0_req->buf = &usb3->ep0_buf;
1079 usb3->ep0_req->length = len;
1080 usb3->ep0_req->complete = complete;
1081 renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1084 static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1085 struct usb_request *req)
1089 static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1090 struct usb_ctrlrequest *ctrl)
1092 bool stall = false;
1093 struct renesas_usb3_ep *usb3_ep;
1094 int num;
1095 u16 status = 0;
1097 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1098 case USB_RECIP_DEVICE:
1099 if (usb3->gadget.is_selfpowered)
1100 status |= 1 << USB_DEVICE_SELF_POWERED;
1101 if (usb3->gadget.speed == USB_SPEED_SUPER)
1102 status |= usb3_feature_get_un_enabled(usb3);
1103 break;
1104 case USB_RECIP_INTERFACE:
1105 break;
1106 case USB_RECIP_ENDPOINT:
1107 num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1108 usb3_ep = usb3_get_ep(usb3, num);
1109 if (usb3_ep->halt)
1110 status |= 1 << USB_ENDPOINT_HALT;
1111 break;
1112 default:
1113 stall = true;
1114 break;
1117 if (!stall) {
1118 status = cpu_to_le16(status);
1119 dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1120 usb_req_to_usb3_req(usb3->ep0_req));
1121 usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
1122 usb3_pipe0_get_status_completion);
1125 return stall;
1128 static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1129 struct usb_ctrlrequest *ctrl, bool set)
1131 bool stall = true;
1132 u16 w_value = le16_to_cpu(ctrl->wValue);
1134 switch (w_value) {
1135 case USB_DEVICE_TEST_MODE:
1136 if (!set)
1137 break;
1138 usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1139 stall = false;
1140 break;
1141 case USB_DEVICE_U1_ENABLE:
1142 case USB_DEVICE_U2_ENABLE:
1143 if (usb3->gadget.speed != USB_SPEED_SUPER)
1144 break;
1145 if (w_value == USB_DEVICE_U1_ENABLE)
1146 usb3_feature_u1_enable(usb3, set);
1147 if (w_value == USB_DEVICE_U2_ENABLE)
1148 usb3_feature_u2_enable(usb3, set);
1149 stall = false;
1150 break;
1151 default:
1152 break;
1155 return stall;
1158 static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1160 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1162 if (unlikely(usb3_ep->num))
1163 return -EINVAL;
1165 usb3_ep->halt = halt;
1166 if (halt)
1167 usb3_set_p0_con_stall(usb3);
1168 else
1169 usb3_set_p0_con_stop(usb3);
1171 return 0;
1174 static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1175 bool is_clear_feature)
1177 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1178 unsigned long flags;
1180 spin_lock_irqsave(&usb3->lock, flags);
1181 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1182 usb3_ep->halt = halt;
1183 if (halt) {
1184 usb3_pn_stall(usb3);
1185 } else if (!is_clear_feature || !usb3_ep->wedge) {
1186 usb3_pn_con_clear(usb3);
1187 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1188 usb3_pn_stop(usb3);
1191 spin_unlock_irqrestore(&usb3->lock, flags);
1193 return 0;
1196 static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1197 bool is_clear_feature)
1199 int ret = 0;
1201 if (halt && usb3_ep->started)
1202 return -EAGAIN;
1204 if (usb3_ep->num)
1205 ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1206 else
1207 ret = usb3_set_halt_p0(usb3_ep, halt);
1209 return ret;
1212 static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1213 struct usb_ctrlrequest *ctrl,
1214 bool set)
1216 int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1217 struct renesas_usb3_ep *usb3_ep;
1218 struct renesas_usb3_request *usb3_req;
1220 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1221 return true; /* stall */
1223 usb3_ep = usb3_get_ep(usb3, num);
1224 usb3_set_halt(usb3_ep, set, true);
1226 /* Restarts a queue if clear feature */
1227 if (!set) {
1228 usb3_ep->started = false;
1229 usb3_req = usb3_get_request(usb3_ep);
1230 if (usb3_req)
1231 usb3_start_pipen(usb3_ep, usb3_req);
1234 return false;
1237 static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1238 struct usb_ctrlrequest *ctrl, bool set)
1240 bool stall = false;
1242 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1243 case USB_RECIP_DEVICE:
1244 stall = usb3_std_req_feature_device(usb3, ctrl, set);
1245 break;
1246 case USB_RECIP_INTERFACE:
1247 break;
1248 case USB_RECIP_ENDPOINT:
1249 stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1250 break;
1251 default:
1252 stall = true;
1253 break;
1256 if (!stall)
1257 usb3_set_p0_con_for_no_data(usb3);
1259 return stall;
1262 static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1263 struct usb_request *req)
1265 /* TODO */
1268 static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1269 struct usb_ctrlrequest *ctrl)
1271 u16 w_length = le16_to_cpu(ctrl->wLength);
1273 if (w_length != 6)
1274 return true; /* stall */
1276 dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1277 usb_req_to_usb3_req(usb3->ep0_req));
1278 usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1280 return false;
1283 static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1284 struct usb_ctrlrequest *ctrl)
1286 if (ctrl->wValue > 0)
1287 usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1288 else
1289 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1291 return false;
1295 * usb3_handle_standard_request - handle some standard requests
1296 * @usb3: the renesas_usb3 pointer
1297 * @ctrl: a pointer of setup data
1299 * Returns true if this function handled a standard request
1301 static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1302 struct usb_ctrlrequest *ctrl)
1304 bool ret = false;
1305 bool stall = false;
1307 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1308 switch (ctrl->bRequest) {
1309 case USB_REQ_SET_ADDRESS:
1310 stall = usb3_std_req_set_address(usb3, ctrl);
1311 ret = true;
1312 break;
1313 case USB_REQ_GET_STATUS:
1314 stall = usb3_std_req_get_status(usb3, ctrl);
1315 ret = true;
1316 break;
1317 case USB_REQ_CLEAR_FEATURE:
1318 stall = usb3_std_req_feature(usb3, ctrl, false);
1319 ret = true;
1320 break;
1321 case USB_REQ_SET_FEATURE:
1322 stall = usb3_std_req_feature(usb3, ctrl, true);
1323 ret = true;
1324 break;
1325 case USB_REQ_SET_SEL:
1326 stall = usb3_std_req_set_sel(usb3, ctrl);
1327 ret = true;
1328 break;
1329 case USB_REQ_SET_ISOCH_DELAY:
1330 /* This hardware doesn't support Isochronous xfer */
1331 stall = true;
1332 ret = true;
1333 break;
1334 case USB_REQ_SET_CONFIGURATION:
1335 usb3_std_req_set_configuration(usb3, ctrl);
1336 break;
1337 default:
1338 break;
1342 if (stall)
1343 usb3_set_p0_con_stall(usb3);
1345 return ret;
1348 static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1350 usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1352 return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1355 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1357 struct usb_ctrlrequest ctrl;
1358 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1360 /* Call giveback function if previous transfer is not completed */
1361 if (usb3_ep->started)
1362 usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1363 -ECONNRESET);
1365 usb3_p0_con_clear_buffer(usb3);
1366 usb3_get_setup_data(usb3, &ctrl);
1367 if (!usb3_handle_standard_request(usb3, &ctrl))
1368 if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1369 usb3_set_p0_con_stall(usb3);
1372 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1374 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1375 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1377 if (!usb3_req)
1378 return;
1380 usb3_p0_xfer(usb3_ep, usb3_req);
1383 static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1385 u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1387 p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1388 usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1389 if (p0_int_sta & P0_INT_STSED)
1390 usb3_irq_epc_pipe0_status_end(usb3);
1391 if (p0_int_sta & P0_INT_SETUP)
1392 usb3_irq_epc_pipe0_setup(usb3);
1393 if (p0_int_sta & P0_INT_BFRDY)
1394 usb3_irq_epc_pipe0_bfrdy(usb3);
1397 static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1398 struct renesas_usb3_ep *usb3_ep,
1399 struct renesas_usb3_request *usb3_req,
1400 int status)
1402 usb3_pn_stop(usb3);
1403 usb3_disable_pipe_irq(usb3, usb3_ep->num);
1404 usb3_request_done(usb3_ep, usb3_req, status);
1406 /* get next usb3_req */
1407 usb3_req = usb3_get_request(usb3_ep);
1408 if (usb3_req)
1409 usb3_start_pipen(usb3_ep, usb3_req);
1412 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1414 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1415 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1417 if (!usb3_req)
1418 return;
1420 if (usb3_ep->dir_in) {
1421 dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1422 __func__, usb3_req->req.length, usb3_req->req.actual);
1423 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1427 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1429 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1430 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1432 if (!usb3_req)
1433 return;
1435 if (usb3_ep->dir_in) {
1436 /* Do not stop the IN pipe here to detect LSTTR interrupt */
1437 if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1438 usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1439 } else {
1440 if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1441 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1445 static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1447 u32 pn_int_sta;
1449 if (usb3_pn_change(usb3, num) < 0)
1450 return;
1452 pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1453 pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1454 usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1455 if (pn_int_sta & PN_INT_LSTTR)
1456 usb3_irq_epc_pipen_lsttr(usb3, num);
1457 if (pn_int_sta & PN_INT_BFRDY)
1458 usb3_irq_epc_pipen_bfrdy(usb3, num);
1461 static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1463 int i;
1465 for (i = 0; i < usb3->num_usb3_eps; i++) {
1466 if (int_sta_2 & USB_INT_2_PIPE(i)) {
1467 if (!i)
1468 usb3_irq_epc_pipe0(usb3);
1469 else
1470 usb3_irq_epc_pipen(usb3, i);
1475 static void usb3_irq_epc(struct renesas_usb3 *usb3)
1477 u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
1478 u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
1480 int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
1481 if (int_sta_1) {
1482 usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
1483 usb3_irq_epc_int_1(usb3, int_sta_1);
1486 int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
1487 if (int_sta_2)
1488 usb3_irq_epc_int_2(usb3, int_sta_2);
1491 static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
1493 struct renesas_usb3 *usb3 = _usb3;
1494 irqreturn_t ret = IRQ_NONE;
1495 u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
1497 if (axi_int_sta & AXI_INT_EPCINT) {
1498 usb3_irq_epc(usb3);
1499 ret = IRQ_HANDLED;
1502 return ret;
1505 static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
1506 const struct usb_endpoint_descriptor *desc)
1508 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1509 u32 val = 0;
1511 val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
1512 val |= PN_MOD_TYPE(usb_endpoint_type(desc));
1513 val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
1514 usb3_write(usb3, val, USB3_PN_MOD);
1517 static u32 usb3_calc_ramarea(int ram_size)
1519 WARN_ON(ram_size > SZ_16K);
1521 if (ram_size <= SZ_1K)
1522 return PN_RAMMAP_RAMAREA_1KB;
1523 else if (ram_size <= SZ_2K)
1524 return PN_RAMMAP_RAMAREA_2KB;
1525 else if (ram_size <= SZ_4K)
1526 return PN_RAMMAP_RAMAREA_4KB;
1527 else if (ram_size <= SZ_8K)
1528 return PN_RAMMAP_RAMAREA_8KB;
1529 else
1530 return PN_RAMMAP_RAMAREA_16KB;
1533 static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
1534 const struct usb_endpoint_descriptor *desc)
1536 return usb3_ep->rammap_val | PN_RAMMAP_MPKT(usb_endpoint_maxp(desc));
1539 static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
1540 const struct usb_endpoint_descriptor *desc)
1542 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1543 unsigned long flags;
1545 usb3_ep->dir_in = usb_endpoint_dir_in(desc);
1547 spin_lock_irqsave(&usb3->lock, flags);
1548 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1549 usb3_write_pn_mod(usb3_ep, desc);
1550 usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
1551 USB3_PN_RAMMAP);
1552 usb3_pn_con_clear(usb3);
1553 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1555 spin_unlock_irqrestore(&usb3->lock, flags);
1557 return 0;
1560 static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
1562 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1563 unsigned long flags;
1565 usb3_ep->halt = false;
1567 spin_lock_irqsave(&usb3->lock, flags);
1568 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1569 usb3_write(usb3, 0, USB3_PN_RAMMAP);
1570 usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
1572 spin_unlock_irqrestore(&usb3->lock, flags);
1574 return 0;
1577 /*------- usb_ep_ops -----------------------------------------------------*/
1578 static int renesas_usb3_ep_enable(struct usb_ep *_ep,
1579 const struct usb_endpoint_descriptor *desc)
1581 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1583 return usb3_enable_pipe_n(usb3_ep, desc);
1586 static int renesas_usb3_ep_disable(struct usb_ep *_ep)
1588 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1589 struct renesas_usb3_request *usb3_req;
1591 do {
1592 usb3_req = usb3_get_request(usb3_ep);
1593 if (!usb3_req)
1594 break;
1595 usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
1596 } while (1);
1598 return usb3_disable_pipe_n(usb3_ep);
1601 static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
1603 struct renesas_usb3_request *usb3_req;
1605 usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
1606 if (!usb3_req)
1607 return NULL;
1609 INIT_LIST_HEAD(&usb3_req->queue);
1611 return &usb3_req->req;
1614 static void __renesas_usb3_ep_free_request(struct usb_request *_req)
1616 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1618 kfree(usb3_req);
1621 static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
1622 gfp_t gfp_flags)
1624 return __renesas_usb3_ep_alloc_request(gfp_flags);
1627 static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
1628 struct usb_request *_req)
1630 __renesas_usb3_ep_free_request(_req);
1633 static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1635 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1636 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1637 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1639 dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
1640 _req->length);
1642 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
1644 return 0;
1647 static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
1649 return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
1652 static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
1654 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1656 usb3_ep->wedge = true;
1657 return usb3_set_halt(usb3_ep, true, false);
1660 static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
1662 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1663 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1664 unsigned long flags;
1666 if (usb3_ep->num) {
1667 spin_lock_irqsave(&usb3->lock, flags);
1668 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1669 usb3_pn_con_clear(usb3);
1670 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1672 spin_unlock_irqrestore(&usb3->lock, flags);
1673 } else {
1674 usb3_p0_con_clear_buffer(usb3);
1678 static struct usb_ep_ops renesas_usb3_ep_ops = {
1679 .enable = renesas_usb3_ep_enable,
1680 .disable = renesas_usb3_ep_disable,
1682 .alloc_request = renesas_usb3_ep_alloc_request,
1683 .free_request = renesas_usb3_ep_free_request,
1685 .queue = renesas_usb3_ep_queue,
1686 .dequeue = renesas_usb3_ep_dequeue,
1688 .set_halt = renesas_usb3_ep_set_halt,
1689 .set_wedge = renesas_usb3_ep_set_wedge,
1690 .fifo_flush = renesas_usb3_ep_fifo_flush,
1693 /*------- usb_gadget_ops -------------------------------------------------*/
1694 static int renesas_usb3_start(struct usb_gadget *gadget,
1695 struct usb_gadget_driver *driver)
1697 struct renesas_usb3 *usb3;
1699 if (!driver || driver->max_speed < USB_SPEED_FULL ||
1700 !driver->setup)
1701 return -EINVAL;
1703 usb3 = gadget_to_renesas_usb3(gadget);
1705 /* hook up the driver */
1706 usb3->driver = driver;
1708 renesas_usb3_init_controller(usb3);
1710 return 0;
1713 static int renesas_usb3_stop(struct usb_gadget *gadget)
1715 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1716 unsigned long flags;
1718 spin_lock_irqsave(&usb3->lock, flags);
1719 usb3->softconnect = false;
1720 usb3->gadget.speed = USB_SPEED_UNKNOWN;
1721 usb3->driver = NULL;
1722 renesas_usb3_stop_controller(usb3);
1723 spin_unlock_irqrestore(&usb3->lock, flags);
1725 return 0;
1728 static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
1730 return -EOPNOTSUPP;
1733 static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
1735 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1737 usb3->softconnect = !!is_on;
1739 return 0;
1742 static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
1744 gadget->is_selfpowered = !!is_self;
1746 return 0;
1749 static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
1750 .get_frame = renesas_usb3_get_frame,
1751 .udc_start = renesas_usb3_start,
1752 .udc_stop = renesas_usb3_stop,
1753 .pullup = renesas_usb3_pullup,
1754 .set_selfpowered = renesas_usb3_set_selfpowered,
1757 /*------- platform_driver ------------------------------------------------*/
1758 static int renesas_usb3_remove(struct platform_device *pdev)
1760 struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
1762 pm_runtime_put(&pdev->dev);
1763 pm_runtime_disable(&pdev->dev);
1765 usb_del_gadget_udc(&usb3->gadget);
1767 __renesas_usb3_ep_free_request(usb3->ep0_req);
1769 return 0;
1772 static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
1773 const struct renesas_usb3_priv *priv)
1775 struct renesas_usb3_ep *usb3_ep;
1776 int i;
1778 /* calculate num_usb3_eps from renesas_usb3_priv */
1779 usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
1780 priv->ramsize_per_pipe + 1;
1782 if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
1783 usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
1785 usb3->usb3_ep = devm_kzalloc(dev, sizeof(*usb3_ep) * usb3->num_usb3_eps,
1786 GFP_KERNEL);
1787 if (!usb3->usb3_ep)
1788 return -ENOMEM;
1790 dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
1792 * This driver prepares pipes as the followings:
1793 * - odd pipes = IN pipe
1794 * - even pipes = OUT pipe (except pipe 0)
1796 usb3_for_each_ep(usb3_ep, usb3, i) {
1797 snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
1798 usb3_ep->usb3 = usb3;
1799 usb3_ep->num = i;
1800 usb3_ep->ep.name = usb3_ep->ep_name;
1801 usb3_ep->ep.ops = &renesas_usb3_ep_ops;
1802 INIT_LIST_HEAD(&usb3_ep->queue);
1803 INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
1804 if (!i) {
1805 /* for control pipe */
1806 usb3->gadget.ep0 = &usb3_ep->ep;
1807 usb_ep_set_maxpacket_limit(&usb3_ep->ep,
1808 USB3_EP0_HSFS_MAX_PACKET_SIZE);
1809 usb3_ep->ep.caps.type_control = true;
1810 usb3_ep->ep.caps.dir_in = true;
1811 usb3_ep->ep.caps.dir_out = true;
1812 continue;
1815 /* for bulk or interrupt pipe */
1816 usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
1817 list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
1818 usb3_ep->ep.caps.type_bulk = true;
1819 usb3_ep->ep.caps.type_int = true;
1820 if (i & 1)
1821 usb3_ep->ep.caps.dir_in = true;
1822 else
1823 usb3_ep->ep.caps.dir_out = true;
1826 return 0;
1829 static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
1830 const struct renesas_usb3_priv *priv)
1832 struct renesas_usb3_ep *usb3_ep;
1833 int i;
1834 u32 ramif[2], basead[2]; /* index 0 = for IN pipes */
1835 u32 *cur_ramif, *cur_basead;
1836 u32 val;
1838 memset(ramif, 0, sizeof(ramif));
1839 memset(basead, 0, sizeof(basead));
1842 * This driver prepares pipes as the followings:
1843 * - all pipes = the same size as "ramsize_per_pipe"
1844 * Please refer to the "Method of Specifying RAM Mapping"
1846 usb3_for_each_ep(usb3_ep, usb3, i) {
1847 if (!i)
1848 continue; /* out of scope if ep num = 0 */
1849 if (usb3_ep->ep.caps.dir_in) {
1850 cur_ramif = &ramif[0];
1851 cur_basead = &basead[0];
1852 } else {
1853 cur_ramif = &ramif[1];
1854 cur_basead = &basead[1];
1857 if (*cur_basead > priv->ramsize_per_ramif)
1858 continue; /* out of memory for IN or OUT pipe */
1860 /* calculate rammap_val */
1861 val = PN_RAMMAP_RAMIF(*cur_ramif);
1862 val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
1863 val |= PN_RAMMAP_BASEAD(*cur_basead);
1864 usb3_ep->rammap_val = val;
1866 dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
1867 i, val, *cur_ramif, *cur_basead);
1869 /* update current ramif */
1870 if (*cur_ramif + 1 == priv->num_ramif) {
1871 *cur_ramif = 0;
1872 *cur_basead += priv->ramsize_per_pipe;
1873 } else {
1874 (*cur_ramif)++;
1879 static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795 = {
1880 .ramsize_per_ramif = SZ_16K,
1881 .num_ramif = 2,
1882 .ramsize_per_pipe = SZ_4K,
1883 .workaround_for_vbus = true,
1886 static const struct of_device_id usb3_of_match[] = {
1888 .compatible = "renesas,r8a7795-usb3-peri",
1889 .data = &renesas_usb3_priv_r8a7795,
1891 { },
1893 MODULE_DEVICE_TABLE(of, usb3_of_match);
1895 static int renesas_usb3_probe(struct platform_device *pdev)
1897 struct renesas_usb3 *usb3;
1898 struct resource *res;
1899 const struct of_device_id *match;
1900 int irq, ret;
1901 const struct renesas_usb3_priv *priv;
1903 match = of_match_node(usb3_of_match, pdev->dev.of_node);
1904 if (!match)
1905 return -ENODEV;
1906 priv = match->data;
1908 irq = platform_get_irq(pdev, 0);
1909 if (irq < 0)
1910 return -ENODEV;
1912 usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
1913 if (!usb3)
1914 return -ENOMEM;
1916 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1917 usb3->reg = devm_ioremap_resource(&pdev->dev, res);
1918 if (IS_ERR(usb3->reg))
1919 return PTR_ERR(usb3->reg);
1921 platform_set_drvdata(pdev, usb3);
1922 spin_lock_init(&usb3->lock);
1924 usb3->gadget.ops = &renesas_usb3_gadget_ops;
1925 usb3->gadget.name = udc_name;
1926 usb3->gadget.max_speed = USB_SPEED_SUPER;
1927 INIT_LIST_HEAD(&usb3->gadget.ep_list);
1928 ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
1929 if (ret < 0)
1930 return ret;
1931 renesas_usb3_init_ram(usb3, &pdev->dev, priv);
1933 ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
1934 dev_name(&pdev->dev), usb3);
1935 if (ret < 0)
1936 return ret;
1938 /* for ep0 handling */
1939 usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
1940 if (!usb3->ep0_req)
1941 return -ENOMEM;
1943 ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
1944 if (ret < 0)
1945 goto err_add_udc;
1947 usb3->workaround_for_vbus = priv->workaround_for_vbus;
1949 pm_runtime_enable(&pdev->dev);
1950 pm_runtime_get_sync(&pdev->dev);
1952 dev_info(&pdev->dev, "probed\n");
1954 return 0;
1956 err_add_udc:
1957 __renesas_usb3_ep_free_request(usb3->ep0_req);
1959 return ret;
1962 static struct platform_driver renesas_usb3_driver = {
1963 .probe = renesas_usb3_probe,
1964 .remove = renesas_usb3_remove,
1965 .driver = {
1966 .name = (char *)udc_name,
1967 .of_match_table = of_match_ptr(usb3_of_match),
1970 module_platform_driver(renesas_usb3_driver);
1972 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
1973 MODULE_LICENSE("GPL v2");
1974 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
1975 MODULE_ALIAS("platform:renesas_usb3");