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