1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
|
/*
* Copyright : (C) 2022 Phytium Information Technology, Inc.
* All Rights Reserved.
*
* This program is OPEN SOURCE software: you can redistribute it and/or modify it
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
* either version 1.0 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Phytium Public License for more details.
*
*
* FilePath: fxhci.c
* Date: 2022-02-11 13:33:12
* LastEditTime: 2022-02-18 09:17:20
* Description: This files is for implmentation of XHCI driver
*
* Modify History:
* Ver Who Date Changes
* ----- ------ -------- --------------------------------------
* 1.0 Zhugengyu 2022/2/7 init commit
*/
#include <string.h>
#include "fsleep.h"
#include "fcache.h"
#include "fparameters.h"
#include "fdebug.h"
#include "fxhci_private.h"
#define FUSB_DEBUG_TAG "FXHCI"
#define FUSB_ERROR(format, ...) FT_DEBUG_PRINT_E(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
#define FUSB_WARN(format, ...) FT_DEBUG_PRINT_W(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
#define FUSB_INFO(format, ...) FT_DEBUG_PRINT_I(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
#define FUSB_DEBUG(format, ...) FT_DEBUG_PRINT_D(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
typedef enum
{
FXHCI_OP_REG,
} FXhciHandShakeType;
static void FXhciStart(FUsbHc *controller);
static void FXhciStop(FUsbHc *controller);
static FXhciTransCode FXhciReset(FUsbHc *controller);
static void FXhciReinit(FUsbHc *controller);
static void FXhciShutdown(FUsbHc *controller);
static FXhciTransCode FXhciBulk(FUsbEndpoint *ep, int size, u8 *data, int finalize);
static FXhciTransCode FXhciControl(FUsbDev *dev, FUsbDirection dir, int drlen, void *devreq,
int dalen, u8 *data);
static void *FXhciCreateIntrQueue(FUsbEndpoint *ep, int reqsize, int reqcount, int reqtiming);
static void FXhciDestoryIntrQueue(FUsbEndpoint *ep, void *queue);
static u8 *FXhciPollIntrQueue(void *queue);
/*
* Some structures must not cross page boundaries. To get this,
* we align them by their size (or the next greater power of 2).
*/
/**
* @name: FXhciAlign
* @msg: 分配一段对齐的内存
* @return {*}
* @param {FXhci} *xhci, XHCI控制器实例
* @param {size_t} min_align, 对齐方式
* @param {size_t} size, 请求的字节数目
*/
void *FXhciAlign(FXhci *const xhci, const size_t min_align, const size_t size)
{
FASSERT(xhci && xhci->usb);
size_t align;
FUsb *instance = xhci->usb;
if (!(size & (size - 1)))
align = size; /* It's a power of 2 */
else
align = 1 << ((sizeof(unsigned) << 3) - __builtin_clz(size));
if (align < min_align)
align = min_align;
return FUSB_ALLOCATE(instance, size, align);
}
#ifdef FMEMP_TAG_DEBUG
void *FXhciAlignTag(FXhci *const xhci, const size_t min_align, const size_t size, const char *file, unsigned long line, const char *msg)
{
FASSERT(xhci && xhci->usb);
size_t align;
FUsb *instance = xhci->usb;
if (!(size & (size - 1)))
align = size; /* It's a power of 2 */
else
align = 1 << ((sizeof(unsigned) << 3) - __builtin_clz(size));
if (align < min_align)
align = min_align;
return FUsbMempAllocateTag(instance, size, align, file, line, msg);
}
#endif
/**
* @name: FXhciClearTrb
* @msg: 清空TRB,反转TRB的Cycle state
* @return {*}
* @param {FXhciTrb} *trb, TRB实例
* @param {int} pcs, TRB ring的Cycle state
*/
void FXhciClearTrb(FXhciTrb *const trb, const int pcs)
{
FASSERT(trb);
trb->ptr_low = 0;
trb->ptr_high = 0;
trb->status = 0;
trb->control = !pcs;
return;
}
/**
* @name: FXhciInitCycleRing
* @msg: 初始化TRB ring
* @return {*}
* @param {FXhciTransRing} *tr, TRB ring实例
* @param {size_t} ring_size, TRB ring中的TRB数目
*/
void FXhciInitCycleRing(FXhciTransRing *const tr, const size_t ring_size)
{
FASSERT(tr && tr->ring);
memset((void *)tr->ring, 0, ring_size * sizeof(*tr->ring));
FXHCI_TRB_SET(TT, &tr->ring[ring_size - 1], FXHCI_TRB_LINK); /* TRB Type */
FXHCI_TRB_SET(TC, &tr->ring[ring_size - 1], 1); /* Toggle Cycle */
/* only one segment that points to itself */
tr->ring[ring_size - 1].ptr_low = (uintptr)(tr->ring);
tr->pcs = 1;
tr->cur = tr->ring;
return;
}
/**
* @name: FXhciHandShake
* @msg: 等待XHCI状态,完成握手
* @return {FError} 等待返回值
* @param {FXhci} *xhci, xhci实例
* @param {FXhciHandShakeType} type, 等待类型,e.g 等待Op寄存器
* @param {uintptr} reg_off, 寄存器偏移量
* @param {u32} mask, 寄存器掩码位
* @param {u32} wait_for, 等待的状态,如果状态到达,成功退出
* @param {s32} timeout, 等待的tick超时
*/
static FError FXhciHandShake(FXhci *xhci, FXhciHandShakeType type, uintptr reg_off, u32 mask, u32 wait_for, s32 timeout)
{
FASSERT(xhci);
FError ret = FUSB_SUCCESS;
switch (type)
{
case FXHCI_OP_REG:
ret = FXhciWaitOper32(&xhci->mmio, reg_off, mask, wait_for, timeout);
break;
default:
FASSERT(0);
break;
}
return ret;
}
/**
* @name: FXhciWaitReady
* @msg: 等待XHCI控制器重置完成
* @return {*}
* @param {FXhci} *xhci, xhci实例
*/
static FError FXhciWaitReady(FXhci *const xhci)
{
FASSERT(xhci);
FUSB_INFO("Waiting for controller to be ready... ");
FError ret = FXhciHandShake(xhci, FXHCI_OP_REG, FXHCI_REG_OP_USBSTS, FXHCI_REG_OP_USBSTS_CNR, 0, FXHCI_TIMEOUT);
if (FUSB_SUCCESS == ret)
FUSB_INFO("ok");
else
FUSB_ERROR("timeout.");
return ret;
}
/**
* @name: FXhciHcInit
* @msg: 创建XHCI USB 控制器实例,完成初始化
* @return {FUsbHc *} XHCI控制器实例
* @param {FUsb} *instance, USB实例
* @param {uintptr} base_addr, XHCI控制器基地址
*/
FUsbHc *FXhciHcInit(FUsb *instance, uintptr base_addr)
{
FASSERT(instance);
int i;
FXhciMMIO *mmio = NULL;
u32 reg_val;
u16 hc_version;
uintptr xhci_base_addr = base_addr + FUSB3_XHCI_OFFSET;
FUSB_DEBUG("xhci base addr: 0x%x", xhci_base_addr);
/* First, allocate and initialize static controller structures */
FUsbHc *const controller = FUsbAllocateHc(instance);
if (NULL == controller)
{
FUSB_ERROR("Out of memory ");
return NULL;
}
/* set USB Hc CB according to XHCI */
controller->type = FUSB_HC_XHCI;
controller->start = FXhciStart;
controller->stop = FXhciStop;
controller->reset = FXhciReset;
controller->init = FXhciReinit;
controller->shutdown = FXhciShutdown;
controller->bulk = FXhciBulk;
controller->control = FXhciControl;
controller->set_address = FXhciSetAddress;
controller->finish_device_config = FXhciFinishDevConfig;
controller->destroy_device = FXhciDestoryDev;
controller->create_intr_queue = FXhciCreateIntrQueue;
controller->destroy_intr_queue = FXhciDestoryIntrQueue;
controller->poll_intr_queue = FXhciPollIntrQueue;
/* allocate xhci instance */
controller->reg_base = base_addr;
FASSERT(NULL == controller->instance);
controller->instance = FUSB_ALLOCATE(instance, sizeof(FXhci), FUSB_DEFAULT_ALIGN);
if (NULL == controller->instance)
{
FUSB_INFO("Out of memory ");
goto _free_controller;
}
controller->usb = instance;
FXhci *const xhci = (FXhci *)controller->instance;
xhci->usb = instance;
/* init roothub at slot-0 */
FUsbInitDevEntry(controller, 0);
xhci->roothub = controller->devices[0];
/* allocate command ring and event ring */
FASSERT((NULL == xhci->cr.ring) && (NULL == xhci->er.ring) &&
(NULL == xhci->ev_ring_table));
xhci->cr.ring = FXHCI_ALIGN(xhci, 64, FXHCI_COMMAND_RING_SIZE * sizeof(FXhciTrb));
xhci->er.ring = FXHCI_ALIGN(xhci, 64, FXHCI_EVENT_RING_SIZE * sizeof(FXhciTrb));
xhci->ev_ring_table = FXHCI_ALIGN(xhci, 64, sizeof(FXhciErstEntry));
if ((NULL == xhci->roothub) || (NULL == xhci->cr.ring) ||
(NULL == xhci->er.ring) || (NULL == xhci->ev_ring_table))
{
FUSB_INFO("Out of memory ");
goto _free_xhci;
}
/* setup xhci mmio for register access */
mmio = &xhci->mmio;
FXhciSetupMMIO(mmio, xhci_base_addr);
/* check if XHCI version is supported */
hc_version = FXhciReadHcVersion(mmio);
if (hc_version < FXHCI_HC_VERSION_MIN || hc_version > FXHCI_HC_VERSION_MAX)
{
FUSB_ERROR("xHCI version 0x%x not support", hc_version);
goto _free_xhci;
}
const unsigned pagesize = FXhciReadOper32(mmio, FXHCI_REG_OP_PAGESIZE) << 12;
FUSB_INFO("regbase: 0x%x", mmio->base);
FUSB_INFO("caplen: 0x%x", FXhciReadCaplen(mmio));
FUSB_INFO("rtsoff: 0X%x", mmio->runtime_base - mmio->base);
FUSB_INFO("dboff: 0X%x", mmio->doorbell_base - mmio->base);
FUSB_INFO("hciversion: 0x%x", FXhciReadHcVersion(mmio));
FUSB_INFO("context size: %dB ", FXhciGetCtxSize(mmio));
FUSB_INFO("page size: %dB ", pagesize);
/*
* We haven't touched the hardware yet. So we allocate all dynamic
* structures at first and can still chicken out easily if we run out
* of memory.
*/
reg_val = FXhciReadCap32(&xhci->mmio, FXHCI_REG_CAP_HCSPARAMS1);
xhci->max_slots_en = FXHCI_REG_CAP_HCSPARAMS1_MAX_SLOTS_GET(reg_val); /* record max slot num */
/* allocate device related memory, DCBAA and device info */
FASSERT(NULL == xhci->dcbaa);
xhci->dcbaa = FXHCI_ALIGN(xhci, 64, (xhci->max_slots_en + 1) * sizeof(u64));
FASSERT(NULL == xhci->dev);
xhci->dev = FUSB_ALLOCATE(instance, (xhci->max_slots_en + 1) * sizeof(*xhci->dev), FUSB_DEFAULT_ALIGN);
if ((NULL == xhci->dcbaa) || (NULL == xhci->dev))
{
FUSB_INFO("Out of memory ");
goto _free_xhci;
}
/*
* Let dcbaa[0] point to another array of pointers, sp_ptrs.
* The pointers therein point to scratchpad buffers (pages).
*/
reg_val = FXhciReadCap32(&xhci->mmio, FXHCI_REG_CAP_HCSPARAMS2);
const size_t max_sp_bufs = FXHCI_REG_CAP_HCSPARAMS2_MAX_SCRATCHPAD_BUFS_GET(reg_val);
FUSB_INFO("max scratchpad bufs: 0x%lx reg_val : 0x%x", max_sp_bufs, reg_val);
if (0 < max_sp_bufs)
{
FUSB_INFO("allocate sp_ptrs");
const size_t sp_ptrs_size = max_sp_bufs * sizeof(u64);
/* allocate scratchpad bufs entry to preserve pointers of scratchpad buf */
FASSERT(NULL == xhci->sp_ptrs);
xhci->sp_ptrs = FXHCI_ALIGN(xhci, 64, sp_ptrs_size);
if (NULL == xhci->sp_ptrs)
{
FUSB_INFO("Out of memory ");
goto _free_xhci_structs;
}
for (i = 0; (size_t)i < max_sp_bufs; ++i)
{
/* allocate each scratchpad buf */
void *const page = FUSB_ALLOCATE(instance, pagesize, pagesize);
if (NULL == page)
{
FUSB_INFO("Out of memory ");
goto _free_xhci_structs;
}
xhci->sp_ptrs[i] = (uintptr)(page); /* assign pointer to scratchpad buf*/
}
xhci->dcbaa[0] = (uintptr)(xhci->sp_ptrs); /* assign pointer to scratchpad bufs entry */
}
/* Now start working on the hardware */
if (FUSB_SUCCESS != FXhciWaitReady(xhci))
goto _free_xhci_structs;
/* TODO: Check if BIOS claims ownership (and hand over) */
if (FUSB_CC_SUCCESS != FXhciReset(controller))
{
goto _free_xhci_structs;
}
FXhciReinit(controller);
/* init roothub device instance */
xhci->roothub->controller = controller;
xhci->roothub->init = FXhciRootHubInit;
xhci->roothub->init(xhci->roothub);
FUSB_INFO("init xHc@%p success", controller);
return controller;
_free_xhci_structs:
if (xhci->sp_ptrs)
{
for (i = 0; (size_t)i < max_sp_bufs; ++i)
{
if (xhci->sp_ptrs[i])
FUSB_FREE(instance, (void *)(uintptr)(xhci->sp_ptrs[i]));
}
}
FUSB_FREE(instance, xhci->sp_ptrs);
FUSB_FREE(instance, xhci->dcbaa);
_free_xhci:
FUSB_FREE(instance, (void *)xhci->ev_ring_table);
FUSB_FREE(instance, (void *)xhci->er.ring);
FUSB_FREE(instance, (void *)xhci->cr.ring);
FUSB_FREE(instance, xhci->roothub);
FUSB_FREE(instance, xhci->dev);
FUSB_FREE(instance, xhci);
_free_controller:
FUsbDetachHc(controller);
FUSB_FREE(instance, controller);
return NULL;
}
/**
* @name: FXhciReset
* @msg: 重置XHCI控制器
* @return {*}
* @param {FUsbHc} *controller, USB Hc控制器
*/
static FUsbTransCode FXhciReset(FUsbHc *const controller)
{
FASSERT(controller);
FXhci *const xhci = FXHCI_INST_GET(controller);
FASSERT(xhci);
u32 reg_val;
/* stop xhci if still run */
FXhciStop(controller);
reg_val = FXhciReadOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD);
reg_val |= FXHCI_REG_OP_USBCMD_HCRST;
FXhciWriteOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD, reg_val);
FUSB_INFO("Resetting hc...");
if (FUSB_SUCCESS != FXhciHandShake(xhci, FXHCI_OP_REG, FXHCI_REG_OP_USBCMD, FXHCI_REG_OP_USBCMD_HCRST, 0, FXHCI_TIMEOUT))
{
FUSB_ERROR("Wait hc reset timeout !!!");
return -1;
}
return FUSB_CC_SUCCESS;
}
/**
* @name: FXhciReinit
* @msg: 重新初始化XHCI控制器
* @return {*}
* @param {FUsbHc} *controller, USB Hc控制器
*/
static void FXhciReinit(FUsbHc *controller)
{
FASSERT(controller);
FXhci *const xhci = FXHCI_INST_GET(controller);
FASSERT(xhci);
const FUsbConfig *const config = &controller->usb->config;
u32 reg_val;
u64 reg_val64;
/* wait xhci ready */
if (FUSB_SUCCESS != FXhciWaitReady(xhci))
return;
/* Enable all available slots */
reg_val = FXhciReadOper32(&xhci->mmio, FXHCI_REG_OP_CONFIG);
reg_val &= ~FXHCI_REG_OP_CONFIG_MAX_SLOTS_EN_MASK;
reg_val |= FXHCI_REG_OP_CONFIG_MAX_SLOTS_EN_SET(xhci->max_slots_en);
FXhciWriteOper32(&xhci->mmio, FXHCI_REG_OP_CONFIG, reg_val);
/* Set DCBAA */
FXhciWriteOper64(&xhci->mmio, FXHCI_REG_OP_DCBAAP, (u64)(uintptr)xhci->dcbaa);
FUSB_INFO("dcba at 0x%lx", FXhciReadOper64(&xhci->mmio, FXHCI_REG_OP_DCBAAP));
/* Initialize command ring */
FXhciInitCycleRing(&xhci->cr, FXHCI_COMMAND_RING_SIZE);
FUSB_INFO("command ring @%p", xhci->cr.ring);
reg_val64 = FXhciReadOper64(&xhci->mmio, FXHCI_REG_OP_CRCR);
reg_val64 = FXHCI_REG_OP_CRCR_CR_PTR_MASK & (u64)(uintptr)xhci->cr.ring;
reg_val64 |= FXHCI_REG_OP_CRCR_RCS & xhci->cr.pcs;
FXhciWriteOper64(&xhci->mmio, FXHCI_REG_OP_CRCR, reg_val64);
/* Make sure interrupts are disabled */
reg_val = FXhciReadOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD);
reg_val &= ~FXHCI_REG_OP_USBCMD_INTE;
FXhciWriteOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD, reg_val);
/* Initialize event ring */
FXhciResetEvtRing(&xhci->er);
FUSB_INFO("event ring @%p (0x%08p) ",
xhci->er.ring, (uintptr)(xhci->er.ring));
reg_val = FXhciReadCap32(&xhci->mmio, FXHCI_REG_CAP_HCSPARAMS2);
FUSB_INFO("ERST Max: %d",
FXHCI_REG_CAP_HCSPARAMS2_ERST_MAX_GET(reg_val));
memset((void *)xhci->ev_ring_table, 0x00, sizeof(FXhciErstEntry));
xhci->ev_ring_table[0].seg_base_lo = (uintptr)(xhci->er.ring);
xhci->ev_ring_table[0].seg_base_hi = 0;
xhci->ev_ring_table[0].seg_size = FXHCI_EVENT_RING_SIZE;
/* pass event ring table to hardware */
WMB();
/* Initialize primary interrupter */
FXhciWriteRt32(&xhci->mmio, 0, FXHCI_REG_RT_IR_ERSTSZ, FXHCI_REG_RT_IR_ERSTSZ_MASK & 1); /* Segment Table Size = 1 */
FXhciUpdateEvtDQ(xhci);
/* erstba has to be written at last */
FXhciWriteRt64(&xhci->mmio, 0, FXHCI_REG_RT_IR_ERSTBA, FXHCI_REG_RT_IR_ERSTBA_MASK & ((u64)(uintptr)xhci->ev_ring_table));
FUSB_INFO("ERST base: 0x%lx == %p",
FXhciReadRt64(&xhci->mmio, 0, FXHCI_REG_RT_IR_ERSTBA),
xhci->ev_ring_table);
FXhciStart(controller);
/* run Cmd Nop to test if command ring okay */
for (int i = 0; i < 3; ++i)
{
FUSB_INFO("NOOP run #%d ", i);
if (FXHCI_CC_SUCCESS != FXhciCmdNop(xhci))
{
FUSB_ERROR("noop command failed. ");
break;
}
}
return;
}
/**
* @name: FXhciShutdown
* @msg: 关闭XHCI控制器
* @return {*}
* @param {FUsbHc} *controller, USB Hc控制器
*/
static void FXhciShutdown(FUsbHc *const controller)
{
int i;
u32 reg_val;
if (controller == NULL)
return;
/* detach the Hc instance */
FUsbDetachHc(controller);
FXhci *const xhci = FXHCI_INST_GET(controller);
FASSERT(xhci && xhci->usb);
FUsb *instance = xhci->usb;
FXhciStop(controller); /* stop xhci instance */
/* free scatchpad bufs */
if (NULL != xhci->sp_ptrs)
{
reg_val = FXhciReadCap32(&xhci->mmio, FXHCI_REG_CAP_HCSPARAMS2);
const size_t max_sp_bufs = FXHCI_REG_CAP_HCSPARAMS2_MAX_SCRATCHPAD_BUFS_GET(reg_val);
for (i = 0; (size_t)i < max_sp_bufs; ++i)
{
if (NULL != (void *)(uintptr)xhci->sp_ptrs[i])
FUSB_FREE(instance, (void *)(uintptr)(xhci->sp_ptrs[i]));
}
}
FUSB_FREE(instance, xhci->sp_ptrs);
FUSB_FREE(instance, xhci->dcbaa);
FUSB_FREE(instance, xhci->dev);
FUSB_FREE(instance, (void *)xhci->ev_ring_table);
FUSB_FREE(instance, (void *)xhci->er.ring);
FUSB_FREE(instance, (void *)xhci->cr.ring);
FUSB_FREE(instance, xhci);
return;
}
/**
* @name: FXhciStart
* @msg: 启动XHCI控制器
* @return {*}
* @param {FUsbHc} *controller, USB Hc控制器
*/
static void FXhciStart(FUsbHc *controller)
{
FASSERT(controller);
FXhci *const xhci = FXHCI_INST_GET(controller);
FASSERT(xhci);
u32 reg_val;
/* setting the USBCMD register Run/Stop (R/S) bit to ‘1’ */
reg_val = FXhciReadOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD);
reg_val |= FXHCI_REG_OP_USBCMD_RUN_STOP;
FXhciWriteOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD, reg_val);
if (FUSB_SUCCESS != FXhciHandShake(xhci, FXHCI_OP_REG, FXHCI_REG_OP_USBSTS, FXHCI_REG_OP_USBSTS_HCH, 0, FXHCI_TIMEOUT))
{
FUSB_ERROR("Wait hc start timeout !!!");
}
return;
}
/**
* @name: FXhciStop
* @msg: 停止XHCI控制器
* @return {*}
* @param {FUsbHc} *controller, USB Hc控制器
*/
static void FXhciStop(FUsbHc *controller)
{
FASSERT(controller);
FXhci *const xhci = FXHCI_INST_GET(controller);
FASSERT(xhci);
u32 reg_val;
/* setting the USBCMD register Run/Stop (R/S) bit to ‘0’ */
reg_val = FXhciReadOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD);
reg_val &= ~FXHCI_REG_OP_USBCMD_RUN_STOP;
FXhciWriteOper32(&xhci->mmio, FXHCI_REG_OP_USBCMD, reg_val);
if (FUSB_SUCCESS != FXhciHandShake(xhci, FXHCI_OP_REG, FXHCI_REG_OP_USBSTS, FXHCI_REG_OP_USBSTS_HCH, FXHCI_REG_OP_USBSTS_HCH, FXHCI_TIMEOUT))
{
FUSB_ERROR("Wait hc stop timeout !!!");
}
return;
}
/**
* @name: FXhciResetEp
* @msg: 重置XHCI端点
* @return {FError} 返回错误码
* @param {FUsbDev} *dev, 端点所在的USB设备实例
* @param {FUsbEndpoint} *ep, 端点实例
*/
static FError FXhciResetEp(FUsbDev *const dev, FUsbEndpoint *const ep)
{
FASSERT(dev && dev->controller);
FXhci *const xhci = FXHCI_INST_GET(dev->controller);
FASSERT(xhci);
const int slot_id = dev->address;
const int ep_id = (NULL != ep) ? FXhciEpId(ep) : FXHCI_EP0_ID; /* ep-0 or normal ep */
FXhciEpCtx *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
FUSB_INFO("Resetting ID %d EP %d (ep state: %d) ",
slot_id, ep_id, FXHCI_EC_GET(STATE, epctx));
/* Run Reset Endpoint Command if the EP is in Halted state */
if (FXHCI_EC_GET(STATE, epctx) == FXHCI_EC_STATE_HALTED)
{
const FXhciTransCode cc = FXhciCmdResetEp(xhci, slot_id, ep_id);
if (cc != FXHCI_CC_SUCCESS)
{
FUSB_INFO("Reset Endpoint Command failed: %d ", cc);
return FUSB_ERR_TRANS_FAIL;
}
}
/* Clear TT buffer for bulk and control endpoints behind a TT */
const int hub = dev->hub;
if (hub && dev->speed < FUSB_HIGH_SPEED &&
dev->controller->devices[hub]->speed == FUSB_HIGH_SPEED)
{
/* TODO */;
}
/* Reset transfer ring if the endpoint is in the right state */
const unsigned ep_state = FXHCI_EC_GET(STATE, epctx);
if (ep_state == FXHCI_EC_STATE_STOPPED || ep_state == FXHCI_EC_STATE_ERROR)
{
FXhciTransRing *const tr =
xhci->dev[slot_id].transfer_rings[ep_id];
const FXhciTransCode cc = FXhciCmdSetTrDq(xhci, slot_id, ep_id,
tr->ring, 1);
if (cc != FXHCI_CC_SUCCESS)
{
FUSB_INFO("Set TR Dequeue Command failed: %d ", cc);
return FUSB_ERR_TRANS_FAIL;
}
FXhciInitCycleRing(tr, FXHCI_TRANSFER_RING_SIZE);
}
FUSB_INFO("Finished resetting ID %d EP %d (ep state: %d) ",
slot_id, ep_id, FXHCI_EC_GET(STATE, epctx));
return FUSB_SUCCESS;
}
/**
* @name: FXhciEnqueueTrb
* @msg: TRB入队,向TRB ring加入一条TRB
* @return {*}
* @param {FXhciTransRing} *tr, TRB ring实例
*/
static void FXhciEnqueueTrb(FXhciTransRing *const tr)
{
FASSERT(tr);
const int chain = FXHCI_TRB_GET(CH, tr->cur);
FXHCI_TRB_SET(C, tr->cur, tr->pcs); /* Cycle Bit */
++tr->cur;
while (FXHCI_TRB_GET(TT, tr->cur) == FXHCI_TRB_LINK)
{
FUSB_DEBUG("Handling LINK pointer ");
const int tc = FXHCI_TRB_GET(TC, tr->cur);
FXHCI_TRB_SET(CH, tr->cur, chain); /* Chain Bit */
WMB();
FXHCI_TRB_SET(C, tr->cur, tr->pcs); /* Cycle Bit */
tr->cur = (void *)(uintptr)(tr->cur->ptr_low);
/* toggle cycle state */
if (tc)
tr->pcs ^= 1;
}
return;
}
/**
* @name: FXhciRingDoorbell
* @msg: 提醒Hc处理刚加入的TRB
* @return {*}
* @param {FUsbEndpoint} *ep, 端点实例
*/
static void FXhciRingDoorbell(FUsbEndpoint *const ep)
{
FASSERT(ep);
FXhci *xhci = FXHCI_INST_GET(ep->dev->controller);
/* Ensure all TRB changes are written to memory. */
WMB();
FXhciWriteDb32(&xhci->mmio, ep->dev->address, FXhciEpId(ep));
return;
}
/**
* @name: FXhciEnqueueTD
* @msg: 填充一条TRB
* @return {*}
* @param {FXhciTransRing} *tr, TRB ring实例
* @param {int} ep, 端点实例
* @param {size_t} mps, 最大包长度
* @param {int} dalen, 数据长度
* @param {void} *data, 数据缓冲区
* @param {int} dir, 发送方向
*/
static void FXhciEnqueueTD(FXhciTransRing *const tr, const int ep, const size_t mps,
const int dalen, void *const data, const int dir)
{
FASSERT(tr);
FXhciTrb *trb = NULL; /* cur TRB */
u8 *cur_start = data; /* cur data pointer */
size_t length = dalen; /* remaining bytes */
size_t packets = (length + mps - 1) / mps; /* remaining packets */
size_t residue = 0; /* residue from last TRB */
size_t trb_count = 0; /* TRBs added so far */
while ((length > 0) || (trb_count == 0) /* enqueue at least one */)
{
const size_t cur_end = ((size_t)cur_start + 0x10000) & ~0xffff; /* best guess, send at most 0x1000 bytes */
size_t cur_length = cur_end - (size_t)cur_start;
if (length < cur_length)
{
cur_length = length;
packets = 0;
length = 0;
}
else
{
packets -= (residue + cur_length) / mps;
residue = (residue + cur_length) % mps;
length -= cur_length;
}
trb = tr->cur;
FXhciClearTrb(trb, tr->pcs);
trb->ptr_low = (uintptr)(cur_start);
FXHCI_TRB_SET(TL, trb, cur_length); /* Transfer Length */
FXHCI_TRB_SET(TDS, trb, min((size_t)FXHCI_TRB_MAX_TD_SIZE, packets)); /* TD Size */
FXHCI_TRB_SET(CH, trb, 1); /* associate this TRB with the next TRB on the Ring */
/* Check for first, data stage TRB, only Ep0 can handle */
if ((trb_count == 0) && ep == FXHCI_EP0_ID)
{
FXHCI_TRB_SET(DIR, trb, dir); /* Direction */
FXHCI_TRB_SET(TT, trb, FXHCI_TRB_DATA_STAGE); /* TRB Type */
}
else
{
FXHCI_TRB_SET(TT, trb, FXHCI_TRB_NORMAL); /* TRB Type */
}
/*
* This is a workaround for Synopsys DWC3. If the ENT flag is
* not set for the Normal and Data Stage TRBs. We get Event TRB
* with length 0x20d from the controller when we enqueue a TRB
* for the IN endpoint with length 0x200.
*/
if (0 == length)
{
/* xHC shall fetch and evaluate the next TRB before saving the endpoint state */
FXHCI_TRB_SET(ENT, trb, 1); /* Evaluate Next TRB */
}
FXhciEnqueueTrb(tr);
cur_start += cur_length;
++trb_count;
}
trb = tr->cur;
FXhciClearTrb(trb, tr->pcs);
trb->ptr_low = (uintptr)(trb); /* for easier debugging only */
FXHCI_TRB_SET(TT, trb, FXHCI_TRB_EVENT_DATA); /* set transfer type */
FXHCI_TRB_SET(IOC, trb, 1); /* xHc shalle notify the system of the completion by placing an Transfer Event TRB on the Event ring */
FXhciEnqueueTrb(tr);
return;
}
/**
* @name: FXhciControl
* @msg: XHCI控制传输
* @return {FXhciTransCode} 传输返回值
* @param {FUsbDev} *dev, USB设备实例
* @param {FUsbDirection} dir, 控制传输类型, IN, OUT, SETUP
* @param {int} drlen, USB请求长度
* @param {void} *devreq, USB请求,参考FUsbDevReq
* @param {int} dalen, 控制传输数据长度
* @param {unsigned char} *src, 控制传输数据
*/
static FXhciTransCode FXhciControl(FUsbDev *const dev, const FUsbDirection dir,
const int drlen, void *const devreq, const int dalen,
unsigned char *const src)
{
FASSERT(dev && dev->controller);
unsigned char *data = src;
FXhci *const xhci = FXHCI_INST_GET(dev->controller);
FASSERT(xhci);
FXhciEpCtx *const epctx = xhci->dev[dev->address].ctx.ep0;
FXhciTransRing *const tr = xhci->dev[dev->address].transfer_rings[1];
FASSERT(epctx && tr);
/* check the transfer data length, less than WORD size, u16 pointer can hold */
const size_t off = (size_t)data & 0xffff;
if ((off + dalen) > ((FXHCI_TRANSFER_RING_SIZE - 4) << 16))
{
FUSB_ERROR("Unsupported transfer size 0x%lx!!!", dalen);
return FXHCI_CC_GENERAL_ERROR;
}
/* Reset endpoint if it's not running and not disabled */
const unsigned ep_state = FXHCI_EC_GET(STATE, epctx);
if (ep_state > FXHCI_EC_STATE_RUNNING)
{
if (FUSB_SUCCESS != FXhciResetEp(dev, NULL))
{
FUSB_ERROR("Reset endpoint failed !!!");
return FXHCI_CC_GENERAL_ERROR;
}
}
/* Fill and enqueue setup TRB */
FXhciTrb *const setup = tr->cur;
FXhciClearTrb(setup, tr->pcs);
setup->ptr_low = ((u32 *)devreq)[0]; /* request data */
setup->ptr_high = ((u32 *)devreq)[1];
FXHCI_TRB_SET(TL, setup, drlen/*8*/); /* Transfer length */
FXHCI_TRB_SET(TRT, setup, (dalen > 0) ? ((dir == FUSB_OUT) ? FXHCI_TRB_TRT_OUT_DATA : FXHCI_TRB_TRT_IN_DATA) : FXHCI_TRB_TRT_NO_DATA);
FXHCI_TRB_SET(TT, setup, FXHCI_TRB_SETUP_STAGE); /* TRB Type */
FXHCI_TRB_SET(IDT, setup, 1); /* Immediate Data (IDT). shall be set to ‘1’ in a Setup Stage TRB */
FXHCI_TRB_SET(IOC, setup, 1); /* Interrupt On Completion (IOC) */
FXhciEnqueueTrb(tr);
/* Fill and enqueue data TRBs (if any) */
if (dalen > 0)
{
const unsigned mps = FXHCI_EC_GET(MPS, epctx);
const unsigned dt_dir = (dir == FUSB_OUT) ? FXHCI_TRB_DIR_OUT : FXHCI_TRB_DIR_IN;
FXhciEnqueueTD(tr, FXHCI_EP0_ID, mps, dalen, data, dt_dir);
}
/* Fill status TRB */
FXhciTrb *const status = tr->cur;
FXhciClearTrb(status, tr->pcs);
FXHCI_TRB_SET(DIR, status, (dir == FUSB_OUT) ? FXHCI_TRB_DIR_IN : FXHCI_TRB_DIR_OUT); /* Direction */
FXHCI_TRB_SET(TT, status, FXHCI_TRB_STATUS_STAGE); /* TRB Type */
FXHCI_TRB_SET(IOC, status, 1); /* Interrupt On Completion */
FXhciEnqueueTrb(tr);
/* Ring doorbell for EP0 */
FXhciRingDoorbell(&dev->endpoints[0]);
/* Wait for transfer events from IN DATA OUT stages */
int i, transferred = 0;
const int n_stages = 2 + !!dalen; /* 2 stage without data */
/* flush cache of request data / transfer data before transfer */
if (dalen > 0)
{
FCacheDCacheInvalidateRange((uintptr)data, dalen);
FCacheDCacheInvalidateRange((uintptr)devreq, drlen);
}
for (i = 0; i < n_stages; ++i)
{
const FXhciTransCode ret = FXhciWaitForTransfer(xhci, dev->address, 1);
transferred += ret; /* record bytes transfered successfully */
if (ret < FXHCI_CC_ZERO_BYTES) /* negative ret means transfer error */
{
if (ret == FXHCI_CC_TIMEOUT)
{
FUSB_ERROR("Stopping ID %d EP 1 ",
dev->address);
FXhciCmdStopEp(xhci, dev->address, FXHCI_EP0_ID);
}
FUSB_ERROR("Stage %d/%d failed: %d \r\n"
" trb ring: @%p \r\n"
" setup trb: @%p \r\n"
" status trb: @%p \r\n"
" ep state: %d -> %d \r\n"
" usbsts: 0x%08x ",
i, n_stages, ret,
tr->ring, setup, status,
ep_state, FXHCI_EC_GET(STATE, epctx),
FXhciReadOper32(&xhci->mmio, FXHCI_REG_OP_USBSTS));
return ret;
}
}
/* flush cache of request data / transfer data after transfer */
if (dalen > 0)
{
FCacheDCacheInvalidateRange((uintptr)data, dalen);
FCacheDCacheInvalidateRange((uintptr)devreq, drlen);
}
return transferred;
}
/**
* @name: FXhciBulk
* @msg: XHCI块传输
* @return {FXhciTransCode}
* @param {FUsbEndpoint} *ep, 端点实例
* @param {int} size,
* @param {u8} *src
* @param {int} finalize
* @note: finalize == 1: if data is of packet aligned size, add a zero length packet
*/
static FXhciTransCode FXhciBulk(FUsbEndpoint *const ep, const int size, u8 *const src,
const int finalize)
{
/* finalize: Hopefully the xHCI controller always does this.
We have no control over the packets. */
FASSERT(ep);
u8 *data = src;
FXhci *const xhci = FXHCI_INST_GET(ep->dev->controller);
FASSERT(xhci);
const int slot_id = ep->dev->address;
const int ep_id = FXhciEpId(ep); /* must not for Ep0 */
FXhciEpCtx *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
FXhciTransRing *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
/* check the transfer data length, less than WORD size, u16 pointer can hold */
const size_t off = (size_t)data & 0xffff;
if ((off + size) > ((FXHCI_TRANSFER_RING_SIZE - 2) << 16))
{
FUSB_INFO("Unsupported transfer size ");
return FXHCI_CC_GENERAL_ERROR;
}
/* Reset endpoint if it's not running */
const unsigned ep_state = FXHCI_EC_GET(STATE, epctx);
if (ep_state > FXHCI_EC_STATE_RUNNING)
{
if (FUSB_SUCCESS != FXhciResetEp(ep->dev, ep))
return FXHCI_CC_GENERAL_ERROR;
}
FCacheDCacheInvalidateRange((uintptr)data, size);
/* Enqueue transfer and ring doorbell */
const unsigned mps = FXHCI_EC_GET(MPS, epctx);
const unsigned dir = (ep->direction == FUSB_OUT) ? FXHCI_TRB_DIR_OUT : FXHCI_TRB_DIR_IN;
FXhciEnqueueTD(tr, ep_id, mps, size, data, dir);
FXhciRingDoorbell(ep);
/* Wait for transfer event */
const FXhciTransCode ret = FXhciWaitForTransfer(xhci, ep->dev->address, ep_id);
if (ret < FXHCI_CC_ZERO_BYTES)
{
if (ret == FXHCI_CC_TIMEOUT)
{
FUSB_INFO("Stopping ID %d EP %d ",
ep->dev->address, ep_id);
FXhciCmdStopEp(xhci, ep->dev->address, ep_id);
}
FUSB_INFO("Bulk transfer failed: %d \r\n"
" ep state: %d -> %d \r\n"
" usbsts: 0x%08x ",
ret, ep_state,
FXHCI_EC_GET(STATE, epctx),
FXhciReadOper32(&xhci->mmio, FXHCI_REG_OP_USBSTS));
return ret;
}
return ret;
}
/**
* @name: FXhciNextTrb
* @msg: 获取下一个可用的TRB
* @return {FXhciTrb*} 可用的TRB
* @param {FXhciTrb} *cur, 当前TRB
* @param {int} *pcs, 传入的待反转的Cycle state
*/
static FXhciTrb *FXhciNextTrb(FXhciTrb *cur, int *const pcs)
{
FASSERT(cur);
++cur;
while (FXHCI_TRB_GET(TT, cur) == FXHCI_TRB_LINK)
{
if (pcs && FXHCI_TRB_GET(TC, cur))
*pcs ^= 1;
cur = (void *)(uintptr)(cur->ptr_low);
}
return cur;
}
/**
* @name: FXhciCreateIntrQueue
* @msg: 创建XHCI的中断队列
* @return {void *} 成功则返回指向中断队列的指针,失败返回NULL
* @param {FUsbEndpoint} *ep, 端点实例
* @param {int} reqsize, 中断队列可接受的请求字节数
* @param {int} reqcount, 中断队列可接受的最大请求数目
* @param {int} reqtiming, 请求超时
* @note create and hook-up an intr queue into device schedul
*/
static void *FXhciCreateIntrQueue(FUsbEndpoint *const ep, const int reqsize, const int reqcount,
const int reqtiming)
{
/* reqtiming: We ignore it and use the interval from the
endpoint descriptor configured earlier. */
FASSERT(ep);
FXhci *const xhci = FXHCI_INST_GET(ep->dev->controller);
FASSERT(xhci && xhci->usb);
FUsb *instance = xhci->usb;
const int slot_id = ep->dev->address;
const int ep_id = FXhciEpId(ep);
FXhciTransRing *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
if (reqcount > (FXHCI_TRANSFER_RING_SIZE - 2))
{
FUSB_INFO("reqcount is too high, at most %d supported ",
FXHCI_TRANSFER_RING_SIZE - 2);
return NULL;
}
if (reqsize > 0x10000)
{
FUSB_INFO("reqsize is too large, at most 64KiB supported ");
return NULL;
}
if (xhci->dev[slot_id].interrupt_queues[ep_id])
{
FUSB_INFO("Only one interrupt queue per endpoint supported ");
return NULL;
}
/* Allocate intrq structure and reqdata chunks */
FXhciIntrQ *const intrq = FUSB_ALLOCATE(instance, sizeof(*intrq), FUSB_DEFAULT_ALIGN);
if (NULL == intrq)
{
FUSB_INFO("Out of memory ");
return NULL;
}
int i;
int pcs = tr->pcs;
FXhciTrb *cur = tr->cur;
for (i = 0; i < reqcount; ++i)
{
if (FXHCI_TRB_GET(C, cur) == (unsigned int)pcs)
{
FUSB_INFO("Not enough empty TRBs ");
goto _free_return;
}
/* allocate request buffer for each TRB */
void *const reqdata = FXHCI_ALIGN(xhci, 1, reqsize);
if (NULL == reqdata)
{
FUSB_INFO("Out of memory ");
goto _free_return;
}
FXhciClearTrb(cur, pcs);
cur->ptr_low = (uintptr)(reqdata);
cur->ptr_high = 0;
FXHCI_TRB_SET(TL, cur, reqsize); /* Transfer Length */
FXHCI_TRB_SET(TT, cur, FXHCI_TRB_NORMAL); /* TRB Type */
FXHCI_TRB_SET(ISP, cur, 1); /* Interrupt-on Short Packet */
FXHCI_TRB_SET(IOC, cur, 1); /* Interrupt On Completion */
cur = FXhciNextTrb(cur, &pcs);
}
intrq->size = reqsize;
intrq->count = reqcount;
intrq->next = tr->cur;
intrq->ready = NULL;
intrq->ep = ep;
xhci->dev[slot_id].interrupt_queues[ep_id] = intrq;
/* Now enqueue all the prepared TRBs but the last
and ring the doorbell. */
for (i = 0; i < (reqcount - 1); ++i)
FXhciEnqueueTrb(tr);
FXhciRingDoorbell(ep);
return intrq;
_free_return:
cur = tr->cur;
for (--i; i >= 0; --i)
{
FUSB_FREE(instance, (void *)(uintptr)(cur->ptr_low));
cur = FXhciNextTrb(cur, NULL);
}
FUSB_FREE(instance, intrq);
return NULL;
}
/**
* @name: FXhciDestoryIntrQueue
* @msg: 删除中断队列
* @return {*}
* @param {FUsbEndpoint} *ep, 端点实例
* @param {void} *q, 中断队列
* @note remove queue from device schedule, dropping all data that came in
*/
static void FXhciDestoryIntrQueue(FUsbEndpoint *const ep, void *const q)
{
FASSERT(ep && q);
FXhci *const xhci = FXHCI_INST_GET(ep->dev->controller);
FASSERT(xhci);
FUsb *instance = xhci->usb;
const int slot_id = ep->dev->address;
const int ep_id = FXhciEpId(ep);
FXhciTransRing *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
FXhciIntrQ *const intrq = (FXhciIntrQ *)q;
/* Make sure the endpoint is stopped */
if (FXHCI_EC_GET(STATE, xhci->dev[slot_id].ctx.ep[ep_id]) == FXHCI_EC_STATE_RUNNING)
{
const FXhciTransCode cc = FXhciCmdStopEp(xhci, slot_id, ep_id);
if (cc != FXHCI_CC_SUCCESS)
FUSB_INFO("Warning: Failed to stop endpoint ");
}
/* Process all remaining transfer events */
FXhciHandleEvts(xhci);
/* Free all pending transfers and the interrupt queue structure */
int i;
for (i = 0; (size_t)i < intrq->count; ++i)
{
FUSB_FREE(instance, (void *)(uintptr)(intrq->next->ptr_low));
intrq->next = FXhciNextTrb(intrq->next, NULL);
}
xhci->dev[slot_id].interrupt_queues[ep_id] = NULL;
FUSB_FREE(instance, (void *)intrq);
/* Reset the controller's dequeue pointer and reinitialize the ring */
FXhciCmdSetTrDq(xhci, slot_id, ep_id, tr->ring, 1);
FXhciInitCycleRing(tr, FXHCI_TRANSFER_RING_SIZE);
return;
}
/**
* @name: FXhciPollIntrQueue
* @msg: 轮询一次中断队列进行处理
* @return {*}
* @param {void} *q, 中断队列
* @note read one intr-packet from queue, if available. extend the queue for new input.
return NULL if nothing new available.
Recommended use: while (data=poll_intr_queue(q)) process(data);
*/
static u8 *FXhciPollIntrQueue(void *const q)
{
if (NULL == q)
return NULL;
FXhciIntrQ *const intrq = (FXhciIntrQ *)q;
FUsbEndpoint *const ep = intrq->ep;
FXhci *const xhci = FXHCI_INST_GET(ep->dev->controller);
FASSERT(xhci);
/* TODO: Reset interrupt queue if it gets halted? */
FXhciHandleEvts(xhci);
u8 *reqdata = NULL;
while (!reqdata && intrq->ready)
{
const int ep_id = FXhciEpId(ep);
FXhciTransRing *const tr =
xhci->dev[ep->dev->address].transfer_rings[ep_id];
/* Fetch the request's buffer */
reqdata = (void *)(uintptr)(intrq->next->ptr_low);
/* Enqueue the last (spare) TRB and ring doorbell */
FXhciEnqueueTrb(tr);
FXhciRingDoorbell(ep);
/* Reuse the current buffer for the next spare TRB */
FXhciClearTrb(tr->cur, tr->pcs);
tr->cur->ptr_low = (uintptr)(reqdata);
tr->cur->ptr_high = 0;
FXHCI_TRB_SET(TL, tr->cur, intrq->size); /* Transfer Length */
FXHCI_TRB_SET(TT, tr->cur, FXHCI_TRB_NORMAL); /* TRB Type */
FXHCI_TRB_SET(ISP, tr->cur, 1); /* Interrupt-on Short Packet */
FXHCI_TRB_SET(IOC, tr->cur, 1); /* Interrupt On Completion */
/* Check if anything was transferred */
const size_t read = FXHCI_TRB_GET(TL, intrq->next);
if (!read)
reqdata = NULL;
else if (read < intrq->size)
/* At least zero it, poll interface is rather limited */
memset(reqdata + read, 0x00, intrq->size - read);
/* Advance the interrupt queue */
if (intrq->ready == intrq->next)
/* This was last TRB being ready */
intrq->ready = NULL;
intrq->next = FXhciNextTrb(intrq->next, NULL);
}
return reqdata;
}
|