1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas USBF USB Function driver
5 * Copyright 2022 Schneider Electric
9 #include <linux/delay.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/kernel.h>
14 #include <linux/kfifo.h>
15 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_platform.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/types.h>
21 #include <linux/usb/composite.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/role.h>
25 #define USBF_NUM_ENDPOINTS 16
26 #define USBF_EP0_MAX_PCKT_SIZE 64
29 #define USBF_REG_USB_CONTROL 0x000
30 #define USBF_USB_PUE2 BIT(2)
31 #define USBF_USB_CONNECTB BIT(3)
32 #define USBF_USB_DEFAULT BIT(4)
33 #define USBF_USB_CONF BIT(5)
34 #define USBF_USB_SUSPEND BIT(6)
35 #define USBF_USB_RSUM_IN BIT(7)
36 #define USBF_USB_SOF_RCV BIT(8)
37 #define USBF_USB_FORCEFS BIT(9)
38 #define USBF_USB_INT_SEL BIT(10)
39 #define USBF_USB_SOF_CLK_MODE BIT(11)
41 #define USBF_REG_USB_STATUS 0x004
42 #define USBF_USB_RSUM_OUT BIT(1)
43 #define USBF_USB_SPND_OUT BIT(2)
44 #define USBF_USB_USB_RST BIT(3)
45 #define USBF_USB_DEFAULT_ST BIT(4)
46 #define USBF_USB_CONF_ST BIT(5)
47 #define USBF_USB_SPEED_MODE BIT(6)
48 #define USBF_USB_SOF_DELAY_STATUS BIT(31)
50 #define USBF_REG_USB_ADDRESS 0x008
51 #define USBF_USB_SOF_STATUS BIT(15)
52 #define USBF_USB_SET_USB_ADDR(_a) ((_a) << 16)
53 #define USBF_USB_GET_FRAME(_r) ((_r) & 0x7FF)
55 #define USBF_REG_SETUP_DATA0 0x018
56 #define USBF_REG_SETUP_DATA1 0x01C
57 #define USBF_REG_USB_INT_STA 0x020
58 #define USBF_USB_RSUM_INT BIT(1)
59 #define USBF_USB_SPND_INT BIT(2)
60 #define USBF_USB_USB_RST_INT BIT(3)
61 #define USBF_USB_SOF_INT BIT(4)
62 #define USBF_USB_SOF_ERROR_INT BIT(5)
63 #define USBF_USB_SPEED_MODE_INT BIT(6)
64 #define USBF_USB_EPN_INT(_n) (BIT(8) << (_n)) /* n=0..15 */
66 #define USBF_REG_USB_INT_ENA 0x024
67 #define USBF_USB_RSUM_EN BIT(1)
68 #define USBF_USB_SPND_EN BIT(2)
69 #define USBF_USB_USB_RST_EN BIT(3)
70 #define USBF_USB_SOF_EN BIT(4)
71 #define USBF_USB_SOF_ERROR_EN BIT(5)
72 #define USBF_USB_SPEED_MODE_EN BIT(6)
73 #define USBF_USB_EPN_EN(_n) (BIT(8) << (_n)) /* n=0..15 */
75 #define USBF_BASE_EP0 0x028
76 /* EP0 registers offsets from Base + USBF_BASE_EP0 (EP0 regs area) */
77 #define USBF_REG_EP0_CONTROL 0x00
78 #define USBF_EP0_ONAK BIT(0)
79 #define USBF_EP0_INAK BIT(1)
80 #define USBF_EP0_STL BIT(2)
81 #define USBF_EP0_PERR_NAK_CLR BIT(3)
82 #define USBF_EP0_INAK_EN BIT(4)
83 #define USBF_EP0_DW_MASK (0x3 << 5)
84 #define USBF_EP0_DW(_s) ((_s) << 5)
85 #define USBF_EP0_DEND BIT(7)
86 #define USBF_EP0_BCLR BIT(8)
87 #define USBF_EP0_PIDCLR BIT(9)
88 #define USBF_EP0_AUTO BIT(16)
89 #define USBF_EP0_OVERSEL BIT(17)
90 #define USBF_EP0_STGSEL BIT(18)
92 #define USBF_REG_EP0_STATUS 0x04
93 #define USBF_EP0_SETUP_INT BIT(0)
94 #define USBF_EP0_STG_START_INT BIT(1)
95 #define USBF_EP0_STG_END_INT BIT(2)
96 #define USBF_EP0_STALL_INT BIT(3)
97 #define USBF_EP0_IN_INT BIT(4)
98 #define USBF_EP0_OUT_INT BIT(5)
99 #define USBF_EP0_OUT_OR_INT BIT(6)
100 #define USBF_EP0_OUT_NULL_INT BIT(7)
101 #define USBF_EP0_IN_EMPTY BIT(8)
102 #define USBF_EP0_IN_FULL BIT(9)
103 #define USBF_EP0_IN_DATA BIT(10)
104 #define USBF_EP0_IN_NAK_INT BIT(11)
105 #define USBF_EP0_OUT_EMPTY BIT(12)
106 #define USBF_EP0_OUT_FULL BIT(13)
107 #define USBF_EP0_OUT_NULL BIT(14)
108 #define USBF_EP0_OUT_NAK_INT BIT(15)
109 #define USBF_EP0_PERR_NAK_INT BIT(16)
110 #define USBF_EP0_PERR_NAK BIT(17)
111 #define USBF_EP0_PID BIT(18)
113 #define USBF_REG_EP0_INT_ENA 0x08
114 #define USBF_EP0_SETUP_EN BIT(0)
115 #define USBF_EP0_STG_START_EN BIT(1)
116 #define USBF_EP0_STG_END_EN BIT(2)
117 #define USBF_EP0_STALL_EN BIT(3)
118 #define USBF_EP0_IN_EN BIT(4)
119 #define USBF_EP0_OUT_EN BIT(5)
120 #define USBF_EP0_OUT_OR_EN BIT(6)
121 #define USBF_EP0_OUT_NULL_EN BIT(7)
122 #define USBF_EP0_IN_NAK_EN BIT(11)
123 #define USBF_EP0_OUT_NAK_EN BIT(15)
124 #define USBF_EP0_PERR_NAK_EN BIT(16)
126 #define USBF_REG_EP0_LENGTH 0x0C
127 #define USBF_EP0_LDATA (0x7FF << 0)
128 #define USBF_REG_EP0_READ 0x10
129 #define USBF_REG_EP0_WRITE 0x14
131 #define USBF_BASE_EPN(_n) (0x040 + (_n) * 0x020)
132 /* EPn registers offsets from Base + USBF_BASE_EPN(n-1). n=1..15 */
133 #define USBF_REG_EPN_CONTROL 0x000
134 #define USBF_EPN_ONAK BIT(0)
135 #define USBF_EPN_OSTL BIT(2)
136 #define USBF_EPN_ISTL BIT(3)
137 #define USBF_EPN_OSTL_EN BIT(4)
138 #define USBF_EPN_DW_MASK (0x3 << 5)
139 #define USBF_EPN_DW(_s) ((_s) << 5)
140 #define USBF_EPN_DEND BIT(7)
141 #define USBF_EPN_CBCLR BIT(8)
142 #define USBF_EPN_BCLR BIT(9)
143 #define USBF_EPN_OPIDCLR BIT(10)
144 #define USBF_EPN_IPIDCLR BIT(11)
145 #define USBF_EPN_AUTO BIT(16)
146 #define USBF_EPN_OVERSEL BIT(17)
147 #define USBF_EPN_MODE_MASK (0x3 << 24)
148 #define USBF_EPN_MODE_BULK (0x0 << 24)
149 #define USBF_EPN_MODE_INTR (0x1 << 24)
150 #define USBF_EPN_MODE_ISO (0x2 << 24)
151 #define USBF_EPN_DIR0 BIT(26)
152 #define USBF_EPN_BUF_TYPE_DOUBLE BIT(30)
153 #define USBF_EPN_EN BIT(31)
155 #define USBF_REG_EPN_STATUS 0x004
156 #define USBF_EPN_IN_EMPTY BIT(0)
157 #define USBF_EPN_IN_FULL BIT(1)
158 #define USBF_EPN_IN_DATA BIT(2)
159 #define USBF_EPN_IN_INT BIT(3)
160 #define USBF_EPN_IN_STALL_INT BIT(4)
161 #define USBF_EPN_IN_NAK_ERR_INT BIT(5)
162 #define USBF_EPN_IN_END_INT BIT(7)
163 #define USBF_EPN_IPID BIT(10)
164 #define USBF_EPN_OUT_EMPTY BIT(16)
165 #define USBF_EPN_OUT_FULL BIT(17)
166 #define USBF_EPN_OUT_NULL_INT BIT(18)
167 #define USBF_EPN_OUT_INT BIT(19)
168 #define USBF_EPN_OUT_STALL_INT BIT(20)
169 #define USBF_EPN_OUT_NAK_ERR_INT BIT(21)
170 #define USBF_EPN_OUT_OR_INT BIT(22)
171 #define USBF_EPN_OUT_END_INT BIT(23)
172 #define USBF_EPN_ISO_CRC BIT(24)
173 #define USBF_EPN_ISO_OR BIT(26)
174 #define USBF_EPN_OUT_NOTKN BIT(27)
175 #define USBF_EPN_ISO_OPID BIT(28)
176 #define USBF_EPN_ISO_PIDERR BIT(29)
178 #define USBF_REG_EPN_INT_ENA 0x008
179 #define USBF_EPN_IN_EN BIT(3)
180 #define USBF_EPN_IN_STALL_EN BIT(4)
181 #define USBF_EPN_IN_NAK_ERR_EN BIT(5)
182 #define USBF_EPN_IN_END_EN BIT(7)
183 #define USBF_EPN_OUT_NULL_EN BIT(18)
184 #define USBF_EPN_OUT_EN BIT(19)
185 #define USBF_EPN_OUT_STALL_EN BIT(20)
186 #define USBF_EPN_OUT_NAK_ERR_EN BIT(21)
187 #define USBF_EPN_OUT_OR_EN BIT(22)
188 #define USBF_EPN_OUT_END_EN BIT(23)
190 #define USBF_REG_EPN_DMA_CTRL 0x00C
191 #define USBF_EPN_DMAMODE0 BIT(0)
192 #define USBF_EPN_DMA_EN BIT(4)
193 #define USBF_EPN_STOP_SET BIT(8)
194 #define USBF_EPN_BURST_SET BIT(9)
195 #define USBF_EPN_DEND_SET BIT(10)
196 #define USBF_EPN_STOP_MODE BIT(11)
198 #define USBF_REG_EPN_PCKT_ADRS 0x010
199 #define USBF_EPN_MPKT(_l) ((_l) << 0)
200 #define USBF_EPN_BASEAD(_a) ((_a) << 16)
202 #define USBF_REG_EPN_LEN_DCNT 0x014
203 #define USBF_EPN_GET_LDATA(_r) ((_r) & 0x7FF)
204 #define USBF_EPN_SET_DMACNT(_c) ((_c) << 16)
205 #define USBF_EPN_GET_DMACNT(_r) (((_r) >> 16) & 0x1ff)
207 #define USBF_REG_EPN_READ 0x018
208 #define USBF_REG_EPN_WRITE 0x01C
210 /* AHB-EPC Bridge registers */
211 #define USBF_REG_AHBSCTR 0x1000
212 #define USBF_REG_AHBMCTR 0x1004
213 #define USBF_SYS_WBURST_TYPE BIT(2)
214 #define USBF_SYS_ARBITER_CTR BIT(31)
216 #define USBF_REG_AHBBINT 0x1008
217 #define USBF_SYS_ERR_MASTER (0x0F << 0)
218 #define USBF_SYS_SBUS_ERRINT0 BIT(4)
219 #define USBF_SYS_SBUS_ERRINT1 BIT(5)
220 #define USBF_SYS_MBUS_ERRINT BIT(6)
221 #define USBF_SYS_VBUS_INT BIT(13)
222 #define USBF_SYS_DMA_ENDINT_EPN(_n) (BIT(16) << (_n)) /* _n=1..15 */
224 #define USBF_REG_AHBBINTEN 0x100C
225 #define USBF_SYS_SBUS_ERRINT0EN BIT(4)
226 #define USBF_SYS_SBUS_ERRINT1EN BIT(5)
227 #define USBF_SYS_MBUS_ERRINTEN BIT(6)
228 #define USBF_SYS_VBUS_INTEN BIT(13)
229 #define USBF_SYS_DMA_ENDINTEN_EPN(_n) (BIT(16) << (_n)) /* _n=1..15 */
231 #define USBF_REG_EPCTR 0x1010
232 #define USBF_SYS_EPC_RST BIT(0)
233 #define USBF_SYS_PLL_RST BIT(2)
234 #define USBF_SYS_PLL_LOCK BIT(4)
235 #define USBF_SYS_PLL_RESUME BIT(5)
236 #define USBF_SYS_VBUS_LEVEL BIT(8)
237 #define USBF_SYS_DIRPD BIT(12)
239 #define USBF_REG_USBSSVER 0x1020
240 #define USBF_REG_USBSSCONF 0x1024
241 #define USBF_SYS_DMA_AVAILABLE(_n) (BIT(0) << (_n)) /* _n=0..15 */
242 #define USBF_SYS_EP_AVAILABLE(_n) (BIT(16) << (_n)) /* _n=0..15 */
244 #define USBF_BASE_DMA_EPN(_n) (0x1110 + (_n) * 0x010)
245 /* EPn DMA registers offsets from Base USBF_BASE_DMA_EPN(n-1). n=1..15*/
246 #define USBF_REG_DMA_EPN_DCR1 0x00
247 #define USBF_SYS_EPN_REQEN BIT(0)
248 #define USBF_SYS_EPN_DIR0 BIT(1)
249 #define USBF_SYS_EPN_SET_DMACNT(_c) ((_c) << 16)
250 #define USBF_SYS_EPN_GET_DMACNT(_r) (((_r) >> 16) & 0x0FF)
252 #define USBF_REG_DMA_EPN_DCR2 0x04
253 #define USBF_SYS_EPN_MPKT(_s) ((_s) << 0)
254 #define USBF_SYS_EPN_LMPKT(_l) ((_l) << 16)
256 #define USBF_REG_DMA_EPN_TADR 0x08
260 struct usb_request req;
261 struct list_head queue;
262 unsigned int is_zero_sent : 1;
263 unsigned int is_mapped : 1;
269 USBF_XFER_WAIT_DMA_SHORT,
270 USBF_XFER_WAIT_BRIDGE,
279 struct list_head queue;
280 unsigned int is_processing : 1;
281 unsigned int is_in : 1;
282 struct usbf_udc *udc;
284 void __iomem *dma_regs;
286 unsigned int disabled : 1;
287 unsigned int is_wedged : 1;
288 unsigned int delayed_status : 1;
290 void (*bridge_on_dma_end)(struct usbf_ep *ep);
297 EP0_OUT_STATUS_START_PHASE,
298 EP0_OUT_STATUS_PHASE,
299 EP0_OUT_STATUS_END_PHASE,
300 EP0_IN_STATUS_START_PHASE,
302 EP0_IN_STATUS_END_PHASE,
306 struct usb_gadget gadget;
307 struct usb_gadget_driver *driver;
311 bool is_remote_wakeup;
312 bool is_usb_suspended;
313 struct usbf_ep ep[USBF_NUM_ENDPOINTS];
314 /* for EP0 control messages */
315 enum usbf_ep0state ep0state;
316 struct usbf_req setup_reply;
317 u8 ep0_buf[USBF_EP0_MAX_PCKT_SIZE];
320 struct usbf_ep_info {
322 struct usb_ep_caps caps;
324 unsigned int is_double : 1;
328 #define USBF_SINGLE_BUFFER 0
329 #define USBF_DOUBLE_BUFFER 1
330 #define USBF_EP_INFO(_name, _caps, _base_addr, _is_double, _maxpacket_limit) \
334 .base_addr = _base_addr, \
335 .is_double = _is_double, \
336 .maxpacket_limit = _maxpacket_limit, \
339 /* This table is computed from the recommended values provided in the SOC
340 * datasheet. The buffer type (single/double) and the endpoint type cannot
341 * be changed. The mapping in internal RAM (base_addr and number of words)
342 * for each endpoints depends on the max packet size and the buffer type.
344 static const struct usbf_ep_info usbf_ep_info[USBF_NUM_ENDPOINTS] = {
345 /* ep0: buf @0x0000 64 bytes, fixed 32 words */
346 [0] = USBF_EP_INFO("ep0-ctrl",
347 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
348 USB_EP_CAPS_DIR_ALL),
349 0x0000, USBF_SINGLE_BUFFER, USBF_EP0_MAX_PCKT_SIZE),
350 /* ep1: buf @0x0020, 2 buffers 512 bytes -> (512 * 2 / 4) words */
351 [1] = USBF_EP_INFO("ep1-bulk",
352 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
353 USB_EP_CAPS_DIR_ALL),
354 0x0020, USBF_DOUBLE_BUFFER, 512),
355 /* ep2: buf @0x0120, 2 buffers 512 bytes -> (512 * 2 / 4) words */
356 [2] = USBF_EP_INFO("ep2-bulk",
357 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
358 USB_EP_CAPS_DIR_ALL),
359 0x0120, USBF_DOUBLE_BUFFER, 512),
360 /* ep3: buf @0x0220, 1 buffer 512 bytes -> (512 * 2 / 4) words */
361 [3] = USBF_EP_INFO("ep3-bulk",
362 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
363 USB_EP_CAPS_DIR_ALL),
364 0x0220, USBF_SINGLE_BUFFER, 512),
365 /* ep4: buf @0x02A0, 1 buffer 512 bytes -> (512 * 1 / 4) words */
366 [4] = USBF_EP_INFO("ep4-bulk",
367 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
368 USB_EP_CAPS_DIR_ALL),
369 0x02A0, USBF_SINGLE_BUFFER, 512),
370 /* ep5: buf @0x0320, 1 buffer 512 bytes -> (512 * 2 / 4) words */
371 [5] = USBF_EP_INFO("ep5-bulk",
372 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
373 USB_EP_CAPS_DIR_ALL),
374 0x0320, USBF_SINGLE_BUFFER, 512),
375 /* ep6: buf @0x03A0, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
376 [6] = USBF_EP_INFO("ep6-int",
377 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
378 USB_EP_CAPS_DIR_ALL),
379 0x03A0, USBF_SINGLE_BUFFER, 1024),
380 /* ep7: buf @0x04A0, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
381 [7] = USBF_EP_INFO("ep7-int",
382 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
383 USB_EP_CAPS_DIR_ALL),
384 0x04A0, USBF_SINGLE_BUFFER, 1024),
385 /* ep8: buf @0x0520, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
386 [8] = USBF_EP_INFO("ep8-int",
387 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
388 USB_EP_CAPS_DIR_ALL),
389 0x0520, USBF_SINGLE_BUFFER, 1024),
390 /* ep9: buf @0x0620, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
391 [9] = USBF_EP_INFO("ep9-int",
392 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
393 USB_EP_CAPS_DIR_ALL),
394 0x0620, USBF_SINGLE_BUFFER, 1024),
395 /* ep10: buf @0x0720, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
396 [10] = USBF_EP_INFO("ep10-iso",
397 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
398 USB_EP_CAPS_DIR_ALL),
399 0x0720, USBF_DOUBLE_BUFFER, 1024),
400 /* ep11: buf @0x0920, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
401 [11] = USBF_EP_INFO("ep11-iso",
402 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
403 USB_EP_CAPS_DIR_ALL),
404 0x0920, USBF_DOUBLE_BUFFER, 1024),
405 /* ep12: buf @0x0B20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
406 [12] = USBF_EP_INFO("ep12-iso",
407 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
408 USB_EP_CAPS_DIR_ALL),
409 0x0B20, USBF_DOUBLE_BUFFER, 1024),
410 /* ep13: buf @0x0D20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
411 [13] = USBF_EP_INFO("ep13-iso",
412 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
413 USB_EP_CAPS_DIR_ALL),
414 0x0D20, USBF_DOUBLE_BUFFER, 1024),
415 /* ep14: buf @0x0F20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
416 [14] = USBF_EP_INFO("ep14-iso",
417 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
418 USB_EP_CAPS_DIR_ALL),
419 0x0F20, USBF_DOUBLE_BUFFER, 1024),
420 /* ep15: buf @0x1120, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
421 [15] = USBF_EP_INFO("ep15-iso",
422 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
423 USB_EP_CAPS_DIR_ALL),
424 0x1120, USBF_DOUBLE_BUFFER, 1024),
427 static inline u32 usbf_reg_readl(struct usbf_udc *udc, uint offset)
429 return readl(udc->regs + offset);
432 static inline void usbf_reg_writel(struct usbf_udc *udc, uint offset, u32 val)
434 writel(val, udc->regs + offset);
437 static inline void usbf_reg_bitset(struct usbf_udc *udc, uint offset, u32 set)
441 tmp = usbf_reg_readl(udc, offset);
443 usbf_reg_writel(udc, offset, tmp);
446 static inline void usbf_reg_bitclr(struct usbf_udc *udc, uint offset, u32 clr)
450 tmp = usbf_reg_readl(udc, offset);
452 usbf_reg_writel(udc, offset, tmp);
455 static inline void usbf_reg_clrset(struct usbf_udc *udc, uint offset,
460 tmp = usbf_reg_readl(udc, offset);
463 usbf_reg_writel(udc, offset, tmp);
466 static inline u32 usbf_ep_reg_readl(struct usbf_ep *ep, uint offset)
468 return readl(ep->regs + offset);
471 static inline void usbf_ep_reg_read_rep(struct usbf_ep *ep, uint offset,
472 void *dst, uint count)
474 readsl(ep->regs + offset, dst, count);
477 static inline void usbf_ep_reg_writel(struct usbf_ep *ep, uint offset, u32 val)
479 writel(val, ep->regs + offset);
482 static inline void usbf_ep_reg_write_rep(struct usbf_ep *ep, uint offset,
483 const void *src, uint count)
485 writesl(ep->regs + offset, src, count);
488 static inline void usbf_ep_reg_bitset(struct usbf_ep *ep, uint offset, u32 set)
492 tmp = usbf_ep_reg_readl(ep, offset);
494 usbf_ep_reg_writel(ep, offset, tmp);
497 static inline void usbf_ep_reg_bitclr(struct usbf_ep *ep, uint offset, u32 clr)
501 tmp = usbf_ep_reg_readl(ep, offset);
503 usbf_ep_reg_writel(ep, offset, tmp);
506 static inline void usbf_ep_reg_clrset(struct usbf_ep *ep, uint offset,
511 tmp = usbf_ep_reg_readl(ep, offset);
514 usbf_ep_reg_writel(ep, offset, tmp);
517 static inline u32 usbf_ep_dma_reg_readl(struct usbf_ep *ep, uint offset)
519 return readl(ep->dma_regs + offset);
522 static inline void usbf_ep_dma_reg_writel(struct usbf_ep *ep, uint offset,
525 writel(val, ep->dma_regs + offset);
528 static inline void usbf_ep_dma_reg_bitset(struct usbf_ep *ep, uint offset,
533 tmp = usbf_ep_dma_reg_readl(ep, offset);
535 usbf_ep_dma_reg_writel(ep, offset, tmp);
538 static inline void usbf_ep_dma_reg_bitclr(struct usbf_ep *ep, uint offset,
543 tmp = usbf_ep_dma_reg_readl(ep, offset);
545 usbf_ep_dma_reg_writel(ep, offset, tmp);
548 static void usbf_ep0_send_null(struct usbf_ep *ep0, bool is_data1)
554 set |= USBF_EP0_PIDCLR;
556 usbf_ep_reg_bitset(ep0, USBF_REG_EP0_CONTROL, set);
559 static int usbf_ep0_pio_in(struct usbf_ep *ep0, struct usbf_req *req)
567 left = req->req.length - req->req.actual;
570 if (!req->is_zero_sent) {
571 if (req->req.length == 0) {
572 dev_dbg(ep0->udc->dev, "ep0 send null\n");
573 usbf_ep0_send_null(ep0, false);
574 req->is_zero_sent = 1;
577 if ((req->req.actual % ep0->ep.maxpacket) == 0) {
579 dev_dbg(ep0->udc->dev, "ep0 send null\n");
580 usbf_ep0_send_null(ep0, false);
581 req->is_zero_sent = 1;
589 if (left > ep0->ep.maxpacket)
590 left = ep0->ep.maxpacket;
593 buf += req->req.actual;
595 nb = left / sizeof(u32);
597 usbf_ep_reg_write_rep(ep0, USBF_REG_EP0_WRITE, buf, nb);
598 buf += (nb * sizeof(u32));
599 req->req.actual += (nb * sizeof(u32));
600 left -= (nb * sizeof(u32));
602 ctrl = usbf_ep_reg_readl(ep0, USBF_REG_EP0_CONTROL);
603 ctrl &= ~USBF_EP0_DW_MASK;
605 memcpy(&last, buf, left);
606 usbf_ep_reg_writel(ep0, USBF_REG_EP0_WRITE, last);
607 ctrl |= USBF_EP0_DW(left);
608 req->req.actual += left;
610 usbf_ep_reg_writel(ep0, USBF_REG_EP0_CONTROL, ctrl | USBF_EP0_DEND);
612 dev_dbg(ep0->udc->dev, "ep0 send %u/%u\n",
613 req->req.actual, req->req.length);
618 static int usbf_ep0_pio_out(struct usbf_ep *ep0, struct usbf_req *req)
628 if (ep0->status & USBF_EP0_OUT_INT) {
629 recv = usbf_ep_reg_readl(ep0, USBF_REG_EP0_LENGTH) & USBF_EP0_LDATA;
633 buf += req->req.actual;
635 left = req->req.length - req->req.actual;
637 dev_dbg(ep0->udc->dev, "ep0 recv %u, left %u\n", count, left);
639 if (left > ep0->ep.maxpacket)
640 left = ep0->ep.maxpacket;
643 req_status = -EOVERFLOW;
648 nb = count / sizeof(u32);
650 usbf_ep_reg_read_rep(ep0, USBF_REG_EP0_READ,
652 buf += (nb * sizeof(u32));
653 req->req.actual += (nb * sizeof(u32));
654 count -= (nb * sizeof(u32));
657 last = usbf_ep_reg_readl(ep0, USBF_REG_EP0_READ);
658 memcpy(buf, &last, count);
659 req->req.actual += count;
662 dev_dbg(ep0->udc->dev, "ep0 recv %u/%u\n",
663 req->req.actual, req->req.length);
666 dev_dbg(ep0->udc->dev, "ep0 req.status=%d\n", req_status);
667 req->req.status = req_status;
671 if (recv < ep0->ep.maxpacket) {
672 dev_dbg(ep0->udc->dev, "ep0 short packet\n");
673 /* This is a short packet -> It is the end */
678 /* The Data stage of a control transfer from an endpoint to the
679 * host is complete when the endpoint does one of the following:
680 * - Has transferred exactly the expected amount of data
681 * - Transfers a packet with a payload size less than
682 * wMaxPacketSize or transfers a zero-length packet
684 if (req->req.actual == req->req.length) {
690 if (ep0->status & USBF_EP0_OUT_NULL_INT) {
691 /* NULL packet received */
692 dev_dbg(ep0->udc->dev, "ep0 null packet\n");
693 if (req->req.actual != req->req.length) {
694 req->req.status = req->req.short_not_ok ?
705 static void usbf_ep0_fifo_flush(struct usbf_ep *ep0)
710 usbf_ep_reg_bitset(ep0, USBF_REG_EP0_CONTROL, USBF_EP0_BCLR);
712 ret = readl_poll_timeout_atomic(ep0->regs + USBF_REG_EP0_STATUS, sts,
713 (sts & (USBF_EP0_IN_DATA | USBF_EP0_IN_EMPTY)) == USBF_EP0_IN_EMPTY,
716 dev_err(ep0->udc->dev, "ep0 flush fifo timed out\n");
720 static void usbf_epn_send_null(struct usbf_ep *epn)
722 usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL, USBF_EPN_DEND);
725 static void usbf_epn_send_residue(struct usbf_ep *epn, const void *buf,
730 memcpy(&tmp, buf, size);
731 usbf_ep_reg_writel(epn, USBF_REG_EPN_WRITE, tmp);
733 usbf_ep_reg_clrset(epn, USBF_REG_EPN_CONTROL,
735 USBF_EPN_DW(size) | USBF_EPN_DEND);
738 static int usbf_epn_pio_in(struct usbf_ep *epn, struct usbf_req *req)
744 left = req->req.length - req->req.actual;
747 if (!req->is_zero_sent) {
748 if (req->req.length == 0) {
749 dev_dbg(epn->udc->dev, "ep%u send_null\n", epn->id);
750 usbf_epn_send_null(epn);
751 req->is_zero_sent = 1;
754 if ((req->req.actual % epn->ep.maxpacket) == 0) {
756 dev_dbg(epn->udc->dev, "ep%u send_null\n",
758 usbf_epn_send_null(epn);
759 req->is_zero_sent = 1;
767 if (left > epn->ep.maxpacket)
768 left = epn->ep.maxpacket;
771 buf += req->req.actual;
773 nb = left / sizeof(u32);
775 usbf_ep_reg_write_rep(epn, USBF_REG_EPN_WRITE, buf, nb);
776 buf += (nb * sizeof(u32));
777 req->req.actual += (nb * sizeof(u32));
778 left -= (nb * sizeof(u32));
782 usbf_epn_send_residue(epn, buf, left);
783 req->req.actual += left;
785 usbf_ep_reg_clrset(epn, USBF_REG_EPN_CONTROL,
790 dev_dbg(epn->udc->dev, "ep%u send %u/%u\n", epn->id, req->req.actual,
796 static void usbf_epn_enable_in_end_int(struct usbf_ep *epn)
798 usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_IN_END_EN);
801 static int usbf_epn_dma_in(struct usbf_ep *epn, struct usbf_req *req)
808 if (!IS_ALIGNED((uintptr_t)req->req.buf, 4)) {
809 dev_dbg(epn->udc->dev, "ep%u buf unaligned -> fallback pio\n",
811 return usbf_epn_pio_in(epn, req);
814 left = req->req.length - req->req.actual;
816 switch (req->xfer_step) {
818 case USBF_XFER_START:
820 dev_dbg(epn->udc->dev, "ep%u send null\n", epn->id);
821 usbf_epn_send_null(epn);
822 req->xfer_step = USBF_XFER_WAIT_END;
826 dev_dbg(epn->udc->dev, "ep%u send residue %u\n", epn->id,
828 usbf_epn_send_residue(epn,
829 req->req.buf + req->req.actual, left);
830 req->req.actual += left;
831 req->xfer_step = USBF_XFER_WAIT_END;
835 ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 1);
837 dev_err(epn->udc->dev, "usb_gadget_map_request failed (%d)\n",
843 npkt = DIV_ROUND_UP(left, epn->ep.maxpacket);
844 lastpkt = (left % epn->ep.maxpacket);
846 lastpkt = epn->ep.maxpacket;
847 lastpkt &= ~0x3; /* DMA is done on 32bit units */
849 usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR2,
850 USBF_SYS_EPN_MPKT(epn->ep.maxpacket) | USBF_SYS_EPN_LMPKT(lastpkt));
851 usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_TADR,
853 usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
854 USBF_SYS_EPN_SET_DMACNT(npkt));
855 usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
858 usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT, USBF_EPN_SET_DMACNT(npkt));
860 usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);
862 /* The end of DMA transfer at the USBF level needs to be handle
863 * after the detection of the end of DMA transfer at the brige
865 * To force this sequence, EPN_IN_END_EN will be set by the
866 * detection of the end of transfer at bridge level (ie. bridge
869 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
870 USBF_EPN_IN_EN | USBF_EPN_IN_END_EN);
871 epn->bridge_on_dma_end = usbf_epn_enable_in_end_int;
873 /* Clear any pending IN_END interrupt */
874 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(u32)USBF_EPN_IN_END_INT);
876 usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
877 USBF_EPN_BURST_SET | USBF_EPN_DMAMODE0);
878 usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
881 req->dma_size = (npkt - 1) * epn->ep.maxpacket + lastpkt;
883 dev_dbg(epn->udc->dev, "ep%u dma xfer %zu\n", epn->id,
886 req->xfer_step = USBF_XFER_WAIT_DMA;
889 case USBF_XFER_WAIT_DMA:
890 if (!(epn->status & USBF_EPN_IN_END_INT)) {
891 dev_dbg(epn->udc->dev, "ep%u dma not done\n", epn->id);
894 dev_dbg(epn->udc->dev, "ep%u dma done\n", epn->id);
896 usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 1);
899 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);
901 usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
905 req->req.actual += req->dma_size;
907 left = req->req.length - req->req.actual;
909 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(u32)USBF_EPN_IN_INT);
911 dev_dbg(epn->udc->dev, "ep%u send residue %u\n", epn->id,
913 usbf_epn_send_residue(epn,
914 req->req.buf + req->req.actual, left);
915 req->req.actual += left;
916 req->xfer_step = USBF_XFER_WAIT_END;
920 if (req->req.actual % epn->ep.maxpacket) {
921 /* last packet was a short packet. Tell the hardware to
924 dev_dbg(epn->udc->dev, "ep%u send short\n", epn->id);
925 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
926 ~(u32)USBF_EPN_IN_INT);
927 usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL,
930 req->xfer_step = USBF_XFER_WAIT_END;
934 /* Last packet size was a maxpacket size
935 * Send null packet if needed
938 req->xfer_step = USBF_XFER_SEND_NULL;
942 /* No more action to do. Wait for the end of the USB transfer */
943 req->xfer_step = USBF_XFER_WAIT_END;
946 case USBF_XFER_SEND_NULL:
947 dev_dbg(epn->udc->dev, "ep%u send null\n", epn->id);
948 usbf_epn_send_null(epn);
949 req->xfer_step = USBF_XFER_WAIT_END;
952 case USBF_XFER_WAIT_END:
953 if (!(epn->status & USBF_EPN_IN_INT)) {
954 dev_dbg(epn->udc->dev, "ep%u end not done\n", epn->id);
957 dev_dbg(epn->udc->dev, "ep%u send done %u/%u\n", epn->id,
958 req->req.actual, req->req.length);
959 req->xfer_step = USBF_XFER_START;
966 static void usbf_epn_recv_residue(struct usbf_ep *epn, void *buf,
971 last = usbf_ep_reg_readl(epn, USBF_REG_EPN_READ);
972 memcpy(buf, &last, size);
975 static int usbf_epn_pio_out(struct usbf_ep *epn, struct usbf_req *req)
984 if (epn->status & USBF_EPN_OUT_INT) {
985 recv = USBF_EPN_GET_LDATA(
986 usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
990 buf += req->req.actual;
992 left = req->req.length - req->req.actual;
994 dev_dbg(epn->udc->dev, "ep%u recv %u, left %u, mpkt %u\n", epn->id,
995 recv, left, epn->ep.maxpacket);
997 if (left > epn->ep.maxpacket)
998 left = epn->ep.maxpacket;
1001 req_status = -EOVERFLOW;
1006 nb = count / sizeof(u32);
1008 usbf_ep_reg_read_rep(epn, USBF_REG_EPN_READ,
1010 buf += (nb * sizeof(u32));
1011 req->req.actual += (nb * sizeof(u32));
1012 count -= (nb * sizeof(u32));
1015 usbf_epn_recv_residue(epn, buf, count);
1016 req->req.actual += count;
1019 dev_dbg(epn->udc->dev, "ep%u recv %u/%u\n", epn->id,
1020 req->req.actual, req->req.length);
1023 dev_dbg(epn->udc->dev, "ep%u req.status=%d\n", epn->id,
1025 req->req.status = req_status;
1029 if (recv < epn->ep.maxpacket) {
1030 dev_dbg(epn->udc->dev, "ep%u short packet\n", epn->id);
1031 /* This is a short packet -> It is the end */
1032 req->req.status = 0;
1036 /* Request full -> complete */
1037 if (req->req.actual == req->req.length) {
1038 req->req.status = 0;
1043 if (epn->status & USBF_EPN_OUT_NULL_INT) {
1044 /* NULL packet received */
1045 dev_dbg(epn->udc->dev, "ep%u null packet\n", epn->id);
1046 if (req->req.actual != req->req.length) {
1047 req->req.status = req->req.short_not_ok ?
1050 req->req.status = 0;
1055 return -EINPROGRESS;
1058 static void usbf_epn_enable_out_end_int(struct usbf_ep *epn)
1060 usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_OUT_END_EN);
1063 static void usbf_epn_process_queue(struct usbf_ep *epn);
1065 static void usbf_epn_dma_out_send_dma(struct usbf_ep *epn, dma_addr_t addr, u32 npkt, bool is_short)
1067 usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR2, USBF_SYS_EPN_MPKT(epn->ep.maxpacket));
1068 usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_TADR, addr);
1071 usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
1072 USBF_SYS_EPN_SET_DMACNT(1) | USBF_SYS_EPN_DIR0);
1073 usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
1074 USBF_SYS_EPN_REQEN);
1076 usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT,
1077 USBF_EPN_SET_DMACNT(0));
1079 /* The end of DMA transfer at the USBF level needs to be handled
1080 * after the detection of the end of DMA transfer at the brige
1082 * To force this sequence, enabling the OUT_END interrupt will
1083 * be donee by the detection of the end of transfer at bridge
1084 * level (ie. bridge interrupt).
1086 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
1087 USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN | USBF_EPN_OUT_END_EN);
1088 epn->bridge_on_dma_end = usbf_epn_enable_out_end_int;
1090 /* Clear any pending OUT_END interrupt */
1091 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
1092 ~(u32)USBF_EPN_OUT_END_INT);
1094 usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
1095 USBF_EPN_STOP_MODE | USBF_EPN_STOP_SET | USBF_EPN_DMAMODE0);
1096 usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
1101 usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
1102 USBF_SYS_EPN_SET_DMACNT(npkt) | USBF_SYS_EPN_DIR0);
1103 usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
1104 USBF_SYS_EPN_REQEN);
1106 usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT,
1107 USBF_EPN_SET_DMACNT(npkt));
1109 /* Here, the bridge may or may not generate an interrupt to signal the
1110 * end of DMA transfer.
1111 * Keep only OUT_END interrupt and let handle the bridge later during
1112 * the OUT_END processing.
1114 usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
1115 USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN,
1116 USBF_EPN_OUT_END_EN);
1118 /* Disable bridge interrupt. It will be renabled later */
1119 usbf_reg_bitclr(epn->udc, USBF_REG_AHBBINTEN,
1120 USBF_SYS_DMA_ENDINTEN_EPN(epn->id));
1122 /* Clear any pending DMA_END interrupt at bridge level */
1123 usbf_reg_writel(epn->udc, USBF_REG_AHBBINT,
1124 USBF_SYS_DMA_ENDINT_EPN(epn->id));
1126 /* Clear any pending OUT_END interrupt */
1127 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
1128 ~(u32)USBF_EPN_OUT_END_INT);
1130 usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
1131 USBF_EPN_STOP_MODE | USBF_EPN_STOP_SET | USBF_EPN_DMAMODE0 | USBF_EPN_BURST_SET);
1132 usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
1136 static size_t usbf_epn_dma_out_complete_dma(struct usbf_ep *epn, bool is_short)
1142 /* Restore interrupt mask */
1143 usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
1144 USBF_EPN_OUT_END_EN,
1145 USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
1148 /* Nothing more to do when the DMA was for a short packet */
1152 /* Enable the bridge interrupt */
1153 usbf_reg_bitset(epn->udc, USBF_REG_AHBBINTEN,
1154 USBF_SYS_DMA_ENDINTEN_EPN(epn->id));
1156 tmp = usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT);
1157 dmacnt = USBF_EPN_GET_DMACNT(tmp);
1160 /* Some packet were not received (halted by a short or a null
1162 * The bridge never raises an interrupt in this case.
1163 * Wait for the end of transfer at bridge level
1165 ret = readl_poll_timeout_atomic(
1166 epn->dma_regs + USBF_REG_DMA_EPN_DCR1,
1167 tmp, (USBF_SYS_EPN_GET_DMACNT(tmp) == dmacnt),
1170 dev_err(epn->udc->dev, "ep%u wait bridge timed out\n",
1174 usbf_ep_dma_reg_bitclr(epn, USBF_REG_DMA_EPN_DCR1,
1175 USBF_SYS_EPN_REQEN);
1177 /* The dmacnt value tells how many packet were not transferred
1178 * from the maximum number of packet we set for the DMA transfer.
1179 * Compute the left DMA size based on this value.
1181 return dmacnt * epn->ep.maxpacket;
1187 static int usbf_epn_dma_out(struct usbf_ep *epn, struct usbf_req *req)
1189 unsigned int dma_left;
1196 if (!IS_ALIGNED((uintptr_t)req->req.buf, 4)) {
1197 dev_dbg(epn->udc->dev, "ep%u buf unaligned -> fallback pio\n",
1199 return usbf_epn_pio_out(epn, req);
1202 switch (req->xfer_step) {
1204 case USBF_XFER_START:
1205 if (epn->status & USBF_EPN_OUT_NULL_INT) {
1206 dev_dbg(epn->udc->dev, "ep%u null packet\n", epn->id);
1207 if (req->req.actual != req->req.length) {
1208 req->req.status = req->req.short_not_ok ?
1211 req->req.status = 0;
1216 if (!(epn->status & USBF_EPN_OUT_INT)) {
1217 dev_dbg(epn->udc->dev, "ep%u OUT_INT not set -> spurious\n",
1222 recv = USBF_EPN_GET_LDATA(
1223 usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
1225 dev_dbg(epn->udc->dev, "ep%u recv = 0 -> spurious\n",
1230 left = req->req.length - req->req.actual;
1232 dev_dbg(epn->udc->dev, "ep%u recv %u, left %u, mpkt %u\n", epn->id,
1233 recv, left, epn->ep.maxpacket);
1236 dev_err(epn->udc->dev, "ep%u overflow (%u/%u)\n",
1237 epn->id, recv, left);
1238 req->req.status = -EOVERFLOW;
1242 if (recv < epn->ep.maxpacket) {
1243 /* Short packet received */
1244 dev_dbg(epn->udc->dev, "ep%u short packet\n", epn->id);
1246 usbf_epn_recv_residue(epn,
1247 req->req.buf + req->req.actual, recv);
1248 req->req.actual += recv;
1250 dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n",
1251 epn->id, req->req.actual, req->req.length);
1253 req->xfer_step = USBF_XFER_START;
1257 ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 0);
1259 dev_err(epn->udc->dev, "map request failed (%d)\n",
1265 usbf_epn_dma_out_send_dma(epn,
1266 req->req.dma + req->req.actual,
1268 req->dma_size = recv & ~0x3;
1270 dev_dbg(epn->udc->dev, "ep%u dma short xfer %zu\n", epn->id,
1273 req->xfer_step = USBF_XFER_WAIT_DMA_SHORT;
1277 ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 0);
1279 dev_err(epn->udc->dev, "map request failed (%d)\n",
1285 /* Use the maximum DMA size according to the request buffer.
1286 * We will adjust the received size later at the end of the DMA
1287 * transfer with the left size computed from
1288 * usbf_epn_dma_out_complete_dma().
1290 npkt = left / epn->ep.maxpacket;
1291 usbf_epn_dma_out_send_dma(epn,
1292 req->req.dma + req->req.actual,
1294 req->dma_size = npkt * epn->ep.maxpacket;
1296 dev_dbg(epn->udc->dev, "ep%u dma xfer %zu (%u)\n", epn->id,
1297 req->dma_size, npkt);
1299 req->xfer_step = USBF_XFER_WAIT_DMA;
1302 case USBF_XFER_WAIT_DMA_SHORT:
1303 if (!(epn->status & USBF_EPN_OUT_END_INT)) {
1304 dev_dbg(epn->udc->dev, "ep%u dma short not done\n", epn->id);
1307 dev_dbg(epn->udc->dev, "ep%u dma short done\n", epn->id);
1309 usbf_epn_dma_out_complete_dma(epn, true);
1311 usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
1314 req->req.actual += req->dma_size;
1316 recv = USBF_EPN_GET_LDATA(
1317 usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
1321 dev_dbg(epn->udc->dev, "ep%u recv residue %u\n", epn->id,
1323 usbf_epn_recv_residue(epn,
1324 req->req.buf + req->req.actual, count);
1325 req->req.actual += count;
1328 dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
1329 req->req.actual, req->req.length);
1331 req->xfer_step = USBF_XFER_START;
1334 case USBF_XFER_WAIT_DMA:
1335 if (!(epn->status & USBF_EPN_OUT_END_INT)) {
1336 dev_dbg(epn->udc->dev, "ep%u dma not done\n", epn->id);
1339 dev_dbg(epn->udc->dev, "ep%u dma done\n", epn->id);
1341 dma_left = usbf_epn_dma_out_complete_dma(epn, false);
1343 /* Adjust the final DMA size with */
1344 count = req->dma_size - dma_left;
1346 dev_dbg(epn->udc->dev, "ep%u dma xfer done %u\n", epn->id,
1349 req->req.actual += count;
1351 if (epn->status & USBF_EPN_OUT_NULL_INT) {
1352 /* DMA was stopped by a null packet reception */
1353 dev_dbg(epn->udc->dev, "ep%u dma stopped by null pckt\n",
1355 usb_gadget_unmap_request(&epn->udc->gadget,
1359 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
1360 ~(u32)USBF_EPN_OUT_NULL_INT);
1362 if (req->req.actual != req->req.length) {
1363 req->req.status = req->req.short_not_ok ?
1366 req->req.status = 0;
1368 dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n",
1369 epn->id, req->req.actual, req->req.length);
1370 req->xfer_step = USBF_XFER_START;
1374 recv = USBF_EPN_GET_LDATA(
1375 usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
1376 left = req->req.length - req->req.actual;
1378 dev_err(epn->udc->dev,
1379 "ep%u overflow (%u/%u)\n", epn->id,
1381 req->req.status = -EOVERFLOW;
1382 usb_gadget_unmap_request(&epn->udc->gadget,
1386 req->xfer_step = USBF_XFER_START;
1391 usbf_epn_dma_out_send_dma(epn,
1392 req->req.dma + req->req.actual,
1394 req->dma_size = recv & ~0x3;
1396 dev_dbg(epn->udc->dev, "ep%u dma short xfer %zu\n",
1397 epn->id, req->dma_size);
1399 req->xfer_step = USBF_XFER_WAIT_DMA_SHORT;
1403 usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
1408 dev_dbg(epn->udc->dev, "ep%u recv residue %u\n",
1410 usbf_epn_recv_residue(epn,
1411 req->req.buf + req->req.actual, count);
1412 req->req.actual += count;
1415 dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
1416 req->req.actual, req->req.length);
1418 req->xfer_step = USBF_XFER_START;
1422 /* Process queue at bridge interrupt only */
1423 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
1424 USBF_EPN_OUT_END_EN | USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
1426 epn->bridge_on_dma_end = usbf_epn_process_queue;
1428 req->xfer_step = USBF_XFER_WAIT_BRIDGE;
1431 case USBF_XFER_WAIT_BRIDGE:
1432 dev_dbg(epn->udc->dev, "ep%u bridge transfers done\n", epn->id);
1434 /* Restore interrupt mask */
1435 usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
1436 USBF_EPN_OUT_END_EN,
1437 USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
1439 usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
1442 req->req.actual += req->dma_size;
1444 req->xfer_step = USBF_XFER_START;
1445 left = req->req.length - req->req.actual;
1447 /* No more data can be added to the buffer */
1448 dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
1449 req->req.actual, req->req.length);
1452 dev_dbg(epn->udc->dev, "ep%u recv done %u/%u, wait more data\n",
1453 epn->id, req->req.actual, req->req.length);
1457 return -EINPROGRESS;
1460 static void usbf_epn_dma_stop(struct usbf_ep *epn)
1462 usbf_ep_dma_reg_bitclr(epn, USBF_REG_DMA_EPN_DCR1, USBF_SYS_EPN_REQEN);
1464 /* In the datasheet:
1465 * If EP[m]_REQEN = 0b is set during DMA transfer, AHB-EPC stops DMA
1466 * after 1 packet transfer completed.
1467 * Therefore, wait sufficient time for ensuring DMA transfer
1468 * completion. The WAIT time depends on the system, especially AHB
1470 * So arbitrary 10ms would be sufficient.
1474 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_DMA_CTRL, USBF_EPN_DMA_EN);
1477 static void usbf_epn_dma_abort(struct usbf_ep *epn, struct usbf_req *req)
1479 dev_dbg(epn->udc->dev, "ep%u %s dma abort\n", epn->id,
1480 epn->is_in ? "in" : "out");
1482 epn->bridge_on_dma_end = NULL;
1484 usbf_epn_dma_stop(epn);
1486 usb_gadget_unmap_request(&epn->udc->gadget, &req->req,
1487 epn->is_in ? 1 : 0);
1490 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);
1493 usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
1497 usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
1498 USBF_EPN_OUT_END_EN,
1499 USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
1502 /* As dma is stopped, be sure that no DMA interrupt are pending */
1503 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
1504 USBF_EPN_IN_END_INT | USBF_EPN_OUT_END_INT);
1506 usbf_reg_writel(epn->udc, USBF_REG_AHBBINT, USBF_SYS_DMA_ENDINT_EPN(epn->id));
1508 /* Enable DMA interrupt the bridge level */
1509 usbf_reg_bitset(epn->udc, USBF_REG_AHBBINTEN,
1510 USBF_SYS_DMA_ENDINTEN_EPN(epn->id));
1512 /* Reset transfer step */
1513 req->xfer_step = USBF_XFER_START;
1516 static void usbf_epn_fifo_flush(struct usbf_ep *epn)
1522 dev_dbg(epn->udc->dev, "ep%u %s fifo flush\n", epn->id,
1523 epn->is_in ? "in" : "out");
1525 ctrl = usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL);
1526 usbf_ep_reg_writel(epn, USBF_REG_EPN_CONTROL, ctrl | USBF_EPN_BCLR);
1528 if (ctrl & USBF_EPN_DIR0)
1531 ret = readl_poll_timeout_atomic(epn->regs + USBF_REG_EPN_STATUS, sts,
1532 (sts & (USBF_EPN_IN_DATA | USBF_EPN_IN_EMPTY)) == USBF_EPN_IN_EMPTY,
1535 dev_err(epn->udc->dev, "ep%u flush fifo timed out\n", epn->id);
1538 static void usbf_ep_req_done(struct usbf_ep *ep, struct usbf_req *req,
1541 list_del_init(&req->queue);
1544 req->req.status = status;
1546 if (req->req.status == -EINPROGRESS)
1547 req->req.status = status;
1550 dev_dbg(ep->udc->dev, "ep%u %s req done length %u/%u, status=%d\n", ep->id,
1551 ep->is_in ? "in" : "out",
1552 req->req.actual, req->req.length, req->req.status);
1555 usbf_epn_dma_abort(ep, req);
1557 spin_unlock(&ep->udc->lock);
1558 usb_gadget_giveback_request(&ep->ep, &req->req);
1559 spin_lock(&ep->udc->lock);
1562 static void usbf_ep_nuke(struct usbf_ep *ep, int status)
1564 struct usbf_req *req;
1566 dev_dbg(ep->udc->dev, "ep%u %s nuke status %d\n", ep->id,
1567 ep->is_in ? "in" : "out",
1570 while (!list_empty(&ep->queue)) {
1571 req = list_first_entry(&ep->queue, struct usbf_req, queue);
1572 usbf_ep_req_done(ep, req, status);
1576 usbf_ep0_fifo_flush(ep);
1578 usbf_epn_fifo_flush(ep);
1581 static bool usbf_ep_is_stalled(struct usbf_ep *ep)
1586 ctrl = usbf_ep_reg_readl(ep, USBF_REG_EP0_CONTROL);
1587 return (ctrl & USBF_EP0_STL) ? true : false;
1590 ctrl = usbf_ep_reg_readl(ep, USBF_REG_EPN_CONTROL);
1592 return (ctrl & USBF_EPN_ISTL) ? true : false;
1594 return (ctrl & USBF_EPN_OSTL) ? true : false;
1597 static int usbf_epn_start_queue(struct usbf_ep *epn)
1599 struct usbf_req *req;
1602 if (usbf_ep_is_stalled(epn))
1605 req = list_first_entry_or_null(&epn->queue, struct usbf_req, queue);
1608 if (req && !epn->is_processing) {
1609 ret = epn->dma_regs ?
1610 usbf_epn_dma_in(epn, req) :
1611 usbf_epn_pio_in(epn, req);
1612 if (ret != -EINPROGRESS) {
1613 dev_err(epn->udc->dev,
1614 "queued next request not in progress\n");
1615 /* The request cannot be completed (ie
1616 * ret == 0) on the first call.
1617 * stall and nuke the endpoint
1619 return ret ? ret : -EIO;
1624 /* Clear ONAK to accept OUT tokens */
1625 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL,
1628 /* Enable interrupts */
1629 usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA,
1630 USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
1632 /* Disable incoming data and interrupt.
1633 * They will be enable on next usb_eb_queue call
1635 usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL,
1637 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
1638 USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
1644 static int usbf_ep_process_queue(struct usbf_ep *ep)
1646 int (*usbf_ep_xfer)(struct usbf_ep *ep, struct usbf_req *req);
1647 struct usbf_req *req;
1652 usbf_ep_xfer = usbf_ep0_pio_in;
1654 usbf_ep_xfer = ep->dma_regs ?
1655 usbf_epn_dma_in : usbf_epn_pio_in;
1658 usbf_ep_xfer = usbf_ep0_pio_out;
1660 usbf_ep_xfer = ep->dma_regs ?
1661 usbf_epn_dma_out : usbf_epn_pio_out;
1665 req = list_first_entry_or_null(&ep->queue, struct usbf_req, queue);
1667 dev_err(ep->udc->dev,
1668 "no request available for ep%u %s process\n", ep->id,
1669 ep->is_in ? "in" : "out");
1674 /* Were going to read the FIFO for this current request.
1675 * NAK any other incoming data to avoid a race condition if no
1676 * more request are available.
1678 if (!ep->is_in && ep->id != 0) {
1679 usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
1683 ret = usbf_ep_xfer(ep, req);
1684 if (ret == -EINPROGRESS) {
1685 if (!ep->is_in && ep->id != 0) {
1686 /* The current request needs more data.
1687 * Allow incoming data
1689 usbf_ep_reg_bitclr(ep, USBF_REG_EPN_CONTROL,
1695 is_processing = ep->is_processing;
1696 ep->is_processing = 1;
1697 usbf_ep_req_done(ep, req, ret);
1698 ep->is_processing = is_processing;
1701 /* An error was detected during the request transfer.
1702 * Any pending DMA transfers were aborted by the
1703 * usbf_ep_req_done() call.
1704 * It's time to flush the fifo
1707 usbf_ep0_fifo_flush(ep);
1709 usbf_epn_fifo_flush(ep);
1712 req = list_first_entry_or_null(&ep->queue, struct usbf_req,
1720 /* An other request is available.
1721 * Allow incoming data
1723 usbf_ep_reg_bitclr(ep, USBF_REG_EPN_CONTROL,
1726 /* No request queued. Disable interrupts.
1727 * They will be enabled on usb_ep_queue
1729 usbf_ep_reg_bitclr(ep, USBF_REG_EPN_INT_ENA,
1730 USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
1733 /* Do not recall usbf_ep_xfer() */
1734 return req ? -EINPROGRESS : 0;
1741 static void usbf_ep_stall(struct usbf_ep *ep, bool stall)
1743 struct usbf_req *first;
1745 dev_dbg(ep->udc->dev, "ep%u %s %s\n", ep->id,
1746 ep->is_in ? "in" : "out",
1747 stall ? "stall" : "unstall");
1751 usbf_ep_reg_bitset(ep, USBF_REG_EP0_CONTROL, USBF_EP0_STL);
1753 usbf_ep_reg_bitclr(ep, USBF_REG_EP0_CONTROL, USBF_EP0_STL);
1759 usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
1762 usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
1763 USBF_EPN_OSTL | USBF_EPN_OSTL_EN);
1765 first = list_first_entry_or_null(&ep->queue, struct usbf_req, queue);
1766 if (first && first->is_mapped) {
1767 /* This can appear if the host halts an endpoint using
1768 * SET_FEATURE and then un-halts the endpoint
1770 usbf_epn_dma_abort(ep, first);
1772 usbf_epn_fifo_flush(ep);
1774 usbf_ep_reg_clrset(ep, USBF_REG_EPN_CONTROL,
1778 usbf_ep_reg_clrset(ep, USBF_REG_EPN_CONTROL,
1780 USBF_EPN_OSTL_EN | USBF_EPN_OPIDCLR);
1782 usbf_epn_start_queue(ep);
1786 static void usbf_ep0_enable(struct usbf_ep *ep0)
1788 usbf_ep_reg_writel(ep0, USBF_REG_EP0_CONTROL, USBF_EP0_INAK_EN | USBF_EP0_BCLR);
1790 usbf_ep_reg_writel(ep0, USBF_REG_EP0_INT_ENA,
1791 USBF_EP0_SETUP_EN | USBF_EP0_STG_START_EN | USBF_EP0_STG_END_EN |
1792 USBF_EP0_OUT_EN | USBF_EP0_OUT_NULL_EN | USBF_EP0_IN_EN);
1794 ep0->udc->ep0state = EP0_IDLE;
1797 /* enable interrupts for the ep0 */
1798 usbf_reg_bitset(ep0->udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(0));
1801 static int usbf_epn_enable(struct usbf_ep *epn)
1806 base_addr = usbf_ep_info[epn->id].base_addr;
1807 usbf_ep_reg_writel(epn, USBF_REG_EPN_PCKT_ADRS,
1808 USBF_EPN_BASEAD(base_addr) | USBF_EPN_MPKT(epn->ep.maxpacket));
1810 /* OUT transfer interrupt are enabled during usb_ep_queue */
1812 /* Will be changed in DMA processing */
1813 usbf_ep_reg_writel(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_IN_EN);
1816 /* Clear, set endpoint direction, set IN/OUT STL, and enable
1817 * Send NAK for Data out as request are not queued yet
1819 ctrl = USBF_EPN_EN | USBF_EPN_BCLR;
1821 ctrl |= USBF_EPN_OSTL | USBF_EPN_OSTL_EN;
1823 ctrl |= USBF_EPN_DIR0 | USBF_EPN_ISTL | USBF_EPN_OSTL_EN | USBF_EPN_ONAK;
1824 usbf_ep_reg_writel(epn, USBF_REG_EPN_CONTROL, ctrl);
1829 static int usbf_ep_enable(struct usb_ep *_ep,
1830 const struct usb_endpoint_descriptor *desc)
1832 struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
1833 struct usbf_udc *udc = ep->udc;
1834 unsigned long flags;
1840 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1843 dev_dbg(ep->udc->dev, "ep%u %s mpkts %d\n", ep->id,
1844 usb_endpoint_dir_in(desc) ? "in" : "out",
1845 usb_endpoint_maxp(desc));
1847 spin_lock_irqsave(&ep->udc->lock, flags);
1848 ep->is_in = usb_endpoint_dir_in(desc);
1849 ep->ep.maxpacket = usb_endpoint_maxp(desc);
1851 ret = usbf_epn_enable(ep);
1857 /* enable interrupts for this endpoint */
1858 usbf_reg_bitset(udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(ep->id));
1860 /* enable DMA interrupt at bridge level if DMA is used */
1862 ep->bridge_on_dma_end = NULL;
1863 usbf_reg_bitset(udc, USBF_REG_AHBBINTEN,
1864 USBF_SYS_DMA_ENDINTEN_EPN(ep->id));
1869 spin_unlock_irqrestore(&ep->udc->lock, flags);
1873 static int usbf_epn_disable(struct usbf_ep *epn)
1875 /* Disable interrupts */
1876 usbf_ep_reg_writel(epn, USBF_REG_EPN_INT_ENA, 0);
1878 /* Disable endpoint */
1879 usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_EN);
1881 /* remove anything that was pending */
1882 usbf_ep_nuke(epn, -ESHUTDOWN);
1887 static int usbf_ep_disable(struct usb_ep *_ep)
1889 struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
1890 struct usbf_udc *udc = ep->udc;
1891 unsigned long flags;
1897 dev_dbg(ep->udc->dev, "ep%u %s mpkts %d\n", ep->id,
1898 ep->is_in ? "in" : "out", ep->ep.maxpacket);
1900 spin_lock_irqsave(&ep->udc->lock, flags);
1902 /* Disable DMA interrupt */
1904 usbf_reg_bitclr(udc, USBF_REG_AHBBINTEN,
1905 USBF_SYS_DMA_ENDINTEN_EPN(ep->id));
1906 ep->bridge_on_dma_end = NULL;
1908 /* disable interrupts for this endpoint */
1909 usbf_reg_bitclr(udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(ep->id));
1910 /* and the endpoint itself */
1911 ret = usbf_epn_disable(ep);
1912 spin_unlock_irqrestore(&ep->udc->lock, flags);
1917 static int usbf_ep0_queue(struct usbf_ep *ep0, struct usbf_req *req,
1922 req->req.actual = 0;
1923 req->req.status = -EINPROGRESS;
1924 req->is_zero_sent = 0;
1926 list_add_tail(&req->queue, &ep0->queue);
1928 if (ep0->udc->ep0state == EP0_IN_STATUS_START_PHASE)
1934 if (ep0->udc->ep0state == EP0_IN_STATUS_PHASE) {
1935 if (req->req.length) {
1936 dev_err(ep0->udc->dev,
1937 "request lng %u for ep0 in status phase\n",
1941 ep0->delayed_status = 0;
1943 if (!ep0->is_processing) {
1944 ret = usbf_ep0_pio_in(ep0, req);
1945 if (ret != -EINPROGRESS) {
1946 dev_err(ep0->udc->dev,
1947 "queued request not in progress\n");
1948 /* The request cannot be completed (ie
1949 * ret == 0) on the first call
1951 return ret ? ret : -EIO;
1958 static int usbf_epn_queue(struct usbf_ep *ep, struct usbf_req *req,
1965 dev_err(ep->udc->dev, "ep%u request queue while disable\n",
1970 req->req.actual = 0;
1971 req->req.status = -EINPROGRESS;
1972 req->is_zero_sent = 0;
1973 req->xfer_step = USBF_XFER_START;
1975 was_empty = list_empty(&ep->queue);
1976 list_add_tail(&req->queue, &ep->queue);
1978 ret = usbf_epn_start_queue(ep);
1985 static int usbf_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1988 struct usbf_req *req = container_of(_req, struct usbf_req, req);
1989 struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
1990 struct usbf_udc *udc = ep->udc;
1991 unsigned long flags;
1994 if (!_req || !_req->buf)
1997 if (!udc || !udc->driver)
2000 dev_dbg(ep->udc->dev, "ep%u %s req queue length %u, zero %u, short_not_ok %u\n",
2001 ep->id, ep->is_in ? "in" : "out",
2002 req->req.length, req->req.zero, req->req.short_not_ok);
2004 spin_lock_irqsave(&ep->udc->lock, flags);
2006 ret = usbf_ep0_queue(ep, req, gfp_flags);
2008 ret = usbf_epn_queue(ep, req, gfp_flags);
2009 spin_unlock_irqrestore(&ep->udc->lock, flags);
2013 static int usbf_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2015 struct usbf_req *req = container_of(_req, struct usbf_req, req);
2016 struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
2017 unsigned long flags;
2022 spin_lock_irqsave(&ep->udc->lock, flags);
2024 dev_dbg(ep->udc->dev, "ep%u %s req dequeue length %u/%u\n",
2025 ep->id, ep->is_in ? "in" : "out",
2026 req->req.actual, req->req.length);
2028 first = list_is_first(&req->queue, &ep->queue);
2030 /* Complete the request but avoid any operation that could be done
2031 * if a new request is queued during the request completion
2033 is_processing = ep->is_processing;
2034 ep->is_processing = 1;
2035 usbf_ep_req_done(ep, req, -ECONNRESET);
2036 ep->is_processing = is_processing;
2039 /* The first item in the list was dequeued.
2040 * This item could already be submitted to the hardware.
2041 * So, flush the fifo
2044 usbf_epn_fifo_flush(ep);
2046 usbf_ep0_fifo_flush(ep);
2050 /* We dequeue a request on ep0. On this endpoint, we can have
2051 * 1 request related to the data stage and/or 1 request
2052 * related to the status stage.
2053 * We dequeue one of them and so the USB control transaction
2054 * is no more coherent. The simple way to be consistent after
2055 * dequeuing is to stall and nuke the endpoint and wait the
2056 * next SETUP packet.
2058 usbf_ep_stall(ep, true);
2059 usbf_ep_nuke(ep, -ECONNRESET);
2060 ep->udc->ep0state = EP0_IDLE;
2067 ret = usbf_epn_start_queue(ep);
2069 usbf_ep_stall(ep, true);
2070 usbf_ep_nuke(ep, -EIO);
2073 spin_unlock_irqrestore(&ep->udc->lock, flags);
2077 static struct usb_request *usbf_ep_alloc_request(struct usb_ep *_ep,
2080 struct usbf_req *req;
2085 req = kzalloc(sizeof(*req), gfp_flags);
2089 INIT_LIST_HEAD(&req->queue);
2094 static void usbf_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
2096 struct usbf_req *req;
2097 unsigned long flags;
2103 req = container_of(_req, struct usbf_req, req);
2104 ep = container_of(_ep, struct usbf_ep, ep);
2106 spin_lock_irqsave(&ep->udc->lock, flags);
2107 list_del_init(&req->queue);
2108 spin_unlock_irqrestore(&ep->udc->lock, flags);
2112 static int usbf_ep_set_halt(struct usb_ep *_ep, int halt)
2114 struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
2115 unsigned long flags;
2121 spin_lock_irqsave(&ep->udc->lock, flags);
2123 if (!list_empty(&ep->queue)) {
2128 usbf_ep_stall(ep, halt);
2134 spin_unlock_irqrestore(&ep->udc->lock, flags);
2139 static int usbf_ep_set_wedge(struct usb_ep *_ep)
2141 struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
2142 unsigned long flags;
2148 spin_lock_irqsave(&ep->udc->lock, flags);
2149 if (!list_empty(&ep->queue)) {
2153 usbf_ep_stall(ep, 1);
2158 spin_unlock_irqrestore(&ep->udc->lock, flags);
2162 static struct usb_ep_ops usbf_ep_ops = {
2163 .enable = usbf_ep_enable,
2164 .disable = usbf_ep_disable,
2165 .queue = usbf_ep_queue,
2166 .dequeue = usbf_ep_dequeue,
2167 .set_halt = usbf_ep_set_halt,
2168 .set_wedge = usbf_ep_set_wedge,
2169 .alloc_request = usbf_ep_alloc_request,
2170 .free_request = usbf_ep_free_request,
2173 static void usbf_ep0_req_complete(struct usb_ep *_ep, struct usb_request *_req)
2177 static void usbf_ep0_fill_req(struct usbf_ep *ep0, struct usbf_req *req,
2178 void *buf, unsigned int length,
2179 void (*complete)(struct usb_ep *_ep,
2180 struct usb_request *_req))
2183 memcpy(ep0->udc->ep0_buf, buf, length);
2185 req->req.buf = ep0->udc->ep0_buf;
2186 req->req.length = length;
2188 req->req.zero = true;
2189 req->req.complete = complete ? complete : usbf_ep0_req_complete;
2190 req->req.status = -EINPROGRESS;
2191 req->req.context = NULL;
2192 req->req.actual = 0;
2195 static struct usbf_ep *usbf_get_ep_by_addr(struct usbf_udc *udc, u8 address)
2200 if ((address & USB_ENDPOINT_NUMBER_MASK) == 0)
2203 for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
2209 if (ep->ep.desc->bEndpointAddress == address)
2216 static int usbf_req_delegate(struct usbf_udc *udc,
2217 const struct usb_ctrlrequest *ctrlrequest)
2221 spin_unlock(&udc->lock);
2222 ret = udc->driver->setup(&udc->gadget, ctrlrequest);
2223 spin_lock(&udc->lock);
2225 dev_dbg(udc->dev, "udc driver setup failed %d\n", ret);
2228 if (ret == USB_GADGET_DELAYED_STATUS) {
2229 dev_dbg(udc->dev, "delayed status set\n");
2230 udc->ep[0].delayed_status = 1;
2236 static int usbf_req_get_status(struct usbf_udc *udc,
2237 const struct usb_ctrlrequest *ctrlrequest)
2245 wValue = le16_to_cpu(ctrlrequest->wValue);
2246 wLength = le16_to_cpu(ctrlrequest->wLength);
2247 wIndex = le16_to_cpu(ctrlrequest->wIndex);
2249 switch (ctrlrequest->bRequestType) {
2250 case USB_DIR_IN | USB_RECIP_DEVICE | USB_TYPE_STANDARD:
2251 if ((wValue != 0) || (wIndex != 0) || (wLength != 2))
2255 if (udc->gadget.is_selfpowered)
2256 status_data |= BIT(USB_DEVICE_SELF_POWERED);
2258 if (udc->is_remote_wakeup)
2259 status_data |= BIT(USB_DEVICE_REMOTE_WAKEUP);
2263 case USB_DIR_IN | USB_RECIP_ENDPOINT | USB_TYPE_STANDARD:
2264 if ((wValue != 0) || (wLength != 2))
2267 ep = usbf_get_ep_by_addr(udc, wIndex);
2272 if (usbf_ep_is_stalled(ep))
2273 status_data |= cpu_to_le16(1);
2276 case USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_STANDARD:
2277 if ((wValue != 0) || (wLength != 2))
2286 usbf_ep0_fill_req(&udc->ep[0], &udc->setup_reply, &status_data,
2287 sizeof(status_data), NULL);
2288 usbf_ep0_queue(&udc->ep[0], &udc->setup_reply, GFP_ATOMIC);
2293 return usbf_req_delegate(udc, ctrlrequest);
2296 static int usbf_req_clear_set_feature(struct usbf_udc *udc,
2297 const struct usb_ctrlrequest *ctrlrequest,
2305 wValue = le16_to_cpu(ctrlrequest->wValue);
2306 wLength = le16_to_cpu(ctrlrequest->wLength);
2307 wIndex = le16_to_cpu(ctrlrequest->wIndex);
2309 switch (ctrlrequest->bRequestType) {
2310 case USB_DIR_OUT | USB_RECIP_DEVICE:
2311 if ((wIndex != 0) || (wLength != 0))
2314 if (wValue != cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP))
2317 udc->is_remote_wakeup = is_set;
2320 case USB_DIR_OUT | USB_RECIP_ENDPOINT:
2324 ep = usbf_get_ep_by_addr(udc, wIndex);
2328 if ((ep->id == 0) && is_set) {
2329 /* Endpoint 0 cannot be halted (stalled)
2330 * Returning an error code leads to a STALL on this ep0
2331 * but keep the automate in a consistent state.
2335 if (ep->is_wedged && !is_set) {
2336 /* Ignore CLEAR_FEATURE(HALT ENDPOINT) when the
2337 * endpoint is wedged
2341 usbf_ep_stall(ep, is_set);
2351 return usbf_req_delegate(udc, ctrlrequest);
2354 static void usbf_ep0_req_set_address_complete(struct usb_ep *_ep,
2355 struct usb_request *_req)
2357 struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
2359 /* The status phase of the SET_ADDRESS request is completed ... */
2360 if (_req->status == 0) {
2361 /* ... without any errors -> Signaled the state to the core. */
2362 usb_gadget_set_state(&ep->udc->gadget, USB_STATE_ADDRESS);
2365 /* In case of request failure, there is no need to revert the address
2366 * value set to the hardware as the hardware will take care of the
2367 * value only if the status stage is completed normally.
2371 static int usbf_req_set_address(struct usbf_udc *udc,
2372 const struct usb_ctrlrequest *ctrlrequest)
2379 wValue = le16_to_cpu(ctrlrequest->wValue);
2380 wLength = le16_to_cpu(ctrlrequest->wLength);
2381 wIndex = le16_to_cpu(ctrlrequest->wIndex);
2383 if (ctrlrequest->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
2386 if ((wIndex != 0) || (wLength != 0) || (wValue > 127))
2390 /* The hardware will take care of this USB address after the status
2391 * stage of the SET_ADDRESS request is completed normally.
2392 * It is safe to write it now
2394 usbf_reg_writel(udc, USBF_REG_USB_ADDRESS, USBF_USB_SET_USB_ADDR(addr));
2396 /* Queued the status request */
2397 usbf_ep0_fill_req(&udc->ep[0], &udc->setup_reply, NULL, 0,
2398 usbf_ep0_req_set_address_complete);
2399 usbf_ep0_queue(&udc->ep[0], &udc->setup_reply, GFP_ATOMIC);
2404 return usbf_req_delegate(udc, ctrlrequest);
2407 static int usbf_req_set_configuration(struct usbf_udc *udc,
2408 const struct usb_ctrlrequest *ctrlrequest)
2415 ret = usbf_req_delegate(udc, ctrlrequest);
2419 wValue = le16_to_cpu(ctrlrequest->wValue);
2420 wLength = le16_to_cpu(ctrlrequest->wLength);
2421 wIndex = le16_to_cpu(ctrlrequest->wIndex);
2423 if ((ctrlrequest->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE)) ||
2424 (wIndex != 0) || (wLength != 0)) {
2425 /* No error detected by driver->setup() but it is not an USB2.0
2426 * Ch9 SET_CONFIGURATION.
2427 * Nothing more to do
2432 if (wValue & 0x00FF) {
2433 usbf_reg_bitset(udc, USBF_REG_USB_CONTROL, USBF_USB_CONF);
2435 usbf_reg_bitclr(udc, USBF_REG_USB_CONTROL, USBF_USB_CONF);
2436 /* Go back to Address State */
2437 spin_unlock(&udc->lock);
2438 usb_gadget_set_state(&udc->gadget, USB_STATE_ADDRESS);
2439 spin_lock(&udc->lock);
2445 static int usbf_handle_ep0_setup(struct usbf_ep *ep0)
2448 struct usb_ctrlrequest ctrlreq;
2451 struct usbf_udc *udc = ep0->udc;
2454 /* Read setup data (ie the USB control request) */
2455 crq.raw[0] = usbf_reg_readl(udc, USBF_REG_SETUP_DATA0);
2456 crq.raw[1] = usbf_reg_readl(udc, USBF_REG_SETUP_DATA1);
2458 dev_dbg(ep0->udc->dev,
2459 "ep0 req%02x.%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
2460 crq.ctrlreq.bRequestType, crq.ctrlreq.bRequest,
2461 crq.ctrlreq.wValue, crq.ctrlreq.wIndex, crq.ctrlreq.wLength);
2463 /* Set current EP0 state according to the received request */
2464 if (crq.ctrlreq.wLength) {
2465 if (crq.ctrlreq.bRequestType & USB_DIR_IN) {
2466 udc->ep0state = EP0_IN_DATA_PHASE;
2467 usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
2472 udc->ep0state = EP0_OUT_DATA_PHASE;
2473 usbf_ep_reg_bitclr(ep0, USBF_REG_EP0_CONTROL,
2478 udc->ep0state = EP0_IN_STATUS_START_PHASE;
2482 /* We starts a new control transfer -> Clear the delayed status flag */
2483 ep0->delayed_status = 0;
2485 if ((crq.ctrlreq.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) {
2486 /* This is not a USB standard request -> delelate */
2490 switch (crq.ctrlreq.bRequest) {
2491 case USB_REQ_GET_STATUS:
2492 ret = usbf_req_get_status(udc, &crq.ctrlreq);
2495 case USB_REQ_CLEAR_FEATURE:
2496 ret = usbf_req_clear_set_feature(udc, &crq.ctrlreq, false);
2499 case USB_REQ_SET_FEATURE:
2500 ret = usbf_req_clear_set_feature(udc, &crq.ctrlreq, true);
2503 case USB_REQ_SET_ADDRESS:
2504 ret = usbf_req_set_address(udc, &crq.ctrlreq);
2507 case USB_REQ_SET_CONFIGURATION:
2508 ret = usbf_req_set_configuration(udc, &crq.ctrlreq);
2518 return usbf_req_delegate(udc, &crq.ctrlreq);
2521 static int usbf_handle_ep0_data_status(struct usbf_ep *ep0,
2522 const char *ep0state_name,
2523 enum usbf_ep0state next_ep0state)
2525 struct usbf_udc *udc = ep0->udc;
2528 ret = usbf_ep_process_queue(ep0);
2532 "no request available for ep0 %s phase\n",
2536 /* More data needs to be processed */
2540 /* All requests in the queue are processed */
2541 udc->ep0state = next_ep0state;
2545 "process queue failed for ep0 %s phase (%d)\n",
2546 ep0state_name, ret);
2552 static int usbf_handle_ep0_out_status_start(struct usbf_ep *ep0)
2554 struct usbf_udc *udc = ep0->udc;
2555 struct usbf_req *req;
2557 usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
2562 req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
2564 usbf_ep0_fill_req(ep0, &udc->setup_reply, NULL, 0, NULL);
2565 usbf_ep0_queue(ep0, &udc->setup_reply, GFP_ATOMIC);
2567 if (req->req.length) {
2569 "queued request length %u for ep0 out status phase\n",
2573 udc->ep0state = EP0_OUT_STATUS_PHASE;
2577 static int usbf_handle_ep0_in_status_start(struct usbf_ep *ep0)
2579 struct usbf_udc *udc = ep0->udc;
2580 struct usbf_req *req;
2583 usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
2585 USBF_EP0_INAK_EN | USBF_EP0_PIDCLR);
2588 /* Queue request for status if needed */
2589 req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
2591 if (ep0->delayed_status) {
2592 dev_dbg(ep0->udc->dev,
2593 "EP0_IN_STATUS_START_PHASE ep0->delayed_status set\n");
2594 udc->ep0state = EP0_IN_STATUS_PHASE;
2598 usbf_ep0_fill_req(ep0, &udc->setup_reply, NULL,
2600 usbf_ep0_queue(ep0, &udc->setup_reply,
2603 req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
2605 if (req->req.length) {
2607 "queued request length %u for ep0 in status phase\n",
2612 ret = usbf_ep0_pio_in(ep0, req);
2613 if (ret != -EINPROGRESS) {
2614 usbf_ep_req_done(ep0, req, ret);
2615 udc->ep0state = EP0_IN_STATUS_END_PHASE;
2619 udc->ep0state = EP0_IN_STATUS_PHASE;
2623 static void usbf_ep0_interrupt(struct usbf_ep *ep0)
2625 struct usbf_udc *udc = ep0->udc;
2630 ep0->status = usbf_ep_reg_readl(ep0, USBF_REG_EP0_STATUS);
2631 usbf_ep_reg_writel(ep0, USBF_REG_EP0_STATUS, ~ep0->status);
2633 dev_dbg(ep0->udc->dev, "ep0 status=0x%08x, enable=%08x\n, ctrl=0x%08x\n",
2635 usbf_ep_reg_readl(ep0, USBF_REG_EP0_INT_ENA),
2636 usbf_ep_reg_readl(ep0, USBF_REG_EP0_CONTROL));
2638 sts = ep0->status & (USBF_EP0_SETUP_INT | USBF_EP0_IN_INT | USBF_EP0_OUT_INT |
2639 USBF_EP0_OUT_NULL_INT | USBF_EP0_STG_START_INT |
2640 USBF_EP0_STG_END_INT);
2644 dev_dbg(ep0->udc->dev, "udc->ep0state=%d\n", udc->ep0state);
2647 prev_ep0state = udc->ep0state;
2648 switch (udc->ep0state) {
2650 if (!(sts & USBF_EP0_SETUP_INT))
2653 sts &= ~USBF_EP0_SETUP_INT;
2654 dev_dbg(ep0->udc->dev, "ep0 handle setup\n");
2655 ret = usbf_handle_ep0_setup(ep0);
2658 case EP0_IN_DATA_PHASE:
2659 if (!(sts & USBF_EP0_IN_INT))
2662 sts &= ~USBF_EP0_IN_INT;
2663 dev_dbg(ep0->udc->dev, "ep0 handle in data phase\n");
2664 ret = usbf_handle_ep0_data_status(ep0,
2665 "in data", EP0_OUT_STATUS_START_PHASE);
2668 case EP0_OUT_STATUS_START_PHASE:
2669 if (!(sts & USBF_EP0_STG_START_INT))
2672 sts &= ~USBF_EP0_STG_START_INT;
2673 dev_dbg(ep0->udc->dev, "ep0 handle out status start phase\n");
2674 ret = usbf_handle_ep0_out_status_start(ep0);
2677 case EP0_OUT_STATUS_PHASE:
2678 if (!(sts & (USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT)))
2681 sts &= ~(USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT);
2682 dev_dbg(ep0->udc->dev, "ep0 handle out status phase\n");
2683 ret = usbf_handle_ep0_data_status(ep0,
2685 EP0_OUT_STATUS_END_PHASE);
2688 case EP0_OUT_STATUS_END_PHASE:
2689 if (!(sts & (USBF_EP0_STG_END_INT | USBF_EP0_SETUP_INT)))
2692 sts &= ~USBF_EP0_STG_END_INT;
2693 dev_dbg(ep0->udc->dev, "ep0 handle out status end phase\n");
2694 udc->ep0state = EP0_IDLE;
2697 case EP0_OUT_DATA_PHASE:
2698 if (!(sts & (USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT)))
2701 sts &= ~(USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT);
2702 dev_dbg(ep0->udc->dev, "ep0 handle out data phase\n");
2703 ret = usbf_handle_ep0_data_status(ep0,
2704 "out data", EP0_IN_STATUS_START_PHASE);
2707 case EP0_IN_STATUS_START_PHASE:
2708 if (!(sts & USBF_EP0_STG_START_INT))
2711 sts &= ~USBF_EP0_STG_START_INT;
2712 dev_dbg(ep0->udc->dev, "ep0 handle in status start phase\n");
2713 ret = usbf_handle_ep0_in_status_start(ep0);
2716 case EP0_IN_STATUS_PHASE:
2717 if (!(sts & USBF_EP0_IN_INT))
2720 sts &= ~USBF_EP0_IN_INT;
2721 dev_dbg(ep0->udc->dev, "ep0 handle in status phase\n");
2722 ret = usbf_handle_ep0_data_status(ep0,
2723 "in status", EP0_IN_STATUS_END_PHASE);
2726 case EP0_IN_STATUS_END_PHASE:
2727 if (!(sts & (USBF_EP0_STG_END_INT | USBF_EP0_SETUP_INT)))
2730 sts &= ~USBF_EP0_STG_END_INT;
2731 dev_dbg(ep0->udc->dev, "ep0 handle in status end\n");
2732 udc->ep0state = EP0_IDLE;
2736 udc->ep0state = EP0_IDLE;
2741 dev_dbg(ep0->udc->dev, "ep0 failed (%d)\n", ret);
2742 /* Failure -> stall.
2743 * This stall state will be automatically cleared when
2744 * the IP receives the next SETUP packet
2746 usbf_ep_stall(ep0, true);
2748 /* Remove anything that was pending */
2749 usbf_ep_nuke(ep0, -EPROTO);
2751 udc->ep0state = EP0_IDLE;
2755 } while ((prev_ep0state != udc->ep0state) || (prev_sts != sts));
2757 dev_dbg(ep0->udc->dev, "ep0 done udc->ep0state=%d, status=0x%08x. next=0x%08x\n",
2759 usbf_ep_reg_readl(ep0, USBF_REG_EP0_STATUS));
2762 static void usbf_epn_process_queue(struct usbf_ep *epn)
2766 ret = usbf_ep_process_queue(epn);
2769 dev_warn(epn->udc->dev, "ep%u %s, no request available\n",
2770 epn->id, epn->is_in ? "in" : "out");
2773 /* More data needs to be processed */
2777 /* All requests in the queue are processed */
2780 dev_err(epn->udc->dev, "ep%u %s, process queue failed (%d)\n",
2781 epn->id, epn->is_in ? "in" : "out", ret);
2786 dev_dbg(epn->udc->dev, "ep%u %s failed (%d)\n", epn->id,
2787 epn->is_in ? "in" : "out", ret);
2788 usbf_ep_stall(epn, true);
2789 usbf_ep_nuke(epn, ret);
2793 static void usbf_epn_interrupt(struct usbf_ep *epn)
2798 epn->status = usbf_ep_reg_readl(epn, USBF_REG_EPN_STATUS);
2799 ena = usbf_ep_reg_readl(epn, USBF_REG_EPN_INT_ENA);
2800 usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(epn->status & ena));
2802 dev_dbg(epn->udc->dev, "ep%u %s status=0x%08x, enable=%08x\n, ctrl=0x%08x\n",
2803 epn->id, epn->is_in ? "in" : "out", epn->status, ena,
2804 usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL));
2806 if (epn->disabled) {
2807 dev_warn(epn->udc->dev, "ep%u %s, interrupt while disabled\n",
2808 epn->id, epn->is_in ? "in" : "out");
2812 sts = epn->status & ena;
2814 if (sts & (USBF_EPN_IN_END_INT | USBF_EPN_IN_INT)) {
2815 sts &= ~(USBF_EPN_IN_END_INT | USBF_EPN_IN_INT);
2816 dev_dbg(epn->udc->dev, "ep%u %s process queue (in interrupts)\n",
2817 epn->id, epn->is_in ? "in" : "out");
2818 usbf_epn_process_queue(epn);
2821 if (sts & (USBF_EPN_OUT_END_INT | USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT)) {
2822 sts &= ~(USBF_EPN_OUT_END_INT | USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
2823 dev_dbg(epn->udc->dev, "ep%u %s process queue (out interrupts)\n",
2824 epn->id, epn->is_in ? "in" : "out");
2825 usbf_epn_process_queue(epn);
2828 dev_dbg(epn->udc->dev, "ep%u %s done status=0x%08x. next=0x%08x\n",
2829 epn->id, epn->is_in ? "in" : "out",
2830 sts, usbf_ep_reg_readl(epn, USBF_REG_EPN_STATUS));
2833 static void usbf_ep_reset(struct usbf_ep *ep)
2836 /* Remove anything that was pending */
2837 usbf_ep_nuke(ep, -ESHUTDOWN);
2840 static void usbf_reset(struct usbf_udc *udc)
2844 for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
2845 if (udc->ep[i].disabled)
2848 usbf_ep_reset(&udc->ep[i]);
2851 if (usbf_reg_readl(udc, USBF_REG_USB_STATUS) & USBF_USB_SPEED_MODE)
2852 udc->gadget.speed = USB_SPEED_HIGH;
2854 udc->gadget.speed = USB_SPEED_FULL;
2856 /* Remote wakeup feature must be disabled on USB bus reset */
2857 udc->is_remote_wakeup = false;
2859 /* Enable endpoint zero */
2860 usbf_ep0_enable(&udc->ep[0]);
2863 /* Signal the reset */
2864 spin_unlock(&udc->lock);
2865 usb_gadget_udc_reset(&udc->gadget, udc->driver);
2866 spin_lock(&udc->lock);
2870 static void usbf_driver_suspend(struct usbf_udc *udc)
2872 if (udc->is_usb_suspended) {
2873 dev_dbg(udc->dev, "already suspended\n");
2877 dev_dbg(udc->dev, "do usb suspend\n");
2878 udc->is_usb_suspended = true;
2880 if (udc->driver && udc->driver->suspend) {
2881 spin_unlock(&udc->lock);
2882 udc->driver->suspend(&udc->gadget);
2883 spin_lock(&udc->lock);
2885 /* The datasheet tells to set the USB_CONTROL register SUSPEND
2886 * bit when the USB bus suspend is detected.
2887 * This bit stops the clocks (clocks for EPC, SIE, USBPHY) but
2888 * these clocks seems not used only by the USB device. Some
2889 * UARTs can be lost ...
2890 * So, do not set the USB_CONTROL register SUSPEND bit.
2895 static void usbf_driver_resume(struct usbf_udc *udc)
2897 if (!udc->is_usb_suspended)
2900 dev_dbg(udc->dev, "do usb resume\n");
2901 udc->is_usb_suspended = false;
2903 if (udc->driver && udc->driver->resume) {
2904 spin_unlock(&udc->lock);
2905 udc->driver->resume(&udc->gadget);
2906 spin_lock(&udc->lock);
2910 static irqreturn_t usbf_epc_irq(int irq, void *_udc)
2912 struct usbf_udc *udc = (struct usbf_udc *)_udc;
2913 unsigned long flags;
2919 spin_lock_irqsave(&udc->lock, flags);
2921 int_en = usbf_reg_readl(udc, USBF_REG_USB_INT_ENA);
2922 int_sts = usbf_reg_readl(udc, USBF_REG_USB_INT_STA) & int_en;
2923 usbf_reg_writel(udc, USBF_REG_USB_INT_STA, ~int_sts);
2925 dev_dbg(udc->dev, "int_sts=0x%08x\n", int_sts);
2927 if (int_sts & USBF_USB_RSUM_INT) {
2928 dev_dbg(udc->dev, "handle resume\n");
2929 usbf_driver_resume(udc);
2932 if (int_sts & USBF_USB_USB_RST_INT) {
2933 dev_dbg(udc->dev, "handle bus reset\n");
2934 usbf_driver_resume(udc);
2938 if (int_sts & USBF_USB_SPEED_MODE_INT) {
2939 if (usbf_reg_readl(udc, USBF_REG_USB_STATUS) & USBF_USB_SPEED_MODE)
2940 udc->gadget.speed = USB_SPEED_HIGH;
2942 udc->gadget.speed = USB_SPEED_FULL;
2943 dev_dbg(udc->dev, "handle speed change (%s)\n",
2944 udc->gadget.speed == USB_SPEED_HIGH ? "High" : "Full");
2947 if (int_sts & USBF_USB_EPN_INT(0)) {
2948 usbf_driver_resume(udc);
2949 usbf_ep0_interrupt(&udc->ep[0]);
2952 for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
2955 if (int_sts & USBF_USB_EPN_INT(i)) {
2956 usbf_driver_resume(udc);
2957 usbf_epn_interrupt(ep);
2961 if (int_sts & USBF_USB_SPND_INT) {
2962 dev_dbg(udc->dev, "handle suspend\n");
2963 usbf_driver_suspend(udc);
2966 spin_unlock_irqrestore(&udc->lock, flags);
2971 static irqreturn_t usbf_ahb_epc_irq(int irq, void *_udc)
2973 struct usbf_udc *udc = (struct usbf_udc *)_udc;
2974 unsigned long flags;
2975 struct usbf_ep *epn;
2977 void (*ep_action)(struct usbf_ep *epn);
2980 spin_lock_irqsave(&udc->lock, flags);
2982 /* Read and ack interrupts */
2983 sysbint = usbf_reg_readl(udc, USBF_REG_AHBBINT);
2984 usbf_reg_writel(udc, USBF_REG_AHBBINT, sysbint);
2986 if ((sysbint & USBF_SYS_VBUS_INT) == USBF_SYS_VBUS_INT) {
2987 if (usbf_reg_readl(udc, USBF_REG_EPCTR) & USBF_SYS_VBUS_LEVEL) {
2988 dev_dbg(udc->dev, "handle vbus (1)\n");
2989 spin_unlock(&udc->lock);
2990 usb_udc_vbus_handler(&udc->gadget, true);
2991 usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
2992 spin_lock(&udc->lock);
2994 dev_dbg(udc->dev, "handle vbus (0)\n");
2995 udc->is_usb_suspended = false;
2996 spin_unlock(&udc->lock);
2997 usb_udc_vbus_handler(&udc->gadget, false);
2998 usb_gadget_set_state(&udc->gadget,
2999 USB_STATE_NOTATTACHED);
3000 spin_lock(&udc->lock);
3004 for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
3005 if (sysbint & USBF_SYS_DMA_ENDINT_EPN(i)) {
3007 dev_dbg(epn->udc->dev,
3008 "ep%u handle DMA complete. action=%ps\n",
3009 epn->id, epn->bridge_on_dma_end);
3010 ep_action = epn->bridge_on_dma_end;
3012 epn->bridge_on_dma_end = NULL;
3018 spin_unlock_irqrestore(&udc->lock, flags);
3023 static int usbf_udc_start(struct usb_gadget *gadget,
3024 struct usb_gadget_driver *driver)
3026 struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
3027 unsigned long flags;
3029 dev_info(udc->dev, "start (driver '%s')\n", driver->driver.name);
3031 spin_lock_irqsave(&udc->lock, flags);
3033 /* hook up the driver */
3034 udc->driver = driver;
3036 /* Enable VBUS interrupt */
3037 usbf_reg_writel(udc, USBF_REG_AHBBINTEN, USBF_SYS_VBUS_INTEN);
3039 spin_unlock_irqrestore(&udc->lock, flags);
3044 static int usbf_udc_stop(struct usb_gadget *gadget)
3046 struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
3047 unsigned long flags;
3049 spin_lock_irqsave(&udc->lock, flags);
3051 /* Disable VBUS interrupt */
3052 usbf_reg_writel(udc, USBF_REG_AHBBINTEN, 0);
3056 spin_unlock_irqrestore(&udc->lock, flags);
3058 dev_info(udc->dev, "stopped\n");
3063 static int usbf_get_frame(struct usb_gadget *gadget)
3065 struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
3067 return USBF_USB_GET_FRAME(usbf_reg_readl(udc, USBF_REG_USB_ADDRESS));
3070 static void usbf_attach(struct usbf_udc *udc)
3072 /* Enable USB signal to Function PHY
3074 * Disable endpoint 0, it will be automatically enable when a USB reset
3076 * Disable the other endpoints
3078 usbf_reg_clrset(udc, USBF_REG_USB_CONTROL,
3079 USBF_USB_CONNECTB | USBF_USB_DEFAULT | USBF_USB_CONF,
3082 /* Enable reset and mode change interrupts */
3083 usbf_reg_bitset(udc, USBF_REG_USB_INT_ENA,
3084 USBF_USB_USB_RST_EN | USBF_USB_SPEED_MODE_EN | USBF_USB_RSUM_EN | USBF_USB_SPND_EN);
3087 static void usbf_detach(struct usbf_udc *udc)
3091 /* Disable interrupts */
3092 usbf_reg_writel(udc, USBF_REG_USB_INT_ENA, 0);
3094 for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
3095 if (udc->ep[i].disabled)
3098 usbf_ep_reset(&udc->ep[i]);
3101 /* Disable USB signal to Function PHY
3102 * Do not Pull-up D+ signal
3103 * Disable endpoint 0
3104 * Disable the other endpoints
3106 usbf_reg_clrset(udc, USBF_REG_USB_CONTROL,
3107 USBF_USB_PUE2 | USBF_USB_DEFAULT | USBF_USB_CONF,
3111 static int usbf_pullup(struct usb_gadget *gadget, int is_on)
3113 struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
3114 unsigned long flags;
3116 dev_dbg(udc->dev, "pullup %d\n", is_on);
3118 spin_lock_irqsave(&udc->lock, flags);
3123 spin_unlock_irqrestore(&udc->lock, flags);
3128 static int usbf_udc_set_selfpowered(struct usb_gadget *gadget,
3131 struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
3132 unsigned long flags;
3134 spin_lock_irqsave(&udc->lock, flags);
3135 gadget->is_selfpowered = (is_selfpowered != 0);
3136 spin_unlock_irqrestore(&udc->lock, flags);
3141 static int usbf_udc_wakeup(struct usb_gadget *gadget)
3143 struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
3144 unsigned long flags;
3147 spin_lock_irqsave(&udc->lock, flags);
3149 if (!udc->is_remote_wakeup) {
3150 dev_dbg(udc->dev, "remote wakeup not allowed\n");
3155 dev_dbg(udc->dev, "do wakeup\n");
3157 /* Send the resume signal */
3158 usbf_reg_bitset(udc, USBF_REG_USB_CONTROL, USBF_USB_RSUM_IN);
3159 usbf_reg_bitclr(udc, USBF_REG_USB_CONTROL, USBF_USB_RSUM_IN);
3163 spin_unlock_irqrestore(&udc->lock, flags);
3167 static struct usb_gadget_ops usbf_gadget_ops = {
3168 .get_frame = usbf_get_frame,
3169 .pullup = usbf_pullup,
3170 .udc_start = usbf_udc_start,
3171 .udc_stop = usbf_udc_stop,
3172 .set_selfpowered = usbf_udc_set_selfpowered,
3173 .wakeup = usbf_udc_wakeup,
3176 static int usbf_epn_check(struct usbf_ep *epn)
3178 const char *type_txt;
3179 const char *buf_txt;
3183 ctrl = usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL);
3185 switch (ctrl & USBF_EPN_MODE_MASK) {
3186 case USBF_EPN_MODE_BULK:
3188 if (epn->ep.caps.type_control || epn->ep.caps.type_iso ||
3189 !epn->ep.caps.type_bulk || epn->ep.caps.type_int) {
3190 dev_err(epn->udc->dev,
3191 "ep%u caps mismatch, bulk expected\n", epn->id);
3195 case USBF_EPN_MODE_INTR:
3197 if (epn->ep.caps.type_control || epn->ep.caps.type_iso ||
3198 epn->ep.caps.type_bulk || !epn->ep.caps.type_int) {
3199 dev_err(epn->udc->dev,
3200 "ep%u caps mismatch, int expected\n", epn->id);
3204 case USBF_EPN_MODE_ISO:
3206 if (epn->ep.caps.type_control || !epn->ep.caps.type_iso ||
3207 epn->ep.caps.type_bulk || epn->ep.caps.type_int) {
3208 dev_err(epn->udc->dev,
3209 "ep%u caps mismatch, iso expected\n", epn->id);
3214 type_txt = "unknown";
3215 dev_err(epn->udc->dev, "ep%u unknown type\n", epn->id);
3220 if (ctrl & USBF_EPN_BUF_TYPE_DOUBLE) {
3222 if (!usbf_ep_info[epn->id].is_double) {
3223 dev_err(epn->udc->dev,
3224 "ep%u buffer mismatch, double expected\n",
3230 if (usbf_ep_info[epn->id].is_double) {
3231 dev_err(epn->udc->dev,
3232 "ep%u buffer mismatch, single expected\n",
3238 dev_dbg(epn->udc->dev, "ep%u (%s) %s, %s buffer %u, checked %s\n",
3239 epn->id, epn->ep.name, type_txt, buf_txt,
3240 epn->ep.maxpacket_limit, ret ? "failed" : "ok");
3245 static int usbf_probe(struct platform_device *pdev)
3247 struct device *dev = &pdev->dev;
3248 struct usbf_udc *udc;
3254 udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL);
3257 platform_set_drvdata(pdev, udc);
3260 spin_lock_init(&udc->lock);
3262 udc->regs = devm_platform_ioremap_resource(pdev, 0);
3263 if (IS_ERR(udc->regs))
3264 return PTR_ERR(udc->regs);
3266 devm_pm_runtime_enable(&pdev->dev);
3267 ret = pm_runtime_resume_and_get(&pdev->dev);
3271 dev_info(dev, "USBF version: %08x\n",
3272 usbf_reg_readl(udc, USBF_REG_USBSSVER));
3274 /* Resetting the PLL is handled via the clock driver as it has common
3275 * registers with USB Host
3277 usbf_reg_bitclr(udc, USBF_REG_EPCTR, USBF_SYS_EPC_RST);
3279 /* modify in register gadget process */
3280 udc->gadget.speed = USB_SPEED_FULL;
3281 udc->gadget.max_speed = USB_SPEED_HIGH;
3282 udc->gadget.ops = &usbf_gadget_ops;
3284 udc->gadget.name = dev->driver->name;
3285 udc->gadget.dev.parent = dev;
3286 udc->gadget.ep0 = &udc->ep[0].ep;
3288 /* The hardware DMA controller needs dma addresses aligned on 32bit.
3289 * A fallback to pio is done if DMA addresses are not aligned.
3291 udc->gadget.quirk_avoids_skb_reserve = 1;
3293 INIT_LIST_HEAD(&udc->gadget.ep_list);
3294 /* we have a canned request structure to allow sending packets as reply
3295 * to get_status requests
3297 INIT_LIST_HEAD(&udc->setup_reply.queue);
3299 for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
3302 if (!(usbf_reg_readl(udc, USBF_REG_USBSSCONF) &
3303 USBF_SYS_EP_AVAILABLE(i))) {
3307 INIT_LIST_HEAD(&ep->queue);
3312 ep->ep.ops = &usbf_ep_ops;
3313 ep->ep.name = usbf_ep_info[i].name;
3314 ep->ep.caps = usbf_ep_info[i].caps;
3315 usb_ep_set_maxpacket_limit(&ep->ep,
3316 usbf_ep_info[i].maxpacket_limit);
3319 ep->regs = ep->udc->regs + USBF_BASE_EP0;
3321 ep->regs = ep->udc->regs + USBF_BASE_EPN(ep->id - 1);
3322 ret = usbf_epn_check(ep);
3325 if (usbf_reg_readl(udc, USBF_REG_USBSSCONF) &
3326 USBF_SYS_DMA_AVAILABLE(i)) {
3327 ep->dma_regs = ep->udc->regs +
3328 USBF_BASE_DMA_EPN(ep->id - 1);
3330 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3334 irq = platform_get_irq(pdev, 0);
3337 ret = devm_request_irq(dev, irq, usbf_epc_irq, 0, "usbf-epc", udc);
3339 dev_err(dev, "cannot request irq %d err %d\n", irq, ret);
3343 irq = platform_get_irq(pdev, 1);
3346 ret = devm_request_irq(dev, irq, usbf_ahb_epc_irq, 0, "usbf-ahb-epc", udc);
3348 dev_err(dev, "cannot request irq %d err %d\n", irq, ret);
3352 usbf_reg_bitset(udc, USBF_REG_AHBMCTR, USBF_SYS_WBURST_TYPE);
3354 usbf_reg_bitset(udc, USBF_REG_USB_CONTROL,
3355 USBF_USB_INT_SEL | USBF_USB_SOF_RCV | USBF_USB_SOF_CLK_MODE);
3357 ret = usb_add_gadget_udc(dev, &udc->gadget);
3364 static int usbf_remove(struct platform_device *pdev)
3366 struct usbf_udc *udc = platform_get_drvdata(pdev);
3368 usb_del_gadget_udc(&udc->gadget);
3370 pm_runtime_put(&pdev->dev);
3375 static const struct of_device_id usbf_match[] = {
3376 { .compatible = "renesas,rzn1-usbf" },
3379 MODULE_DEVICE_TABLE(of, usbf_match);
3381 static struct platform_driver udc_driver = {
3383 .name = "usbf_renesas",
3384 .owner = THIS_MODULE,
3385 .of_match_table = usbf_match,
3387 .probe = usbf_probe,
3388 .remove = usbf_remove,
3391 module_platform_driver(udc_driver);
3394 MODULE_DESCRIPTION("Renesas R-Car Gen3 & RZ/N1 USB Function driver");
3395 MODULE_LICENSE("GPL");