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