blob: a1e79fcf25b35d680b54f6c548671bed6d0b71d6 [file] [log] [blame]
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09001/*
2 * Renesas USB3.0 Peripheral driver (USB gadget)
3 *
4 * Copyright (C) 2015 Renesas Electronics Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 */
10
11#include <linux/delay.h>
12#include <linux/err.h>
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +090013#include <linux/extcon.h>
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +090014#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/of_device.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/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
25/* register definitions */
26#define USB3_AXI_INT_STA 0x008
27#define USB3_AXI_INT_ENA 0x00c
28#define USB3_DMA_INT_STA 0x010
29#define USB3_DMA_INT_ENA 0x014
30#define USB3_USB_COM_CON 0x200
31#define USB3_USB20_CON 0x204
32#define USB3_USB30_CON 0x208
33#define USB3_USB_STA 0x210
34#define USB3_DRD_CON 0x218
35#define USB3_USB_INT_STA_1 0x220
36#define USB3_USB_INT_STA_2 0x224
37#define USB3_USB_INT_ENA_1 0x228
38#define USB3_USB_INT_ENA_2 0x22c
39#define USB3_STUP_DAT_0 0x230
40#define USB3_STUP_DAT_1 0x234
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +090041#define USB3_USB_OTG_STA 0x268
42#define USB3_USB_OTG_INT_STA 0x26c
43#define USB3_USB_OTG_INT_ENA 0x270
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +090044#define USB3_P0_MOD 0x280
45#define USB3_P0_CON 0x288
46#define USB3_P0_STA 0x28c
47#define USB3_P0_INT_STA 0x290
48#define USB3_P0_INT_ENA 0x294
49#define USB3_P0_LNG 0x2a0
50#define USB3_P0_READ 0x2a4
51#define USB3_P0_WRITE 0x2a8
52#define USB3_PIPE_COM 0x2b0
53#define USB3_PN_MOD 0x2c0
54#define USB3_PN_RAMMAP 0x2c4
55#define USB3_PN_CON 0x2c8
56#define USB3_PN_STA 0x2cc
57#define USB3_PN_INT_STA 0x2d0
58#define USB3_PN_INT_ENA 0x2d4
59#define USB3_PN_LNG 0x2e0
60#define USB3_PN_READ 0x2e4
61#define USB3_PN_WRITE 0x2e8
62#define USB3_SSIFCMD 0x340
63
64/* AXI_INT_ENA and AXI_INT_STA */
65#define AXI_INT_DMAINT BIT(31)
66#define AXI_INT_EPCINT BIT(30)
67
68/* LCLKSEL */
69#define LCLKSEL_LSEL BIT(18)
70
71/* USB_COM_CON */
72#define USB_COM_CON_CONF BIT(24)
73#define USB_COM_CON_SPD_MODE BIT(17)
74#define USB_COM_CON_EP0_EN BIT(16)
75#define USB_COM_CON_DEV_ADDR_SHIFT 8
76#define USB_COM_CON_DEV_ADDR_MASK GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
77#define USB_COM_CON_DEV_ADDR(n) (((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
78 USB_COM_CON_DEV_ADDR_MASK)
79#define USB_COM_CON_RX_DETECTION BIT(1)
80#define USB_COM_CON_PIPE_CLR BIT(0)
81
82/* USB20_CON */
83#define USB20_CON_B2_PUE BIT(31)
84#define USB20_CON_B2_SUSPEND BIT(24)
85#define USB20_CON_B2_CONNECT BIT(17)
86#define USB20_CON_B2_TSTMOD_SHIFT 8
87#define USB20_CON_B2_TSTMOD_MASK GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
88#define USB20_CON_B2_TSTMOD(n) (((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
89 USB20_CON_B2_TSTMOD_MASK)
90#define USB20_CON_B2_TSTMOD_EN BIT(0)
91
92/* USB30_CON */
93#define USB30_CON_POW_SEL_SHIFT 24
94#define USB30_CON_POW_SEL_MASK GENMASK(26, USB30_CON_POW_SEL_SHIFT)
95#define USB30_CON_POW_SEL_IN_U3 BIT(26)
96#define USB30_CON_POW_SEL_IN_DISCON 0
97#define USB30_CON_POW_SEL_P2_TO_P0 BIT(25)
98#define USB30_CON_POW_SEL_P0_TO_P3 BIT(24)
99#define USB30_CON_POW_SEL_P0_TO_P2 0
100#define USB30_CON_B3_PLLWAKE BIT(23)
101#define USB30_CON_B3_CONNECT BIT(17)
102#define USB30_CON_B3_HOTRST_CMP BIT(1)
103
104/* USB_STA */
105#define USB_STA_SPEED_MASK (BIT(2) | BIT(1))
106#define USB_STA_SPEED_HS BIT(2)
107#define USB_STA_SPEED_FS BIT(1)
108#define USB_STA_SPEED_SS 0
109#define USB_STA_VBUS_STA BIT(0)
110
111/* DRD_CON */
112#define DRD_CON_PERI_CON BIT(24)
Yoshihiro Shimodab2f1eaa2016-08-23 21:11:13 +0900113#define DRD_CON_VBOUT BIT(0)
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900114
115/* USB_INT_ENA_1 and USB_INT_STA_1 */
116#define USB_INT_1_B3_PLLWKUP BIT(31)
117#define USB_INT_1_B3_LUPSUCS BIT(30)
118#define USB_INT_1_B3_DISABLE BIT(27)
119#define USB_INT_1_B3_WRMRST BIT(21)
120#define USB_INT_1_B3_HOTRST BIT(20)
121#define USB_INT_1_B2_USBRST BIT(12)
122#define USB_INT_1_B2_L1SPND BIT(11)
123#define USB_INT_1_B2_SPND BIT(9)
124#define USB_INT_1_B2_RSUM BIT(8)
125#define USB_INT_1_SPEED BIT(1)
126#define USB_INT_1_VBUS_CNG BIT(0)
127
128/* USB_INT_ENA_2 and USB_INT_STA_2 */
129#define USB_INT_2_PIPE(n) BIT(n)
130
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +0900131/* USB_OTG_STA, USB_OTG_INT_STA and USB_OTG_INT_ENA */
132#define USB_OTG_IDMON BIT(4)
133
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900134/* P0_MOD */
135#define P0_MOD_DIR BIT(6)
136
137/* P0_CON and PN_CON */
138#define PX_CON_BYTE_EN_MASK (BIT(10) | BIT(9))
139#define PX_CON_BYTE_EN_SHIFT 9
140#define PX_CON_BYTE_EN_BYTES(n) (((n) << PX_CON_BYTE_EN_SHIFT) & \
141 PX_CON_BYTE_EN_MASK)
142#define PX_CON_SEND BIT(8)
143
144/* P0_CON */
145#define P0_CON_ST_RES_MASK (BIT(27) | BIT(26))
146#define P0_CON_ST_RES_FORCE_STALL BIT(27)
147#define P0_CON_ST_RES_NORMAL BIT(26)
148#define P0_CON_ST_RES_FORCE_NRDY 0
149#define P0_CON_OT_RES_MASK (BIT(25) | BIT(24))
150#define P0_CON_OT_RES_FORCE_STALL BIT(25)
151#define P0_CON_OT_RES_NORMAL BIT(24)
152#define P0_CON_OT_RES_FORCE_NRDY 0
153#define P0_CON_IN_RES_MASK (BIT(17) | BIT(16))
154#define P0_CON_IN_RES_FORCE_STALL BIT(17)
155#define P0_CON_IN_RES_NORMAL BIT(16)
156#define P0_CON_IN_RES_FORCE_NRDY 0
157#define P0_CON_RES_WEN BIT(7)
158#define P0_CON_BCLR BIT(1)
159
160/* P0_STA and PN_STA */
161#define PX_STA_BUFSTS BIT(0)
162
163/* P0_INT_ENA and P0_INT_STA */
164#define P0_INT_STSED BIT(18)
165#define P0_INT_STSST BIT(17)
166#define P0_INT_SETUP BIT(16)
167#define P0_INT_RCVNL BIT(8)
168#define P0_INT_ERDY BIT(7)
169#define P0_INT_FLOW BIT(6)
170#define P0_INT_STALL BIT(2)
171#define P0_INT_NRDY BIT(1)
172#define P0_INT_BFRDY BIT(0)
173#define P0_INT_ALL_BITS (P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
174
175/* PN_MOD */
176#define PN_MOD_DIR BIT(6)
177#define PN_MOD_TYPE_SHIFT 4
178#define PN_MOD_TYPE_MASK GENMASK(5, PN_MOD_TYPE_SHIFT)
179#define PN_MOD_TYPE(n) (((n) << PN_MOD_TYPE_SHIFT) & \
180 PN_MOD_TYPE_MASK)
181#define PN_MOD_EPNUM_MASK GENMASK(3, 0)
182#define PN_MOD_EPNUM(n) ((n) & PN_MOD_EPNUM_MASK)
183
184/* PN_RAMMAP */
185#define PN_RAMMAP_RAMAREA_SHIFT 29
186#define PN_RAMMAP_RAMAREA_MASK GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
187#define PN_RAMMAP_RAMAREA_16KB BIT(31)
188#define PN_RAMMAP_RAMAREA_8KB (BIT(30) | BIT(29))
189#define PN_RAMMAP_RAMAREA_4KB BIT(30)
190#define PN_RAMMAP_RAMAREA_2KB BIT(29)
191#define PN_RAMMAP_RAMAREA_1KB 0
192#define PN_RAMMAP_MPKT_SHIFT 16
193#define PN_RAMMAP_MPKT_MASK GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
194#define PN_RAMMAP_MPKT(n) (((n) << PN_RAMMAP_MPKT_SHIFT) & \
195 PN_RAMMAP_MPKT_MASK)
196#define PN_RAMMAP_RAMIF_SHIFT 14
197#define PN_RAMMAP_RAMIF_MASK GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
198#define PN_RAMMAP_RAMIF(n) (((n) << PN_RAMMAP_RAMIF_SHIFT) & \
199 PN_RAMMAP_RAMIF_MASK)
200#define PN_RAMMAP_BASEAD_MASK GENMASK(13, 0)
201#define PN_RAMMAP_BASEAD(offs) (((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
202#define PN_RAMMAP_DATA(area, ramif, basead) ((PN_RAMMAP_##area) | \
203 (PN_RAMMAP_RAMIF(ramif)) | \
204 (PN_RAMMAP_BASEAD(basead)))
205
206/* PN_CON */
207#define PN_CON_EN BIT(31)
208#define PN_CON_DATAIF_EN BIT(30)
209#define PN_CON_RES_MASK (BIT(17) | BIT(16))
210#define PN_CON_RES_FORCE_STALL BIT(17)
211#define PN_CON_RES_NORMAL BIT(16)
212#define PN_CON_RES_FORCE_NRDY 0
213#define PN_CON_LAST BIT(11)
214#define PN_CON_RES_WEN BIT(7)
215#define PN_CON_CLR BIT(0)
216
217/* PN_INT_STA and PN_INT_ENA */
218#define PN_INT_LSTTR BIT(4)
219#define PN_INT_BFRDY BIT(0)
220
221/* USB3_SSIFCMD */
222#define SSIFCMD_URES_U2 BIT(9)
223#define SSIFCMD_URES_U1 BIT(8)
224#define SSIFCMD_UDIR_U2 BIT(7)
225#define SSIFCMD_UDIR_U1 BIT(6)
226#define SSIFCMD_UREQ_U2 BIT(5)
227#define SSIFCMD_UREQ_U1 BIT(4)
228
229#define USB3_EP0_SS_MAX_PACKET_SIZE 512
230#define USB3_EP0_HSFS_MAX_PACKET_SIZE 64
231#define USB3_EP0_BUF_SIZE 8
232#define USB3_MAX_NUM_PIPES 30
233#define USB3_WAIT_US 3
234
235struct renesas_usb3;
236struct renesas_usb3_request {
237 struct usb_request req;
238 struct list_head queue;
239};
240
241#define USB3_EP_NAME_SIZE 8
242struct renesas_usb3_ep {
243 struct usb_ep ep;
244 struct renesas_usb3 *usb3;
245 int num;
246 char ep_name[USB3_EP_NAME_SIZE];
247 struct list_head queue;
248 u32 rammap_val;
249 bool dir_in;
250 bool halt;
251 bool wedge;
252 bool started;
253};
254
255struct renesas_usb3_priv {
256 int ramsize_per_ramif; /* unit = bytes */
257 int num_ramif;
258 int ramsize_per_pipe; /* unit = bytes */
259 bool workaround_for_vbus; /* if true, don't check vbus signal */
260};
261
262struct renesas_usb3 {
263 void __iomem *reg;
264
265 struct usb_gadget gadget;
266 struct usb_gadget_driver *driver;
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +0900267 struct extcon_dev *extcon;
268 struct work_struct extcon_work;
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900269
270 struct renesas_usb3_ep *usb3_ep;
271 int num_usb3_eps;
272
273 spinlock_t lock;
274 int disabled_count;
275
276 struct usb_request *ep0_req;
277 u16 test_mode;
278 u8 ep0_buf[USB3_EP0_BUF_SIZE];
279 bool softconnect;
280 bool workaround_for_vbus;
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +0900281 bool extcon_host; /* check id and set EXTCON_USB_HOST */
282 bool extcon_usb; /* check vbus and set EXTCON_USB */
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900283};
284
285#define gadget_to_renesas_usb3(_gadget) \
286 container_of(_gadget, struct renesas_usb3, gadget)
287#define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
288#define usb3_to_dev(_usb3) (_usb3->gadget.dev.parent)
289
290#define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
291#define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
292#define usb_req_to_usb3_req(_req) container_of(_req, \
293 struct renesas_usb3_request, req)
294
295#define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
296#define usb3_for_each_ep(usb3_ep, usb3, i) \
297 for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
298 (i) < (usb3)->num_usb3_eps; \
299 (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
300
301static const char udc_name[] = "renesas_usb3";
302
303static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
304{
305 iowrite32(data, usb3->reg + offs);
306}
307
308static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
309{
310 return ioread32(usb3->reg + offs);
311}
312
313static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
314{
315 u32 val = usb3_read(usb3, offs);
316
317 val |= bits;
318 usb3_write(usb3, val, offs);
319}
320
321static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
322{
323 u32 val = usb3_read(usb3, offs);
324
325 val &= ~bits;
326 usb3_write(usb3, val, offs);
327}
328
329static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
330 u32 expected)
331{
332 int i;
333
334 for (i = 0; i < USB3_WAIT_US; i++) {
335 if ((usb3_read(usb3, reg) & mask) == expected)
336 return 0;
337 udelay(1);
338 }
339
340 dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
341 __func__, reg, mask, expected);
342
343 return -EBUSY;
344}
345
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +0900346static void renesas_usb3_extcon_work(struct work_struct *work)
347{
348 struct renesas_usb3 *usb3 = container_of(work, struct renesas_usb3,
349 extcon_work);
350
351 extcon_set_state_sync(usb3->extcon, EXTCON_USB_HOST, usb3->extcon_host);
352 extcon_set_state_sync(usb3->extcon, EXTCON_USB, usb3->extcon_usb);
353}
354
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900355static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
356{
357 usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
358}
359
360static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
361{
362 usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
363}
364
365static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
366{
367 usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
368}
369
370static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
371{
372 usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
373}
374
375static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
376{
377 /* Set AXI_INT */
378 usb3_write(usb3, ~0, USB3_DMA_INT_STA);
379 usb3_write(usb3, 0, USB3_DMA_INT_ENA);
380 usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
381}
382
383static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
384{
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900385 usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
386 usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
387}
388
389static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
390{
391 if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
392 return true; /* already waked it up */
393
394 usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
395 usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
396
397 return false;
398}
399
400static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
401{
402 u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
403
404 if (usb3->softconnect && pullup)
405 usb3_set_bit(usb3, bits, USB3_USB20_CON);
406 else
407 usb3_clear_bit(usb3, bits, USB3_USB20_CON);
408}
409
410static void usb3_set_test_mode(struct renesas_usb3 *usb3)
411{
412 u32 val = usb3_read(usb3, USB3_USB20_CON);
413
414 val &= ~USB20_CON_B2_TSTMOD_MASK;
415 val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
416 usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
417 if (!usb3->test_mode)
418 usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
419}
420
421static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
422{
423 usb3->disabled_count++;
424 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
425 usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
426 usb3_usb2_pullup(usb3, 1);
427}
428
429static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
430{
431 return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
432}
433
434static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
435{
436 if (!usb3_is_usb3_phy_in_u3(usb3))
437 return true; /* already waked it up */
438
439 usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
440 usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
441
442 return false;
443}
444
445static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
446{
447 u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
448 u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
449 u32 val = usb3_read(usb3, USB3_SSIFCMD);
450 u16 ret = 0;
451
452 /* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
453 if (!(val & mask_u2))
454 ret |= 1 << USB_DEV_STAT_U2_ENABLED;
455 if (!(val & mask_u1))
456 ret |= 1 << USB_DEV_STAT_U1_ENABLED;
457
458 return ret;
459}
460
461static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
462{
463 u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
464
465 /* Enables U2 if the bits of UDIR and UREQ are set to 0 */
466 if (enable)
467 usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
468 else
469 usb3_set_bit(usb3, bits, USB3_SSIFCMD);
470}
471
472static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
473{
474 u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
475
476 /* Enables U1 if the bits of UDIR and UREQ are set to 0 */
477 if (enable)
478 usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
479 else
480 usb3_set_bit(usb3, bits, USB3_SSIFCMD);
481}
482
483static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
484{
485 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
486 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
487 usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
488}
489
490static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
491{
492 usb3_start_operation_for_usb3(usb3);
493 usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
494
495 usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
496 USB_INT_1_SPEED);
497}
498
499static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
500{
501 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
502}
503
504static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
505 bool is_usb3)
506{
507 usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
508 usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
509 usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
510
511 if (is_usb3)
512 usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
513 USB_INT_1_B3_HOTRST);
514 else
515 usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
516 USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
517}
518
519static void usb3_connect(struct renesas_usb3 *usb3)
520{
521 if (usb3_wakeup_usb3_phy(usb3))
522 usb3_start_usb3_connection(usb3);
523}
524
525static void usb3_reset_epc(struct renesas_usb3 *usb3)
526{
527 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
528 usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
529 usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
530 usb3->test_mode = 0;
531 usb3_set_test_mode(usb3);
532}
533
534static void usb3_disconnect(struct renesas_usb3 *usb3)
535{
536 usb3->disabled_count = 0;
537 usb3_usb2_pullup(usb3, 0);
538 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
539 usb3_reset_epc(usb3);
540
541 if (usb3->driver)
542 usb3->driver->disconnect(&usb3->gadget);
543}
544
545static void usb3_check_vbus(struct renesas_usb3 *usb3)
546{
547 if (usb3->workaround_for_vbus) {
548 usb3_connect(usb3);
549 } else {
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +0900550 usb3->extcon_usb = !!(usb3_read(usb3, USB3_USB_STA) &
551 USB_STA_VBUS_STA);
552 if (usb3->extcon_usb)
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900553 usb3_connect(usb3);
554 else
555 usb3_disconnect(usb3);
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +0900556
557 schedule_work(&usb3->extcon_work);
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900558 }
559}
560
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +0900561static void usb3_set_mode(struct renesas_usb3 *usb3, bool host)
562{
563 if (host)
564 usb3_clear_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
565 else
566 usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
567}
568
569static void usb3_vbus_out(struct renesas_usb3 *usb3, bool enable)
570{
571 if (enable)
572 usb3_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
573 else
574 usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON);
575}
576
577static void usb3_mode_config(struct renesas_usb3 *usb3, bool host, bool a_dev)
578{
579 usb3_set_mode(usb3, host);
580 usb3_vbus_out(usb3, a_dev);
581}
582
583static bool usb3_is_a_device(struct renesas_usb3 *usb3)
584{
585 return !(usb3_read(usb3, USB3_USB_OTG_STA) & USB_OTG_IDMON);
586}
587
588static void usb3_check_id(struct renesas_usb3 *usb3)
589{
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +0900590 usb3->extcon_host = usb3_is_a_device(usb3);
591
592 if (usb3->extcon_host)
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +0900593 usb3_mode_config(usb3, true, true);
594 else
595 usb3_mode_config(usb3, false, false);
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +0900596
597 schedule_work(&usb3->extcon_work);
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +0900598}
599
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900600static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
601{
602 usb3_init_axi_bridge(usb3);
603 usb3_init_epc_registers(usb3);
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +0900604 usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_STA);
605 usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_ENA);
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900606
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +0900607 usb3_check_id(usb3);
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900608 usb3_check_vbus(usb3);
609}
610
611static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
612{
613 usb3_disconnect(usb3);
614 usb3_write(usb3, 0, USB3_P0_INT_ENA);
615 usb3_write(usb3, 0, USB3_PN_INT_ENA);
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +0900616 usb3_write(usb3, 0, USB3_USB_OTG_INT_ENA);
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +0900617 usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
618 usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
619 usb3_write(usb3, 0, USB3_AXI_INT_ENA);
620}
621
622static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
623{
624 usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
625 usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
626 usb3_start_usb3_connection(usb3);
627}
628
629static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
630{
631 usb3_transition_to_default_state(usb3, true);
632}
633
634static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
635{
636 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
637 usb3_start_usb2_connection(usb3);
638 usb3_transition_to_default_state(usb3, false);
639}
640
641static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
642{
643 usb3_stop_usb3_connection(usb3);
644 if (usb3_wakeup_usb2_phy(usb3))
645 usb3_irq_epc_int_1_resume(usb3);
646}
647
648static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
649{
650 usb3_reset_epc(usb3);
651 if (usb3->disabled_count < 3)
652 usb3_start_usb3_connection(usb3);
653 else
654 usb3_start_usb2_connection(usb3);
655}
656
657static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
658{
659 usb3_check_vbus(usb3);
660}
661
662static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
663{
664 usb3_reset_epc(usb3);
665 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
666
667 /* This bit shall be set within 12ms from the start of HotReset */
668 usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
669}
670
671static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
672{
673 usb3_reset_epc(usb3);
674 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
675
676 usb3_start_operation_for_usb3(usb3);
677 usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
678}
679
680static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
681{
682 u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
683
684 switch (speed) {
685 case USB_STA_SPEED_SS:
686 usb3->gadget.speed = USB_SPEED_SUPER;
687 break;
688 case USB_STA_SPEED_HS:
689 usb3->gadget.speed = USB_SPEED_HIGH;
690 break;
691 case USB_STA_SPEED_FS:
692 usb3->gadget.speed = USB_SPEED_FULL;
693 break;
694 default:
695 usb3->gadget.speed = USB_SPEED_UNKNOWN;
696 break;
697 }
698}
699
700static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
701{
702 if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
703 usb3_irq_epc_int_1_pll_wakeup(usb3);
704
705 if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
706 usb3_irq_epc_int_1_linkup_success(usb3);
707
708 if (int_sta_1 & USB_INT_1_B3_HOTRST)
709 usb3_irq_epc_int_1_hot_reset(usb3);
710
711 if (int_sta_1 & USB_INT_1_B3_WRMRST)
712 usb3_irq_epc_int_1_warm_reset(usb3);
713
714 if (int_sta_1 & USB_INT_1_B3_DISABLE)
715 usb3_irq_epc_int_1_disable(usb3);
716
717 if (int_sta_1 & USB_INT_1_B2_USBRST)
718 usb3_irq_epc_int_1_bus_reset(usb3);
719
720 if (int_sta_1 & USB_INT_1_B2_RSUM)
721 usb3_irq_epc_int_1_resume(usb3);
722
723 if (int_sta_1 & USB_INT_1_SPEED)
724 usb3_irq_epc_int_1_speed(usb3);
725
726 if (int_sta_1 & USB_INT_1_VBUS_CNG)
727 usb3_irq_epc_int_1_vbus_change(usb3);
728}
729
730static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
731 *usb3_ep)
732{
733 return list_first_entry_or_null(&usb3_ep->queue,
734 struct renesas_usb3_request, queue);
735}
736
737static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
738 *usb3_ep)
739{
740 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
741 struct renesas_usb3_request *usb3_req;
742 unsigned long flags;
743
744 spin_lock_irqsave(&usb3->lock, flags);
745 usb3_req = __usb3_get_request(usb3_ep);
746 spin_unlock_irqrestore(&usb3->lock, flags);
747
748 return usb3_req;
749}
750
751static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
752 struct renesas_usb3_request *usb3_req, int status)
753{
754 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
755 unsigned long flags;
756
757 dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
758 usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
759 status);
760 usb3_req->req.status = status;
761 spin_lock_irqsave(&usb3->lock, flags);
762 usb3_ep->started = false;
763 list_del_init(&usb3_req->queue);
764 spin_unlock_irqrestore(&usb3->lock, flags);
765 usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
766}
767
768static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
769{
770 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
771 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
772
773 if (usb3_req)
774 usb3_request_done(usb3_ep, usb3_req, 0);
775 if (usb3->test_mode)
776 usb3_set_test_mode(usb3);
777}
778
779static void usb3_get_setup_data(struct renesas_usb3 *usb3,
780 struct usb_ctrlrequest *ctrl)
781{
782 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
783 u32 *data = (u32 *)ctrl;
784
785 *data++ = usb3_read(usb3, USB3_STUP_DAT_0);
786 *data = usb3_read(usb3, USB3_STUP_DAT_1);
787
788 /* update this driver's flag */
789 usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
790}
791
792static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
793{
794 u32 val = usb3_read(usb3, USB3_P0_CON);
795
796 val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
797 val |= res | P0_CON_RES_WEN;
798 usb3_write(usb3, val, USB3_P0_CON);
799}
800
801static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
802{
803 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
804 P0_CON_OT_RES_FORCE_STALL |
805 P0_CON_IN_RES_NORMAL);
806}
807
808static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
809{
810 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
811 P0_CON_OT_RES_FORCE_STALL |
812 P0_CON_IN_RES_NORMAL);
813}
814
815static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
816{
817 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
818 P0_CON_OT_RES_NORMAL |
819 P0_CON_IN_RES_FORCE_STALL);
820}
821
822static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
823{
824 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
825 P0_CON_OT_RES_NORMAL |
826 P0_CON_IN_RES_FORCE_STALL);
827}
828
829static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
830{
831 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
832 P0_CON_OT_RES_FORCE_STALL |
833 P0_CON_IN_RES_FORCE_STALL);
834}
835
836static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
837{
838 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
839 P0_CON_OT_RES_FORCE_STALL |
840 P0_CON_IN_RES_FORCE_STALL);
841}
842
843static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
844{
845 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
846 P0_CON_OT_RES_FORCE_NRDY |
847 P0_CON_IN_RES_FORCE_NRDY);
848}
849
850static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
851{
852 if (num == 0 || num > usb3->num_usb3_eps)
853 return -ENXIO;
854
855 usb3_write(usb3, num, USB3_PIPE_COM);
856
857 return 0;
858}
859
860static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
861{
862 u32 val = usb3_read(usb3, USB3_PN_CON);
863
864 val &= ~PN_CON_RES_MASK;
865 val |= res & PN_CON_RES_MASK;
866 val |= PN_CON_RES_WEN;
867 usb3_write(usb3, val, USB3_PN_CON);
868}
869
870static void usb3_pn_start(struct renesas_usb3 *usb3)
871{
872 usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
873}
874
875static void usb3_pn_stop(struct renesas_usb3 *usb3)
876{
877 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
878}
879
880static void usb3_pn_stall(struct renesas_usb3 *usb3)
881{
882 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
883}
884
885static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
886{
887 usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
888
889 return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
890}
891
892static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
893 struct renesas_usb3_request *usb3_req)
894{
895 struct usb_request *req = &usb3_req->req;
896
897 if ((!req->zero && req->actual == req->length) ||
898 (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
899 return true;
900 else
901 return false;
902}
903
904static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
905{
906 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
907 u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
908
909 return usb3_wait(usb3, sta_reg, mask, mask);
910}
911
912static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
913 bool last)
914{
915 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
916 u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
917 u32 val = usb3_read(usb3, con_reg);
918
919 val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
920 val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
921 usb3_write(usb3, val, con_reg);
922}
923
924static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
925 struct renesas_usb3_request *usb3_req,
926 u32 fifo_reg)
927{
928 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
929 int i;
930 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
931 usb3_ep->ep.maxpacket);
932 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
933 u32 tmp = 0;
934 bool is_last;
935
936 if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
937 return -EBUSY;
938
939 /* Update gadget driver parameter */
940 usb3_req->req.actual += len;
941
942 /* Write data to the register */
943 if (len >= 4) {
944 iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
945 buf += (len / 4) * 4;
946 len %= 4; /* update len to use usb3_set_pX_con_send() */
947 }
948
949 if (len) {
950 for (i = 0; i < len; i++)
951 tmp |= buf[i] << (8 * i);
952 usb3_write(usb3, tmp, fifo_reg);
953 }
954
955 is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
956 /* Send the data */
957 usb3_set_px_con_send(usb3_ep, len, is_last);
958
959 return is_last ? 0 : -EAGAIN;
960}
961
962static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
963{
964 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
965 u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
966
967 return usb3_read(usb3, lng_reg);
968}
969
970static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
971 struct renesas_usb3_request *usb3_req, u32 fifo_reg)
972{
973 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
974 int i;
975 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
976 usb3_get_received_length(usb3_ep));
977 u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
978 u32 tmp = 0;
979
980 if (!len)
981 return 0;
982
983 /* Update gadget driver parameter */
984 usb3_req->req.actual += len;
985
986 /* Read data from the register */
987 if (len >= 4) {
988 ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
989 buf += (len / 4) * 4;
990 len %= 4;
991 }
992
993 if (len) {
994 tmp = usb3_read(usb3, fifo_reg);
995 for (i = 0; i < len; i++)
996 buf[i] = (tmp >> (8 * i)) & 0xff;
997 }
998
999 return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
1000}
1001
1002static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
1003 struct renesas_usb3_request *usb3_req)
1004{
1005 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1006
1007 if (usb3_ep->dir_in) {
1008 usb3_set_p0_con_for_ctrl_read_status(usb3);
1009 } else {
1010 if (!usb3_req->req.length)
1011 usb3_set_p0_con_for_no_data(usb3);
1012 else
1013 usb3_set_p0_con_for_ctrl_write_status(usb3);
1014 }
1015}
1016
1017static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
1018 struct renesas_usb3_request *usb3_req)
1019{
1020 int ret = -EAGAIN;
1021
1022 if (usb3_ep->dir_in)
1023 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
1024 else
1025 ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
1026
1027 if (!ret)
1028 usb3_set_status_stage(usb3_ep, usb3_req);
1029}
1030
1031static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
1032 struct renesas_usb3_request *usb3_req)
1033{
1034 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1035
1036 if (usb3_ep->started)
1037 return;
1038
1039 usb3_ep->started = true;
1040
1041 if (usb3_ep->dir_in) {
1042 usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
1043 usb3_set_p0_con_for_ctrl_read_data(usb3);
1044 } else {
1045 usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
1046 usb3_set_p0_con_for_ctrl_write_data(usb3);
1047 }
1048
1049 usb3_p0_xfer(usb3_ep, usb3_req);
1050}
1051
1052static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
1053 struct renesas_usb3_request *usb3_req)
1054{
1055 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1056 struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
1057 unsigned long flags;
1058 int ret = -EAGAIN;
1059 u32 enable_bits = 0;
1060
1061 if (usb3_ep->halt || usb3_ep->started)
1062 return;
1063 if (usb3_req != usb3_req_first)
1064 return;
1065
1066 spin_lock_irqsave(&usb3->lock, flags);
1067 if (usb3_pn_change(usb3, usb3_ep->num) < 0)
1068 goto out;
1069
1070 usb3_ep->started = true;
1071 usb3_pn_start(usb3);
1072
1073 if (usb3_ep->dir_in) {
1074 ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
1075 enable_bits |= PN_INT_LSTTR;
1076 }
1077
1078 if (ret < 0)
1079 enable_bits |= PN_INT_BFRDY;
1080
1081 if (enable_bits) {
1082 usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
1083 usb3_enable_pipe_irq(usb3, usb3_ep->num);
1084 }
1085out:
1086 spin_unlock_irqrestore(&usb3->lock, flags);
1087}
1088
1089static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1090 gfp_t gfp_flags)
1091{
1092 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1093 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1094 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1095 unsigned long flags;
1096
1097 dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
1098 _req->length);
1099
1100 _req->status = -EINPROGRESS;
1101 _req->actual = 0;
1102 spin_lock_irqsave(&usb3->lock, flags);
1103 list_add_tail(&usb3_req->queue, &usb3_ep->queue);
1104 spin_unlock_irqrestore(&usb3->lock, flags);
1105
1106 if (!usb3_ep->num)
1107 usb3_start_pipe0(usb3_ep, usb3_req);
1108 else
1109 usb3_start_pipen(usb3_ep, usb3_req);
1110
1111 return 0;
1112}
1113
1114static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
1115{
1116 /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
1117 usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
1118}
1119
1120static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
1121 struct usb_ctrlrequest *ctrl)
1122{
1123 if (ctrl->wValue >= 128)
1124 return true; /* stall */
1125
1126 usb3_set_device_address(usb3, ctrl->wValue);
1127 usb3_set_p0_con_for_no_data(usb3);
1128
1129 return false;
1130}
1131
1132static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
1133 void *tx_data, size_t len,
1134 void (*complete)(struct usb_ep *ep,
1135 struct usb_request *req))
1136{
1137 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1138
1139 if (tx_data)
1140 memcpy(usb3->ep0_buf, tx_data,
1141 min_t(size_t, len, USB3_EP0_BUF_SIZE));
1142
1143 usb3->ep0_req->buf = &usb3->ep0_buf;
1144 usb3->ep0_req->length = len;
1145 usb3->ep0_req->complete = complete;
1146 renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
1147}
1148
1149static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
1150 struct usb_request *req)
1151{
1152}
1153
1154static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
1155 struct usb_ctrlrequest *ctrl)
1156{
1157 bool stall = false;
1158 struct renesas_usb3_ep *usb3_ep;
1159 int num;
1160 u16 status = 0;
1161
1162 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1163 case USB_RECIP_DEVICE:
1164 if (usb3->gadget.is_selfpowered)
1165 status |= 1 << USB_DEVICE_SELF_POWERED;
1166 if (usb3->gadget.speed == USB_SPEED_SUPER)
1167 status |= usb3_feature_get_un_enabled(usb3);
1168 break;
1169 case USB_RECIP_INTERFACE:
1170 break;
1171 case USB_RECIP_ENDPOINT:
1172 num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1173 usb3_ep = usb3_get_ep(usb3, num);
1174 if (usb3_ep->halt)
1175 status |= 1 << USB_ENDPOINT_HALT;
1176 break;
1177 default:
1178 stall = true;
1179 break;
1180 }
1181
1182 if (!stall) {
1183 status = cpu_to_le16(status);
1184 dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
1185 usb_req_to_usb3_req(usb3->ep0_req));
1186 usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
1187 usb3_pipe0_get_status_completion);
1188 }
1189
1190 return stall;
1191}
1192
1193static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
1194 struct usb_ctrlrequest *ctrl, bool set)
1195{
1196 bool stall = true;
1197 u16 w_value = le16_to_cpu(ctrl->wValue);
1198
1199 switch (w_value) {
1200 case USB_DEVICE_TEST_MODE:
1201 if (!set)
1202 break;
1203 usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
1204 stall = false;
1205 break;
1206 case USB_DEVICE_U1_ENABLE:
1207 case USB_DEVICE_U2_ENABLE:
1208 if (usb3->gadget.speed != USB_SPEED_SUPER)
1209 break;
1210 if (w_value == USB_DEVICE_U1_ENABLE)
1211 usb3_feature_u1_enable(usb3, set);
1212 if (w_value == USB_DEVICE_U2_ENABLE)
1213 usb3_feature_u2_enable(usb3, set);
1214 stall = false;
1215 break;
1216 default:
1217 break;
1218 }
1219
1220 return stall;
1221}
1222
1223static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
1224{
1225 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1226
1227 if (unlikely(usb3_ep->num))
1228 return -EINVAL;
1229
1230 usb3_ep->halt = halt;
1231 if (halt)
1232 usb3_set_p0_con_stall(usb3);
1233 else
1234 usb3_set_p0_con_stop(usb3);
1235
1236 return 0;
1237}
1238
1239static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
1240 bool is_clear_feature)
1241{
1242 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1243 unsigned long flags;
1244
1245 spin_lock_irqsave(&usb3->lock, flags);
1246 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1247 usb3_ep->halt = halt;
1248 if (halt) {
1249 usb3_pn_stall(usb3);
1250 } else if (!is_clear_feature || !usb3_ep->wedge) {
1251 usb3_pn_con_clear(usb3);
1252 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1253 usb3_pn_stop(usb3);
1254 }
1255 }
1256 spin_unlock_irqrestore(&usb3->lock, flags);
1257
1258 return 0;
1259}
1260
1261static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
1262 bool is_clear_feature)
1263{
1264 int ret = 0;
1265
1266 if (halt && usb3_ep->started)
1267 return -EAGAIN;
1268
1269 if (usb3_ep->num)
1270 ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
1271 else
1272 ret = usb3_set_halt_p0(usb3_ep, halt);
1273
1274 return ret;
1275}
1276
1277static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
1278 struct usb_ctrlrequest *ctrl,
1279 bool set)
1280{
1281 int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1282 struct renesas_usb3_ep *usb3_ep;
1283 struct renesas_usb3_request *usb3_req;
1284
1285 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
1286 return true; /* stall */
1287
1288 usb3_ep = usb3_get_ep(usb3, num);
1289 usb3_set_halt(usb3_ep, set, true);
1290
1291 /* Restarts a queue if clear feature */
1292 if (!set) {
1293 usb3_ep->started = false;
1294 usb3_req = usb3_get_request(usb3_ep);
1295 if (usb3_req)
1296 usb3_start_pipen(usb3_ep, usb3_req);
1297 }
1298
1299 return false;
1300}
1301
1302static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
1303 struct usb_ctrlrequest *ctrl, bool set)
1304{
1305 bool stall = false;
1306
1307 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1308 case USB_RECIP_DEVICE:
1309 stall = usb3_std_req_feature_device(usb3, ctrl, set);
1310 break;
1311 case USB_RECIP_INTERFACE:
1312 break;
1313 case USB_RECIP_ENDPOINT:
1314 stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
1315 break;
1316 default:
1317 stall = true;
1318 break;
1319 }
1320
1321 if (!stall)
1322 usb3_set_p0_con_for_no_data(usb3);
1323
1324 return stall;
1325}
1326
1327static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
1328 struct usb_request *req)
1329{
1330 /* TODO */
1331}
1332
1333static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
1334 struct usb_ctrlrequest *ctrl)
1335{
1336 u16 w_length = le16_to_cpu(ctrl->wLength);
1337
1338 if (w_length != 6)
1339 return true; /* stall */
1340
1341 dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
1342 usb_req_to_usb3_req(usb3->ep0_req));
1343 usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
1344
1345 return false;
1346}
1347
1348static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
1349 struct usb_ctrlrequest *ctrl)
1350{
1351 if (ctrl->wValue > 0)
1352 usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1353 else
1354 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
1355
1356 return false;
1357}
1358
1359/**
1360 * usb3_handle_standard_request - handle some standard requests
1361 * @usb3: the renesas_usb3 pointer
1362 * @ctrl: a pointer of setup data
1363 *
1364 * Returns true if this function handled a standard request
1365 */
1366static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
1367 struct usb_ctrlrequest *ctrl)
1368{
1369 bool ret = false;
1370 bool stall = false;
1371
1372 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1373 switch (ctrl->bRequest) {
1374 case USB_REQ_SET_ADDRESS:
1375 stall = usb3_std_req_set_address(usb3, ctrl);
1376 ret = true;
1377 break;
1378 case USB_REQ_GET_STATUS:
1379 stall = usb3_std_req_get_status(usb3, ctrl);
1380 ret = true;
1381 break;
1382 case USB_REQ_CLEAR_FEATURE:
1383 stall = usb3_std_req_feature(usb3, ctrl, false);
1384 ret = true;
1385 break;
1386 case USB_REQ_SET_FEATURE:
1387 stall = usb3_std_req_feature(usb3, ctrl, true);
1388 ret = true;
1389 break;
1390 case USB_REQ_SET_SEL:
1391 stall = usb3_std_req_set_sel(usb3, ctrl);
1392 ret = true;
1393 break;
1394 case USB_REQ_SET_ISOCH_DELAY:
1395 /* This hardware doesn't support Isochronous xfer */
1396 stall = true;
1397 ret = true;
1398 break;
1399 case USB_REQ_SET_CONFIGURATION:
1400 usb3_std_req_set_configuration(usb3, ctrl);
1401 break;
1402 default:
1403 break;
1404 }
1405 }
1406
1407 if (stall)
1408 usb3_set_p0_con_stall(usb3);
1409
1410 return ret;
1411}
1412
1413static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
1414{
1415 usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
1416
1417 return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
1418}
1419
1420static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
1421{
1422 struct usb_ctrlrequest ctrl;
1423 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1424
1425 /* Call giveback function if previous transfer is not completed */
1426 if (usb3_ep->started)
1427 usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
1428 -ECONNRESET);
1429
1430 usb3_p0_con_clear_buffer(usb3);
1431 usb3_get_setup_data(usb3, &ctrl);
1432 if (!usb3_handle_standard_request(usb3, &ctrl))
1433 if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
1434 usb3_set_p0_con_stall(usb3);
1435}
1436
1437static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
1438{
1439 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
1440 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1441
1442 if (!usb3_req)
1443 return;
1444
1445 usb3_p0_xfer(usb3_ep, usb3_req);
1446}
1447
1448static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
1449{
1450 u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
1451
1452 p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
1453 usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
1454 if (p0_int_sta & P0_INT_STSED)
1455 usb3_irq_epc_pipe0_status_end(usb3);
1456 if (p0_int_sta & P0_INT_SETUP)
1457 usb3_irq_epc_pipe0_setup(usb3);
1458 if (p0_int_sta & P0_INT_BFRDY)
1459 usb3_irq_epc_pipe0_bfrdy(usb3);
1460}
1461
1462static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
1463 struct renesas_usb3_ep *usb3_ep,
1464 struct renesas_usb3_request *usb3_req,
1465 int status)
1466{
1467 usb3_pn_stop(usb3);
1468 usb3_disable_pipe_irq(usb3, usb3_ep->num);
1469 usb3_request_done(usb3_ep, usb3_req, status);
1470
1471 /* get next usb3_req */
1472 usb3_req = usb3_get_request(usb3_ep);
1473 if (usb3_req)
1474 usb3_start_pipen(usb3_ep, usb3_req);
1475}
1476
1477static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
1478{
1479 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1480 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1481
1482 if (!usb3_req)
1483 return;
1484
1485 if (usb3_ep->dir_in) {
1486 dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
1487 __func__, usb3_req->req.length, usb3_req->req.actual);
1488 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1489 }
1490}
1491
1492static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
1493{
1494 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
1495 struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
1496
1497 if (!usb3_req)
1498 return;
1499
1500 if (usb3_ep->dir_in) {
1501 /* Do not stop the IN pipe here to detect LSTTR interrupt */
1502 if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
1503 usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
1504 } else {
1505 if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
1506 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
1507 }
1508}
1509
1510static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
1511{
1512 u32 pn_int_sta;
1513
1514 if (usb3_pn_change(usb3, num) < 0)
1515 return;
1516
1517 pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
1518 pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
1519 usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
1520 if (pn_int_sta & PN_INT_LSTTR)
1521 usb3_irq_epc_pipen_lsttr(usb3, num);
1522 if (pn_int_sta & PN_INT_BFRDY)
1523 usb3_irq_epc_pipen_bfrdy(usb3, num);
1524}
1525
1526static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
1527{
1528 int i;
1529
1530 for (i = 0; i < usb3->num_usb3_eps; i++) {
1531 if (int_sta_2 & USB_INT_2_PIPE(i)) {
1532 if (!i)
1533 usb3_irq_epc_pipe0(usb3);
1534 else
1535 usb3_irq_epc_pipen(usb3, i);
1536 }
1537 }
1538}
1539
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +09001540static void usb3_irq_idmon_change(struct renesas_usb3 *usb3)
1541{
1542 usb3_check_id(usb3);
1543}
1544
1545static void usb3_irq_otg_int(struct renesas_usb3 *usb3, u32 otg_int_sta)
1546{
1547 if (otg_int_sta & USB_OTG_IDMON)
1548 usb3_irq_idmon_change(usb3);
1549}
1550
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09001551static void usb3_irq_epc(struct renesas_usb3 *usb3)
1552{
1553 u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
1554 u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +09001555 u32 otg_int_sta = usb3_read(usb3, USB3_USB_OTG_INT_STA);
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09001556
1557 int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
1558 if (int_sta_1) {
1559 usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
1560 usb3_irq_epc_int_1(usb3, int_sta_1);
1561 }
1562
1563 int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
1564 if (int_sta_2)
1565 usb3_irq_epc_int_2(usb3, int_sta_2);
Yoshihiro Shimoda77172a12017-03-30 11:16:04 +09001566
1567 otg_int_sta &= usb3_read(usb3, USB3_USB_OTG_INT_ENA);
1568 if (otg_int_sta) {
1569 usb3_write(usb3, otg_int_sta, USB3_USB_OTG_INT_STA);
1570 usb3_irq_otg_int(usb3, otg_int_sta);
1571 }
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09001572}
1573
1574static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
1575{
1576 struct renesas_usb3 *usb3 = _usb3;
1577 irqreturn_t ret = IRQ_NONE;
1578 u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
1579
1580 if (axi_int_sta & AXI_INT_EPCINT) {
1581 usb3_irq_epc(usb3);
1582 ret = IRQ_HANDLED;
1583 }
1584
1585 return ret;
1586}
1587
1588static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
1589 const struct usb_endpoint_descriptor *desc)
1590{
1591 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1592 u32 val = 0;
1593
1594 val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
1595 val |= PN_MOD_TYPE(usb_endpoint_type(desc));
1596 val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
1597 usb3_write(usb3, val, USB3_PN_MOD);
1598}
1599
1600static u32 usb3_calc_ramarea(int ram_size)
1601{
1602 WARN_ON(ram_size > SZ_16K);
1603
1604 if (ram_size <= SZ_1K)
1605 return PN_RAMMAP_RAMAREA_1KB;
1606 else if (ram_size <= SZ_2K)
1607 return PN_RAMMAP_RAMAREA_2KB;
1608 else if (ram_size <= SZ_4K)
1609 return PN_RAMMAP_RAMAREA_4KB;
1610 else if (ram_size <= SZ_8K)
1611 return PN_RAMMAP_RAMAREA_8KB;
1612 else
1613 return PN_RAMMAP_RAMAREA_16KB;
1614}
1615
1616static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
1617 const struct usb_endpoint_descriptor *desc)
1618{
1619 return usb3_ep->rammap_val | PN_RAMMAP_MPKT(usb_endpoint_maxp(desc));
1620}
1621
1622static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
1623 const struct usb_endpoint_descriptor *desc)
1624{
1625 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1626 unsigned long flags;
1627
1628 usb3_ep->dir_in = usb_endpoint_dir_in(desc);
1629
1630 spin_lock_irqsave(&usb3->lock, flags);
1631 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1632 usb3_write_pn_mod(usb3_ep, desc);
1633 usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
1634 USB3_PN_RAMMAP);
1635 usb3_pn_con_clear(usb3);
1636 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1637 }
1638 spin_unlock_irqrestore(&usb3->lock, flags);
1639
1640 return 0;
1641}
1642
1643static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
1644{
1645 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1646 unsigned long flags;
1647
1648 usb3_ep->halt = false;
1649
1650 spin_lock_irqsave(&usb3->lock, flags);
1651 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1652 usb3_write(usb3, 0, USB3_PN_RAMMAP);
1653 usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
1654 }
1655 spin_unlock_irqrestore(&usb3->lock, flags);
1656
1657 return 0;
1658}
1659
1660/*------- usb_ep_ops -----------------------------------------------------*/
1661static int renesas_usb3_ep_enable(struct usb_ep *_ep,
1662 const struct usb_endpoint_descriptor *desc)
1663{
1664 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1665
1666 return usb3_enable_pipe_n(usb3_ep, desc);
1667}
1668
1669static int renesas_usb3_ep_disable(struct usb_ep *_ep)
1670{
1671 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1672 struct renesas_usb3_request *usb3_req;
1673
1674 do {
1675 usb3_req = usb3_get_request(usb3_ep);
1676 if (!usb3_req)
1677 break;
1678 usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
1679 } while (1);
1680
1681 return usb3_disable_pipe_n(usb3_ep);
1682}
1683
1684static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
1685{
1686 struct renesas_usb3_request *usb3_req;
1687
1688 usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
1689 if (!usb3_req)
1690 return NULL;
1691
1692 INIT_LIST_HEAD(&usb3_req->queue);
1693
1694 return &usb3_req->req;
1695}
1696
1697static void __renesas_usb3_ep_free_request(struct usb_request *_req)
1698{
1699 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1700
1701 kfree(usb3_req);
1702}
1703
1704static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
1705 gfp_t gfp_flags)
1706{
1707 return __renesas_usb3_ep_alloc_request(gfp_flags);
1708}
1709
1710static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
1711 struct usb_request *_req)
1712{
1713 __renesas_usb3_ep_free_request(_req);
1714}
1715
1716static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1717{
1718 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1719 struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
1720 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1721
1722 dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
1723 _req->length);
1724
1725 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
1726
1727 return 0;
1728}
1729
1730static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
1731{
1732 return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
1733}
1734
1735static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
1736{
1737 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1738
1739 usb3_ep->wedge = true;
1740 return usb3_set_halt(usb3_ep, true, false);
1741}
1742
1743static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
1744{
1745 struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
1746 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
1747 unsigned long flags;
1748
1749 if (usb3_ep->num) {
1750 spin_lock_irqsave(&usb3->lock, flags);
1751 if (!usb3_pn_change(usb3, usb3_ep->num)) {
1752 usb3_pn_con_clear(usb3);
1753 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
1754 }
1755 spin_unlock_irqrestore(&usb3->lock, flags);
1756 } else {
1757 usb3_p0_con_clear_buffer(usb3);
1758 }
1759}
1760
1761static struct usb_ep_ops renesas_usb3_ep_ops = {
1762 .enable = renesas_usb3_ep_enable,
1763 .disable = renesas_usb3_ep_disable,
1764
1765 .alloc_request = renesas_usb3_ep_alloc_request,
1766 .free_request = renesas_usb3_ep_free_request,
1767
1768 .queue = renesas_usb3_ep_queue,
1769 .dequeue = renesas_usb3_ep_dequeue,
1770
1771 .set_halt = renesas_usb3_ep_set_halt,
1772 .set_wedge = renesas_usb3_ep_set_wedge,
1773 .fifo_flush = renesas_usb3_ep_fifo_flush,
1774};
1775
1776/*------- usb_gadget_ops -------------------------------------------------*/
1777static int renesas_usb3_start(struct usb_gadget *gadget,
1778 struct usb_gadget_driver *driver)
1779{
1780 struct renesas_usb3 *usb3;
1781
1782 if (!driver || driver->max_speed < USB_SPEED_FULL ||
1783 !driver->setup)
1784 return -EINVAL;
1785
1786 usb3 = gadget_to_renesas_usb3(gadget);
1787
1788 /* hook up the driver */
1789 usb3->driver = driver;
1790
1791 renesas_usb3_init_controller(usb3);
1792
1793 return 0;
1794}
1795
1796static int renesas_usb3_stop(struct usb_gadget *gadget)
1797{
1798 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1799 unsigned long flags;
1800
1801 spin_lock_irqsave(&usb3->lock, flags);
1802 usb3->softconnect = false;
1803 usb3->gadget.speed = USB_SPEED_UNKNOWN;
1804 usb3->driver = NULL;
1805 renesas_usb3_stop_controller(usb3);
1806 spin_unlock_irqrestore(&usb3->lock, flags);
1807
1808 return 0;
1809}
1810
1811static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
1812{
1813 return -EOPNOTSUPP;
1814}
1815
1816static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
1817{
1818 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
1819
1820 usb3->softconnect = !!is_on;
1821
1822 return 0;
1823}
1824
1825static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
1826{
1827 gadget->is_selfpowered = !!is_self;
1828
1829 return 0;
1830}
1831
1832static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
1833 .get_frame = renesas_usb3_get_frame,
1834 .udc_start = renesas_usb3_start,
1835 .udc_stop = renesas_usb3_stop,
1836 .pullup = renesas_usb3_pullup,
1837 .set_selfpowered = renesas_usb3_set_selfpowered,
1838};
1839
1840/*------- platform_driver ------------------------------------------------*/
1841static int renesas_usb3_remove(struct platform_device *pdev)
1842{
1843 struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
1844
1845 pm_runtime_put(&pdev->dev);
1846 pm_runtime_disable(&pdev->dev);
1847
1848 usb_del_gadget_udc(&usb3->gadget);
1849
1850 __renesas_usb3_ep_free_request(usb3->ep0_req);
1851
1852 return 0;
1853}
1854
1855static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
1856 const struct renesas_usb3_priv *priv)
1857{
1858 struct renesas_usb3_ep *usb3_ep;
1859 int i;
1860
1861 /* calculate num_usb3_eps from renesas_usb3_priv */
1862 usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
1863 priv->ramsize_per_pipe + 1;
1864
1865 if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
1866 usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
1867
1868 usb3->usb3_ep = devm_kzalloc(dev, sizeof(*usb3_ep) * usb3->num_usb3_eps,
1869 GFP_KERNEL);
1870 if (!usb3->usb3_ep)
1871 return -ENOMEM;
1872
1873 dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
1874 /*
Masahiro Yamada4091fb92017-02-27 14:29:56 -08001875 * This driver prepares pipes as follows:
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09001876 * - odd pipes = IN pipe
1877 * - even pipes = OUT pipe (except pipe 0)
1878 */
1879 usb3_for_each_ep(usb3_ep, usb3, i) {
1880 snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
1881 usb3_ep->usb3 = usb3;
1882 usb3_ep->num = i;
1883 usb3_ep->ep.name = usb3_ep->ep_name;
1884 usb3_ep->ep.ops = &renesas_usb3_ep_ops;
1885 INIT_LIST_HEAD(&usb3_ep->queue);
1886 INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
1887 if (!i) {
1888 /* for control pipe */
1889 usb3->gadget.ep0 = &usb3_ep->ep;
1890 usb_ep_set_maxpacket_limit(&usb3_ep->ep,
1891 USB3_EP0_HSFS_MAX_PACKET_SIZE);
1892 usb3_ep->ep.caps.type_control = true;
1893 usb3_ep->ep.caps.dir_in = true;
1894 usb3_ep->ep.caps.dir_out = true;
1895 continue;
1896 }
1897
1898 /* for bulk or interrupt pipe */
1899 usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
1900 list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
1901 usb3_ep->ep.caps.type_bulk = true;
1902 usb3_ep->ep.caps.type_int = true;
1903 if (i & 1)
1904 usb3_ep->ep.caps.dir_in = true;
1905 else
1906 usb3_ep->ep.caps.dir_out = true;
1907 }
1908
1909 return 0;
1910}
1911
1912static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
1913 const struct renesas_usb3_priv *priv)
1914{
1915 struct renesas_usb3_ep *usb3_ep;
1916 int i;
1917 u32 ramif[2], basead[2]; /* index 0 = for IN pipes */
1918 u32 *cur_ramif, *cur_basead;
1919 u32 val;
1920
1921 memset(ramif, 0, sizeof(ramif));
1922 memset(basead, 0, sizeof(basead));
1923
1924 /*
Masahiro Yamada4091fb92017-02-27 14:29:56 -08001925 * This driver prepares pipes as follows:
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09001926 * - all pipes = the same size as "ramsize_per_pipe"
1927 * Please refer to the "Method of Specifying RAM Mapping"
1928 */
1929 usb3_for_each_ep(usb3_ep, usb3, i) {
1930 if (!i)
1931 continue; /* out of scope if ep num = 0 */
1932 if (usb3_ep->ep.caps.dir_in) {
1933 cur_ramif = &ramif[0];
1934 cur_basead = &basead[0];
1935 } else {
1936 cur_ramif = &ramif[1];
1937 cur_basead = &basead[1];
1938 }
1939
1940 if (*cur_basead > priv->ramsize_per_ramif)
1941 continue; /* out of memory for IN or OUT pipe */
1942
1943 /* calculate rammap_val */
1944 val = PN_RAMMAP_RAMIF(*cur_ramif);
1945 val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
1946 val |= PN_RAMMAP_BASEAD(*cur_basead);
1947 usb3_ep->rammap_val = val;
1948
1949 dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
1950 i, val, *cur_ramif, *cur_basead);
1951
1952 /* update current ramif */
1953 if (*cur_ramif + 1 == priv->num_ramif) {
1954 *cur_ramif = 0;
1955 *cur_basead += priv->ramsize_per_pipe;
1956 } else {
1957 (*cur_ramif)++;
1958 }
1959 }
1960}
1961
1962static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795 = {
1963 .ramsize_per_ramif = SZ_16K,
1964 .num_ramif = 2,
1965 .ramsize_per_pipe = SZ_4K,
1966 .workaround_for_vbus = true,
1967};
1968
1969static const struct of_device_id usb3_of_match[] = {
1970 {
1971 .compatible = "renesas,r8a7795-usb3-peri",
1972 .data = &renesas_usb3_priv_r8a7795,
1973 },
1974 { },
1975};
1976MODULE_DEVICE_TABLE(of, usb3_of_match);
1977
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +09001978static const unsigned int renesas_usb3_cable[] = {
1979 EXTCON_USB,
1980 EXTCON_USB_HOST,
1981 EXTCON_NONE,
1982};
1983
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09001984static int renesas_usb3_probe(struct platform_device *pdev)
1985{
1986 struct renesas_usb3 *usb3;
1987 struct resource *res;
1988 const struct of_device_id *match;
1989 int irq, ret;
1990 const struct renesas_usb3_priv *priv;
1991
1992 match = of_match_node(usb3_of_match, pdev->dev.of_node);
1993 if (!match)
1994 return -ENODEV;
1995 priv = match->data;
1996
1997 irq = platform_get_irq(pdev, 0);
1998 if (irq < 0)
1999 return -ENODEV;
2000
2001 usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
2002 if (!usb3)
2003 return -ENOMEM;
2004
2005 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2006 usb3->reg = devm_ioremap_resource(&pdev->dev, res);
2007 if (IS_ERR(usb3->reg))
2008 return PTR_ERR(usb3->reg);
2009
2010 platform_set_drvdata(pdev, usb3);
2011 spin_lock_init(&usb3->lock);
2012
2013 usb3->gadget.ops = &renesas_usb3_gadget_ops;
2014 usb3->gadget.name = udc_name;
2015 usb3->gadget.max_speed = USB_SPEED_SUPER;
2016 INIT_LIST_HEAD(&usb3->gadget.ep_list);
2017 ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
2018 if (ret < 0)
2019 return ret;
2020 renesas_usb3_init_ram(usb3, &pdev->dev, priv);
2021
2022 ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
2023 dev_name(&pdev->dev), usb3);
2024 if (ret < 0)
2025 return ret;
2026
Yoshihiro Shimoda3b68e7c2017-03-30 11:16:05 +09002027 INIT_WORK(&usb3->extcon_work, renesas_usb3_extcon_work);
2028 usb3->extcon = devm_extcon_dev_allocate(&pdev->dev, renesas_usb3_cable);
2029 if (IS_ERR(usb3->extcon))
2030 return PTR_ERR(usb3->extcon);
2031
2032 ret = devm_extcon_dev_register(&pdev->dev, usb3->extcon);
2033 if (ret < 0) {
2034 dev_err(&pdev->dev, "Failed to register extcon\n");
2035 return ret;
2036 }
2037
Yoshihiro Shimoda746bfe62015-12-21 18:40:04 +09002038 /* for ep0 handling */
2039 usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
2040 if (!usb3->ep0_req)
2041 return -ENOMEM;
2042
2043 ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
2044 if (ret < 0)
2045 goto err_add_udc;
2046
2047 usb3->workaround_for_vbus = priv->workaround_for_vbus;
2048
2049 pm_runtime_enable(&pdev->dev);
2050 pm_runtime_get_sync(&pdev->dev);
2051
2052 dev_info(&pdev->dev, "probed\n");
2053
2054 return 0;
2055
2056err_add_udc:
2057 __renesas_usb3_ep_free_request(usb3->ep0_req);
2058
2059 return ret;
2060}
2061
2062static struct platform_driver renesas_usb3_driver = {
2063 .probe = renesas_usb3_probe,
2064 .remove = renesas_usb3_remove,
2065 .driver = {
2066 .name = (char *)udc_name,
2067 .of_match_table = of_match_ptr(usb3_of_match),
2068 },
2069};
2070module_platform_driver(renesas_usb3_driver);
2071
2072MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
2073MODULE_LICENSE("GPL v2");
2074MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
2075MODULE_ALIAS("platform:renesas_usb3");