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