1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cadence CDNSP DRD Driver.
4 *
5 * Copyright (C) 2020 Cadence.
6 *
7 * Author: Pawel Laszczak <pawell@cadence.com>
8 *
9 */
10
11 #include <linux/moduleparam.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/module.h>
14 #include <linux/iopoll.h>
15 #include <linux/delay.h>
16 #include <linux/log2.h>
17 #include <linux/slab.h>
18 #include <linux/pci.h>
19 #include <linux/irq.h>
20 #include <linux/dmi.h>
21
22 #include "core.h"
23 #include "gadget-export.h"
24 #include "drd.h"
25 #include "cdnsp-gadget.h"
26 #include "cdnsp-trace.h"
27
cdnsp_port_speed(unsigned int port_status)28 unsigned int cdnsp_port_speed(unsigned int port_status)
29 {
30 /*Detect gadget speed based on PORTSC register*/
31 if (DEV_SUPERSPEEDPLUS(port_status) ||
32 DEV_SSP_GEN1x2(port_status) || DEV_SSP_GEN2x2(port_status))
33 return USB_SPEED_SUPER_PLUS;
34 else if (DEV_SUPERSPEED(port_status))
35 return USB_SPEED_SUPER;
36 else if (DEV_HIGHSPEED(port_status))
37 return USB_SPEED_HIGH;
38 else if (DEV_FULLSPEED(port_status))
39 return USB_SPEED_FULL;
40
41 /* If device is detached then speed will be USB_SPEED_UNKNOWN.*/
42 return USB_SPEED_UNKNOWN;
43 }
44
45 /*
46 * Given a port state, this function returns a value that would result in the
47 * port being in the same state, if the value was written to the port status
48 * control register.
49 * Save Read Only (RO) bits and save read/write bits where
50 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
51 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
52 */
cdnsp_port_state_to_neutral(u32 state)53 u32 cdnsp_port_state_to_neutral(u32 state)
54 {
55 /* Save read-only status and port state. */
56 return (state & CDNSP_PORT_RO) | (state & CDNSP_PORT_RWS);
57 }
58
59 /**
60 * cdnsp_find_next_ext_cap - Find the offset of the extended capabilities
61 * with capability ID id.
62 * @base: PCI MMIO registers base address.
63 * @start: Address at which to start looking, (0 or HCC_PARAMS to start at
64 * beginning of list)
65 * @id: Extended capability ID to search for.
66 *
67 * Returns the offset of the next matching extended capability structure.
68 * Some capabilities can occur several times,
69 * e.g., the EXT_CAPS_PROTOCOL, and this provides a way to find them all.
70 */
cdnsp_find_next_ext_cap(void __iomem * base,u32 start,int id)71 int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id)
72 {
73 u32 offset = start;
74 u32 next;
75 u32 val;
76
77 if (!start || start == HCC_PARAMS_OFFSET) {
78 val = readl(base + HCC_PARAMS_OFFSET);
79 if (val == ~0)
80 return 0;
81
82 offset = HCC_EXT_CAPS(val) << 2;
83 if (!offset)
84 return 0;
85 }
86
87 do {
88 val = readl(base + offset);
89 if (val == ~0)
90 return 0;
91
92 if (EXT_CAPS_ID(val) == id && offset != start)
93 return offset;
94
95 next = EXT_CAPS_NEXT(val);
96 offset += next << 2;
97 } while (next);
98
99 return 0;
100 }
101
cdnsp_set_link_state(struct cdnsp_device * pdev,__le32 __iomem * port_regs,u32 link_state)102 void cdnsp_set_link_state(struct cdnsp_device *pdev,
103 __le32 __iomem *port_regs,
104 u32 link_state)
105 {
106 int port_num = 0xFF;
107 u32 temp;
108
109 temp = readl(port_regs);
110 temp = cdnsp_port_state_to_neutral(temp);
111 temp |= PORT_WKCONN_E | PORT_WKDISC_E;
112 writel(temp, port_regs);
113
114 temp &= ~PORT_PLS_MASK;
115 temp |= PORT_LINK_STROBE | link_state;
116
117 if (pdev->active_port)
118 port_num = pdev->active_port->port_num;
119
120 trace_cdnsp_handle_port_status(port_num, readl(port_regs));
121 writel(temp, port_regs);
122 trace_cdnsp_link_state_changed(port_num, readl(port_regs));
123 }
124
cdnsp_disable_port(struct cdnsp_device * pdev,__le32 __iomem * port_regs)125 static void cdnsp_disable_port(struct cdnsp_device *pdev,
126 __le32 __iomem *port_regs)
127 {
128 u32 temp = cdnsp_port_state_to_neutral(readl(port_regs));
129
130 writel(temp | PORT_PED, port_regs);
131 }
132
cdnsp_clear_port_change_bit(struct cdnsp_device * pdev,__le32 __iomem * port_regs)133 static void cdnsp_clear_port_change_bit(struct cdnsp_device *pdev,
134 __le32 __iomem *port_regs)
135 {
136 u32 portsc = readl(port_regs);
137
138 writel(cdnsp_port_state_to_neutral(portsc) |
139 (portsc & PORT_CHANGE_BITS), port_regs);
140 }
141
cdnsp_set_apb_timeout_value(struct cdnsp_device * pdev)142 static void cdnsp_set_apb_timeout_value(struct cdnsp_device *pdev)
143 {
144 struct cdns *cdns = dev_get_drvdata(pdev->dev);
145 __le32 __iomem *reg;
146 void __iomem *base;
147 u32 offset = 0;
148 u32 val;
149
150 if (!cdns->override_apb_timeout)
151 return;
152
153 base = &pdev->cap_regs->hc_capbase;
154 offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
155 reg = base + offset + REG_CHICKEN_BITS_3_OFFSET;
156
157 val = le32_to_cpu(readl(reg));
158 val = CHICKEN_APB_TIMEOUT_SET(val, cdns->override_apb_timeout);
159 writel(cpu_to_le32(val), reg);
160 }
161
cdnsp_set_chicken_bits_2(struct cdnsp_device * pdev,u32 bit)162 static void cdnsp_set_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
163 {
164 __le32 __iomem *reg;
165 void __iomem *base;
166 u32 offset = 0;
167
168 base = &pdev->cap_regs->hc_capbase;
169 offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
170 reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
171
172 bit = readl(reg) | bit;
173 writel(bit, reg);
174 }
175
cdnsp_clear_chicken_bits_2(struct cdnsp_device * pdev,u32 bit)176 static void cdnsp_clear_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
177 {
178 __le32 __iomem *reg;
179 void __iomem *base;
180 u32 offset = 0;
181
182 base = &pdev->cap_regs->hc_capbase;
183 offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
184 reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
185
186 bit = readl(reg) & ~bit;
187 writel(bit, reg);
188 }
189
190 /*
191 * Disable interrupts and begin the controller halting process.
192 */
cdnsp_quiesce(struct cdnsp_device * pdev)193 static void cdnsp_quiesce(struct cdnsp_device *pdev)
194 {
195 u32 halted;
196 u32 mask;
197 u32 cmd;
198
199 mask = ~(u32)(CDNSP_IRQS);
200
201 halted = readl(&pdev->op_regs->status) & STS_HALT;
202 if (!halted)
203 mask &= ~(CMD_R_S | CMD_DEVEN);
204
205 cmd = readl(&pdev->op_regs->command);
206 cmd &= mask;
207 writel(cmd, &pdev->op_regs->command);
208 }
209
210 /*
211 * Force controller into halt state.
212 *
213 * Disable any IRQs and clear the run/stop bit.
214 * Controller will complete any current and actively pipelined transactions, and
215 * should halt within 16 ms of the run/stop bit being cleared.
216 * Read controller Halted bit in the status register to see when the
217 * controller is finished.
218 */
cdnsp_halt(struct cdnsp_device * pdev)219 int cdnsp_halt(struct cdnsp_device *pdev)
220 {
221 int ret;
222 u32 val;
223
224 cdnsp_quiesce(pdev);
225
226 ret = readl_poll_timeout_atomic(&pdev->op_regs->status, val,
227 val & STS_HALT, 1,
228 CDNSP_MAX_HALT_USEC);
229 if (ret) {
230 dev_err(pdev->dev, "ERROR: Device halt failed\n");
231 return ret;
232 }
233
234 pdev->cdnsp_state |= CDNSP_STATE_HALTED;
235
236 return 0;
237 }
238
239 /*
240 * device controller died, register read returns 0xffffffff, or command never
241 * ends.
242 */
cdnsp_died(struct cdnsp_device * pdev)243 void cdnsp_died(struct cdnsp_device *pdev)
244 {
245 dev_err(pdev->dev, "ERROR: CDNSP controller not responding\n");
246 pdev->cdnsp_state |= CDNSP_STATE_DYING;
247 cdnsp_halt(pdev);
248 }
249
250 /*
251 * Set the run bit and wait for the device to be running.
252 */
cdnsp_start(struct cdnsp_device * pdev)253 static int cdnsp_start(struct cdnsp_device *pdev)
254 {
255 u32 temp;
256 int ret;
257
258 temp = readl(&pdev->op_regs->command);
259 temp |= (CMD_R_S | CMD_DEVEN);
260 writel(temp, &pdev->op_regs->command);
261
262 pdev->cdnsp_state = 0;
263
264 /*
265 * Wait for the STS_HALT Status bit to be 0 to indicate the device is
266 * running.
267 */
268 ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
269 !(temp & STS_HALT), 1,
270 CDNSP_MAX_HALT_USEC);
271 if (ret) {
272 pdev->cdnsp_state = CDNSP_STATE_DYING;
273 dev_err(pdev->dev, "ERROR: Controller run failed\n");
274 }
275
276 return ret;
277 }
278
279 /*
280 * Reset a halted controller.
281 *
282 * This resets pipelines, timers, counters, state machines, etc.
283 * Transactions will be terminated immediately, and operational registers
284 * will be set to their defaults.
285 */
cdnsp_reset(struct cdnsp_device * pdev)286 int cdnsp_reset(struct cdnsp_device *pdev)
287 {
288 u32 command;
289 u32 temp;
290 int ret;
291
292 temp = readl(&pdev->op_regs->status);
293
294 if (temp == ~(u32)0) {
295 dev_err(pdev->dev, "Device not accessible, reset failed.\n");
296 return -ENODEV;
297 }
298
299 if ((temp & STS_HALT) == 0) {
300 dev_err(pdev->dev, "Controller not halted, aborting reset.\n");
301 return -EINVAL;
302 }
303
304 command = readl(&pdev->op_regs->command);
305 command |= CMD_RESET;
306 writel(command, &pdev->op_regs->command);
307
308 ret = readl_poll_timeout_atomic(&pdev->op_regs->command, temp,
309 !(temp & CMD_RESET), 1,
310 10 * 1000);
311 if (ret) {
312 dev_err(pdev->dev, "ERROR: Controller reset failed\n");
313 return ret;
314 }
315
316 /*
317 * CDNSP cannot write any doorbells or operational registers other
318 * than status until the "Controller Not Ready" flag is cleared.
319 */
320 ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
321 !(temp & STS_CNR), 1,
322 10 * 1000);
323
324 if (ret) {
325 dev_err(pdev->dev, "ERROR: Controller not ready to work\n");
326 return ret;
327 }
328
329 dev_dbg(pdev->dev, "Controller ready to work");
330
331 return ret;
332 }
333
334 /*
335 * cdnsp_get_endpoint_index - Find the index for an endpoint given its
336 * descriptor.Use the return value to right shift 1 for the bitmask.
337 *
338 * Index = (epnum * 2) + direction - 1,
339 * where direction = 0 for OUT, 1 for IN.
340 * For control endpoints, the IN index is used (OUT index is unused), so
341 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
342 */
343 static unsigned int
cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor * desc)344 cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor *desc)
345 {
346 unsigned int index = (unsigned int)usb_endpoint_num(desc);
347
348 if (usb_endpoint_xfer_control(desc))
349 return index * 2;
350
351 return (index * 2) + (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
352 }
353
354 /*
355 * Find the flag for this endpoint (for use in the control context). Use the
356 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
357 * bit 1, etc.
358 */
359 static unsigned int
cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor * desc)360 cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor *desc)
361 {
362 return 1 << (cdnsp_get_endpoint_index(desc) + 1);
363 }
364
cdnsp_ep_enqueue(struct cdnsp_ep * pep,struct cdnsp_request * preq)365 int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
366 {
367 struct cdnsp_device *pdev = pep->pdev;
368 struct usb_request *request;
369 int ret;
370
371 if (preq->epnum == 0 && !list_empty(&pep->pending_list)) {
372 trace_cdnsp_request_enqueue_busy(preq);
373 return -EBUSY;
374 }
375
376 request = &preq->request;
377 request->actual = 0;
378 request->status = -EINPROGRESS;
379 preq->direction = pep->direction;
380 preq->epnum = pep->number;
381 preq->td.drbl = 0;
382
383 ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->direction);
384 if (ret) {
385 trace_cdnsp_request_enqueue_error(preq);
386 return ret;
387 }
388
389 list_add_tail(&preq->list, &pep->pending_list);
390
391 trace_cdnsp_request_enqueue(preq);
392
393 switch (usb_endpoint_type(pep->endpoint.desc)) {
394 case USB_ENDPOINT_XFER_CONTROL:
395 ret = cdnsp_queue_ctrl_tx(pdev, preq);
396 break;
397 case USB_ENDPOINT_XFER_BULK:
398 case USB_ENDPOINT_XFER_INT:
399 ret = cdnsp_queue_bulk_tx(pdev, preq);
400 break;
401 case USB_ENDPOINT_XFER_ISOC:
402 ret = cdnsp_queue_isoc_tx(pdev, preq);
403 }
404
405 if (ret)
406 goto unmap;
407
408 return 0;
409
410 unmap:
411 usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
412 pep->direction);
413 list_del(&preq->list);
414 trace_cdnsp_request_enqueue_error(preq);
415
416 return ret;
417 }
418
419 /*
420 * Remove the request's TD from the endpoint ring. This may cause the
421 * controller to stop USB transfers, potentially stopping in the middle of a
422 * TRB buffer. The controller should pick up where it left off in the TD,
423 * unless a Set Transfer Ring Dequeue Pointer is issued.
424 *
425 * The TRBs that make up the buffers for the canceled request will be "removed"
426 * from the ring. Since the ring is a contiguous structure, they can't be
427 * physically removed. Instead, there are two options:
428 *
429 * 1) If the controller is in the middle of processing the request to be
430 * canceled, we simply move the ring's dequeue pointer past those TRBs
431 * using the Set Transfer Ring Dequeue Pointer command. This will be
432 * the common case, when drivers timeout on the last submitted request
433 * and attempt to cancel.
434 *
435 * 2) If the controller is in the middle of a different TD, we turn the TRBs
436 * into a series of 1-TRB transfer no-op TDs. No-ops shouldn't be chained.
437 * The controller will need to invalidate the any TRBs it has cached after
438 * the stop endpoint command.
439 *
440 * 3) The TD may have completed by the time the Stop Endpoint Command
441 * completes, so software needs to handle that case too.
442 *
443 */
cdnsp_ep_dequeue(struct cdnsp_ep * pep,struct cdnsp_request * preq)444 int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
445 {
446 struct cdnsp_device *pdev = pep->pdev;
447 int ret_stop = 0;
448 int ret_rem;
449
450 trace_cdnsp_request_dequeue(preq);
451
452 if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_RUNNING)
453 ret_stop = cdnsp_cmd_stop_ep(pdev, pep);
454
455 ret_rem = cdnsp_remove_request(pdev, preq, pep);
456
457 return ret_rem ? ret_rem : ret_stop;
458 }
459
cdnsp_zero_in_ctx(struct cdnsp_device * pdev)460 static void cdnsp_zero_in_ctx(struct cdnsp_device *pdev)
461 {
462 struct cdnsp_input_control_ctx *ctrl_ctx;
463 struct cdnsp_slot_ctx *slot_ctx;
464 struct cdnsp_ep_ctx *ep_ctx;
465 int i;
466
467 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
468
469 /*
470 * When a device's add flag and drop flag are zero, any subsequent
471 * configure endpoint command will leave that endpoint's state
472 * untouched. Make sure we don't leave any old state in the input
473 * endpoint contexts.
474 */
475 ctrl_ctx->drop_flags = 0;
476 ctrl_ctx->add_flags = 0;
477 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
478 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
479
480 /* Endpoint 0 is always valid */
481 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
482 for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i) {
483 ep_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, i);
484 ep_ctx->ep_info = 0;
485 ep_ctx->ep_info2 = 0;
486 ep_ctx->deq = 0;
487 ep_ctx->tx_info = 0;
488 }
489 }
490
491 /* Issue a configure endpoint command and wait for it to finish. */
cdnsp_configure_endpoint(struct cdnsp_device * pdev)492 static int cdnsp_configure_endpoint(struct cdnsp_device *pdev)
493 {
494 int ret;
495
496 cdnsp_queue_configure_endpoint(pdev, pdev->cmd.in_ctx->dma);
497 cdnsp_ring_cmd_db(pdev);
498 ret = cdnsp_wait_for_cmd_compl(pdev);
499 if (ret) {
500 dev_err(pdev->dev,
501 "ERR: unexpected command completion code 0x%x.\n", ret);
502 return -EINVAL;
503 }
504
505 return ret;
506 }
507
cdnsp_invalidate_ep_events(struct cdnsp_device * pdev,struct cdnsp_ep * pep)508 static void cdnsp_invalidate_ep_events(struct cdnsp_device *pdev,
509 struct cdnsp_ep *pep)
510 {
511 struct cdnsp_segment *segment;
512 union cdnsp_trb *event;
513 u32 cycle_state;
514 u32 data;
515
516 event = pdev->event_ring->dequeue;
517 segment = pdev->event_ring->deq_seg;
518 cycle_state = pdev->event_ring->cycle_state;
519
520 while (1) {
521 data = le32_to_cpu(event->trans_event.flags);
522
523 /* Check the owner of the TRB. */
524 if ((data & TRB_CYCLE) != cycle_state)
525 break;
526
527 if (TRB_FIELD_TO_TYPE(data) == TRB_TRANSFER &&
528 TRB_TO_EP_ID(data) == (pep->idx + 1)) {
529 data |= TRB_EVENT_INVALIDATE;
530 event->trans_event.flags = cpu_to_le32(data);
531 }
532
533 if (cdnsp_last_trb_on_seg(segment, event)) {
534 cycle_state ^= 1;
535 segment = pdev->event_ring->deq_seg->next;
536 event = segment->trbs;
537 } else {
538 event++;
539 }
540 }
541 }
542
cdnsp_wait_for_cmd_compl(struct cdnsp_device * pdev)543 int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev)
544 {
545 struct cdnsp_segment *event_deq_seg;
546 union cdnsp_trb *cmd_trb;
547 dma_addr_t cmd_deq_dma;
548 union cdnsp_trb *event;
549 u32 cycle_state;
550 u32 retry = 10;
551 int ret, val;
552 u64 cmd_dma;
553 u32 flags;
554
555 cmd_trb = pdev->cmd.command_trb;
556 pdev->cmd.status = 0;
557
558 trace_cdnsp_cmd_wait_for_compl(pdev->cmd_ring, &cmd_trb->generic);
559
560 ret = readl_poll_timeout_atomic(&pdev->op_regs->cmd_ring, val,
561 !CMD_RING_BUSY(val), 1,
562 CDNSP_CMD_TIMEOUT);
563 if (ret) {
564 dev_err(pdev->dev, "ERR: Timeout while waiting for command\n");
565 trace_cdnsp_cmd_timeout(pdev->cmd_ring, &cmd_trb->generic);
566 pdev->cdnsp_state = CDNSP_STATE_DYING;
567 return -ETIMEDOUT;
568 }
569
570 event = pdev->event_ring->dequeue;
571 event_deq_seg = pdev->event_ring->deq_seg;
572 cycle_state = pdev->event_ring->cycle_state;
573
574 cmd_deq_dma = cdnsp_trb_virt_to_dma(pdev->cmd_ring->deq_seg, cmd_trb);
575 if (!cmd_deq_dma)
576 return -EINVAL;
577
578 while (1) {
579 flags = le32_to_cpu(event->event_cmd.flags);
580
581 /* Check the owner of the TRB. */
582 if ((flags & TRB_CYCLE) != cycle_state) {
583 /*
584 * Give some extra time to get chance controller
585 * to finish command before returning error code.
586 * Checking CMD_RING_BUSY is not sufficient because
587 * this bit is cleared to '0' when the Command
588 * Descriptor has been executed by controller
589 * and not when command completion event has
590 * be added to event ring.
591 */
592 if (retry--) {
593 udelay(20);
594 continue;
595 }
596
597 return -EINVAL;
598 }
599
600 cmd_dma = le64_to_cpu(event->event_cmd.cmd_trb);
601
602 /*
603 * Check whether the completion event is for last queued
604 * command.
605 */
606 if (TRB_FIELD_TO_TYPE(flags) != TRB_COMPLETION ||
607 cmd_dma != (u64)cmd_deq_dma) {
608 if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
609 event++;
610 continue;
611 }
612
613 if (cdnsp_last_trb_on_ring(pdev->event_ring,
614 event_deq_seg, event))
615 cycle_state ^= 1;
616
617 event_deq_seg = event_deq_seg->next;
618 event = event_deq_seg->trbs;
619 continue;
620 }
621
622 trace_cdnsp_handle_command(pdev->cmd_ring, &cmd_trb->generic);
623
624 pdev->cmd.status = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status));
625 if (pdev->cmd.status == COMP_SUCCESS)
626 return 0;
627
628 return -pdev->cmd.status;
629 }
630 }
631
cdnsp_halt_endpoint(struct cdnsp_device * pdev,struct cdnsp_ep * pep,int value)632 int cdnsp_halt_endpoint(struct cdnsp_device *pdev,
633 struct cdnsp_ep *pep,
634 int value)
635 {
636 int ret;
637
638 trace_cdnsp_ep_halt(value ? "Set" : "Clear");
639
640 ret = cdnsp_cmd_stop_ep(pdev, pep);
641 if (ret)
642 return ret;
643
644 if (value) {
645 if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_STOPPED) {
646 cdnsp_queue_halt_endpoint(pdev, pep->idx);
647 cdnsp_ring_cmd_db(pdev);
648 ret = cdnsp_wait_for_cmd_compl(pdev);
649 }
650
651 pep->ep_state |= EP_HALTED;
652 } else {
653 cdnsp_queue_reset_ep(pdev, pep->idx);
654 cdnsp_ring_cmd_db(pdev);
655 ret = cdnsp_wait_for_cmd_compl(pdev);
656 trace_cdnsp_handle_cmd_reset_ep(pep->out_ctx);
657
658 if (ret)
659 return ret;
660
661 pep->ep_state &= ~EP_HALTED;
662
663 if (pep->idx != 0 && !(pep->ep_state & EP_WEDGE))
664 cdnsp_ring_doorbell_for_active_rings(pdev, pep);
665
666 pep->ep_state &= ~EP_WEDGE;
667 }
668
669 return 0;
670 }
671
cdnsp_update_eps_configuration(struct cdnsp_device * pdev,struct cdnsp_ep * pep)672 static int cdnsp_update_eps_configuration(struct cdnsp_device *pdev,
673 struct cdnsp_ep *pep)
674 {
675 struct cdnsp_input_control_ctx *ctrl_ctx;
676 struct cdnsp_slot_ctx *slot_ctx;
677 int ret = 0;
678 u32 ep_sts;
679 int i;
680
681 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
682
683 /* Don't issue the command if there's no endpoints to update. */
684 if (ctrl_ctx->add_flags == 0 && ctrl_ctx->drop_flags == 0)
685 return 0;
686
687 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
688 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
689 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
690
691 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
692 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
693 for (i = CDNSP_ENDPOINTS_NUM; i >= 1; i--) {
694 __le32 le32 = cpu_to_le32(BIT(i));
695
696 if ((pdev->eps[i - 1].ring && !(ctrl_ctx->drop_flags & le32)) ||
697 (ctrl_ctx->add_flags & le32) || i == 1) {
698 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
699 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
700 break;
701 }
702 }
703
704 ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
705
706 if ((ctrl_ctx->add_flags != cpu_to_le32(SLOT_FLAG) &&
707 ep_sts == EP_STATE_DISABLED) ||
708 (ep_sts != EP_STATE_DISABLED && ctrl_ctx->drop_flags))
709 ret = cdnsp_configure_endpoint(pdev);
710
711 trace_cdnsp_configure_endpoint(cdnsp_get_slot_ctx(&pdev->out_ctx));
712 trace_cdnsp_handle_cmd_config_ep(pep->out_ctx);
713
714 cdnsp_zero_in_ctx(pdev);
715
716 return ret;
717 }
718
719 /*
720 * This submits a Reset Device Command, which will set the device state to 0,
721 * set the device address to 0, and disable all the endpoints except the default
722 * control endpoint. The USB core should come back and call
723 * cdnsp_setup_device(), and then re-set up the configuration.
724 */
cdnsp_reset_device(struct cdnsp_device * pdev)725 int cdnsp_reset_device(struct cdnsp_device *pdev)
726 {
727 struct cdnsp_slot_ctx *slot_ctx;
728 int slot_state;
729 int ret, i;
730
731 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
732 slot_ctx->dev_info = 0;
733 pdev->device_address = 0;
734
735 /* If device is not setup, there is no point in resetting it. */
736 slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
737 slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
738 trace_cdnsp_reset_device(slot_ctx);
739
740 if (slot_state <= SLOT_STATE_DEFAULT &&
741 pdev->eps[0].ep_state & EP_HALTED) {
742 cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
743 }
744
745 /*
746 * During Reset Device command controller shall transition the
747 * endpoint ep0 to the Running State.
748 */
749 pdev->eps[0].ep_state &= ~(EP_STOPPED | EP_HALTED);
750 pdev->eps[0].ep_state |= EP_ENABLED;
751
752 if (slot_state <= SLOT_STATE_DEFAULT)
753 return 0;
754
755 cdnsp_queue_reset_device(pdev);
756 cdnsp_ring_cmd_db(pdev);
757 ret = cdnsp_wait_for_cmd_compl(pdev);
758
759 /*
760 * After Reset Device command all not default endpoints
761 * are in Disabled state.
762 */
763 for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i)
764 pdev->eps[i].ep_state |= EP_STOPPED | EP_UNCONFIGURED;
765
766 trace_cdnsp_handle_cmd_reset_dev(slot_ctx);
767
768 if (ret)
769 dev_err(pdev->dev, "Reset device failed with error code %d",
770 ret);
771
772 return ret;
773 }
774
775 /*
776 * Sets the MaxPStreams field and the Linear Stream Array field.
777 * Sets the dequeue pointer to the stream context array.
778 */
cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device * pdev,struct cdnsp_ep_ctx * ep_ctx,struct cdnsp_stream_info * stream_info)779 static void cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device *pdev,
780 struct cdnsp_ep_ctx *ep_ctx,
781 struct cdnsp_stream_info *stream_info)
782 {
783 u32 max_primary_streams;
784
785 /* MaxPStreams is the number of stream context array entries, not the
786 * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
787 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
788 */
789 max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
790 ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
791 ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
792 | EP_HAS_LSA);
793 ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma);
794 }
795
796 /*
797 * The drivers use this function to prepare a bulk endpoints to use streams.
798 *
799 * Don't allow the call to succeed if endpoint only supports one stream
800 * (which means it doesn't support streams at all).
801 */
cdnsp_alloc_streams(struct cdnsp_device * pdev,struct cdnsp_ep * pep)802 int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
803 {
804 unsigned int num_streams = usb_ss_max_streams(pep->endpoint.comp_desc);
805 unsigned int num_stream_ctxs;
806 int ret;
807
808 if (num_streams == 0)
809 return 0;
810
811 if (num_streams > STREAM_NUM_STREAMS)
812 return -EINVAL;
813
814 /*
815 * Add two to the number of streams requested to account for
816 * stream 0 that is reserved for controller usage and one additional
817 * for TASK SET FULL response.
818 */
819 num_streams += 2;
820
821 /* The stream context array size must be a power of two */
822 num_stream_ctxs = roundup_pow_of_two(num_streams);
823
824 trace_cdnsp_stream_number(pep, num_stream_ctxs, num_streams);
825
826 ret = cdnsp_alloc_stream_info(pdev, pep, num_stream_ctxs, num_streams);
827 if (ret)
828 return ret;
829
830 cdnsp_setup_streams_ep_input_ctx(pdev, pep->in_ctx, &pep->stream_info);
831
832 pep->ep_state |= EP_HAS_STREAMS;
833 pep->stream_info.td_count = 0;
834 pep->stream_info.first_prime_det = 0;
835
836 /* Subtract 1 for stream 0, which drivers can't use. */
837 return num_streams - 1;
838 }
839
cdnsp_disable_slot(struct cdnsp_device * pdev)840 int cdnsp_disable_slot(struct cdnsp_device *pdev)
841 {
842 int ret;
843
844 cdnsp_queue_slot_control(pdev, TRB_DISABLE_SLOT);
845 cdnsp_ring_cmd_db(pdev);
846 ret = cdnsp_wait_for_cmd_compl(pdev);
847
848 pdev->slot_id = 0;
849 pdev->active_port = NULL;
850
851 trace_cdnsp_handle_cmd_disable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
852
853 memset(pdev->in_ctx.bytes, 0, CDNSP_CTX_SIZE);
854 memset(pdev->out_ctx.bytes, 0, CDNSP_CTX_SIZE);
855
856 return ret;
857 }
858
cdnsp_enable_slot(struct cdnsp_device * pdev)859 int cdnsp_enable_slot(struct cdnsp_device *pdev)
860 {
861 struct cdnsp_slot_ctx *slot_ctx;
862 int slot_state;
863 int ret;
864
865 /* If device is not setup, there is no point in resetting it */
866 slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
867 slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
868
869 if (slot_state != SLOT_STATE_DISABLED)
870 return 0;
871
872 cdnsp_queue_slot_control(pdev, TRB_ENABLE_SLOT);
873 cdnsp_ring_cmd_db(pdev);
874 ret = cdnsp_wait_for_cmd_compl(pdev);
875 if (ret)
876 goto show_trace;
877
878 pdev->slot_id = 1;
879
880 show_trace:
881 trace_cdnsp_handle_cmd_enable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
882
883 return ret;
884 }
885
886 /*
887 * Issue an Address Device command with BSR=0 if setup is SETUP_CONTEXT_ONLY
888 * or with BSR = 1 if set_address is SETUP_CONTEXT_ADDRESS.
889 */
cdnsp_setup_device(struct cdnsp_device * pdev,enum cdnsp_setup_dev setup)890 int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup)
891 {
892 struct cdnsp_input_control_ctx *ctrl_ctx;
893 struct cdnsp_slot_ctx *slot_ctx;
894 int dev_state = 0;
895 int ret;
896
897 if (!pdev->slot_id) {
898 trace_cdnsp_slot_id("incorrect");
899 return -EINVAL;
900 }
901
902 if (!pdev->active_port->port_num)
903 return -EINVAL;
904
905 slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
906 dev_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
907
908 if (setup == SETUP_CONTEXT_ONLY && dev_state == SLOT_STATE_DEFAULT) {
909 trace_cdnsp_slot_already_in_default(slot_ctx);
910 return 0;
911 }
912
913 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
914 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
915
916 if (!slot_ctx->dev_info || dev_state == SLOT_STATE_DEFAULT) {
917 ret = cdnsp_setup_addressable_priv_dev(pdev);
918 if (ret)
919 return ret;
920 }
921
922 cdnsp_copy_ep0_dequeue_into_input_ctx(pdev);
923
924 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
925 ctrl_ctx->drop_flags = 0;
926
927 trace_cdnsp_setup_device_slot(slot_ctx);
928
929 cdnsp_queue_address_device(pdev, pdev->in_ctx.dma, setup);
930 cdnsp_ring_cmd_db(pdev);
931 ret = cdnsp_wait_for_cmd_compl(pdev);
932
933 trace_cdnsp_handle_cmd_addr_dev(cdnsp_get_slot_ctx(&pdev->out_ctx));
934
935 /* Zero the input context control for later use. */
936 ctrl_ctx->add_flags = 0;
937 ctrl_ctx->drop_flags = 0;
938
939 return ret;
940 }
941
cdnsp_set_usb2_hardware_lpm(struct cdnsp_device * pdev,struct usb_request * req,int enable)942 void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *pdev,
943 struct usb_request *req,
944 int enable)
945 {
946 if (pdev->active_port != &pdev->usb2_port || !pdev->gadget.lpm_capable)
947 return;
948
949 trace_cdnsp_lpm(enable);
950
951 if (enable)
952 writel(PORT_BESL(CDNSP_DEFAULT_BESL) | PORT_L1S_NYET | PORT_HLE,
953 &pdev->active_port->regs->portpmsc);
954 else
955 writel(PORT_L1S_NYET, &pdev->active_port->regs->portpmsc);
956 }
957
cdnsp_get_frame(struct cdnsp_device * pdev)958 static int cdnsp_get_frame(struct cdnsp_device *pdev)
959 {
960 return readl(&pdev->run_regs->microframe_index) >> 3;
961 }
962
cdnsp_gadget_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)963 static int cdnsp_gadget_ep_enable(struct usb_ep *ep,
964 const struct usb_endpoint_descriptor *desc)
965 {
966 struct cdnsp_input_control_ctx *ctrl_ctx;
967 struct cdnsp_device *pdev;
968 struct cdnsp_ep *pep;
969 unsigned long flags;
970 u32 added_ctxs;
971 int ret;
972
973 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT ||
974 !desc->wMaxPacketSize)
975 return -EINVAL;
976
977 pep = to_cdnsp_ep(ep);
978 pdev = pep->pdev;
979 pep->ep_state &= ~EP_UNCONFIGURED;
980
981 if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED,
982 "%s is already enabled\n", pep->name))
983 return 0;
984
985 spin_lock_irqsave(&pdev->lock, flags);
986
987 added_ctxs = cdnsp_get_endpoint_flag(desc);
988 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
989 dev_err(pdev->dev, "ERROR: Bad endpoint number\n");
990 ret = -EINVAL;
991 goto unlock;
992 }
993
994 pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
995
996 if (pdev->gadget.speed == USB_SPEED_FULL) {
997 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT)
998 pep->interval = desc->bInterval << 3;
999 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC)
1000 pep->interval = BIT(desc->bInterval - 1) << 3;
1001 }
1002
1003 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) {
1004 if (pep->interval > BIT(12)) {
1005 dev_err(pdev->dev, "bInterval %d not supported\n",
1006 desc->bInterval);
1007 ret = -EINVAL;
1008 goto unlock;
1009 }
1010 cdnsp_set_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
1011 }
1012
1013 ret = cdnsp_endpoint_init(pdev, pep, GFP_ATOMIC);
1014 if (ret)
1015 goto unlock;
1016
1017 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
1018 ctrl_ctx->add_flags = cpu_to_le32(added_ctxs);
1019 ctrl_ctx->drop_flags = 0;
1020
1021 ret = cdnsp_update_eps_configuration(pdev, pep);
1022 if (ret) {
1023 cdnsp_free_endpoint_rings(pdev, pep);
1024 goto unlock;
1025 }
1026
1027 pep->ep_state |= EP_ENABLED;
1028 pep->ep_state &= ~EP_STOPPED;
1029
1030 unlock:
1031 trace_cdnsp_ep_enable_end(pep, 0);
1032 spin_unlock_irqrestore(&pdev->lock, flags);
1033
1034 return ret;
1035 }
1036
cdnsp_gadget_ep_disable(struct usb_ep * ep)1037 static int cdnsp_gadget_ep_disable(struct usb_ep *ep)
1038 {
1039 struct cdnsp_input_control_ctx *ctrl_ctx;
1040 struct cdnsp_request *preq;
1041 struct cdnsp_device *pdev;
1042 struct cdnsp_ep *pep;
1043 unsigned long flags;
1044 u32 drop_flag;
1045 int ret = 0;
1046
1047 if (!ep)
1048 return -EINVAL;
1049
1050 pep = to_cdnsp_ep(ep);
1051 pdev = pep->pdev;
1052
1053 spin_lock_irqsave(&pdev->lock, flags);
1054
1055 if (!(pep->ep_state & EP_ENABLED)) {
1056 dev_err(pdev->dev, "%s is already disabled\n", pep->name);
1057 ret = -EINVAL;
1058 goto finish;
1059 }
1060
1061 pep->ep_state |= EP_DIS_IN_RROGRESS;
1062
1063 /* Endpoint was unconfigured by Reset Device command. */
1064 if (!(pep->ep_state & EP_UNCONFIGURED)) {
1065 cdnsp_cmd_stop_ep(pdev, pep);
1066 cdnsp_cmd_flush_ep(pdev, pep);
1067 }
1068
1069 /* Remove all queued USB requests. */
1070 while (!list_empty(&pep->pending_list)) {
1071 preq = next_request(&pep->pending_list);
1072 cdnsp_ep_dequeue(pep, preq);
1073 }
1074
1075 cdnsp_invalidate_ep_events(pdev, pep);
1076
1077 pep->ep_state &= ~EP_DIS_IN_RROGRESS;
1078 drop_flag = cdnsp_get_endpoint_flag(pep->endpoint.desc);
1079 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
1080 ctrl_ctx->drop_flags = cpu_to_le32(drop_flag);
1081 ctrl_ctx->add_flags = 0;
1082
1083 cdnsp_endpoint_zero(pdev, pep);
1084
1085 if (!(pep->ep_state & EP_UNCONFIGURED))
1086 ret = cdnsp_update_eps_configuration(pdev, pep);
1087
1088 cdnsp_free_endpoint_rings(pdev, pep);
1089
1090 pep->ep_state &= ~(EP_ENABLED | EP_UNCONFIGURED);
1091 pep->ep_state |= EP_STOPPED;
1092
1093 finish:
1094 trace_cdnsp_ep_disable_end(pep, 0);
1095 spin_unlock_irqrestore(&pdev->lock, flags);
1096
1097 return ret;
1098 }
1099
cdnsp_gadget_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)1100 static struct usb_request *cdnsp_gadget_ep_alloc_request(struct usb_ep *ep,
1101 gfp_t gfp_flags)
1102 {
1103 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1104 struct cdnsp_request *preq;
1105
1106 preq = kzalloc(sizeof(*preq), gfp_flags);
1107 if (!preq)
1108 return NULL;
1109
1110 preq->epnum = pep->number;
1111 preq->pep = pep;
1112
1113 trace_cdnsp_alloc_request(preq);
1114
1115 return &preq->request;
1116 }
1117
cdnsp_gadget_ep_free_request(struct usb_ep * ep,struct usb_request * request)1118 static void cdnsp_gadget_ep_free_request(struct usb_ep *ep,
1119 struct usb_request *request)
1120 {
1121 struct cdnsp_request *preq = to_cdnsp_request(request);
1122
1123 trace_cdnsp_free_request(preq);
1124 kfree(preq);
1125 }
1126
cdnsp_gadget_ep_queue(struct usb_ep * ep,struct usb_request * request,gfp_t gfp_flags)1127 static int cdnsp_gadget_ep_queue(struct usb_ep *ep,
1128 struct usb_request *request,
1129 gfp_t gfp_flags)
1130 {
1131 struct cdnsp_request *preq;
1132 struct cdnsp_device *pdev;
1133 struct cdnsp_ep *pep;
1134 unsigned long flags;
1135 int ret;
1136
1137 if (!request || !ep)
1138 return -EINVAL;
1139
1140 pep = to_cdnsp_ep(ep);
1141 pdev = pep->pdev;
1142
1143 if (!(pep->ep_state & EP_ENABLED)) {
1144 dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n",
1145 pep->name);
1146 return -EINVAL;
1147 }
1148
1149 preq = to_cdnsp_request(request);
1150 spin_lock_irqsave(&pdev->lock, flags);
1151 ret = cdnsp_ep_enqueue(pep, preq);
1152 spin_unlock_irqrestore(&pdev->lock, flags);
1153
1154 return ret;
1155 }
1156
cdnsp_gadget_ep_dequeue(struct usb_ep * ep,struct usb_request * request)1157 static int cdnsp_gadget_ep_dequeue(struct usb_ep *ep,
1158 struct usb_request *request)
1159 {
1160 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1161 struct cdnsp_device *pdev = pep->pdev;
1162 unsigned long flags;
1163 int ret;
1164
1165 if (request->status != -EINPROGRESS)
1166 return 0;
1167
1168 if (!pep->endpoint.desc) {
1169 dev_err(pdev->dev,
1170 "%s: can't dequeue to disabled endpoint\n",
1171 pep->name);
1172 return -ESHUTDOWN;
1173 }
1174
1175 /* Requests has been dequeued during disabling endpoint. */
1176 if (!(pep->ep_state & EP_ENABLED))
1177 return 0;
1178
1179 spin_lock_irqsave(&pdev->lock, flags);
1180 ret = cdnsp_ep_dequeue(pep, to_cdnsp_request(request));
1181 spin_unlock_irqrestore(&pdev->lock, flags);
1182
1183 return ret;
1184 }
1185
cdnsp_gadget_ep_set_halt(struct usb_ep * ep,int value)1186 static int cdnsp_gadget_ep_set_halt(struct usb_ep *ep, int value)
1187 {
1188 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1189 struct cdnsp_device *pdev = pep->pdev;
1190 struct cdnsp_request *preq;
1191 unsigned long flags;
1192 int ret;
1193
1194 spin_lock_irqsave(&pdev->lock, flags);
1195
1196 preq = next_request(&pep->pending_list);
1197 if (value) {
1198 if (preq) {
1199 trace_cdnsp_ep_busy_try_halt_again(pep, 0);
1200 ret = -EAGAIN;
1201 goto done;
1202 }
1203 }
1204
1205 ret = cdnsp_halt_endpoint(pdev, pep, value);
1206
1207 done:
1208 spin_unlock_irqrestore(&pdev->lock, flags);
1209 return ret;
1210 }
1211
cdnsp_gadget_ep_set_wedge(struct usb_ep * ep)1212 static int cdnsp_gadget_ep_set_wedge(struct usb_ep *ep)
1213 {
1214 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1215 struct cdnsp_device *pdev = pep->pdev;
1216 unsigned long flags;
1217 int ret;
1218
1219 spin_lock_irqsave(&pdev->lock, flags);
1220 pep->ep_state |= EP_WEDGE;
1221 ret = cdnsp_halt_endpoint(pdev, pep, 1);
1222 spin_unlock_irqrestore(&pdev->lock, flags);
1223
1224 return ret;
1225 }
1226
1227 static const struct usb_ep_ops cdnsp_gadget_ep0_ops = {
1228 .enable = cdnsp_gadget_ep_enable,
1229 .disable = cdnsp_gadget_ep_disable,
1230 .alloc_request = cdnsp_gadget_ep_alloc_request,
1231 .free_request = cdnsp_gadget_ep_free_request,
1232 .queue = cdnsp_gadget_ep_queue,
1233 .dequeue = cdnsp_gadget_ep_dequeue,
1234 .set_halt = cdnsp_gadget_ep_set_halt,
1235 .set_wedge = cdnsp_gadget_ep_set_wedge,
1236 };
1237
1238 static const struct usb_ep_ops cdnsp_gadget_ep_ops = {
1239 .enable = cdnsp_gadget_ep_enable,
1240 .disable = cdnsp_gadget_ep_disable,
1241 .alloc_request = cdnsp_gadget_ep_alloc_request,
1242 .free_request = cdnsp_gadget_ep_free_request,
1243 .queue = cdnsp_gadget_ep_queue,
1244 .dequeue = cdnsp_gadget_ep_dequeue,
1245 .set_halt = cdnsp_gadget_ep_set_halt,
1246 .set_wedge = cdnsp_gadget_ep_set_wedge,
1247 };
1248
cdnsp_gadget_giveback(struct cdnsp_ep * pep,struct cdnsp_request * preq,int status)1249 void cdnsp_gadget_giveback(struct cdnsp_ep *pep,
1250 struct cdnsp_request *preq,
1251 int status)
1252 {
1253 struct cdnsp_device *pdev = pep->pdev;
1254
1255 list_del(&preq->list);
1256
1257 if (preq->request.status == -EINPROGRESS)
1258 preq->request.status = status;
1259
1260 usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
1261 preq->direction);
1262
1263 trace_cdnsp_request_giveback(preq);
1264
1265 if (preq != &pdev->ep0_preq) {
1266 spin_unlock(&pdev->lock);
1267 usb_gadget_giveback_request(&pep->endpoint, &preq->request);
1268 spin_lock(&pdev->lock);
1269 }
1270 }
1271
1272 static struct usb_endpoint_descriptor cdnsp_gadget_ep0_desc = {
1273 .bLength = USB_DT_ENDPOINT_SIZE,
1274 .bDescriptorType = USB_DT_ENDPOINT,
1275 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1276 };
1277
cdnsp_run(struct cdnsp_device * pdev,enum usb_device_speed speed)1278 static int cdnsp_run(struct cdnsp_device *pdev,
1279 enum usb_device_speed speed)
1280 {
1281 u32 fs_speed = 0;
1282 u32 temp;
1283 int ret;
1284
1285 temp = readl(&pdev->ir_set->irq_control);
1286 temp &= ~IMOD_INTERVAL_MASK;
1287 temp |= ((IMOD_DEFAULT_INTERVAL / 250) & IMOD_INTERVAL_MASK);
1288 writel(temp, &pdev->ir_set->irq_control);
1289
1290 temp = readl(&pdev->port3x_regs->mode_addr);
1291
1292 switch (speed) {
1293 case USB_SPEED_SUPER_PLUS:
1294 temp |= CFG_3XPORT_SSP_SUPPORT;
1295 break;
1296 case USB_SPEED_SUPER:
1297 temp &= ~CFG_3XPORT_SSP_SUPPORT;
1298 break;
1299 case USB_SPEED_HIGH:
1300 break;
1301 case USB_SPEED_FULL:
1302 fs_speed = PORT_REG6_FORCE_FS;
1303 break;
1304 default:
1305 dev_err(pdev->dev, "invalid maximum_speed parameter %d\n",
1306 speed);
1307 fallthrough;
1308 case USB_SPEED_UNKNOWN:
1309 /* Default to superspeed. */
1310 speed = USB_SPEED_SUPER;
1311 break;
1312 }
1313
1314 if (speed >= USB_SPEED_SUPER) {
1315 writel(temp, &pdev->port3x_regs->mode_addr);
1316 cdnsp_set_link_state(pdev, &pdev->usb3_port.regs->portsc,
1317 XDEV_RXDETECT);
1318 } else {
1319 cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
1320 }
1321
1322 cdnsp_set_link_state(pdev, &pdev->usb2_port.regs->portsc,
1323 XDEV_RXDETECT);
1324
1325 cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1326
1327 writel(PORT_REG6_L1_L0_HW_EN | fs_speed, &pdev->port20_regs->port_reg6);
1328
1329 ret = cdnsp_start(pdev);
1330 if (ret) {
1331 ret = -ENODEV;
1332 goto err;
1333 }
1334
1335 temp = readl(&pdev->op_regs->command);
1336 temp |= (CMD_INTE);
1337 writel(temp, &pdev->op_regs->command);
1338
1339 temp = readl(&pdev->ir_set->irq_pending);
1340 writel(IMAN_IE_SET(temp), &pdev->ir_set->irq_pending);
1341
1342 trace_cdnsp_init("Controller ready to work");
1343 return 0;
1344 err:
1345 cdnsp_halt(pdev);
1346 return ret;
1347 }
1348
cdnsp_gadget_udc_start(struct usb_gadget * g,struct usb_gadget_driver * driver)1349 static int cdnsp_gadget_udc_start(struct usb_gadget *g,
1350 struct usb_gadget_driver *driver)
1351 {
1352 enum usb_device_speed max_speed = driver->max_speed;
1353 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1354 unsigned long flags;
1355 int ret;
1356
1357 spin_lock_irqsave(&pdev->lock, flags);
1358 pdev->gadget_driver = driver;
1359
1360 /* limit speed if necessary */
1361 max_speed = min(driver->max_speed, g->max_speed);
1362 ret = cdnsp_run(pdev, max_speed);
1363
1364 spin_unlock_irqrestore(&pdev->lock, flags);
1365
1366 return ret;
1367 }
1368
1369 /*
1370 * Update Event Ring Dequeue Pointer:
1371 * - When all events have finished
1372 * - To avoid "Event Ring Full Error" condition
1373 */
cdnsp_update_erst_dequeue(struct cdnsp_device * pdev,union cdnsp_trb * event_ring_deq,u8 clear_ehb)1374 void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
1375 union cdnsp_trb *event_ring_deq,
1376 u8 clear_ehb)
1377 {
1378 u64 temp_64;
1379 dma_addr_t deq;
1380
1381 temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
1382
1383 /* If necessary, update the HW's version of the event ring deq ptr. */
1384 if (event_ring_deq != pdev->event_ring->dequeue) {
1385 deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
1386 pdev->event_ring->dequeue);
1387 temp_64 &= ERST_PTR_MASK;
1388 temp_64 |= ((u64)deq & (u64)~ERST_PTR_MASK);
1389 }
1390
1391 /* Clear the event handler busy flag (RW1C). */
1392 if (clear_ehb)
1393 temp_64 |= ERST_EHB;
1394 else
1395 temp_64 &= ~ERST_EHB;
1396
1397 cdnsp_write_64(temp_64, &pdev->ir_set->erst_dequeue);
1398 }
1399
cdnsp_clear_cmd_ring(struct cdnsp_device * pdev)1400 static void cdnsp_clear_cmd_ring(struct cdnsp_device *pdev)
1401 {
1402 struct cdnsp_segment *seg;
1403 u64 val_64;
1404 int i;
1405
1406 cdnsp_initialize_ring_info(pdev->cmd_ring);
1407
1408 seg = pdev->cmd_ring->first_seg;
1409 for (i = 0; i < pdev->cmd_ring->num_segs; i++) {
1410 memset(seg->trbs, 0,
1411 sizeof(union cdnsp_trb) * (TRBS_PER_SEGMENT - 1));
1412 seg = seg->next;
1413 }
1414
1415 /* Set the address in the Command Ring Control register. */
1416 val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
1417 val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
1418 (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
1419 pdev->cmd_ring->cycle_state;
1420 cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
1421 }
1422
cdnsp_consume_all_events(struct cdnsp_device * pdev)1423 static void cdnsp_consume_all_events(struct cdnsp_device *pdev)
1424 {
1425 struct cdnsp_segment *event_deq_seg;
1426 union cdnsp_trb *event_ring_deq;
1427 union cdnsp_trb *event;
1428 u32 cycle_bit;
1429
1430 event_ring_deq = pdev->event_ring->dequeue;
1431 event_deq_seg = pdev->event_ring->deq_seg;
1432 event = pdev->event_ring->dequeue;
1433
1434 /* Update ring dequeue pointer. */
1435 while (1) {
1436 cycle_bit = (le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE);
1437
1438 /* Does the controller or driver own the TRB? */
1439 if (cycle_bit != pdev->event_ring->cycle_state)
1440 break;
1441
1442 cdnsp_inc_deq(pdev, pdev->event_ring);
1443
1444 if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
1445 event++;
1446 continue;
1447 }
1448
1449 if (cdnsp_last_trb_on_ring(pdev->event_ring, event_deq_seg,
1450 event))
1451 cycle_bit ^= 1;
1452
1453 event_deq_seg = event_deq_seg->next;
1454 event = event_deq_seg->trbs;
1455 }
1456
1457 cdnsp_update_erst_dequeue(pdev, event_ring_deq, 1);
1458 }
1459
cdnsp_stop(struct cdnsp_device * pdev)1460 static void cdnsp_stop(struct cdnsp_device *pdev)
1461 {
1462 u32 temp;
1463
1464 cdnsp_cmd_flush_ep(pdev, &pdev->eps[0]);
1465
1466 /* Remove internally queued request for ep0. */
1467 if (!list_empty(&pdev->eps[0].pending_list)) {
1468 struct cdnsp_request *req;
1469
1470 req = next_request(&pdev->eps[0].pending_list);
1471 if (req == &pdev->ep0_preq)
1472 cdnsp_ep_dequeue(&pdev->eps[0], req);
1473 }
1474
1475 cdnsp_disable_port(pdev, &pdev->usb2_port.regs->portsc);
1476 cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
1477 cdnsp_disable_slot(pdev);
1478 cdnsp_halt(pdev);
1479
1480 temp = readl(&pdev->op_regs->status);
1481 writel((temp & ~0x1fff) | STS_EINT, &pdev->op_regs->status);
1482 temp = readl(&pdev->ir_set->irq_pending);
1483 writel(IMAN_IE_CLEAR(temp), &pdev->ir_set->irq_pending);
1484
1485 cdnsp_clear_port_change_bit(pdev, &pdev->usb2_port.regs->portsc);
1486 cdnsp_clear_port_change_bit(pdev, &pdev->usb3_port.regs->portsc);
1487
1488 /* Clear interrupt line */
1489 temp = readl(&pdev->ir_set->irq_pending);
1490 temp |= IMAN_IP;
1491 writel(temp, &pdev->ir_set->irq_pending);
1492
1493 cdnsp_consume_all_events(pdev);
1494 cdnsp_clear_cmd_ring(pdev);
1495
1496 trace_cdnsp_exit("Controller stopped.");
1497 }
1498
1499 /*
1500 * Stop controller.
1501 * This function is called by the gadget core when the driver is removed.
1502 * Disable slot, disable IRQs, and quiesce the controller.
1503 */
cdnsp_gadget_udc_stop(struct usb_gadget * g)1504 static int cdnsp_gadget_udc_stop(struct usb_gadget *g)
1505 {
1506 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1507 unsigned long flags;
1508
1509 spin_lock_irqsave(&pdev->lock, flags);
1510 cdnsp_stop(pdev);
1511 pdev->gadget_driver = NULL;
1512 spin_unlock_irqrestore(&pdev->lock, flags);
1513
1514 return 0;
1515 }
1516
cdnsp_gadget_get_frame(struct usb_gadget * g)1517 static int cdnsp_gadget_get_frame(struct usb_gadget *g)
1518 {
1519 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1520
1521 return cdnsp_get_frame(pdev);
1522 }
1523
__cdnsp_gadget_wakeup(struct cdnsp_device * pdev)1524 static void __cdnsp_gadget_wakeup(struct cdnsp_device *pdev)
1525 {
1526 struct cdnsp_port_regs __iomem *port_regs;
1527 u32 portpm, portsc;
1528
1529 port_regs = pdev->active_port->regs;
1530 portsc = readl(&port_regs->portsc) & PORT_PLS_MASK;
1531
1532 /* Remote wakeup feature is not enabled by host. */
1533 if (pdev->gadget.speed < USB_SPEED_SUPER && portsc == XDEV_U2) {
1534 portpm = readl(&port_regs->portpmsc);
1535
1536 if (!(portpm & PORT_RWE))
1537 return;
1538 }
1539
1540 if (portsc == XDEV_U3 && !pdev->may_wakeup)
1541 return;
1542
1543 cdnsp_set_link_state(pdev, &port_regs->portsc, XDEV_U0);
1544
1545 pdev->cdnsp_state |= CDNSP_WAKEUP_PENDING;
1546 }
1547
cdnsp_gadget_wakeup(struct usb_gadget * g)1548 static int cdnsp_gadget_wakeup(struct usb_gadget *g)
1549 {
1550 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1551 unsigned long flags;
1552
1553 spin_lock_irqsave(&pdev->lock, flags);
1554 __cdnsp_gadget_wakeup(pdev);
1555 spin_unlock_irqrestore(&pdev->lock, flags);
1556
1557 return 0;
1558 }
1559
cdnsp_gadget_set_selfpowered(struct usb_gadget * g,int is_selfpowered)1560 static int cdnsp_gadget_set_selfpowered(struct usb_gadget *g,
1561 int is_selfpowered)
1562 {
1563 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1564 unsigned long flags;
1565
1566 spin_lock_irqsave(&pdev->lock, flags);
1567 g->is_selfpowered = !!is_selfpowered;
1568 spin_unlock_irqrestore(&pdev->lock, flags);
1569
1570 return 0;
1571 }
1572
cdnsp_gadget_pullup(struct usb_gadget * gadget,int is_on)1573 static int cdnsp_gadget_pullup(struct usb_gadget *gadget, int is_on)
1574 {
1575 struct cdnsp_device *pdev = gadget_to_cdnsp(gadget);
1576 struct cdns *cdns = dev_get_drvdata(pdev->dev);
1577 unsigned long flags;
1578
1579 trace_cdnsp_pullup(is_on);
1580
1581 /*
1582 * Disable events handling while controller is being
1583 * enabled/disabled.
1584 */
1585 disable_irq(cdns->dev_irq);
1586 spin_lock_irqsave(&pdev->lock, flags);
1587
1588 if (!is_on) {
1589 cdnsp_reset_device(pdev);
1590 cdns_clear_vbus(cdns);
1591 } else {
1592 cdns_set_vbus(cdns);
1593 }
1594
1595 spin_unlock_irqrestore(&pdev->lock, flags);
1596 enable_irq(cdns->dev_irq);
1597
1598 return 0;
1599 }
1600
1601 static const struct usb_gadget_ops cdnsp_gadget_ops = {
1602 .get_frame = cdnsp_gadget_get_frame,
1603 .wakeup = cdnsp_gadget_wakeup,
1604 .set_selfpowered = cdnsp_gadget_set_selfpowered,
1605 .pullup = cdnsp_gadget_pullup,
1606 .udc_start = cdnsp_gadget_udc_start,
1607 .udc_stop = cdnsp_gadget_udc_stop,
1608 };
1609
cdnsp_get_ep_buffering(struct cdnsp_device * pdev,struct cdnsp_ep * pep)1610 static void cdnsp_get_ep_buffering(struct cdnsp_device *pdev,
1611 struct cdnsp_ep *pep)
1612 {
1613 void __iomem *reg = &pdev->cap_regs->hc_capbase;
1614 int endpoints;
1615
1616 reg += cdnsp_find_next_ext_cap(reg, 0, XBUF_CAP_ID);
1617
1618 if (!pep->direction) {
1619 pep->buffering = readl(reg + XBUF_RX_TAG_MASK_0_OFFSET);
1620 pep->buffering_period = readl(reg + XBUF_RX_TAG_MASK_1_OFFSET);
1621 pep->buffering = (pep->buffering + 1) / 2;
1622 pep->buffering_period = (pep->buffering_period + 1) / 2;
1623 return;
1624 }
1625
1626 endpoints = HCS_ENDPOINTS(pdev->hcs_params1) / 2;
1627
1628 /* Set to XBUF_TX_TAG_MASK_0 register. */
1629 reg += XBUF_TX_CMD_OFFSET + (endpoints * 2 + 2) * sizeof(u32);
1630 /* Set reg to XBUF_TX_TAG_MASK_N related with this endpoint. */
1631 reg += pep->number * sizeof(u32) * 2;
1632
1633 pep->buffering = (readl(reg) + 1) / 2;
1634 pep->buffering_period = pep->buffering;
1635 }
1636
cdnsp_gadget_init_endpoints(struct cdnsp_device * pdev)1637 static int cdnsp_gadget_init_endpoints(struct cdnsp_device *pdev)
1638 {
1639 int max_streams = HCC_MAX_PSA(pdev->hcc_params);
1640 struct cdnsp_ep *pep;
1641 int i;
1642
1643 INIT_LIST_HEAD(&pdev->gadget.ep_list);
1644
1645 if (max_streams < STREAM_LOG_STREAMS) {
1646 dev_err(pdev->dev, "Stream size %d not supported\n",
1647 max_streams);
1648 return -EINVAL;
1649 }
1650
1651 max_streams = STREAM_LOG_STREAMS;
1652
1653 for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
1654 bool direction = !(i & 1); /* Start from OUT endpoint. */
1655 u8 epnum = ((i + 1) >> 1);
1656
1657 if (!CDNSP_IF_EP_EXIST(pdev, epnum, direction))
1658 continue;
1659
1660 pep = &pdev->eps[i];
1661 pep->pdev = pdev;
1662 pep->number = epnum;
1663 pep->direction = direction; /* 0 for OUT, 1 for IN. */
1664
1665 /*
1666 * Ep0 is bidirectional, so ep0in and ep0out are represented by
1667 * pdev->eps[0]
1668 */
1669 if (epnum == 0) {
1670 snprintf(pep->name, sizeof(pep->name), "ep%d%s",
1671 epnum, "BiDir");
1672
1673 pep->idx = 0;
1674 usb_ep_set_maxpacket_limit(&pep->endpoint, 512);
1675 pep->endpoint.maxburst = 1;
1676 pep->endpoint.ops = &cdnsp_gadget_ep0_ops;
1677 pep->endpoint.desc = &cdnsp_gadget_ep0_desc;
1678 pep->endpoint.comp_desc = NULL;
1679 pep->endpoint.caps.type_control = true;
1680 pep->endpoint.caps.dir_in = true;
1681 pep->endpoint.caps.dir_out = true;
1682
1683 pdev->ep0_preq.epnum = pep->number;
1684 pdev->ep0_preq.pep = pep;
1685 pdev->gadget.ep0 = &pep->endpoint;
1686 } else {
1687 snprintf(pep->name, sizeof(pep->name), "ep%d%s",
1688 epnum, (pep->direction) ? "in" : "out");
1689
1690 pep->idx = (epnum * 2 + (direction ? 1 : 0)) - 1;
1691 usb_ep_set_maxpacket_limit(&pep->endpoint, 1024);
1692
1693 pep->endpoint.max_streams = max_streams;
1694 pep->endpoint.ops = &cdnsp_gadget_ep_ops;
1695 list_add_tail(&pep->endpoint.ep_list,
1696 &pdev->gadget.ep_list);
1697
1698 pep->endpoint.caps.type_iso = true;
1699 pep->endpoint.caps.type_bulk = true;
1700 pep->endpoint.caps.type_int = true;
1701
1702 pep->endpoint.caps.dir_in = direction;
1703 pep->endpoint.caps.dir_out = !direction;
1704 }
1705
1706 pep->endpoint.name = pep->name;
1707 pep->in_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, pep->idx);
1708 pep->out_ctx = cdnsp_get_ep_ctx(&pdev->out_ctx, pep->idx);
1709 cdnsp_get_ep_buffering(pdev, pep);
1710
1711 dev_dbg(pdev->dev, "Init %s, MPS: %04x SupType: "
1712 "CTRL: %s, INT: %s, BULK: %s, ISOC %s, "
1713 "SupDir IN: %s, OUT: %s\n",
1714 pep->name, 1024,
1715 (pep->endpoint.caps.type_control) ? "yes" : "no",
1716 (pep->endpoint.caps.type_int) ? "yes" : "no",
1717 (pep->endpoint.caps.type_bulk) ? "yes" : "no",
1718 (pep->endpoint.caps.type_iso) ? "yes" : "no",
1719 (pep->endpoint.caps.dir_in) ? "yes" : "no",
1720 (pep->endpoint.caps.dir_out) ? "yes" : "no");
1721
1722 INIT_LIST_HEAD(&pep->pending_list);
1723 }
1724
1725 return 0;
1726 }
1727
cdnsp_gadget_free_endpoints(struct cdnsp_device * pdev)1728 static void cdnsp_gadget_free_endpoints(struct cdnsp_device *pdev)
1729 {
1730 struct cdnsp_ep *pep;
1731 int i;
1732
1733 for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
1734 pep = &pdev->eps[i];
1735 if (pep->number != 0 && pep->out_ctx)
1736 list_del(&pep->endpoint.ep_list);
1737 }
1738 }
1739
cdnsp_disconnect_gadget(struct cdnsp_device * pdev)1740 void cdnsp_disconnect_gadget(struct cdnsp_device *pdev)
1741 {
1742 pdev->cdnsp_state |= CDNSP_STATE_DISCONNECT_PENDING;
1743
1744 if (pdev->gadget_driver && pdev->gadget_driver->disconnect) {
1745 spin_unlock(&pdev->lock);
1746 pdev->gadget_driver->disconnect(&pdev->gadget);
1747 spin_lock(&pdev->lock);
1748 }
1749
1750 pdev->gadget.speed = USB_SPEED_UNKNOWN;
1751 usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED);
1752
1753 pdev->cdnsp_state &= ~CDNSP_STATE_DISCONNECT_PENDING;
1754 }
1755
cdnsp_suspend_gadget(struct cdnsp_device * pdev)1756 void cdnsp_suspend_gadget(struct cdnsp_device *pdev)
1757 {
1758 if (pdev->gadget_driver && pdev->gadget_driver->suspend) {
1759 spin_unlock(&pdev->lock);
1760 pdev->gadget_driver->suspend(&pdev->gadget);
1761 spin_lock(&pdev->lock);
1762 }
1763 }
1764
cdnsp_resume_gadget(struct cdnsp_device * pdev)1765 void cdnsp_resume_gadget(struct cdnsp_device *pdev)
1766 {
1767 if (pdev->gadget_driver && pdev->gadget_driver->resume) {
1768 spin_unlock(&pdev->lock);
1769 pdev->gadget_driver->resume(&pdev->gadget);
1770 spin_lock(&pdev->lock);
1771 }
1772 }
1773
cdnsp_irq_reset(struct cdnsp_device * pdev)1774 void cdnsp_irq_reset(struct cdnsp_device *pdev)
1775 {
1776 struct cdnsp_port_regs __iomem *port_regs;
1777
1778 cdnsp_reset_device(pdev);
1779
1780 port_regs = pdev->active_port->regs;
1781 pdev->gadget.speed = cdnsp_port_speed(readl(port_regs));
1782
1783 spin_unlock(&pdev->lock);
1784 usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver);
1785 spin_lock(&pdev->lock);
1786
1787 switch (pdev->gadget.speed) {
1788 case USB_SPEED_SUPER_PLUS:
1789 case USB_SPEED_SUPER:
1790 cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1791 pdev->gadget.ep0->maxpacket = 512;
1792 break;
1793 case USB_SPEED_HIGH:
1794 case USB_SPEED_FULL:
1795 cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1796 pdev->gadget.ep0->maxpacket = 64;
1797 break;
1798 default:
1799 /* Low speed is not supported. */
1800 dev_err(pdev->dev, "Unknown device speed\n");
1801 break;
1802 }
1803
1804 cdnsp_clear_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
1805 cdnsp_setup_device(pdev, SETUP_CONTEXT_ONLY);
1806 usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
1807 }
1808
cdnsp_get_rev_cap(struct cdnsp_device * pdev)1809 static void cdnsp_get_rev_cap(struct cdnsp_device *pdev)
1810 {
1811 void __iomem *reg = &pdev->cap_regs->hc_capbase;
1812
1813 reg += cdnsp_find_next_ext_cap(reg, 0, RTL_REV_CAP);
1814 pdev->rev_cap = reg;
1815
1816 pdev->rtl_revision = readl(&pdev->rev_cap->rtl_revision);
1817
1818 dev_info(pdev->dev, "Rev: %08x/%08x, eps: %08x, buff: %08x/%08x\n",
1819 readl(&pdev->rev_cap->ctrl_revision),
1820 readl(&pdev->rev_cap->rtl_revision),
1821 readl(&pdev->rev_cap->ep_supported),
1822 readl(&pdev->rev_cap->rx_buff_size),
1823 readl(&pdev->rev_cap->tx_buff_size));
1824 }
1825
cdnsp_gen_setup(struct cdnsp_device * pdev)1826 static int cdnsp_gen_setup(struct cdnsp_device *pdev)
1827 {
1828 int ret;
1829 u32 reg;
1830
1831 pdev->cap_regs = pdev->regs;
1832 pdev->op_regs = pdev->regs +
1833 HC_LENGTH(readl(&pdev->cap_regs->hc_capbase));
1834 pdev->run_regs = pdev->regs +
1835 (readl(&pdev->cap_regs->run_regs_off) & RTSOFF_MASK);
1836
1837 /* Cache read-only capability registers */
1838 pdev->hcs_params1 = readl(&pdev->cap_regs->hcs_params1);
1839 pdev->hcc_params = readl(&pdev->cap_regs->hc_capbase);
1840 pdev->hci_version = HC_VERSION(pdev->hcc_params);
1841 pdev->hcc_params = readl(&pdev->cap_regs->hcc_params);
1842
1843 /*
1844 * Override the APB timeout value to give the controller more time for
1845 * enabling UTMI clock and synchronizing APB and UTMI clock domains.
1846 * This fix is platform specific and is required to fixes issue with
1847 * reading incorrect value from PORTSC register after resuming
1848 * from L1 state.
1849 */
1850 cdnsp_set_apb_timeout_value(pdev);
1851
1852 cdnsp_get_rev_cap(pdev);
1853
1854 /* Make sure the Device Controller is halted. */
1855 ret = cdnsp_halt(pdev);
1856 if (ret)
1857 return ret;
1858
1859 /* Reset the internal controller memory state and registers. */
1860 ret = cdnsp_reset(pdev);
1861 if (ret)
1862 return ret;
1863
1864 /*
1865 * Set dma_mask and coherent_dma_mask to 64-bits,
1866 * if controller supports 64-bit addressing.
1867 */
1868 if (HCC_64BIT_ADDR(pdev->hcc_params) &&
1869 !dma_set_mask(pdev->dev, DMA_BIT_MASK(64))) {
1870 dev_dbg(pdev->dev, "Enabling 64-bit DMA addresses.\n");
1871 dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(64));
1872 } else {
1873 /*
1874 * This is to avoid error in cases where a 32-bit USB
1875 * controller is used on a 64-bit capable system.
1876 */
1877 ret = dma_set_mask(pdev->dev, DMA_BIT_MASK(32));
1878 if (ret)
1879 return ret;
1880
1881 dev_dbg(pdev->dev, "Enabling 32-bit DMA addresses.\n");
1882 dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(32));
1883 }
1884
1885 spin_lock_init(&pdev->lock);
1886
1887 ret = cdnsp_mem_init(pdev);
1888 if (ret)
1889 return ret;
1890
1891 /*
1892 * Software workaround for U1: after transition
1893 * to U1 the controller starts gating clock, and in some cases,
1894 * it causes that controller stack.
1895 */
1896 reg = readl(&pdev->port3x_regs->mode_2);
1897 reg &= ~CFG_3XPORT_U1_PIPE_CLK_GATE_EN;
1898 writel(reg, &pdev->port3x_regs->mode_2);
1899
1900 return 0;
1901 }
1902
__cdnsp_gadget_init(struct cdns * cdns)1903 static int __cdnsp_gadget_init(struct cdns *cdns)
1904 {
1905 struct cdnsp_device *pdev;
1906 u32 max_speed;
1907 int ret = -ENOMEM;
1908
1909 cdns_drd_gadget_on(cdns);
1910
1911 pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
1912 if (!pdev)
1913 return -ENOMEM;
1914
1915 pm_runtime_get_sync(cdns->dev);
1916
1917 cdns->gadget_dev = pdev;
1918 pdev->dev = cdns->dev;
1919 pdev->regs = cdns->dev_regs;
1920 max_speed = usb_get_maximum_speed(cdns->dev);
1921
1922 switch (max_speed) {
1923 case USB_SPEED_FULL:
1924 case USB_SPEED_HIGH:
1925 case USB_SPEED_SUPER:
1926 case USB_SPEED_SUPER_PLUS:
1927 break;
1928 default:
1929 dev_err(cdns->dev, "invalid speed parameter %d\n", max_speed);
1930 fallthrough;
1931 case USB_SPEED_UNKNOWN:
1932 /* Default to SSP */
1933 max_speed = USB_SPEED_SUPER_PLUS;
1934 break;
1935 }
1936
1937 pdev->gadget.ops = &cdnsp_gadget_ops;
1938 pdev->gadget.name = "cdnsp-gadget";
1939 pdev->gadget.speed = USB_SPEED_UNKNOWN;
1940 pdev->gadget.sg_supported = 1;
1941 pdev->gadget.max_speed = max_speed;
1942 pdev->gadget.lpm_capable = 1;
1943
1944 pdev->setup_buf = kzalloc(CDNSP_EP0_SETUP_SIZE, GFP_KERNEL);
1945 if (!pdev->setup_buf)
1946 goto free_pdev;
1947
1948 /*
1949 * Controller supports not aligned buffer but it should improve
1950 * performance.
1951 */
1952 pdev->gadget.quirk_ep_out_aligned_size = true;
1953
1954 ret = cdnsp_gen_setup(pdev);
1955 if (ret) {
1956 dev_err(pdev->dev, "Generic initialization failed %d\n", ret);
1957 goto free_setup;
1958 }
1959
1960 ret = cdnsp_gadget_init_endpoints(pdev);
1961 if (ret) {
1962 dev_err(pdev->dev, "failed to initialize endpoints\n");
1963 goto halt_pdev;
1964 }
1965
1966 ret = usb_add_gadget_udc(pdev->dev, &pdev->gadget);
1967 if (ret) {
1968 dev_err(pdev->dev, "failed to register udc\n");
1969 goto free_endpoints;
1970 }
1971
1972 ret = devm_request_threaded_irq(pdev->dev, cdns->dev_irq,
1973 cdnsp_irq_handler,
1974 cdnsp_thread_irq_handler, IRQF_SHARED,
1975 dev_name(pdev->dev), pdev);
1976 if (ret)
1977 goto del_gadget;
1978
1979 return 0;
1980
1981 del_gadget:
1982 usb_del_gadget_udc(&pdev->gadget);
1983 free_endpoints:
1984 cdnsp_gadget_free_endpoints(pdev);
1985 halt_pdev:
1986 cdnsp_halt(pdev);
1987 cdnsp_reset(pdev);
1988 cdnsp_mem_cleanup(pdev);
1989 free_setup:
1990 kfree(pdev->setup_buf);
1991 free_pdev:
1992 kfree(pdev);
1993
1994 return ret;
1995 }
1996
cdnsp_gadget_exit(struct cdns * cdns)1997 static void cdnsp_gadget_exit(struct cdns *cdns)
1998 {
1999 struct cdnsp_device *pdev = cdns->gadget_dev;
2000
2001 devm_free_irq(pdev->dev, cdns->dev_irq, pdev);
2002 pm_runtime_mark_last_busy(cdns->dev);
2003 pm_runtime_put_autosuspend(cdns->dev);
2004 usb_del_gadget_udc(&pdev->gadget);
2005 cdnsp_gadget_free_endpoints(pdev);
2006 cdnsp_mem_cleanup(pdev);
2007 kfree(pdev);
2008 cdns->gadget_dev = NULL;
2009 cdns_drd_gadget_off(cdns);
2010 }
2011
cdnsp_gadget_suspend(struct cdns * cdns,bool do_wakeup)2012 static int cdnsp_gadget_suspend(struct cdns *cdns, bool do_wakeup)
2013 {
2014 struct cdnsp_device *pdev = cdns->gadget_dev;
2015 unsigned long flags;
2016
2017 if (pdev->link_state == XDEV_U3)
2018 return 0;
2019
2020 spin_lock_irqsave(&pdev->lock, flags);
2021 cdnsp_disconnect_gadget(pdev);
2022 cdnsp_stop(pdev);
2023 spin_unlock_irqrestore(&pdev->lock, flags);
2024
2025 return 0;
2026 }
2027
cdnsp_gadget_resume(struct cdns * cdns,bool hibernated)2028 static int cdnsp_gadget_resume(struct cdns *cdns, bool hibernated)
2029 {
2030 struct cdnsp_device *pdev = cdns->gadget_dev;
2031 enum usb_device_speed max_speed;
2032 unsigned long flags;
2033 int ret;
2034
2035 if (!pdev->gadget_driver)
2036 return 0;
2037
2038 spin_lock_irqsave(&pdev->lock, flags);
2039 max_speed = pdev->gadget_driver->max_speed;
2040
2041 /* Limit speed if necessary. */
2042 max_speed = min(max_speed, pdev->gadget.max_speed);
2043
2044 ret = cdnsp_run(pdev, max_speed);
2045
2046 if (pdev->link_state == XDEV_U3)
2047 __cdnsp_gadget_wakeup(pdev);
2048
2049 spin_unlock_irqrestore(&pdev->lock, flags);
2050
2051 return ret;
2052 }
2053
2054 /**
2055 * cdnsp_gadget_init - initialize device structure
2056 * @cdns: cdnsp instance
2057 *
2058 * This function initializes the gadget.
2059 */
cdnsp_gadget_init(struct cdns * cdns)2060 int cdnsp_gadget_init(struct cdns *cdns)
2061 {
2062 struct cdns_role_driver *rdrv;
2063
2064 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
2065 if (!rdrv)
2066 return -ENOMEM;
2067
2068 rdrv->start = __cdnsp_gadget_init;
2069 rdrv->stop = cdnsp_gadget_exit;
2070 rdrv->suspend = cdnsp_gadget_suspend;
2071 rdrv->resume = cdnsp_gadget_resume;
2072 rdrv->state = CDNS_ROLE_STATE_INACTIVE;
2073 rdrv->name = "gadget";
2074 cdns->roles[USB_ROLE_DEVICE] = rdrv;
2075
2076 return 0;
2077 }
2078