blob: 4266bd630c30f26e86f4c391cb72fd9bec8c9899 [file] [log] [blame]
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cadence USBSS DRD Driver - gadget side.
4 *
5 * Copyright (C) 2018 Cadence Design Systems.
6 * Copyright (C) 2017-2018 NXP
7 *
8 * Authors: Pawel Jez <pjez@cadence.com>,
9 * Pawel Laszczak <pawell@cadence.com>
10 * Peter Chen <peter.chen@nxp.com>
11 */
12
Vignesh Raghavendra42360c42019-12-05 13:29:25 +053013#include <cpu_func.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +053016#include <linux/usb/composite.h>
17#include <linux/iopoll.h>
18
19#include "gadget.h"
20#include "trace.h"
21
22#define readl_poll_timeout_atomic readl_poll_timeout
23#define usleep_range(a, b) udelay((b))
24
25static struct usb_endpoint_descriptor cdns3_gadget_ep0_desc = {
26 .bLength = USB_DT_ENDPOINT_SIZE,
27 .bDescriptorType = USB_DT_ENDPOINT,
28 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
29};
30
31/**
32 * cdns3_ep0_run_transfer - Do transfer on default endpoint hardware
33 * @priv_dev: extended gadget object
34 * @dma_addr: physical address where data is/will be stored
35 * @length: data length
36 * @erdy: set it to 1 when ERDY packet should be sent -
37 * exit from flow control state
38 */
39static void cdns3_ep0_run_transfer(struct cdns3_device *priv_dev,
40 dma_addr_t dma_addr,
41 unsigned int length, int erdy, int zlp)
42{
43 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
44 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
45
46 priv_ep->trb_pool[0].buffer = TRB_BUFFER(dma_addr);
47 priv_ep->trb_pool[0].length = TRB_LEN(length);
48
49 if (zlp) {
50 priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_TYPE(TRB_NORMAL);
51 priv_ep->trb_pool[1].buffer = TRB_BUFFER(dma_addr);
52 priv_ep->trb_pool[1].length = TRB_LEN(0);
53 priv_ep->trb_pool[1].control = TRB_CYCLE | TRB_IOC |
54 TRB_TYPE(TRB_NORMAL);
55 } else {
56 priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_IOC |
57 TRB_TYPE(TRB_NORMAL);
58 priv_ep->trb_pool[1].control = 0;
59 }
60
61 /* Flush both TRBs */
62 flush_dcache_range((unsigned long)priv_ep->trb_pool,
63 (unsigned long)priv_ep->trb_pool +
64 ROUND(sizeof(struct cdns3_trb) * 2,
65 CONFIG_SYS_CACHELINE_SIZE));
66
67 trace_cdns3_prepare_trb(priv_ep, priv_ep->trb_pool);
68
69 cdns3_select_ep(priv_dev, priv_dev->ep0_data_dir);
70
71 writel(EP_STS_TRBERR, &regs->ep_sts);
72 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma), &regs->ep_traddr);
73 trace_cdns3_doorbell_ep0(priv_dev->ep0_data_dir ? "ep0in" : "ep0out",
74 readl(&regs->ep_traddr));
75
76 /* TRB should be prepared before starting transfer. */
77 writel(EP_CMD_DRDY, &regs->ep_cmd);
78
79 /* Resume controller before arming transfer. */
80 __cdns3_gadget_wakeup(priv_dev);
81
82 if (erdy)
83 writel(EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
84}
85
86/**
87 * cdns3_ep0_delegate_req - Returns status of handling setup packet
88 * Setup is handled by gadget driver
89 * @priv_dev: extended gadget object
90 * @ctrl_req: pointer to received setup packet
91 *
92 * Returns zero on success or negative value on failure
93 */
94static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev,
95 struct usb_ctrlrequest *ctrl_req)
96{
97 int ret;
98
99 spin_unlock(&priv_dev->lock);
100 priv_dev->setup_pending = 1;
101 ret = priv_dev->gadget_driver->setup(&priv_dev->gadget, ctrl_req);
102 priv_dev->setup_pending = 0;
103 spin_lock(&priv_dev->lock);
104 return ret;
105}
106
107static void cdns3_prepare_setup_packet(struct cdns3_device *priv_dev)
108{
109 priv_dev->ep0_data_dir = 0;
110 priv_dev->ep0_stage = CDNS3_SETUP_STAGE;
111 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
112 sizeof(struct usb_ctrlrequest), 0, 0);
113}
114
115static void cdns3_ep0_complete_setup(struct cdns3_device *priv_dev,
116 u8 send_stall, u8 send_erdy)
117{
118 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
119 struct usb_request *request;
120
121 request = cdns3_next_request(&priv_ep->pending_req_list);
122 if (request)
123 list_del_init(&request->list);
124
125 if (send_stall) {
126 trace_cdns3_halt(priv_ep, send_stall, 0);
127 /* set_stall on ep0 */
128 cdns3_select_ep(priv_dev, 0x00);
129 writel(EP_CMD_SSTALL, &priv_dev->regs->ep_cmd);
130 } else {
131 cdns3_prepare_setup_packet(priv_dev);
132 }
133
134 priv_dev->ep0_stage = CDNS3_SETUP_STAGE;
135 writel((send_erdy ? EP_CMD_ERDY : 0) | EP_CMD_REQ_CMPL,
136 &priv_dev->regs->ep_cmd);
137
138 cdns3_allow_enable_l1(priv_dev, 1);
139}
140
141/**
142 * cdns3_req_ep0_set_configuration - Handling of SET_CONFIG standard USB request
143 * @priv_dev: extended gadget object
144 * @ctrl_req: pointer to received setup packet
145 *
146 * Returns 0 if success, USB_GADGET_DELAYED_STATUS on deferred status stage,
147 * error code on error
148 */
149static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev,
150 struct usb_ctrlrequest *ctrl_req)
151{
152 enum usb_device_state device_state = priv_dev->gadget.state;
153 struct cdns3_endpoint *priv_ep;
154 u32 config = le16_to_cpu(ctrl_req->wValue);
155 int result = 0;
156 int i;
157
158 switch (device_state) {
159 case USB_STATE_ADDRESS:
160 /* Configure non-control EPs */
161 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
162 priv_ep = priv_dev->eps[i];
163 if (!priv_ep)
164 continue;
165
166 if (priv_ep->flags & EP_CLAIMED)
167 cdns3_ep_config(priv_ep);
168 }
169
170 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
171
172 if (result)
173 return result;
174
175 if (config) {
176 cdns3_set_hw_configuration(priv_dev);
177 } else {
178 cdns3_hw_reset_eps_config(priv_dev);
179 usb_gadget_set_state(&priv_dev->gadget,
180 USB_STATE_ADDRESS);
181 }
182 break;
183 case USB_STATE_CONFIGURED:
184 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
185
186 if (!config && !result) {
187 cdns3_hw_reset_eps_config(priv_dev);
188 usb_gadget_set_state(&priv_dev->gadget,
189 USB_STATE_ADDRESS);
190 }
191 break;
192 default:
193 result = -EINVAL;
194 }
195
196 return result;
197}
198
199/**
200 * cdns3_req_ep0_set_address - Handling of SET_ADDRESS standard USB request
201 * @priv_dev: extended gadget object
202 * @ctrl_req: pointer to received setup packet
203 *
204 * Returns 0 if success, error code on error
205 */
206static int cdns3_req_ep0_set_address(struct cdns3_device *priv_dev,
207 struct usb_ctrlrequest *ctrl_req)
208{
209 enum usb_device_state device_state = priv_dev->gadget.state;
210 u32 reg;
211 u32 addr;
212
213 addr = le16_to_cpu(ctrl_req->wValue);
214
215 if (addr > USB_DEVICE_MAX_ADDRESS) {
216 dev_err(priv_dev->dev,
217 "Device address (%d) cannot be greater than %d\n",
218 addr, USB_DEVICE_MAX_ADDRESS);
219 return -EINVAL;
220 }
221
222 if (device_state == USB_STATE_CONFIGURED) {
223 dev_err(priv_dev->dev,
224 "can't set_address from configured state\n");
225 return -EINVAL;
226 }
227
228 reg = readl(&priv_dev->regs->usb_cmd);
229
230 writel(reg | USB_CMD_FADDR(addr) | USB_CMD_SET_ADDR,
231 &priv_dev->regs->usb_cmd);
232
233 usb_gadget_set_state(&priv_dev->gadget,
234 (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT));
235
236 return 0;
237}
238
239/**
240 * cdns3_req_ep0_get_status - Handling of GET_STATUS standard USB request
241 * @priv_dev: extended gadget object
242 * @ctrl_req: pointer to received setup packet
243 *
244 * Returns 0 if success, error code on error
245 */
246static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev,
247 struct usb_ctrlrequest *ctrl)
248{
249 __le16 *response_pkt;
250 u16 usb_status = 0;
251 u32 recip;
252
253 recip = ctrl->bRequestType & USB_RECIP_MASK;
254
255 switch (recip) {
256 case USB_RECIP_DEVICE:
257 /* self powered */
258 if (priv_dev->is_selfpowered)
259 usb_status = BIT(USB_DEVICE_SELF_POWERED);
260
261 if (priv_dev->wake_up_flag)
262 usb_status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
263
264 if (priv_dev->gadget.speed != USB_SPEED_SUPER)
265 break;
266
267 if (priv_dev->u1_allowed)
268 usb_status |= BIT(USB_DEV_STAT_U1_ENABLED);
269
270 if (priv_dev->u2_allowed)
271 usb_status |= BIT(USB_DEV_STAT_U2_ENABLED);
272
273 break;
274 case USB_RECIP_INTERFACE:
275 return cdns3_ep0_delegate_req(priv_dev, ctrl);
276 case USB_RECIP_ENDPOINT:
277 /* check if endpoint is stalled */
278 cdns3_select_ep(priv_dev, ctrl->wIndex);
279 if (EP_STS_STALL(readl(&priv_dev->regs->ep_sts)))
280 usb_status = BIT(USB_ENDPOINT_HALT);
281 break;
282 default:
283 return -EINVAL;
284 }
285
286 response_pkt = (__le16 *)priv_dev->setup_buf;
287 *response_pkt = cpu_to_le16(usb_status);
288
289 /* Flush setup response */
290 flush_dcache_range((unsigned long)priv_dev->setup_buf,
291 (unsigned long)priv_dev->setup_buf +
292 ROUND(sizeof(struct usb_ctrlrequest),
293 CONFIG_SYS_CACHELINE_SIZE));
294
295 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
296 sizeof(*response_pkt), 1, 0);
297 return 0;
298}
299
300static int cdns3_ep0_feature_handle_device(struct cdns3_device *priv_dev,
301 struct usb_ctrlrequest *ctrl,
302 int set)
303{
304 enum usb_device_state state;
305 enum usb_device_speed speed;
306 int ret = 0;
307 u16 tmode;
308
309 state = priv_dev->gadget.state;
310 speed = priv_dev->gadget.speed;
311
312 switch (ctrl->wValue) {
313 case USB_DEVICE_REMOTE_WAKEUP:
314 priv_dev->wake_up_flag = !!set;
315 break;
316 case USB_DEVICE_U1_ENABLE:
317 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
318 return -EINVAL;
319
320 priv_dev->u1_allowed = !!set;
321 break;
322 case USB_DEVICE_U2_ENABLE:
323 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
324 return -EINVAL;
325
326 priv_dev->u2_allowed = !!set;
327 break;
328 case USB_DEVICE_LTM_ENABLE:
329 ret = -EINVAL;
330 break;
331 case USB_DEVICE_TEST_MODE:
332 if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
333 return -EINVAL;
334
335 tmode = le16_to_cpu(ctrl->wIndex);
336
337 if (!set || (tmode & 0xff) != 0)
338 return -EINVAL;
339
340 switch (tmode >> 8) {
341 case TEST_J:
342 case TEST_K:
343 case TEST_SE0_NAK:
344 case TEST_PACKET:
345 cdns3_ep0_complete_setup(priv_dev, 0, 1);
346 /**
347 * Little delay to give the controller some time
348 * for sending status stage.
349 * This time should be less then 3ms.
350 */
351 usleep_range(1000, 2000);
352 cdns3_set_register_bit(&priv_dev->regs->usb_cmd,
353 USB_CMD_STMODE |
354 USB_STS_TMODE_SEL(tmode - 1));
355 break;
356 default:
357 ret = -EINVAL;
358 }
359 break;
360 default:
361 ret = -EINVAL;
362 }
363
364 return ret;
365}
366
367static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev,
368 struct usb_ctrlrequest *ctrl,
369 int set)
370{
371 u32 wValue;
372 int ret = 0;
373
374 wValue = le16_to_cpu(ctrl->wValue);
375
376 switch (wValue) {
377 case USB_INTRF_FUNC_SUSPEND:
378 break;
379 default:
380 ret = -EINVAL;
381 }
382
383 return ret;
384}
385
386static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev,
387 struct usb_ctrlrequest *ctrl,
388 int set)
389{
390 struct cdns3_endpoint *priv_ep;
391 int ret = 0;
392 u8 index;
393
394 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
395 return -EINVAL;
396
397 if (!(ctrl->wIndex & ~USB_DIR_IN))
398 return 0;
399
400 index = cdns3_ep_addr_to_index(ctrl->wIndex);
401 priv_ep = priv_dev->eps[index];
402
403 cdns3_select_ep(priv_dev, ctrl->wIndex);
404
405 if (set)
406 __cdns3_gadget_ep_set_halt(priv_ep);
407 else if (!(priv_ep->flags & EP_WEDGE))
408 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
409
410 cdns3_select_ep(priv_dev, 0x00);
411
412 return ret;
413}
414
415/**
416 * cdns3_req_ep0_handle_feature -
417 * Handling of GET/SET_FEATURE standard USB request
418 *
419 * @priv_dev: extended gadget object
420 * @ctrl_req: pointer to received setup packet
421 * @set: must be set to 1 for SET_FEATURE request
422 *
423 * Returns 0 if success, error code on error
424 */
425static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev,
426 struct usb_ctrlrequest *ctrl,
427 int set)
428{
429 int ret = 0;
430 u32 recip;
431
432 recip = ctrl->bRequestType & USB_RECIP_MASK;
433
434 switch (recip) {
435 case USB_RECIP_DEVICE:
436 ret = cdns3_ep0_feature_handle_device(priv_dev, ctrl, set);
437 break;
438 case USB_RECIP_INTERFACE:
439 ret = cdns3_ep0_feature_handle_intf(priv_dev, ctrl, set);
440 break;
441 case USB_RECIP_ENDPOINT:
442 ret = cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set);
443 break;
444 default:
445 return -EINVAL;
446 }
447
448 return ret;
449}
450
451/**
452 * cdns3_req_ep0_set_sel - Handling of SET_SEL standard USB request
453 * @priv_dev: extended gadget object
454 * @ctrl_req: pointer to received setup packet
455 *
456 * Returns 0 if success, error code on error
457 */
458static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev,
459 struct usb_ctrlrequest *ctrl_req)
460{
461 if (priv_dev->gadget.state < USB_STATE_ADDRESS)
462 return -EINVAL;
463
464 if (ctrl_req->wLength != 6) {
465 dev_err(priv_dev->dev, "Set SEL should be 6 bytes, got %d\n",
466 ctrl_req->wLength);
467 return -EINVAL;
468 }
469
470 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0);
471 return 0;
472}
473
474/**
475 * cdns3_req_ep0_set_isoch_delay -
476 * Handling of GET_ISOCH_DELAY standard USB request
477 * @priv_dev: extended gadget object
478 * @ctrl_req: pointer to received setup packet
479 *
480 * Returns 0 if success, error code on error
481 */
482static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev,
483 struct usb_ctrlrequest *ctrl_req)
484{
485 if (ctrl_req->wIndex || ctrl_req->wLength)
486 return -EINVAL;
487
488 priv_dev->isoch_delay = ctrl_req->wValue;
489
490 return 0;
491}
492
493/**
494 * cdns3_ep0_standard_request - Handling standard USB requests
495 * @priv_dev: extended gadget object
496 * @ctrl_req: pointer to received setup packet
497 *
498 * Returns 0 if success, error code on error
499 */
500static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev,
501 struct usb_ctrlrequest *ctrl_req)
502{
503 int ret;
504
505 switch (ctrl_req->bRequest) {
506 case USB_REQ_SET_ADDRESS:
507 ret = cdns3_req_ep0_set_address(priv_dev, ctrl_req);
508 break;
509 case USB_REQ_SET_CONFIGURATION:
510 ret = cdns3_req_ep0_set_configuration(priv_dev, ctrl_req);
511 break;
512 case USB_REQ_GET_STATUS:
513 ret = cdns3_req_ep0_get_status(priv_dev, ctrl_req);
514 break;
515 case USB_REQ_CLEAR_FEATURE:
516 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0);
517 break;
518 case USB_REQ_SET_FEATURE:
519 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 1);
520 break;
521 case USB_REQ_SET_SEL:
522 ret = cdns3_req_ep0_set_sel(priv_dev, ctrl_req);
523 break;
524 case USB_REQ_SET_ISOCH_DELAY:
525 ret = cdns3_req_ep0_set_isoch_delay(priv_dev, ctrl_req);
526 break;
527 default:
528 ret = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
529 break;
530 }
531
532 return ret;
533}
534
535static void __pending_setup_status_handler(struct cdns3_device *priv_dev)
536{
537 struct usb_request *request = priv_dev->pending_status_request;
538
539 if (priv_dev->status_completion_no_call && request &&
540 request->complete) {
541 request->complete(&priv_dev->eps[0]->endpoint, request);
542 priv_dev->status_completion_no_call = 0;
543 }
544}
545
546void cdns3_pending_setup_status_handler(struct work_struct *work)
547{
548 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
549 pending_status_wq);
550 unsigned long flags;
551
552 spin_lock_irqsave(&priv_dev->lock, flags);
553 __pending_setup_status_handler(priv_dev);
554 spin_unlock_irqrestore(&priv_dev->lock, flags);
555}
556
557/**
558 * cdns3_ep0_setup_phase - Handling setup USB requests
559 * @priv_dev: extended gadget object
560 */
561static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev)
562{
563 struct usb_ctrlrequest *ctrl = priv_dev->setup_buf;
564 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
565 int result;
566
Vignesh Raghavendra6e0c11a2020-01-27 17:55:54 +0530567 /* Invalidate Setup Packet received */
568 invalidate_dcache_range(priv_dev->setup_dma,
569 priv_dev->setup_dma + ARCH_DMA_MINALIGN);
570
Vignesh Raghavendrab1a49282019-10-01 17:26:33 +0530571 priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN;
572
573 trace_cdns3_ctrl_req(ctrl);
574
575 if (!list_empty(&priv_ep->pending_req_list)) {
576 struct usb_request *request;
577
578 request = cdns3_next_request(&priv_ep->pending_req_list);
579 priv_ep->dir = priv_dev->ep0_data_dir;
580 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
581 -ECONNRESET);
582 }
583
584 if (le16_to_cpu(ctrl->wLength))
585 priv_dev->ep0_stage = CDNS3_DATA_STAGE;
586 else
587 priv_dev->ep0_stage = CDNS3_STATUS_STAGE;
588
589 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
590 result = cdns3_ep0_standard_request(priv_dev, ctrl);
591 else
592 result = cdns3_ep0_delegate_req(priv_dev, ctrl);
593
594 if (result == USB_GADGET_DELAYED_STATUS)
595 return;
596
597 if (result < 0)
598 cdns3_ep0_complete_setup(priv_dev, 1, 1);
599 else if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE)
600 cdns3_ep0_complete_setup(priv_dev, 0, 1);
601}
602
603static void cdns3_transfer_completed(struct cdns3_device *priv_dev)
604{
605 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
606
607 if (!list_empty(&priv_ep->pending_req_list)) {
608 struct usb_request *request;
609
610 trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool);
611 request = cdns3_next_request(&priv_ep->pending_req_list);
612
613 /* Invalidate TRB before accessing it */
614 invalidate_dcache_range((unsigned long)priv_ep->trb_pool,
615 (unsigned long)priv_ep->trb_pool +
616 ROUND(sizeof(struct cdns3_trb),
617 CONFIG_SYS_CACHELINE_SIZE));
618
619 request->actual =
620 TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length));
621
622 priv_ep->dir = priv_dev->ep0_data_dir;
623 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0);
624 }
625
626 cdns3_ep0_complete_setup(priv_dev, 0, 0);
627}
628
629/**
630 * cdns3_check_new_setup - Check if controller receive new SETUP packet.
631 * @priv_dev: extended gadget object
632 *
633 * The SETUP packet can be kept in on-chip memory or in system memory.
634 */
635static bool cdns3_check_new_setup(struct cdns3_device *priv_dev)
636{
637 u32 ep_sts_reg;
638
639 cdns3_select_ep(priv_dev, 0 | USB_DIR_OUT);
640 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
641
642 return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT));
643}
644
645/**
646 * cdns3_check_ep0_interrupt_proceed - Processes interrupt related to endpoint 0
647 * @priv_dev: extended gadget object
648 * @dir: USB_DIR_IN for IN direction, USB_DIR_OUT for OUT direction
649 */
650void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir)
651{
652 u32 ep_sts_reg;
653
654 cdns3_select_ep(priv_dev, dir);
655
656 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
657 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
658
659 trace_cdns3_ep0_irq(priv_dev, ep_sts_reg);
660
661 __pending_setup_status_handler(priv_dev);
662
663 if (ep_sts_reg & EP_STS_SETUP)
664 priv_dev->wait_for_setup = 1;
665
666 if (priv_dev->wait_for_setup && ep_sts_reg & EP_STS_IOC) {
667 priv_dev->wait_for_setup = 0;
668 cdns3_allow_enable_l1(priv_dev, 0);
669 cdns3_ep0_setup_phase(priv_dev);
670 } else if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP)) {
671 priv_dev->ep0_data_dir = dir;
672 cdns3_transfer_completed(priv_dev);
673 }
674
675 if (ep_sts_reg & EP_STS_DESCMIS) {
676 if (dir == 0 && !priv_dev->setup_pending)
677 cdns3_prepare_setup_packet(priv_dev);
678 }
679}
680
681/**
682 * cdns3_gadget_ep0_enable
683 * Function shouldn't be called by gadget driver,
684 * endpoint 0 is allways active
685 */
686static int cdns3_gadget_ep0_enable(struct usb_ep *ep,
687 const struct usb_endpoint_descriptor *desc)
688{
689 return -EINVAL;
690}
691
692/**
693 * cdns3_gadget_ep0_disable
694 * Function shouldn't be called by gadget driver,
695 * endpoint 0 is allways active
696 */
697static int cdns3_gadget_ep0_disable(struct usb_ep *ep)
698{
699 return -EINVAL;
700}
701
702/**
703 * cdns3_gadget_ep0_set_halt
704 * @ep: pointer to endpoint zero object
705 * @value: 1 for set stall, 0 for clear stall
706 *
707 * Returns 0
708 */
709static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
710{
711 /* TODO */
712 return 0;
713}
714
715/**
716 * cdns3_gadget_ep0_queue Transfer data on endpoint zero
717 * @ep: pointer to endpoint zero object
718 * @request: pointer to request object
719 * @gfp_flags: gfp flags
720 *
721 * Returns 0 on success, error code elsewhere
722 */
723static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
724 struct usb_request *request,
725 gfp_t gfp_flags)
726{
727 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
728 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
729 unsigned long flags;
730 int erdy_sent = 0;
731 int ret = 0;
732 u8 zlp = 0;
733
734 trace_cdns3_ep0_queue(priv_dev, request);
735
736 /* cancel the request if controller receive new SETUP packet. */
737 if (cdns3_check_new_setup(priv_dev))
738 return -ECONNRESET;
739
740 /* send STATUS stage. Should be called only for SET_CONFIGURATION */
741 if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) {
742 spin_lock_irqsave(&priv_dev->lock, flags);
743 cdns3_select_ep(priv_dev, 0x00);
744
745 erdy_sent = !priv_dev->hw_configured_flag;
746 cdns3_set_hw_configuration(priv_dev);
747
748 if (!erdy_sent)
749 cdns3_ep0_complete_setup(priv_dev, 0, 1);
750
751 cdns3_allow_enable_l1(priv_dev, 1);
752
753 request->actual = 0;
754 priv_dev->status_completion_no_call = true;
755 priv_dev->pending_status_request = request;
756 spin_unlock_irqrestore(&priv_dev->lock, flags);
757
758 /*
759 * Since there is no completion interrupt for status stage,
760 * it needs to call ->completion in software after
761 * ep0_queue is back.
762 */
763#ifndef __UBOOT__
764 queue_work(system_freezable_wq, &priv_dev->pending_status_wq);
765#else
766 __pending_setup_status_handler(priv_dev);
767#endif
768 return 0;
769 }
770
771 spin_lock_irqsave(&priv_dev->lock, flags);
772 if (!list_empty(&priv_ep->pending_req_list)) {
773 dev_err(priv_dev->dev,
774 "can't handle multiple requests for ep0\n");
775 spin_unlock_irqrestore(&priv_dev->lock, flags);
776 return -EBUSY;
777 }
778
779 ret = usb_gadget_map_request(&priv_dev->gadget, request,
780 priv_dev->ep0_data_dir);
781 if (ret) {
782 spin_unlock_irqrestore(&priv_dev->lock, flags);
783 dev_err(priv_dev->dev, "failed to map request\n");
784 return -EINVAL;
785 }
786
787 request->status = -EINPROGRESS;
788 list_add_tail(&request->list, &priv_ep->pending_req_list);
789
790 if (request->zero && request->length &&
791 (request->length % ep->maxpacket == 0))
792 zlp = 1;
793
794 cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp);
795
796 spin_unlock_irqrestore(&priv_dev->lock, flags);
797
798 return ret;
799}
800
801/**
802 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint
803 * @ep: endpoint object
804 *
805 * Returns 0
806 */
807int cdns3_gadget_ep_set_wedge(struct usb_ep *ep)
808{
809 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
810
811 dev_dbg(priv_dev->dev, "Wedge for %s\n", ep->name);
812 cdns3_gadget_ep_set_halt(ep, 1);
813 priv_ep->flags |= EP_WEDGE;
814
815 return 0;
816}
817
818const struct usb_ep_ops cdns3_gadget_ep0_ops = {
819 .enable = cdns3_gadget_ep0_enable,
820 .disable = cdns3_gadget_ep0_disable,
821 .alloc_request = cdns3_gadget_ep_alloc_request,
822 .free_request = cdns3_gadget_ep_free_request,
823 .queue = cdns3_gadget_ep0_queue,
824 .dequeue = cdns3_gadget_ep_dequeue,
825 .set_halt = cdns3_gadget_ep0_set_halt,
826 .set_wedge = cdns3_gadget_ep_set_wedge,
827};
828
829/**
830 * cdns3_ep0_config - Configures default endpoint
831 * @priv_dev: extended gadget object
832 *
833 * Functions sets parameters: maximal packet size and enables interrupts
834 */
835void cdns3_ep0_config(struct cdns3_device *priv_dev)
836{
837 struct cdns3_usb_regs __iomem *regs;
838 struct cdns3_endpoint *priv_ep;
839 u32 max_packet_size = 64;
840
841 regs = priv_dev->regs;
842
843 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
844 max_packet_size = 512;
845
846 priv_ep = priv_dev->eps[0];
847
848 if (!list_empty(&priv_ep->pending_req_list)) {
849 struct usb_request *request;
850
851 request = cdns3_next_request(&priv_ep->pending_req_list);
852 list_del_init(&request->list);
853 }
854
855 priv_dev->u1_allowed = 0;
856 priv_dev->u2_allowed = 0;
857
858 priv_dev->gadget.ep0->maxpacket = max_packet_size;
859 cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size);
860
861 /* init ep out */
862 cdns3_select_ep(priv_dev, USB_DIR_OUT);
863
864 if (priv_dev->dev_ver >= DEV_VER_V3) {
865 cdns3_set_register_bit(&priv_dev->regs->dtrans,
866 BIT(0) | BIT(16));
867 cdns3_set_register_bit(&priv_dev->regs->tdl_from_trb,
868 BIT(0) | BIT(16));
869 }
870
871 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
872 &regs->ep_cfg);
873
874 writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN,
875 &regs->ep_sts_en);
876
877 /* init ep in */
878 cdns3_select_ep(priv_dev, USB_DIR_IN);
879
880 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
881 &regs->ep_cfg);
882
883 writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, &regs->ep_sts_en);
884
885 cdns3_set_register_bit(&regs->usb_conf, USB_CONF_U1DS | USB_CONF_U2DS);
886}
887
888/**
889 * cdns3_init_ep0 Initializes software endpoint 0 of gadget
890 * @priv_dev: extended gadget object
891 * @ep_priv: extended endpoint object
892 *
893 * Returns 0 on success else error code.
894 */
895int cdns3_init_ep0(struct cdns3_device *priv_dev,
896 struct cdns3_endpoint *priv_ep)
897{
898 sprintf(priv_ep->name, "ep0");
899
900 /* fill linux fields */
901 priv_ep->endpoint.ops = &cdns3_gadget_ep0_ops;
902 priv_ep->endpoint.maxburst = 1;
903 usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
904 CDNS3_EP0_MAX_PACKET_LIMIT);
905#ifndef __UBOOT__
906 priv_ep->endpoint.address = 0;
907#endif
908 priv_ep->endpoint.caps.type_control = 1;
909 priv_ep->endpoint.caps.dir_in = 1;
910 priv_ep->endpoint.caps.dir_out = 1;
911 priv_ep->endpoint.name = priv_ep->name;
912 priv_ep->endpoint.desc = &cdns3_gadget_ep0_desc;
913 priv_dev->gadget.ep0 = &priv_ep->endpoint;
914 priv_ep->type = USB_ENDPOINT_XFER_CONTROL;
915
916 return cdns3_allocate_trb_pool(priv_ep);
917}