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