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