Rev 4547 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3520 | clevermous | 1 | ; Code for EHCI controllers. |
2 | |||
4418 | clevermous | 3 | ; Standard driver stuff |
4 | format PE DLL native |
||
5 | entry start |
||
6 | __DEBUG__ equ 1 |
||
7 | __DEBUG_LEVEL__ equ 1 |
||
8 | section '.reloc' data readable discardable fixups |
||
9 | section '.text' code readable executable |
||
10 | include '../proc32.inc' |
||
11 | include '../struct.inc' |
||
12 | include '../macros.inc' |
||
13 | include '../fdo.inc' |
||
14 | include '../../kernel/trunk/bus/usb/common.inc' |
||
15 | |||
3520 | clevermous | 16 | ; ============================================================================= |
17 | ; ================================= Constants ================================= |
||
18 | ; ============================================================================= |
||
19 | ; EHCI register declarations. |
||
20 | ; Part 1. Capability registers. |
||
21 | ; Base is MMIO from the PCI space. |
||
22 | EhciCapLengthReg = 0 |
||
23 | EhciVersionReg = 2 |
||
24 | EhciStructParamsReg = 4 |
||
25 | EhciCapParamsReg = 8 |
||
26 | EhciPortRouteReg = 0Ch |
||
27 | ; Part 2. Operational registers. |
||
28 | ; Base is (base for part 1) + (value of EhciCapLengthReg). |
||
29 | EhciCommandReg = 0 |
||
30 | EhciStatusReg = 4 |
||
31 | EhciInterruptReg = 8 |
||
32 | EhciFrameIndexReg = 0Ch |
||
33 | EhciCtrlDataSegReg = 10h |
||
34 | EhciPeriodicListReg = 14h |
||
35 | EhciAsyncListReg = 18h |
||
36 | EhciConfigFlagReg = 40h |
||
37 | EhciPortsReg = 44h |
||
38 | |||
39 | ; Possible values of ehci_pipe.NextQH.Type bitfield. |
||
40 | EHCI_TYPE_ITD = 0 ; isochronous transfer descriptor |
||
41 | EHCI_TYPE_QH = 1 ; queue head |
||
42 | EHCI_TYPE_SITD = 2 ; split-transaction isochronous TD |
||
43 | EHCI_TYPE_FSTN = 3 ; frame span traversal node |
||
44 | |||
45 | ; ============================================================================= |
||
46 | ; ================================ Structures ================================= |
||
47 | ; ============================================================================= |
||
48 | |||
49 | ; Hardware part of EHCI general transfer descriptor. |
||
50 | struct ehci_hardware_td |
||
51 | NextTD dd ? |
||
52 | ; Bit 0 is Terminate bit, 1 = there is no next TD. |
||
53 | ; Bits 1-4 must be zero. |
||
54 | ; With masked 5 lower bits, this is the physical address of the next TD, if any. |
||
55 | AlternateNextTD dd ? |
||
56 | ; Similar to NextTD, used if the transfer terminates with a short packet. |
||
57 | Token dd ? |
||
58 | ; 1. Lower byte is Status field: |
||
59 | ; bit 0 = ping state for USB2 endpoints, ERR handshake signal for USB1 endpoints |
||
60 | ; bit 1 = split transaction state, meaningless for USB2 endpoints |
||
61 | ; bit 2 = missed micro-frame |
||
62 | ; bit 3 = transaction error |
||
63 | ; bit 4 = babble detected |
||
64 | ; bit 5 = data buffer error |
||
65 | ; bit 6 = halted |
||
66 | ; bit 7 = active |
||
67 | ; 2. Next two bits (bits 8-9) are PID code, 0 = OUT, 1 = IN, 2 = SETUP. |
||
68 | ; 3. Next two bits (bits 10-11) is ErrorCounter. Initialized as 3, decremented |
||
69 | ; on each error; if it goes to zero, transaction is stopped. |
||
70 | ; 4. Next 3 bits (bits 12-14) are CurrentPage field. |
||
71 | ; 5. Next bit (bit 15) is InterruptOnComplete bit. |
||
72 | ; 6. Next 15 bits (bits 16-30) are TransferLength field, |
||
73 | ; number of bytes to transfer. |
||
74 | ; 7. Upper bit (bit 31) is DataToggle bit. |
||
75 | BufferPointers rd 5 |
||
76 | ; The buffer to be transferred can be spanned on up to 5 physical pages. |
||
77 | ; The first item of this array is the physical address of the first byte in |
||
78 | ; the buffer, other items are physical addresses of next pages. Lower 12 bits |
||
79 | ; in other items must be set to zero; ehci_pipe.Overlay reuses some of them. |
||
80 | BufferPointersHigh rd 5 |
||
81 | ; Upper dwords of BufferPointers for controllers with 64-bit memory access. |
||
82 | ; Always zero. |
||
83 | ends |
||
84 | |||
85 | ; EHCI general transfer descriptor. |
||
86 | ; * The structure describes transfers to be performed on Control, Bulk or |
||
87 | ; Interrupt endpoints. |
||
88 | ; * The structure includes two parts, the hardware part and the software part. |
||
89 | ; * The hardware part consists of first 52 bytes and corresponds to |
||
90 | ; the Queue Element Transfer Descriptor from EHCI specification. |
||
91 | ; * The hardware requires 32-bytes alignment of the hardware part, so |
||
92 | ; the entire descriptor must be 32-bytes aligned. Since the allocator |
||
93 | ; (usb_allocate_common) allocates memory sequentially from page start |
||
3653 | clevermous | 94 | ; (aligned on 0x1000 bytes), block size for the allocator must be divisible |
95 | ; by 32; ehci_alloc_td ensures this. |
||
3520 | clevermous | 96 | ; * The hardware also requires that the hardware part must not cross page |
97 | ; boundary; the allocator satisfies this automatically. |
||
98 | struct ehci_gtd ehci_hardware_td |
||
99 | Flags dd ? |
||
100 | ; Copy of flags from the call to usb_*_transfer_async. |
||
101 | ends |
||
102 | |||
103 | ; EHCI-specific part of a pipe descriptor. |
||
104 | ; * This structure corresponds to the Queue Head from the EHCI specification. |
||
105 | ; * The hardware requires 32-bytes alignment of the hardware part. |
||
106 | ; Since the allocator (usb_allocate_common) allocates memory sequentially |
||
3653 | clevermous | 107 | ; from page start (aligned on 0x1000 bytes), block size for the allocator |
108 | ; must be divisible by 32; ehci_alloc_pipe ensures this. |
||
3520 | clevermous | 109 | ; * The hardware requires also that the hardware part must not cross page |
110 | ; boundary; the allocator satisfies this automatically. |
||
111 | struct ehci_pipe |
||
112 | NextQH dd ? |
||
113 | ; 1. First bit (bit 0) is Terminate bit, 1 = there is no next QH. |
||
114 | ; 2. Next two bits (bits 1-2) are Type field of the next QH, |
||
115 | ; one of EHCI_TYPE_* constants. |
||
116 | ; 3. Next two bits (bits 3-4) are reserved, must be zero. |
||
117 | ; 4. With masked 5 lower bits, this is the physical address of the next object |
||
118 | ; to be processed, usually next QH. |
||
119 | Token dd ? |
||
120 | ; 1. Lower 7 bits are DeviceAddress field. This is the address of the |
||
121 | ; target device on the USB bus. |
||
122 | ; 2. Next bit (bit 7) is Inactivate-on-next-transaction bit. Can be nonzero |
||
123 | ; only for interrupt/isochronous USB1 endpoints. |
||
124 | ; 3. Next 4 bits (bits 8-11) are Endpoint field. This is the target endpoint |
||
125 | ; number. |
||
126 | ; 4. Next 2 bits (bits 12-13) are EndpointSpeed field, one of EHCI_SPEED_*. |
||
127 | ; 5. Next bit (bit 14) is DataToggleControl bit, |
||
128 | ; 0 = use DataToggle bit from QH, 1 = from TD. |
||
129 | ; 6. Next bit (bit 15) is Head-of-reclamation-list. The head of Control list |
||
130 | ; has 1 here, all other QHs have zero. |
||
131 | ; 7. Next 11 bits (bits 16-26) are MaximumPacketLength field for the target |
||
132 | ; endpoint. |
||
133 | ; 8. Next bit (bit 27) is ControlEndpoint bit, must be 1 for USB1 control |
||
134 | ; endpoints and 0 for all others. |
||
135 | ; 9. Upper 4 bits (bits 28-31) are NakCountReload field. |
||
136 | ; Zero for USB1 endpoints, zero for periodic endpoints. |
||
137 | ; For control/bulk USB2 endpoints, the code sets it to 4, |
||
138 | ; which is rather arbitrary. |
||
139 | Flags dd ? |
||
140 | ; 1. Lower byte is S-mask, each bit corresponds to one microframe per frame; |
||
141 | ; bit is set <=> enable transactions in this microframe. |
||
142 | ; 2. Next byte is C-mask, each bit corresponds to one microframe per frame; |
||
143 | ; bit is set <=> enable complete-split transactions in this microframe. |
||
144 | ; Meaningful only for USB1 endpoints. |
||
145 | ; 3. Next 14 bits give address of the target device as hub:port, bits 16-22 |
||
146 | ; are the USB address of the hub, bits 23-29 are the port number. |
||
147 | ; Meaningful only for USB1 endpoints. |
||
148 | ; 4. Upper 2 bits define number of consequetive transactions per micro-frame |
||
149 | ; which host is allowed to permit for this endpoint. |
||
150 | ; For control/bulk endpoints, it must be 1. |
||
151 | ; For periodic endpoints, the value is taken from the endpoint descriptor. |
||
152 | HeadTD dd ? |
||
153 | ; The physical address of the first TD for this pipe. |
||
154 | ; Lower 5 bits must be zero. |
||
155 | Overlay ehci_hardware_td ? |
||
156 | ; Working area for the current TD, if there is any. |
||
157 | ; When TD is retired, it is written to that TD and Overlay is loaded |
||
158 | ; from the new TD, if any. |
||
159 | ends |
||
160 | |||
161 | ; This structure describes the static head of every list of pipes. |
||
162 | ; The hardware requires 32-bytes alignment of this structure. |
||
163 | ; All instances of this structure are located sequentially in ehci_controller, |
||
164 | ; ehci_controller is page-aligned, so it is sufficient to make this structure |
||
165 | ; 32-bytes aligned and verify that the first instance is 32-bytes aligned |
||
166 | ; inside ehci_controller. |
||
167 | ; The hardware also requires that 44h bytes (size of 64-bit Queue Head |
||
168 | ; Descriptor) starting at the beginning of this structure must not cross page |
||
169 | ; boundary. If not, most hardware still behaves correctly (in fact, the last |
||
170 | ; dword can have any value and this structure is never written), but on some |
||
171 | ; hardware some things just break in mysterious ways. |
||
172 | struct ehci_static_ep |
||
173 | ; Hardware fields are the same as in ehci_pipe. |
||
174 | ; Only NextQH and Overlay.Token are actually used. |
||
175 | ; NB: some emulators ignore Token.Halted bit (probably assuming that it is set |
||
176 | ; only when device fails and emulation never fails) and always follow |
||
177 | ; [Alternate]NextTD when they see that OverlayToken.Active bit is zero; |
||
178 | ; so it is important to also set [Alternate]NextTD to 1. |
||
179 | NextQH dd ? |
||
180 | Token dd ? |
||
181 | Flags dd ? |
||
182 | HeadTD dd ? |
||
183 | NextTD dd ? |
||
184 | AlternateNextTD dd ? |
||
185 | OverlayToken dd ? |
||
186 | NextList dd ? |
||
187 | SoftwarePart rd sizeof.usb_static_ep/4 |
||
188 | Bandwidths rw 8 |
||
189 | dd ? |
||
190 | ends |
||
191 | |||
192 | if sizeof.ehci_static_ep mod 32 |
||
193 | .err ehci_static_ep must be 32-bytes aligned |
||
194 | end if |
||
195 | |||
196 | if ehci_static_ep.OverlayToken <> ehci_pipe.Overlay.Token |
||
197 | .err ehci_static_ep.OverlayToken misplaced |
||
198 | end if |
||
199 | |||
200 | ; EHCI-specific part of controller data. |
||
201 | ; * The structure includes two parts, the hardware part and the software part. |
||
202 | ; * The hardware part consists of first 4096 bytes and corresponds to |
||
203 | ; the Periodic Frame List from the EHCI specification. |
||
204 | ; * The hardware requires page-alignment of the hardware part, so |
||
205 | ; the entire descriptor must be page-aligned. |
||
206 | ; This structure is allocated with kernel_alloc (see usb_init_controller), |
||
207 | ; this gives page-aligned data. |
||
208 | ; * The controller is described by both ehci_controller and usb_controller |
||
209 | ; structures, for each controller there is one ehci_controller and one |
||
210 | ; usb_controller structure. These structures are located sequentially |
||
211 | ; in the memory: beginning from some page start, there is ehci_controller |
||
212 | ; structure - this enforces hardware alignment requirements - and then |
||
213 | ; usb_controller structure. |
||
214 | ; * The code keeps pointer to usb_controller structure. The ehci_controller |
||
215 | ; structure is addressed as [ptr + ehci_controller.field - sizeof.ehci_controller]. |
||
216 | struct ehci_controller |
||
217 | ; ------------------------------ hardware fields ------------------------------ |
||
218 | FrameList rd 1024 |
||
219 | ; Entry n corresponds to the head of the frame list to be executed in |
||
3578 | clevermous | 220 | ; the frames n,n+1024,n+2048,n+3072,... |
3520 | clevermous | 221 | ; The first bit of each entry is Terminate bit, 1 = the frame is empty. |
222 | ; Bits 1-2 are Type field, one of EHCI_TYPE_* constants. |
||
223 | ; Bits 3-4 must be zero. |
||
224 | ; With masked 5 lower bits, the entry is a physical address of the first QH/TD |
||
225 | ; to be executed. |
||
226 | ; ------------------------------ software fields ------------------------------ |
||
227 | ; Every list has the static head, which is an always halted QH. |
||
228 | ; The following fields are static heads, one per list: |
||
229 | ; 32+16+8+4+2+1 = 63 for Periodic lists, 1 for Control list and 1 for Bulk list. |
||
230 | IntEDs ehci_static_ep |
||
231 | rb 62 * sizeof.ehci_static_ep |
||
232 | ; Beware. |
||
233 | ; Two following strings ensure that 44h bytes at any static head |
||
234 | ; do not cross page boundary. Without that, the code "works on my machine"... |
||
235 | ; but fails on some hardware in seemingly unrelated ways. |
||
236 | ; One hardware TD (without any software fields) fit in the rest of the page. |
||
237 | ehci_controller.ControlDelta = 2000h - (ehci_controller.IntEDs + 63 * sizeof.ehci_static_ep) |
||
238 | StopQueueTD ehci_hardware_td |
||
239 | ; Used as AlternateNextTD for transfers when short packet is considered |
||
240 | ; as an error; short packet must stop the queue in this case, not advance |
||
241 | ; to the next transfer. |
||
242 | rb ehci_controller.ControlDelta - sizeof.ehci_hardware_td |
||
243 | ; Padding for page-alignment. |
||
244 | ControlED ehci_static_ep |
||
245 | Bulk |