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