1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <common.h>
23#include <cpu_func.h>
24#include <dm.h>
25#include <dm/device_compat.h>
26#include <log.h>
27#include <malloc.h>
28#include <usb.h>
29#include <usb/xhci.h>
30#include <watchdog.h>
31#include <asm/byteorder.h>
32#include <asm/cache.h>
33#include <asm/unaligned.h>
34#include <linux/bitops.h>
35#include <linux/bug.h>
36#include <linux/delay.h>
37#include <linux/errno.h>
38#include <linux/iopoll.h>
39
40#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
41#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
42#endif
43
44static struct descriptor {
45 struct usb_hub_descriptor hub;
46 struct usb_device_descriptor device;
47 struct usb_config_descriptor config;
48 struct usb_interface_descriptor interface;
49 struct usb_endpoint_descriptor endpoint;
50 struct usb_ss_ep_comp_descriptor ep_companion;
51} __attribute__ ((packed)) descriptor = {
52 {
53 0xc,
54 0x2a,
55 2,
56 cpu_to_le16(0x8),
57 10,
58 0,
59 {
60 }
61 },
62 {
63 0x12,
64 1,
65 cpu_to_le16(0x0300),
66 9,
67 0,
68 3,
69 9,
70 0x0000,
71 0x0000,
72 cpu_to_le16(0x0100),
73 1,
74 2,
75 0,
76 1
77 },
78 {
79 0x9,
80 2,
81 cpu_to_le16(0x1f),
82 1,
83 1,
84 0,
85 0x40,
86 0
87 },
88 {
89 0x9,
90 4,
91 0,
92 0,
93 1,
94 9,
95 0,
96 0,
97 0
98 },
99 {
100 0x7,
101 5,
102 0x81,
103 3,
104 8,
105 255
106 },
107 {
108 0x06,
109 0x30,
110 0x00,
111
112 0x00,
113
114 cpu_to_le16(0x02),
115 },
116};
117
118#if !CONFIG_IS_ENABLED(DM_USB)
119static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
120#endif
121
122struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
123{
124#if CONFIG_IS_ENABLED(DM_USB)
125 struct udevice *dev;
126
127
128 for (dev = udev->dev;
129 device_get_uclass_id(dev) != UCLASS_USB;
130 dev = dev->parent)
131 ;
132 return dev_get_priv(dev);
133#else
134 return udev->controller;
135#endif
136}
137
138
139
140
141
142
143
144
145
146
147
148static int
149handshake(uint32_t volatile *ptr, uint32_t mask, uint32_t done, int usec)
150{
151 uint32_t result;
152 int ret;
153
154 ret = readx_poll_sleep_timeout(xhci_readl, ptr, result,
155 (result & mask) == done || result == U32_MAX,
156 1, usec);
157 if (result == U32_MAX)
158 return -ENODEV;
159
160 return ret;
161}
162
163
164
165
166
167
168
169static int xhci_start(struct xhci_hcor *hcor)
170{
171 u32 temp;
172 int ret;
173
174 puts("Starting the controller\n");
175 temp = xhci_readl(&hcor->or_usbcmd);
176 temp |= (CMD_RUN);
177 xhci_writel(&hcor->or_usbcmd, temp);
178
179
180
181
182
183 ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
184 if (ret)
185 debug("Host took too long to start, "
186 "waited %u microseconds.\n",
187 XHCI_MAX_HALT_USEC);
188 return ret;
189}
190
191
192
193
194
195
196
197static int xhci_reset(struct xhci_hcor *hcor)
198{
199 u32 cmd;
200 u32 state;
201 int ret;
202
203
204 debug("// Halt the HC: %p\n", hcor);
205 state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
206 if (!state) {
207 cmd = xhci_readl(&hcor->or_usbcmd);
208 cmd &= ~CMD_RUN;
209 xhci_writel(&hcor->or_usbcmd, cmd);
210 }
211
212 ret = handshake(&hcor->or_usbsts,
213 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
214 if (ret) {
215 printf("Host not halted after %u microseconds.\n",
216 XHCI_MAX_HALT_USEC);
217 return -EBUSY;
218 }
219
220 debug("// Reset the HC\n");
221 cmd = xhci_readl(&hcor->or_usbcmd);
222 cmd |= CMD_RESET;
223 xhci_writel(&hcor->or_usbcmd, cmd);
224
225 ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
226 if (ret)
227 return ret;
228
229
230
231
232
233 return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
234}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
250{
251 unsigned int index;
252
253 if (usb_endpoint_xfer_control(desc))
254 index = (unsigned int)(usb_endpoint_num(desc) * 2);
255 else
256 index = (unsigned int)((usb_endpoint_num(desc) * 2) -
257 (usb_endpoint_dir_in(desc) ? 0 : 1));
258
259 return index;
260}
261
262
263
264
265
266static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
267 unsigned int min_exponent,
268 unsigned int max_exponent)
269{
270 unsigned int interval;
271
272 interval = fls(desc_interval) - 1;
273 interval = clamp_val(interval, min_exponent, max_exponent);
274 if ((1 << interval) != desc_interval)
275 debug("rounding interval to %d microframes, "\
276 "ep desc says %d microframes\n",
277 1 << interval, desc_interval);
278
279 return interval;
280}
281
282static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
283 struct usb_endpoint_descriptor *endpt_desc)
284{
285 if (endpt_desc->bInterval == 0)
286 return 0;
287
288 return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
289}
290
291static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
292 struct usb_endpoint_descriptor *endpt_desc)
293{
294 return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
295}
296
297
298
299
300
301static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
302 struct usb_endpoint_descriptor *endpt_desc)
303{
304 unsigned int interval;
305
306 interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
307 if (interval != endpt_desc->bInterval - 1)
308 debug("ep %#x - rounding interval to %d %sframes\n",
309 endpt_desc->bEndpointAddress, 1 << interval,
310 udev->speed == USB_SPEED_FULL ? "" : "micro");
311
312 if (udev->speed == USB_SPEED_FULL) {
313
314
315
316
317
318 interval += 3;
319 }
320
321 return interval;
322}
323
324
325
326
327
328
329
330
331
332
333static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
334 struct usb_endpoint_descriptor *endpt_desc)
335{
336 unsigned int interval = 0;
337
338 switch (udev->speed) {
339 case USB_SPEED_HIGH:
340
341 if (usb_endpoint_xfer_control(endpt_desc) ||
342 usb_endpoint_xfer_bulk(endpt_desc)) {
343 interval = xhci_parse_microframe_interval(udev,
344 endpt_desc);
345 break;
346 }
347
348
349 case USB_SPEED_SUPER:
350 if (usb_endpoint_xfer_int(endpt_desc) ||
351 usb_endpoint_xfer_isoc(endpt_desc)) {
352 interval = xhci_parse_exponent_interval(udev,
353 endpt_desc);
354 }
355 break;
356
357 case USB_SPEED_FULL:
358 if (usb_endpoint_xfer_isoc(endpt_desc)) {
359 interval = xhci_parse_exponent_interval(udev,
360 endpt_desc);
361 break;
362 }
363
364
365
366
367
368
369 case USB_SPEED_LOW:
370 if (usb_endpoint_xfer_int(endpt_desc) ||
371 usb_endpoint_xfer_isoc(endpt_desc)) {
372 interval = xhci_parse_frame_interval(udev, endpt_desc);
373 }
374 break;
375
376 default:
377 BUG();
378 }
379
380 return interval;
381}
382
383
384
385
386
387
388
389static u32 xhci_get_endpoint_mult(struct usb_device *udev,
390 struct usb_endpoint_descriptor *endpt_desc,
391 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
392{
393 if (udev->speed < USB_SPEED_SUPER ||
394 !usb_endpoint_xfer_isoc(endpt_desc))
395 return 0;
396
397 return ss_ep_comp_desc->bmAttributes;
398}
399
400static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
401 struct usb_endpoint_descriptor *endpt_desc,
402 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
403{
404
405 if (udev->speed >= USB_SPEED_SUPER)
406 return ss_ep_comp_desc->bMaxBurst;
407
408 if (udev->speed == USB_SPEED_HIGH &&
409 (usb_endpoint_xfer_isoc(endpt_desc) ||
410 usb_endpoint_xfer_int(endpt_desc)))
411 return usb_endpoint_maxp_mult(endpt_desc) - 1;
412
413 return 0;
414}
415
416
417
418
419
420
421static u32 xhci_get_max_esit_payload(struct usb_device *udev,
422 struct usb_endpoint_descriptor *endpt_desc,
423 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
424{
425 int max_burst;
426 int max_packet;
427
428
429 if (usb_endpoint_xfer_control(endpt_desc) ||
430 usb_endpoint_xfer_bulk(endpt_desc))
431 return 0;
432
433
434 if (udev->speed >= USB_SPEED_SUPER)
435 return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
436
437 max_packet = usb_endpoint_maxp(endpt_desc);
438 max_burst = usb_endpoint_maxp_mult(endpt_desc);
439
440
441 return max_packet * max_burst;
442}
443
444
445
446
447
448
449
450
451
452static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
453{
454 struct xhci_container_ctx *in_ctx;
455 struct xhci_virt_device *virt_dev;
456 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
457 union xhci_trb *event;
458
459 virt_dev = ctrl->devs[udev->slot_id];
460 in_ctx = virt_dev->in_ctx;
461
462 xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
463 xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
464 ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
465 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
466 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
467 != udev->slot_id);
468
469 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
470 case COMP_SUCCESS:
471 debug("Successful %s command\n",
472 ctx_change ? "Evaluate Context" : "Configure Endpoint");
473 break;
474 default:
475 printf("ERROR: %s command returned completion code %d.\n",
476 ctx_change ? "Evaluate Context" : "Configure Endpoint",
477 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
478 return -EINVAL;
479 }
480
481 xhci_acknowledge_event(ctrl);
482
483 return 0;
484}
485
486
487
488
489
490
491
492static int xhci_set_configuration(struct usb_device *udev)
493{
494 struct xhci_container_ctx *in_ctx;
495 struct xhci_container_ctx *out_ctx;
496 struct xhci_input_control_ctx *ctrl_ctx;
497 struct xhci_slot_ctx *slot_ctx;
498 struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
499 int cur_ep;
500 int max_ep_flag = 0;
501 int ep_index;
502 unsigned int dir;
503 unsigned int ep_type;
504 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
505 int num_of_ep;
506 int ep_flag = 0;
507 u64 trb_64 = 0;
508 int slot_id = udev->slot_id;
509 struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
510 struct usb_interface *ifdesc;
511 u32 max_esit_payload;
512 unsigned int interval;
513 unsigned int mult;
514 unsigned int max_burst;
515 unsigned int avg_trb_len;
516 unsigned int err_count = 0;
517
518 out_ctx = virt_dev->out_ctx;
519 in_ctx = virt_dev->in_ctx;
520
521 num_of_ep = udev->config.if_desc[0].no_of_ep;
522 ifdesc = &udev->config.if_desc[0];
523
524 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
525
526 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
527 ctrl_ctx->drop_flags = 0;
528
529
530 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
531 ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
532 ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
533 if (max_ep_flag < ep_flag)
534 max_ep_flag = ep_flag;
535 }
536
537 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
538
539
540 xhci_slot_copy(ctrl, in_ctx, out_ctx);
541 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
542 slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
543 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
544
545 xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
546
547
548 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
549 struct usb_endpoint_descriptor *endpt_desc = NULL;
550 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
551
552 endpt_desc = &ifdesc->ep_desc[cur_ep];
553 ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
554 trb_64 = 0;
555
556
557
558
559
560
561
562
563 max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
564 ss_ep_comp_desc);
565 interval = xhci_get_endpoint_interval(udev, endpt_desc);
566 mult = xhci_get_endpoint_mult(udev, endpt_desc,
567 ss_ep_comp_desc);
568 max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
569 ss_ep_comp_desc);
570 avg_trb_len = max_esit_payload;
571
572 ep_index = xhci_get_ep_index(endpt_desc);
573 ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
574
575
576 virt_dev->eps[ep_index].ring = xhci_ring_alloc(ctrl, 1, true);
577 if (!virt_dev->eps[ep_index].ring)
578 return -ENOMEM;
579
580
581 dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
582 ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
583
584 ep_ctx[ep_index]->ep_info =
585 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
586 EP_INTERVAL(interval) | EP_MULT(mult));
587
588 ep_ctx[ep_index]->ep_info2 = cpu_to_le32(EP_TYPE(ep_type));
589 ep_ctx[ep_index]->ep_info2 |=
590 cpu_to_le32(MAX_PACKET
591 (get_unaligned(&endpt_desc->wMaxPacketSize)));
592
593
594 if (!usb_endpoint_xfer_isoc(endpt_desc))
595 err_count = 3;
596 ep_ctx[ep_index]->ep_info2 |=
597 cpu_to_le32(MAX_BURST(max_burst) |
598 ERROR_COUNT(err_count));
599
600 trb_64 = xhci_virt_to_bus(ctrl, virt_dev->eps[ep_index].ring->enqueue);
601 ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
602 virt_dev->eps[ep_index].ring->cycle_state);
603
604
605
606
607
608 if (usb_endpoint_xfer_control(endpt_desc))
609 avg_trb_len = 8;
610 ep_ctx[ep_index]->tx_info =
611 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
612 EP_AVG_TRB_LENGTH(avg_trb_len));
613
614
615
616
617
618
619 if (ctrl->quirks & XHCI_MTK_HOST) {
620 ep_ctx[ep_index]->reserved[0] =
621 cpu_to_le32(EP_BPKTS(1) | EP_BBM(1));
622 }
623 }
624
625 return xhci_configure_endpoints(udev, false);
626}
627
628
629
630
631
632
633
634
635static int xhci_address_device(struct usb_device *udev, int root_portnr)
636{
637 int ret = 0;
638 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
639 struct xhci_slot_ctx *slot_ctx;
640 struct xhci_input_control_ctx *ctrl_ctx;
641 struct xhci_virt_device *virt_dev;
642 int slot_id = udev->slot_id;
643 union xhci_trb *event;
644
645 virt_dev = ctrl->devs[slot_id];
646
647
648
649
650
651 debug("Setting up addressable devices %p\n", ctrl->dcbaa);
652 xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
653
654 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
655 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
656 ctrl_ctx->drop_flags = 0;
657
658 xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
659 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
660 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
661
662 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
663 case COMP_CTX_STATE:
664 case COMP_EBADSLT:
665 printf("Setup ERROR: address device command for slot %d.\n",
666 slot_id);
667 ret = -EINVAL;
668 break;
669 case COMP_TX_ERR:
670 puts("Device not responding to set address.\n");
671 ret = -EPROTO;
672 break;
673 case COMP_DEV_ERR:
674 puts("ERROR: Incompatible device"
675 "for address device command.\n");
676 ret = -ENODEV;
677 break;
678 case COMP_SUCCESS:
679 debug("Successful Address Device command\n");
680 udev->status = 0;
681 break;
682 default:
683 printf("ERROR: unexpected command completion code 0x%x.\n",
684 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
685 ret = -EINVAL;
686 break;
687 }
688
689 xhci_acknowledge_event(ctrl);
690
691 if (ret < 0)
692
693
694
695
696 return ret;
697
698 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
699 virt_dev->out_ctx->size);
700 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
701
702 debug("xHC internal address is: %d\n",
703 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
704
705 return 0;
706}
707
708
709
710
711
712
713
714
715
716
717static int _xhci_alloc_device(struct usb_device *udev)
718{
719 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
720 union xhci_trb *event;
721 int ret;
722
723
724
725
726
727
728 if (ctrl->rootdev == 0) {
729 udev->speed = USB_SPEED_SUPER;
730 return 0;
731 }
732
733 xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
734 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
735 BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
736 != COMP_SUCCESS);
737
738 udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
739
740 xhci_acknowledge_event(ctrl);
741
742 ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
743 if (ret < 0) {
744
745
746
747
748 puts("Could not allocate xHCI USB device data structures\n");
749 return ret;
750 }
751
752 return 0;
753}
754
755#if !CONFIG_IS_ENABLED(DM_USB)
756int usb_alloc_device(struct usb_device *udev)
757{
758 return _xhci_alloc_device(udev);
759}
760#endif
761
762
763
764
765
766
767
768
769
770
771int xhci_check_maxpacket(struct usb_device *udev)
772{
773 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
774 unsigned int slot_id = udev->slot_id;
775 int ep_index = 0;
776 struct xhci_container_ctx *in_ctx;
777 struct xhci_container_ctx *out_ctx;
778 struct xhci_input_control_ctx *ctrl_ctx;
779 struct xhci_ep_ctx *ep_ctx;
780 int max_packet_size;
781 int hw_max_packet_size;
782 int ret = 0;
783
784 out_ctx = ctrl->devs[slot_id]->out_ctx;
785 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
786
787 ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
788 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
789 max_packet_size = udev->epmaxpacketin[0];
790 if (hw_max_packet_size != max_packet_size) {
791 debug("Max Packet Size for ep 0 changed.\n");
792 debug("Max packet size in usb_device = %d\n", max_packet_size);
793 debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
794 debug("Issuing evaluate context command.\n");
795
796
797 xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
798 ctrl->devs[slot_id]->out_ctx, ep_index);
799 in_ctx = ctrl->devs[slot_id]->in_ctx;
800 ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
801 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET(MAX_PACKET_MASK));
802 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
803
804
805
806
807
808
809 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
810 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
811 ctrl_ctx->drop_flags = 0;
812
813 ret = xhci_configure_endpoints(udev, true);
814 }
815 return ret;
816}
817
818
819
820
821
822
823
824
825
826
827static void xhci_clear_port_change_bit(u16 wValue,
828 u16 wIndex, volatile uint32_t *addr, u32 port_status)
829{
830 char *port_change_bit;
831 u32 status;
832
833 switch (wValue) {
834 case USB_PORT_FEAT_C_RESET:
835 status = PORT_RC;
836 port_change_bit = "reset";
837 break;
838 case USB_PORT_FEAT_C_CONNECTION:
839 status = PORT_CSC;
840 port_change_bit = "connect";
841 break;
842 case USB_PORT_FEAT_C_OVER_CURRENT:
843 status = PORT_OCC;
844 port_change_bit = "over-current";
845 break;
846 case USB_PORT_FEAT_C_ENABLE:
847 status = PORT_PEC;
848 port_change_bit = "enable/disable";
849 break;
850 case USB_PORT_FEAT_C_SUSPEND:
851 status = PORT_PLC;
852 port_change_bit = "suspend/resume";
853 break;
854 default:
855
856 return;
857 }
858
859
860 xhci_writel(addr, port_status | status);
861
862 port_status = xhci_readl(addr);
863 debug("clear port %s change, actual port %d status = 0x%x\n",
864 port_change_bit, wIndex, port_status);
865}
866
867
868
869
870
871
872
873
874
875
876
877static u32 xhci_port_state_to_neutral(u32 state)
878{
879
880 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
881}
882
883
884
885
886
887
888
889
890
891static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
892 void *buffer, struct devrequest *req)
893{
894 uint8_t tmpbuf[4];
895 u16 typeReq;
896 void *srcptr = NULL;
897 int len, srclen;
898 uint32_t reg;
899 volatile uint32_t *status_reg;
900 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
901 struct xhci_hccr *hccr = ctrl->hccr;
902 struct xhci_hcor *hcor = ctrl->hcor;
903 int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
904
905 if ((req->requesttype & USB_RT_PORT) &&
906 le16_to_cpu(req->index) > max_ports) {
907 printf("The request port(%d) exceeds maximum port number\n",
908 le16_to_cpu(req->index) - 1);
909 return -EINVAL;
910 }
911
912 status_reg = (volatile uint32_t *)
913 (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
914 srclen = 0;
915
916 typeReq = req->request | req->requesttype << 8;
917
918 switch (typeReq) {
919 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
920 switch (le16_to_cpu(req->value) >> 8) {
921 case USB_DT_DEVICE:
922 debug("USB_DT_DEVICE request\n");
923 srcptr = &descriptor.device;
924 srclen = 0x12;
925 break;
926 case USB_DT_CONFIG:
927 debug("USB_DT_CONFIG config\n");
928 srcptr = &descriptor.config;
929 srclen = 0x19;
930 break;
931 case USB_DT_STRING:
932 debug("USB_DT_STRING config\n");
933 switch (le16_to_cpu(req->value) & 0xff) {
934 case 0:
935 srcptr = "\4\3\11\4";
936 srclen = 4;
937 break;
938 case 1:
939 srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
940 srclen = 14;
941 break;
942 case 2:
943 srcptr = "\52\3X\0H\0C\0I\0 "
944 "\0H\0o\0s\0t\0 "
945 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
946 srclen = 42;
947 break;
948 default:
949 printf("unknown value DT_STRING %x\n",
950 le16_to_cpu(req->value));
951 goto unknown;
952 }
953 break;
954 default:
955 printf("unknown value %x\n", le16_to_cpu(req->value));
956 goto unknown;
957 }
958 break;
959 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
960 switch (le16_to_cpu(req->value) >> 8) {
961 case USB_DT_HUB:
962 case USB_DT_SS_HUB:
963 debug("USB_DT_HUB config\n");
964 srcptr = &descriptor.hub;
965 srclen = 0x8;
966 break;
967 default:
968 printf("unknown value %x\n", le16_to_cpu(req->value));
969 goto unknown;
970 }
971 break;
972 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
973 debug("USB_REQ_SET_ADDRESS\n");
974 ctrl->rootdev = le16_to_cpu(req->value);
975 break;
976 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
977
978 break;
979 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
980 tmpbuf[0] = 1;
981 tmpbuf[1] = 0;
982 srcptr = tmpbuf;
983 srclen = 2;
984 break;
985 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
986 memset(tmpbuf, 0, 4);
987 reg = xhci_readl(status_reg);
988 if (reg & PORT_CONNECT) {
989 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
990 switch (reg & DEV_SPEED_MASK) {
991 case XDEV_FS:
992 debug("SPEED = FULLSPEED\n");
993 break;
994 case XDEV_LS:
995 debug("SPEED = LOWSPEED\n");
996 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
997 break;
998 case XDEV_HS:
999 debug("SPEED = HIGHSPEED\n");
1000 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
1001 break;
1002 case XDEV_SS:
1003 debug("SPEED = SUPERSPEED\n");
1004 tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
1005 break;
1006 }
1007 }
1008 if (reg & PORT_PE)
1009 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
1010 if ((reg & PORT_PLS_MASK) == XDEV_U3)
1011 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
1012 if (reg & PORT_OC)
1013 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
1014 if (reg & PORT_RESET)
1015 tmpbuf[0] |= USB_PORT_STAT_RESET;
1016 if (reg & PORT_POWER)
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
1027 if (reg & PORT_CSC)
1028 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
1029 if (reg & PORT_PEC)
1030 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
1031 if (reg & PORT_OCC)
1032 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
1033 if (reg & PORT_RC)
1034 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
1035
1036 srcptr = tmpbuf;
1037 srclen = 4;
1038 break;
1039 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1040 reg = xhci_readl(status_reg);
1041 reg = xhci_port_state_to_neutral(reg);
1042 switch (le16_to_cpu(req->value)) {
1043 case USB_PORT_FEAT_ENABLE:
1044 reg |= PORT_PE;
1045 xhci_writel(status_reg, reg);
1046 break;
1047 case USB_PORT_FEAT_POWER:
1048 reg |= PORT_POWER;
1049 xhci_writel(status_reg, reg);
1050 break;
1051 case USB_PORT_FEAT_RESET:
1052 reg |= PORT_RESET;
1053 xhci_writel(status_reg, reg);
1054 break;
1055 default:
1056 printf("unknown feature %x\n", le16_to_cpu(req->value));
1057 goto unknown;
1058 }
1059 break;
1060 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1061 reg = xhci_readl(status_reg);
1062 reg = xhci_port_state_to_neutral(reg);
1063 switch (le16_to_cpu(req->value)) {
1064 case USB_PORT_FEAT_ENABLE:
1065 reg &= ~PORT_PE;
1066 break;
1067 case USB_PORT_FEAT_POWER:
1068 reg &= ~PORT_POWER;
1069 break;
1070 case USB_PORT_FEAT_C_RESET:
1071 case USB_PORT_FEAT_C_CONNECTION:
1072 case USB_PORT_FEAT_C_OVER_CURRENT:
1073 case USB_PORT_FEAT_C_ENABLE:
1074 xhci_clear_port_change_bit((le16_to_cpu(req->value)),
1075 le16_to_cpu(req->index),
1076 status_reg, reg);
1077 break;
1078 default:
1079 printf("unknown feature %x\n", le16_to_cpu(req->value));
1080 goto unknown;
1081 }
1082 xhci_writel(status_reg, reg);
1083 break;
1084 default:
1085 puts("Unknown request\n");
1086 goto unknown;
1087 }
1088
1089 debug("scrlen = %d\n req->length = %d\n",
1090 srclen, le16_to_cpu(req->length));
1091
1092 len = min(srclen, (int)le16_to_cpu(req->length));
1093
1094 if (srcptr != NULL && len > 0)
1095 memcpy(buffer, srcptr, len);
1096 else
1097 debug("Len is 0\n");
1098
1099 udev->act_len = len;
1100 udev->status = 0;
1101
1102 return 0;
1103
1104unknown:
1105 udev->act_len = 0;
1106 udev->status = USB_ST_STALLED;
1107
1108 return -ENODEV;
1109}
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
1122 void *buffer, int length, int interval,
1123 bool nonblock)
1124{
1125 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1126 printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1127 return -EINVAL;
1128 }
1129
1130
1131
1132
1133
1134
1135
1136 return xhci_bulk_tx(udev, pipe, length, buffer);
1137}
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
1149 void *buffer, int length)
1150{
1151 if (usb_pipetype(pipe) != PIPE_BULK) {
1152 printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1153 return -EINVAL;
1154 }
1155
1156 return xhci_bulk_tx(udev, pipe, length, buffer);
1157}
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
1171 void *buffer, int length,
1172 struct devrequest *setup, int root_portnr)
1173{
1174 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
1175 int ret = 0;
1176
1177 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1178 printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
1179 return -EINVAL;
1180 }
1181
1182 if (usb_pipedevice(pipe) == ctrl->rootdev)
1183 return xhci_submit_root(udev, pipe, buffer, setup);
1184
1185 if (setup->request == USB_REQ_SET_ADDRESS &&
1186 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
1187 return xhci_address_device(udev, root_portnr);
1188
1189 if (setup->request == USB_REQ_SET_CONFIGURATION &&
1190 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1191 ret = xhci_set_configuration(udev);
1192 if (ret) {
1193 puts("Failed to configure xHCI endpoint\n");
1194 return ret;
1195 }
1196 }
1197
1198 return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
1199}
1200
1201static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
1202{
1203 struct xhci_hccr *hccr;
1204 struct xhci_hcor *hcor;
1205 uint32_t val;
1206 uint32_t val2;
1207 uint32_t reg;
1208
1209 hccr = ctrl->hccr;
1210 hcor = ctrl->hcor;
1211
1212
1213
1214
1215 val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
1216 val2 = xhci_readl(&hcor->or_config);
1217 val |= (val2 & ~HCS_SLOTS_MASK);
1218 xhci_writel(&hcor->or_config, val);
1219
1220
1221 if (xhci_mem_init(ctrl, hccr, hcor) < 0)
1222 return -ENOMEM;
1223
1224 reg = xhci_readl(&hccr->cr_hcsparams1);
1225 descriptor.hub.bNbrPorts = HCS_MAX_PORTS(reg);
1226 printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
1227
1228
1229 reg = xhci_readl(&hccr->cr_hccparams);
1230 if (HCS_INDICATOR(reg))
1231 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1232 | 0x80, &descriptor.hub.wHubCharacteristics);
1233
1234
1235 if (HCC_PPC(reg))
1236 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1237 | 0x01, &descriptor.hub.wHubCharacteristics);
1238
1239 if (xhci_start(hcor)) {
1240 xhci_reset(hcor);
1241 return -ENODEV;
1242 }
1243
1244
1245 xhci_writel(&ctrl->ir_set->irq_control, 0x0);
1246 xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
1247
1248 reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
1249 printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
1250 ctrl->hci_version = reg;
1251
1252 return 0;
1253}
1254
1255static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
1256{
1257 u32 temp;
1258
1259 xhci_reset(ctrl->hcor);
1260
1261 debug("// Disabling event ring interrupts\n");
1262 temp = xhci_readl(&ctrl->hcor->or_usbsts);
1263 xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
1264 temp = xhci_readl(&ctrl->ir_set->irq_pending);
1265 xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
1266
1267 return 0;
1268}
1269
1270#if !CONFIG_IS_ENABLED(DM_USB)
1271int submit_control_msg(struct usb_device *udev, unsigned long pipe,
1272 void *buffer, int length, struct devrequest *setup)
1273{
1274 struct usb_device *hop = udev;
1275
1276 if (hop->parent)
1277 while (hop->parent->parent)
1278 hop = hop->parent;
1279
1280 return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1281 hop->portnr);
1282}
1283
1284int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1285 int length)
1286{
1287 return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1288}
1289
1290int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1291 int length, int interval, bool nonblock)
1292{
1293 return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
1294 nonblock);
1295}
1296
1297
1298
1299
1300
1301
1302
1303
1304int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1305{
1306 struct xhci_hccr *hccr;
1307 struct xhci_hcor *hcor;
1308 struct xhci_ctrl *ctrl;
1309 int ret;
1310
1311 *controller = NULL;
1312
1313 if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
1314 return -ENODEV;
1315
1316 if (xhci_reset(hcor) != 0)
1317 return -ENODEV;
1318
1319 ctrl = &xhcic[index];
1320
1321 ctrl->hccr = hccr;
1322 ctrl->hcor = hcor;
1323
1324 ret = xhci_lowlevel_init(ctrl);
1325
1326 if (ret) {
1327 ctrl->hccr = NULL;
1328 ctrl->hcor = NULL;
1329 } else {
1330 *controller = &xhcic[index];
1331 }
1332
1333 return ret;
1334}
1335
1336
1337
1338
1339
1340
1341
1342
1343int usb_lowlevel_stop(int index)
1344{
1345 struct xhci_ctrl *ctrl = (xhcic + index);
1346
1347 if (ctrl->hcor) {
1348 xhci_lowlevel_stop(ctrl);
1349 xhci_hcd_stop(index);
1350 xhci_cleanup(ctrl);
1351 }
1352
1353 return 0;
1354}
1355#endif
1356
1357#if CONFIG_IS_ENABLED(DM_USB)
1358
1359static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1360 unsigned long pipe, void *buffer, int length,
1361 struct devrequest *setup)
1362{
1363 struct usb_device *uhop;
1364 struct udevice *hub;
1365 int root_portnr = 0;
1366
1367 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1368 dev->name, udev, udev->dev->name, udev->portnr);
1369 hub = udev->dev;
1370 if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
1371
1372 if (usb_hub_is_root_hub(hub)) {
1373 root_portnr = udev->portnr;
1374 } else {
1375 while (!usb_hub_is_root_hub(hub->parent))
1376 hub = hub->parent;
1377 uhop = dev_get_parent_priv(hub);
1378 root_portnr = uhop->portnr;
1379 }
1380 }
1381
1382
1383
1384
1385
1386
1387
1388 return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1389 root_portnr);
1390}
1391
1392static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1393 unsigned long pipe, void *buffer, int length)
1394{
1395 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1396 return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1397}
1398
1399static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1400 unsigned long pipe, void *buffer, int length,
1401 int interval, bool nonblock)
1402{
1403 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1404 return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
1405 nonblock);
1406}
1407
1408static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
1409{
1410 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1411 return _xhci_alloc_device(udev);
1412}
1413
1414static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
1415{
1416 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1417 struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
1418 struct xhci_virt_device *virt_dev;
1419 struct xhci_input_control_ctx *ctrl_ctx;
1420 struct xhci_container_ctx *out_ctx;
1421 struct xhci_container_ctx *in_ctx;
1422 struct xhci_slot_ctx *slot_ctx;
1423 int slot_id = udev->slot_id;
1424 unsigned think_time;
1425
1426 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1427
1428
1429 if (usb_hub_is_root_hub(udev->dev))
1430 return 0;
1431
1432 virt_dev = ctrl->devs[slot_id];
1433 BUG_ON(!virt_dev);
1434
1435 out_ctx = virt_dev->out_ctx;
1436 in_ctx = virt_dev->in_ctx;
1437
1438 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1439
1440 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1441 ctrl_ctx->drop_flags = 0;
1442
1443 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
1444
1445
1446 xhci_slot_copy(ctrl, in_ctx, out_ctx);
1447 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
1448
1449
1450 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
1451
1452
1453
1454
1455
1456 if (hub->tt.multi)
1457 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
1458 else if (udev->speed == USB_SPEED_FULL)
1459 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
1460 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470 think_time = hub->tt.think_time;
1471 if (think_time != 0)
1472 think_time = (think_time / 666) - 1;
1473 if (udev->speed == USB_SPEED_HIGH)
1474 slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
1475 slot_ctx->dev_state = 0;
1476
1477 return xhci_configure_endpoints(udev, false);
1478}
1479
1480static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
1481{
1482
1483
1484
1485
1486
1487
1488
1489 *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
1490
1491 return 0;
1492}
1493
1494int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1495 struct xhci_hcor *hcor)
1496{
1497 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1498 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1499 int ret;
1500
1501 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
1502 ctrl, hccr, hcor);
1503
1504 ctrl->dev = dev;
1505
1506
1507
1508
1509
1510
1511
1512 priv->desc_before_addr = false;
1513
1514 ret = xhci_reset(hcor);
1515 if (ret)
1516 goto err;
1517
1518 ctrl->hccr = hccr;
1519 ctrl->hcor = hcor;
1520 ret = xhci_lowlevel_init(ctrl);
1521 if (ret)
1522 goto err;
1523
1524 return 0;
1525err:
1526 free(ctrl);
1527 debug("%s: failed, ret=%d\n", __func__, ret);
1528 return ret;
1529}
1530
1531int xhci_deregister(struct udevice *dev)
1532{
1533 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1534
1535 xhci_lowlevel_stop(ctrl);
1536 xhci_cleanup(ctrl);
1537
1538 return 0;
1539}
1540
1541struct dm_usb_ops xhci_usb_ops = {
1542 .control = xhci_submit_control_msg,
1543 .bulk = xhci_submit_bulk_msg,
1544 .interrupt = xhci_submit_int_msg,
1545 .alloc_device = xhci_alloc_device,
1546 .update_hub_device = xhci_update_hub_device,
1547 .get_max_xfer_size = xhci_get_max_xfer_size,
1548};
1549
1550#endif
1551