]>
Commit | Line | Data |
---|---|---|
0d92ed30 PB |
1 | /* |
2 | * QEMU USB OHCI Emulation | |
3 | * Copyright (c) 2004 Gianni Tedesco | |
4 | * Copyright (c) 2006 CodeSourcery | |
e24ad6f1 | 5 | * Copyright (c) 2006 Openedhand Ltd. |
0d92ed30 PB |
6 | * |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * TODO: | |
22 | * o Isochronous transfers | |
23 | * o Allocate bandwidth in frames properly | |
24 | * o Disable timers when nothing needs to be done, or remove timer usage | |
25 | * all together. | |
26 | * o Handle unrecoverable errors properly | |
27 | * o BIOS work to boot from USB storage | |
28 | */ | |
29 | ||
30 | #include "vl.h" | |
31 | ||
32 | //#define DEBUG_OHCI | |
33 | /* Dump packet contents. */ | |
34 | //#define DEBUG_PACKET | |
35 | /* This causes frames to occur 1000x slower */ | |
36 | //#define OHCI_TIME_WARP 1 | |
37 | ||
38 | #ifdef DEBUG_OHCI | |
39 | #define dprintf printf | |
40 | #else | |
41 | #define dprintf(...) | |
42 | #endif | |
43 | ||
44 | /* Number of Downstream Ports on the root hub. */ | |
45 | ||
46 | #define OHCI_MAX_PORTS 15 | |
47 | ||
48 | static int64_t usb_frame_time; | |
49 | static int64_t usb_bit_time; | |
50 | ||
51 | typedef struct OHCIPort { | |
52 | USBPort port; | |
53 | uint32_t ctrl; | |
54 | } OHCIPort; | |
55 | ||
e24ad6f1 PB |
56 | enum ohci_type { |
57 | OHCI_TYPE_PCI, | |
58 | OHCI_TYPE_PXA | |
59 | }; | |
60 | ||
0d92ed30 | 61 | typedef struct { |
d537cf6c | 62 | qemu_irq irq; |
e24ad6f1 | 63 | enum ohci_type type; |
0d92ed30 PB |
64 | target_phys_addr_t mem_base; |
65 | int mem; | |
66 | int num_ports; | |
e24ad6f1 | 67 | const char *name; |
0d92ed30 PB |
68 | |
69 | QEMUTimer *eof_timer; | |
70 | int64_t sof_time; | |
71 | ||
72 | /* OHCI state */ | |
73 | /* Control partition */ | |
74 | uint32_t ctl, status; | |
75 | uint32_t intr_status; | |
76 | uint32_t intr; | |
77 | ||
78 | /* memory pointer partition */ | |
79 | uint32_t hcca; | |
80 | uint32_t ctrl_head, ctrl_cur; | |
81 | uint32_t bulk_head, bulk_cur; | |
82 | uint32_t per_cur; | |
83 | uint32_t done; | |
84 | int done_count; | |
85 | ||
86 | /* Frame counter partition */ | |
87 | uint32_t fsmps:15; | |
88 | uint32_t fit:1; | |
89 | uint32_t fi:14; | |
90 | uint32_t frt:1; | |
91 | uint16_t frame_number; | |
92 | uint16_t padding; | |
93 | uint32_t pstart; | |
94 | uint32_t lst; | |
95 | ||
96 | /* Root Hub partition */ | |
97 | uint32_t rhdesc_a, rhdesc_b; | |
98 | uint32_t rhstatus; | |
99 | OHCIPort rhport[OHCI_MAX_PORTS]; | |
4d611c9a | 100 | |
e24ad6f1 PB |
101 | /* PXA27x Non-OHCI events */ |
102 | uint32_t hstatus; | |
103 | uint32_t hmask; | |
104 | uint32_t hreset; | |
105 | uint32_t htest; | |
106 | ||
4d611c9a PB |
107 | /* Active packets. */ |
108 | uint32_t old_ctl; | |
109 | USBPacket usb_packet; | |
110 | uint8_t usb_buf[8192]; | |
111 | uint32_t async_td; | |
112 | int async_complete; | |
113 | ||
0d92ed30 PB |
114 | } OHCIState; |
115 | ||
116 | /* Host Controller Communications Area */ | |
117 | struct ohci_hcca { | |
118 | uint32_t intr[32]; | |
119 | uint16_t frame, pad; | |
120 | uint32_t done; | |
121 | }; | |
122 | ||
123 | /* Bitfields for the first word of an Endpoint Desciptor. */ | |
124 | #define OHCI_ED_FA_SHIFT 0 | |
125 | #define OHCI_ED_FA_MASK (0x7f<<OHCI_ED_FA_SHIFT) | |
126 | #define OHCI_ED_EN_SHIFT 7 | |
127 | #define OHCI_ED_EN_MASK (0xf<<OHCI_ED_EN_SHIFT) | |
128 | #define OHCI_ED_D_SHIFT 11 | |
129 | #define OHCI_ED_D_MASK (3<<OHCI_ED_D_SHIFT) | |
130 | #define OHCI_ED_S (1<<13) | |
131 | #define OHCI_ED_K (1<<14) | |
132 | #define OHCI_ED_F (1<<15) | |
133 | #define OHCI_ED_MPS_SHIFT 7 | |
134 | #define OHCI_ED_MPS_MASK (0xf<<OHCI_ED_FA_SHIFT) | |
135 | ||
136 | /* Flags in the head field of an Endpoint Desciptor. */ | |
137 | #define OHCI_ED_H 1 | |
138 | #define OHCI_ED_C 2 | |
139 | ||
140 | /* Bitfields for the first word of a Transfer Desciptor. */ | |
141 | #define OHCI_TD_R (1<<18) | |
142 | #define OHCI_TD_DP_SHIFT 19 | |
143 | #define OHCI_TD_DP_MASK (3<<OHCI_TD_DP_SHIFT) | |
144 | #define OHCI_TD_DI_SHIFT 21 | |
145 | #define OHCI_TD_DI_MASK (7<<OHCI_TD_DI_SHIFT) | |
146 | #define OHCI_TD_T0 (1<<24) | |
147 | #define OHCI_TD_T1 (1<<24) | |
148 | #define OHCI_TD_EC_SHIFT 26 | |
149 | #define OHCI_TD_EC_MASK (3<<OHCI_TD_EC_SHIFT) | |
150 | #define OHCI_TD_CC_SHIFT 28 | |
151 | #define OHCI_TD_CC_MASK (0xf<<OHCI_TD_CC_SHIFT) | |
152 | ||
153 | #define OHCI_DPTR_MASK 0xfffffff0 | |
154 | ||
155 | #define OHCI_BM(val, field) \ | |
156 | (((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT) | |
157 | ||
158 | #define OHCI_SET_BM(val, field, newval) do { \ | |
159 | val &= ~OHCI_##field##_MASK; \ | |
160 | val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \ | |
161 | } while(0) | |
162 | ||
163 | /* endpoint descriptor */ | |
164 | struct ohci_ed { | |
165 | uint32_t flags; | |
166 | uint32_t tail; | |
167 | uint32_t head; | |
168 | uint32_t next; | |
169 | }; | |
170 | ||
171 | /* General transfer descriptor */ | |
172 | struct ohci_td { | |
173 | uint32_t flags; | |
174 | uint32_t cbp; | |
175 | uint32_t next; | |
176 | uint32_t be; | |
177 | }; | |
178 | ||
179 | #define USB_HZ 12000000 | |
180 | ||
181 | /* OHCI Local stuff */ | |
182 | #define OHCI_CTL_CBSR ((1<<0)|(1<<1)) | |
183 | #define OHCI_CTL_PLE (1<<2) | |
184 | #define OHCI_CTL_IE (1<<3) | |
185 | #define OHCI_CTL_CLE (1<<4) | |
186 | #define OHCI_CTL_BLE (1<<5) | |
187 | #define OHCI_CTL_HCFS ((1<<6)|(1<<7)) | |
188 | #define OHCI_USB_RESET 0x00 | |
189 | #define OHCI_USB_RESUME 0x40 | |
190 | #define OHCI_USB_OPERATIONAL 0x80 | |
191 | #define OHCI_USB_SUSPEND 0xc0 | |
192 | #define OHCI_CTL_IR (1<<8) | |
193 | #define OHCI_CTL_RWC (1<<9) | |
194 | #define OHCI_CTL_RWE (1<<10) | |
195 | ||
196 | #define OHCI_STATUS_HCR (1<<0) | |
197 | #define OHCI_STATUS_CLF (1<<1) | |
198 | #define OHCI_STATUS_BLF (1<<2) | |
199 | #define OHCI_STATUS_OCR (1<<3) | |
200 | #define OHCI_STATUS_SOC ((1<<6)|(1<<7)) | |
201 | ||
202 | #define OHCI_INTR_SO (1<<0) /* Scheduling overrun */ | |
203 | #define OHCI_INTR_WD (1<<1) /* HcDoneHead writeback */ | |
204 | #define OHCI_INTR_SF (1<<2) /* Start of frame */ | |
205 | #define OHCI_INTR_RD (1<<3) /* Resume detect */ | |
206 | #define OHCI_INTR_UE (1<<4) /* Unrecoverable error */ | |
207 | #define OHCI_INTR_FNO (1<<5) /* Frame number overflow */ | |
208 | #define OHCI_INTR_RHSC (1<<6) /* Root hub status change */ | |
209 | #define OHCI_INTR_OC (1<<30) /* Ownership change */ | |
210 | #define OHCI_INTR_MIE (1<<31) /* Master Interrupt Enable */ | |
211 | ||
212 | #define OHCI_HCCA_SIZE 0x100 | |
213 | #define OHCI_HCCA_MASK 0xffffff00 | |
214 | ||
215 | #define OHCI_EDPTR_MASK 0xfffffff0 | |
216 | ||
217 | #define OHCI_FMI_FI 0x00003fff | |
218 | #define OHCI_FMI_FSMPS 0xffff0000 | |
219 | #define OHCI_FMI_FIT 0x80000000 | |
220 | ||
221 | #define OHCI_FR_RT (1<<31) | |
222 | ||
223 | #define OHCI_LS_THRESH 0x628 | |
224 | ||
225 | #define OHCI_RHA_RW_MASK 0x00000000 /* Mask of supported features. */ | |
226 | #define OHCI_RHA_PSM (1<<8) | |
227 | #define OHCI_RHA_NPS (1<<9) | |
228 | #define OHCI_RHA_DT (1<<10) | |
229 | #define OHCI_RHA_OCPM (1<<11) | |
230 | #define OHCI_RHA_NOCP (1<<12) | |
231 | #define OHCI_RHA_POTPGT_MASK 0xff000000 | |
232 | ||
233 | #define OHCI_RHS_LPS (1<<0) | |
234 | #define OHCI_RHS_OCI (1<<1) | |
235 | #define OHCI_RHS_DRWE (1<<15) | |
236 | #define OHCI_RHS_LPSC (1<<16) | |
237 | #define OHCI_RHS_OCIC (1<<17) | |
238 | #define OHCI_RHS_CRWE (1<<31) | |
239 | ||
240 | #define OHCI_PORT_CCS (1<<0) | |
241 | #define OHCI_PORT_PES (1<<1) | |
242 | #define OHCI_PORT_PSS (1<<2) | |
243 | #define OHCI_PORT_POCI (1<<3) | |
244 | #define OHCI_PORT_PRS (1<<4) | |
245 | #define OHCI_PORT_PPS (1<<8) | |
246 | #define OHCI_PORT_LSDA (1<<9) | |
247 | #define OHCI_PORT_CSC (1<<16) | |
248 | #define OHCI_PORT_PESC (1<<17) | |
249 | #define OHCI_PORT_PSSC (1<<18) | |
250 | #define OHCI_PORT_OCIC (1<<19) | |
251 | #define OHCI_PORT_PRSC (1<<20) | |
252 | #define OHCI_PORT_WTC (OHCI_PORT_CSC|OHCI_PORT_PESC|OHCI_PORT_PSSC \ | |
253 | |OHCI_PORT_OCIC|OHCI_PORT_PRSC) | |
254 | ||
255 | #define OHCI_TD_DIR_SETUP 0x0 | |
256 | #define OHCI_TD_DIR_OUT 0x1 | |
257 | #define OHCI_TD_DIR_IN 0x2 | |
258 | #define OHCI_TD_DIR_RESERVED 0x3 | |
259 | ||
260 | #define OHCI_CC_NOERROR 0x0 | |
261 | #define OHCI_CC_CRC 0x1 | |
262 | #define OHCI_CC_BITSTUFFING 0x2 | |
263 | #define OHCI_CC_DATATOGGLEMISMATCH 0x3 | |
264 | #define OHCI_CC_STALL 0x4 | |
265 | #define OHCI_CC_DEVICENOTRESPONDING 0x5 | |
266 | #define OHCI_CC_PIDCHECKFAILURE 0x6 | |
267 | #define OHCI_CC_UNDEXPETEDPID 0x7 | |
268 | #define OHCI_CC_DATAOVERRUN 0x8 | |
269 | #define OHCI_CC_DATAUNDERRUN 0x9 | |
270 | #define OHCI_CC_BUFFEROVERRUN 0xc | |
271 | #define OHCI_CC_BUFFERUNDERRUN 0xd | |
272 | ||
e24ad6f1 PB |
273 | #define OHCI_HRESET_FSBIR (1 << 0) |
274 | ||
61064870 PB |
275 | /* Update IRQ levels */ |
276 | static inline void ohci_intr_update(OHCIState *ohci) | |
277 | { | |
278 | int level = 0; | |
279 | ||
280 | if ((ohci->intr & OHCI_INTR_MIE) && | |
281 | (ohci->intr_status & ohci->intr)) | |
282 | level = 1; | |
283 | ||
d537cf6c | 284 | qemu_set_irq(ohci->irq, level); |
61064870 PB |
285 | } |
286 | ||
287 | /* Set an interrupt */ | |
288 | static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr) | |
289 | { | |
290 | ohci->intr_status |= intr; | |
291 | ohci_intr_update(ohci); | |
292 | } | |
293 | ||
294 | /* Attach or detach a device on a root hub port. */ | |
0d92ed30 PB |
295 | static void ohci_attach(USBPort *port1, USBDevice *dev) |
296 | { | |
297 | OHCIState *s = port1->opaque; | |
298 | OHCIPort *port = &s->rhport[port1->index]; | |
61064870 | 299 | uint32_t old_state = port->ctrl; |
0d92ed30 PB |
300 | |
301 | if (dev) { | |
302 | if (port->port.dev) { | |
303 | usb_attach(port1, NULL); | |
304 | } | |
305 | /* set connect status */ | |
61064870 PB |
306 | port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC; |
307 | ||
0d92ed30 PB |
308 | /* update speed */ |
309 | if (dev->speed == USB_SPEED_LOW) | |
310 | port->ctrl |= OHCI_PORT_LSDA; | |
311 | else | |
312 | port->ctrl &= ~OHCI_PORT_LSDA; | |
313 | port->port.dev = dev; | |
e24ad6f1 PB |
314 | |
315 | /* notify of remote-wakeup */ | |
316 | if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) | |
317 | ohci_set_interrupt(s, OHCI_INTR_RD); | |
318 | ||
0d92ed30 | 319 | /* send the attach message */ |
4d611c9a | 320 | usb_send_msg(dev, USB_MSG_ATTACH); |
0d92ed30 PB |
321 | dprintf("usb-ohci: Attached port %d\n", port1->index); |
322 | } else { | |
323 | /* set connect status */ | |
61064870 PB |
324 | if (port->ctrl & OHCI_PORT_CCS) { |
325 | port->ctrl &= ~OHCI_PORT_CCS; | |
326 | port->ctrl |= OHCI_PORT_CSC; | |
0d92ed30 PB |
327 | } |
328 | /* disable port */ | |
329 | if (port->ctrl & OHCI_PORT_PES) { | |
330 | port->ctrl &= ~OHCI_PORT_PES; | |
331 | port->ctrl |= OHCI_PORT_PESC; | |
332 | } | |
333 | dev = port->port.dev; | |
334 | if (dev) { | |
335 | /* send the detach message */ | |
4d611c9a | 336 | usb_send_msg(dev, USB_MSG_DETACH); |
0d92ed30 PB |
337 | } |
338 | port->port.dev = NULL; | |
339 | dprintf("usb-ohci: Detached port %d\n", port1->index); | |
340 | } | |
61064870 PB |
341 | |
342 | if (old_state != port->ctrl) | |
343 | ohci_set_interrupt(s, OHCI_INTR_RHSC); | |
0d92ed30 PB |
344 | } |
345 | ||
346 | /* Reset the controller */ | |
347 | static void ohci_reset(OHCIState *ohci) | |
348 | { | |
349 | OHCIPort *port; | |
350 | int i; | |
351 | ||
352 | ohci->ctl = 0; | |
4d611c9a | 353 | ohci->old_ctl = 0; |
0d92ed30 PB |
354 | ohci->status = 0; |
355 | ohci->intr_status = 0; | |
356 | ohci->intr = OHCI_INTR_MIE; | |
357 | ||
358 | ohci->hcca = 0; | |
359 | ohci->ctrl_head = ohci->ctrl_cur = 0; | |
360 | ohci->bulk_head = ohci->bulk_cur = 0; | |
361 | ohci->per_cur = 0; | |
362 | ohci->done = 0; | |
363 | ohci->done_count = 7; | |
364 | ||
365 | /* FSMPS is marked TBD in OCHI 1.0, what gives ffs? | |
366 | * I took the value linux sets ... | |
367 | */ | |
368 | ohci->fsmps = 0x2778; | |
369 | ohci->fi = 0x2edf; | |
370 | ohci->fit = 0; | |
371 | ohci->frt = 0; | |
372 | ohci->frame_number = 0; | |
373 | ohci->pstart = 0; | |
374 | ohci->lst = OHCI_LS_THRESH; | |
375 | ||
376 | ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports; | |
377 | ohci->rhdesc_b = 0x0; /* Impl. specific */ | |
378 | ohci->rhstatus = 0; | |
379 | ||
380 | for (i = 0; i < ohci->num_ports; i++) | |
381 | { | |
382 | port = &ohci->rhport[i]; | |
383 | port->ctrl = 0; | |
384 | if (port->port.dev) | |
385 | ohci_attach(&port->port, port->port.dev); | |
386 | } | |
4d611c9a PB |
387 | if (ohci->async_td) { |
388 | usb_cancel_packet(&ohci->usb_packet); | |
389 | ohci->async_td = 0; | |
390 | } | |
e24ad6f1 | 391 | dprintf("usb-ohci: Reset %s\n", ohci->name); |
0d92ed30 PB |
392 | } |
393 | ||
0d92ed30 PB |
394 | /* Get an array of dwords from main memory */ |
395 | static inline int get_dwords(uint32_t addr, uint32_t *buf, int num) | |
396 | { | |
397 | int i; | |
398 | ||
399 | for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { | |
400 | cpu_physical_memory_rw(addr, (uint8_t *)buf, sizeof(*buf), 0); | |
401 | *buf = le32_to_cpu(*buf); | |
402 | } | |
403 | ||
404 | return 1; | |
405 | } | |
406 | ||
407 | /* Put an array of dwords in to main memory */ | |
408 | static inline int put_dwords(uint32_t addr, uint32_t *buf, int num) | |
409 | { | |
410 | int i; | |
411 | ||
412 | for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { | |
413 | uint32_t tmp = cpu_to_le32(*buf); | |
414 | cpu_physical_memory_rw(addr, (uint8_t *)&tmp, sizeof(tmp), 1); | |
415 | } | |
416 | ||
417 | return 1; | |
418 | } | |
419 | ||
420 | static inline int ohci_read_ed(uint32_t addr, struct ohci_ed *ed) | |
421 | { | |
422 | return get_dwords(addr, (uint32_t *)ed, sizeof(*ed) >> 2); | |
423 | } | |
424 | ||
425 | static inline int ohci_read_td(uint32_t addr, struct ohci_td *td) | |
426 | { | |
427 | return get_dwords(addr, (uint32_t *)td, sizeof(*td) >> 2); | |
428 | } | |
429 | ||
430 | static inline int ohci_put_ed(uint32_t addr, struct ohci_ed *ed) | |
431 | { | |
432 | return put_dwords(addr, (uint32_t *)ed, sizeof(*ed) >> 2); | |
433 | } | |
434 | ||
435 | static inline int ohci_put_td(uint32_t addr, struct ohci_td *td) | |
436 | { | |
437 | return put_dwords(addr, (uint32_t *)td, sizeof(*td) >> 2); | |
438 | } | |
439 | ||
440 | /* Read/Write the contents of a TD from/to main memory. */ | |
441 | static void ohci_copy_td(struct ohci_td *td, uint8_t *buf, int len, int write) | |
442 | { | |
443 | uint32_t ptr; | |
444 | uint32_t n; | |
445 | ||
446 | ptr = td->cbp; | |
447 | n = 0x1000 - (ptr & 0xfff); | |
448 | if (n > len) | |
449 | n = len; | |
450 | cpu_physical_memory_rw(ptr, buf, n, write); | |
451 | if (n == len) | |
452 | return; | |
453 | ptr = td->be & ~0xfffu; | |
e6f3e5e0 | 454 | buf += n; |
0d92ed30 PB |
455 | cpu_physical_memory_rw(ptr, buf, len - n, write); |
456 | } | |
457 | ||
4d611c9a PB |
458 | static void ohci_process_lists(OHCIState *ohci); |
459 | ||
460 | static void ohci_async_complete_packet(USBPacket * packet, void *opaque) | |
461 | { | |
462 | OHCIState *ohci = opaque; | |
463 | #ifdef DEBUG_PACKET | |
464 | dprintf("Async packet complete\n"); | |
465 | #endif | |
466 | ohci->async_complete = 1; | |
467 | ohci_process_lists(ohci); | |
468 | } | |
469 | ||
0d92ed30 PB |
470 | /* Service a transport descriptor. |
471 | Returns nonzero to terminate processing of this endpoint. */ | |
472 | ||
473 | static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed) | |
474 | { | |
475 | int dir; | |
476 | size_t len = 0; | |
0d92ed30 PB |
477 | char *str = NULL; |
478 | int pid; | |
479 | int ret; | |
480 | int i; | |
481 | USBDevice *dev; | |
482 | struct ohci_td td; | |
483 | uint32_t addr; | |
484 | int flag_r; | |
4d611c9a | 485 | int completion; |
0d92ed30 PB |
486 | |
487 | addr = ed->head & OHCI_DPTR_MASK; | |
4d611c9a PB |
488 | /* See if this TD has already been submitted to the device. */ |
489 | completion = (addr == ohci->async_td); | |
490 | if (completion && !ohci->async_complete) { | |
491 | #ifdef DEBUG_PACKET | |
492 | dprintf("Skipping async TD\n"); | |
493 | #endif | |
494 | return 1; | |
495 | } | |
0d92ed30 PB |
496 | if (!ohci_read_td(addr, &td)) { |
497 | fprintf(stderr, "usb-ohci: TD read error at %x\n", addr); | |
498 | return 0; | |
499 | } | |
500 | ||
501 | dir = OHCI_BM(ed->flags, ED_D); | |
502 | switch (dir) { | |
503 | case OHCI_TD_DIR_OUT: | |
504 | case OHCI_TD_DIR_IN: | |
505 | /* Same value. */ | |
506 | break; | |
507 | default: | |
508 | dir = OHCI_BM(td.flags, TD_DP); | |
509 | break; | |
510 | } | |
511 | ||
512 | switch (dir) { | |
513 | case OHCI_TD_DIR_IN: | |
514 | str = "in"; | |
515 | pid = USB_TOKEN_IN; | |
516 | break; | |
517 | case OHCI_TD_DIR_OUT: | |
518 | str = "out"; | |
519 | pid = USB_TOKEN_OUT; | |
520 | break; | |
521 | case OHCI_TD_DIR_SETUP: | |
522 | str = "setup"; | |
523 | pid = USB_TOKEN_SETUP; | |
524 | break; | |
525 | default: | |
526 | fprintf(stderr, "usb-ohci: Bad direction\n"); | |
527 | return 1; | |
528 | } | |
529 | if (td.cbp && td.be) { | |
e6f3e5e0 PB |
530 | if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) { |
531 | len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff); | |
532 | } else { | |
533 | len = (td.be - td.cbp) + 1; | |
534 | } | |
535 | ||
4d611c9a PB |
536 | if (len && dir != OHCI_TD_DIR_IN && !completion) { |
537 | ohci_copy_td(&td, ohci->usb_buf, len, 0); | |
0d92ed30 PB |
538 | } |
539 | } | |
540 | ||
541 | flag_r = (td.flags & OHCI_TD_R) != 0; | |
542 | #ifdef DEBUG_PACKET | |
543 | dprintf(" TD @ 0x%.8x %u bytes %s r=%d cbp=0x%.8x be=0x%.8x\n", | |
544 | addr, len, str, flag_r, td.cbp, td.be); | |
545 | ||
546 | if (len >= 0 && dir != OHCI_TD_DIR_IN) { | |
547 | dprintf(" data:"); | |
548 | for (i = 0; i < len; i++) | |
4d611c9a | 549 | printf(" %.2x", ohci->usb_buf[i]); |
0d92ed30 PB |
550 | dprintf("\n"); |
551 | } | |
552 | #endif | |
4d611c9a PB |
553 | if (completion) { |
554 | ret = ohci->usb_packet.len; | |
555 | ohci->async_td = 0; | |
556 | ohci->async_complete = 0; | |
557 | } else { | |
558 | ret = USB_RET_NODEV; | |
559 | for (i = 0; i < ohci->num_ports; i++) { | |
560 | dev = ohci->rhport[i].port.dev; | |
561 | if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0) | |
562 | continue; | |
563 | ||
564 | if (ohci->async_td) { | |
565 | /* ??? The hardware should allow one active packet per | |
566 | endpoint. We only allow one active packet per controller. | |
567 | This should be sufficient as long as devices respond in a | |
568 | timely manner. | |
569 | */ | |
0d92ed30 | 570 | #ifdef DEBUG_PACKET |
4d611c9a | 571 | dprintf("Too many pending packets\n"); |
0d92ed30 | 572 | #endif |
4d611c9a PB |
573 | return 1; |
574 | } | |
575 | ohci->usb_packet.pid = pid; | |
576 | ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA); | |
577 | ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN); | |
578 | ohci->usb_packet.data = ohci->usb_buf; | |
579 | ohci->usb_packet.len = len; | |
580 | ohci->usb_packet.complete_cb = ohci_async_complete_packet; | |
581 | ohci->usb_packet.complete_opaque = ohci; | |
582 | ret = dev->handle_packet(dev, &ohci->usb_packet); | |
583 | if (ret != USB_RET_NODEV) | |
584 | break; | |
585 | } | |
586 | #ifdef DEBUG_PACKET | |
587 | dprintf("ret=%d\n", ret); | |
588 | #endif | |
589 | if (ret == USB_RET_ASYNC) { | |
590 | ohci->async_td = addr; | |
591 | return 1; | |
592 | } | |
593 | } | |
0d92ed30 PB |
594 | if (ret >= 0) { |
595 | if (dir == OHCI_TD_DIR_IN) { | |
4d611c9a | 596 | ohci_copy_td(&td, ohci->usb_buf, ret, 1); |
0d92ed30 PB |
597 | #ifdef DEBUG_PACKET |
598 | dprintf(" data:"); | |
599 | for (i = 0; i < ret; i++) | |
4d611c9a | 600 | printf(" %.2x", ohci->usb_buf[i]); |
0d92ed30 PB |
601 | dprintf("\n"); |
602 | #endif | |
603 | } else { | |
604 | ret = len; | |
605 | } | |
606 | } | |
607 | ||
608 | /* Writeback */ | |
609 | if (ret == len || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) { | |
610 | /* Transmission succeeded. */ | |
611 | if (ret == len) { | |
612 | td.cbp = 0; | |
613 | } else { | |
614 | td.cbp += ret; | |
615 | if ((td.cbp & 0xfff) + ret > 0xfff) { | |
616 | td.cbp &= 0xfff; | |
617 | td.cbp |= td.be & ~0xfff; | |
618 | } | |
619 | } | |
620 | td.flags |= OHCI_TD_T1; | |
621 | td.flags ^= OHCI_TD_T0; | |
622 | OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR); | |
623 | OHCI_SET_BM(td.flags, TD_EC, 0); | |
624 | ||
625 | ed->head &= ~OHCI_ED_C; | |
626 | if (td.flags & OHCI_TD_T0) | |
627 | ed->head |= OHCI_ED_C; | |
628 | } else { | |
629 | if (ret >= 0) { | |
630 | dprintf("usb-ohci: Underrun\n"); | |
631 | OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN); | |
632 | } else { | |
633 | switch (ret) { | |
634 | case USB_RET_NODEV: | |
635 | OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING); | |
636 | case USB_RET_NAK: | |
637 | dprintf("usb-ohci: got NAK\n"); | |
638 | return 1; | |
639 | case USB_RET_STALL: | |
640 | dprintf("usb-ohci: got STALL\n"); | |
641 | OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL); | |
642 | break; | |
643 | case USB_RET_BABBLE: | |
644 | dprintf("usb-ohci: got BABBLE\n"); | |
645 | OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN); | |
646 | break; | |
647 | default: | |
648 | fprintf(stderr, "usb-ohci: Bad device response %d\n", ret); | |
649 | OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID); | |
650 | OHCI_SET_BM(td.flags, TD_EC, 3); | |
651 | break; | |
652 | } | |
653 | } | |
654 | ed->head |= OHCI_ED_H; | |
655 | } | |
656 | ||
657 | /* Retire this TD */ | |
658 | ed->head &= ~OHCI_DPTR_MASK; | |
659 | ed->head |= td.next & OHCI_DPTR_MASK; | |
660 | td.next = ohci->done; | |
661 | ohci->done = addr; | |
662 | i = OHCI_BM(td.flags, TD_DI); | |
663 | if (i < ohci->done_count) | |
664 | ohci->done_count = i; | |
665 | ohci_put_td(addr, &td); | |
666 | return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR; | |
667 | } | |
668 | ||
669 | /* Service an endpoint list. Returns nonzero if active TD were found. */ | |
670 | static int ohci_service_ed_list(OHCIState *ohci, uint32_t head) | |
671 | { | |
672 | struct ohci_ed ed; | |
673 | uint32_t next_ed; | |
674 | uint32_t cur; | |
675 | int active; | |
676 | ||
677 | active = 0; | |
678 | ||
679 | if (head == 0) | |
680 | return 0; | |
681 | ||
682 | for (cur = head; cur; cur = next_ed) { | |
683 | if (!ohci_read_ed(cur, &ed)) { | |
684 | fprintf(stderr, "usb-ohci: ED read error at %x\n", cur); | |
685 | return 0; | |
686 | } | |
687 | ||
688 | next_ed = ed.next & OHCI_DPTR_MASK; | |
689 | ||
4d611c9a PB |
690 | if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) { |
691 | uint32_t addr; | |
692 | /* Cancel pending packets for ED that have been paused. */ | |
693 | addr = ed.head & OHCI_DPTR_MASK; | |
694 | if (ohci->async_td && addr == ohci->async_td) { | |
695 | usb_cancel_packet(&ohci->usb_packet); | |
696 | ohci->async_td = 0; | |
697 | } | |
0d92ed30 | 698 | continue; |
4d611c9a | 699 | } |
0d92ed30 PB |
700 | |
701 | /* Skip isochronous endpoints. */ | |
702 | if (ed.flags & OHCI_ED_F) | |
703 | continue; | |
704 | ||
705 | while ((ed.head & OHCI_DPTR_MASK) != ed.tail) { | |
706 | #ifdef DEBUG_PACKET | |
707 | dprintf("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u " | |
708 | "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur, | |
709 | OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN), | |
710 | OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0, | |
711 | (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0, | |
712 | OHCI_BM(ed.flags, ED_MPS), (ed.head & OHCI_ED_H) != 0, | |
713 | (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK, | |
714 | ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK); | |
715 | #endif | |
716 | active = 1; | |
717 | ||
718 | if (ohci_service_td(ohci, &ed)) | |
719 | break; | |
720 | } | |
721 | ||
722 | ohci_put_ed(cur, &ed); | |
723 | } | |
724 | ||
725 | return active; | |
726 | } | |
727 | ||
728 | /* Generate a SOF event, and set a timer for EOF */ | |
729 | static void ohci_sof(OHCIState *ohci) | |
730 | { | |
731 | ohci->sof_time = qemu_get_clock(vm_clock); | |
732 | qemu_mod_timer(ohci->eof_timer, ohci->sof_time + usb_frame_time); | |
733 | ohci_set_interrupt(ohci, OHCI_INTR_SF); | |
734 | } | |
735 | ||
4d611c9a PB |
736 | /* Process Control and Bulk lists. */ |
737 | static void ohci_process_lists(OHCIState *ohci) | |
738 | { | |
739 | if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) { | |
740 | if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) | |
741 | dprintf("usb-ohci: head %x, cur %x\n", ohci->ctrl_head, ohci->ctrl_cur); | |
742 | if (!ohci_service_ed_list(ohci, ohci->ctrl_head)) { | |
743 | ohci->ctrl_cur = 0; | |
744 | ohci->status &= ~OHCI_STATUS_CLF; | |
745 | } | |
746 | } | |
747 | ||
748 | if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) { | |
749 | if (!ohci_service_ed_list(ohci, ohci->bulk_head)) { | |
750 | ohci->bulk_cur = 0; | |
751 | ohci->status &= ~OHCI_STATUS_BLF; | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
0d92ed30 PB |
756 | /* Do frame processing on frame boundary */ |
757 | static void ohci_frame_boundary(void *opaque) | |
758 | { | |
759 | OHCIState *ohci = opaque; | |
760 | struct ohci_hcca hcca; | |
761 | ||
762 | cpu_physical_memory_rw(ohci->hcca, (uint8_t *)&hcca, sizeof(hcca), 0); | |
763 | ||
764 | /* Process all the lists at the end of the frame */ | |
765 | if (ohci->ctl & OHCI_CTL_PLE) { | |
766 | int n; | |
767 | ||
768 | n = ohci->frame_number & 0x1f; | |
769 | ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n])); | |
770 | } | |
0d92ed30 | 771 | |
4d611c9a PB |
772 | /* Cancel all pending packets if either of the lists has been disabled. */ |
773 | if (ohci->async_td && | |
774 | ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) { | |
775 | usb_cancel_packet(&ohci->usb_packet); | |
776 | ohci->async_td = 0; | |
0d92ed30 | 777 | } |
4d611c9a PB |
778 | ohci->old_ctl = ohci->ctl; |
779 | ohci_process_lists(ohci); | |
0d92ed30 PB |
780 | |
781 | /* Frame boundary, so do EOF stuf here */ | |
782 | ohci->frt = ohci->fit; | |
783 | ||
784 | /* XXX: endianness */ | |
785 | ohci->frame_number = (ohci->frame_number + 1) & 0xffff; | |
786 | hcca.frame = cpu_to_le32(ohci->frame_number); | |
787 | ||
788 | if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) { | |
789 | if (!ohci->done) | |
790 | abort(); | |
791 | if (ohci->intr & ohci->intr_status) | |
792 | ohci->done |= 1; | |
793 | hcca.done = cpu_to_le32(ohci->done); | |
794 | ohci->done = 0; | |
795 | ohci->done_count = 7; | |
796 | ohci_set_interrupt(ohci, OHCI_INTR_WD); | |
797 | } | |
798 | ||
799 | if (ohci->done_count != 7 && ohci->done_count != 0) | |
800 | ohci->done_count--; | |
801 | ||
802 | /* Do SOF stuff here */ | |
803 | ohci_sof(ohci); | |
804 | ||
805 | /* Writeback HCCA */ | |
806 | cpu_physical_memory_rw(ohci->hcca, (uint8_t *)&hcca, sizeof(hcca), 1); | |
807 | } | |
808 | ||
809 | /* Start sending SOF tokens across the USB bus, lists are processed in | |
810 | * next frame | |
811 | */ | |
812 | static int ohci_bus_start(OHCIState *ohci) | |
813 | { | |
814 | ohci->eof_timer = qemu_new_timer(vm_clock, | |
815 | ohci_frame_boundary, | |
816 | ohci); | |
817 | ||
818 | if (ohci->eof_timer == NULL) { | |
e24ad6f1 | 819 | fprintf(stderr, "usb-ohci: %s: qemu_new_timer failed\n", ohci->name); |
0d92ed30 PB |
820 | /* TODO: Signal unrecoverable error */ |
821 | return 0; | |
822 | } | |
823 | ||
e24ad6f1 | 824 | dprintf("usb-ohci: %s: USB Operational\n", ohci->name); |
0d92ed30 PB |
825 | |
826 | ohci_sof(ohci); | |
827 | ||
828 | return 1; | |
829 | } | |
830 | ||
831 | /* Stop sending SOF tokens on the bus */ | |
832 | static void ohci_bus_stop(OHCIState *ohci) | |
833 | { | |
834 | if (ohci->eof_timer) | |
835 | qemu_del_timer(ohci->eof_timer); | |
836 | } | |
837 | ||
838 | /* Sets a flag in a port status register but only set it if the port is | |
839 | * connected, if not set ConnectStatusChange flag. If flag is enabled | |
840 | * return 1. | |
841 | */ | |
842 | static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val) | |
843 | { | |
844 | int ret = 1; | |
845 | ||
846 | /* writing a 0 has no effect */ | |
847 | if (val == 0) | |
848 | return 0; | |
849 | ||
850 | /* If CurrentConnectStatus is cleared we set | |
851 | * ConnectStatusChange | |
852 | */ | |
853 | if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) { | |
854 | ohci->rhport[i].ctrl |= OHCI_PORT_CSC; | |
855 | if (ohci->rhstatus & OHCI_RHS_DRWE) { | |
856 | /* TODO: CSC is a wakeup event */ | |
857 | } | |
858 | return 0; | |
859 | } | |
860 | ||
861 | if (ohci->rhport[i].ctrl & val) | |
862 | ret = 0; | |
863 | ||
864 | /* set the bit */ | |
865 | ohci->rhport[i].ctrl |= val; | |
866 | ||
867 | return ret; | |
868 | } | |
869 | ||
870 | /* Set the frame interval - frame interval toggle is manipulated by the hcd only */ | |
871 | static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val) | |
872 | { | |
873 | val &= OHCI_FMI_FI; | |
874 | ||
875 | if (val != ohci->fi) { | |
876 | dprintf("usb-ohci: %s: FrameInterval = 0x%x (%u)\n", | |
e24ad6f1 | 877 | ohci->name, ohci->fi, ohci->fi); |
0d92ed30 PB |
878 | } |
879 | ||
880 | ohci->fi = val; | |
881 | } | |
882 | ||
883 | static void ohci_port_power(OHCIState *ohci, int i, int p) | |
884 | { | |
885 | if (p) { | |
886 | ohci->rhport[i].ctrl |= OHCI_PORT_PPS; | |
887 | } else { | |
888 | ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS| | |
889 | OHCI_PORT_CCS| | |
890 | OHCI_PORT_PSS| | |
891 | OHCI_PORT_PRS); | |
892 | } | |
893 | } | |
894 | ||
895 | /* Set HcControlRegister */ | |
896 | static void ohci_set_ctl(OHCIState *ohci, uint32_t val) | |
897 | { | |
898 | uint32_t old_state; | |
899 | uint32_t new_state; | |
900 | ||
901 | old_state = ohci->ctl & OHCI_CTL_HCFS; | |
902 | ohci->ctl = val; | |
903 | new_state = ohci->ctl & OHCI_CTL_HCFS; | |
904 | ||
905 | /* no state change */ | |
906 | if (old_state == new_state) | |
907 | return; | |
908 | ||
909 | switch (new_state) { | |
910 | case OHCI_USB_OPERATIONAL: | |
911 | ohci_bus_start(ohci); | |
912 | break; | |
913 | case OHCI_USB_SUSPEND: | |
914 | ohci_bus_stop(ohci); | |
e24ad6f1 | 915 | dprintf("usb-ohci: %s: USB Suspended\n", ohci->name); |
0d92ed30 PB |
916 | break; |
917 | case OHCI_USB_RESUME: | |
e24ad6f1 | 918 | dprintf("usb-ohci: %s: USB Resume\n", ohci->name); |
0d92ed30 PB |
919 | break; |
920 | case OHCI_USB_RESET: | |
e24ad6f1 | 921 | dprintf("usb-ohci: %s: USB Reset\n", ohci->name); |
0d92ed30 PB |
922 | break; |
923 | } | |
924 | } | |
925 | ||
926 | static uint32_t ohci_get_frame_remaining(OHCIState *ohci) | |
927 | { | |
928 | uint16_t fr; | |
929 | int64_t tks; | |
930 | ||
931 | if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL) | |
932 | return (ohci->frt << 31); | |
933 | ||
934 | /* Being in USB operational state guarnatees sof_time was | |
935 | * set already. | |
936 | */ | |
937 | tks = qemu_get_clock(vm_clock) - ohci->sof_time; | |
938 | ||
939 | /* avoid muldiv if possible */ | |
940 | if (tks >= usb_frame_time) | |
941 | return (ohci->frt << 31); | |
942 | ||
943 | tks = muldiv64(1, tks, usb_bit_time); | |
944 | fr = (uint16_t)(ohci->fi - tks); | |
945 | ||
946 | return (ohci->frt << 31) | fr; | |
947 | } | |
948 | ||
949 | ||
950 | /* Set root hub status */ | |
951 | static void ohci_set_hub_status(OHCIState *ohci, uint32_t val) | |
952 | { | |
953 | uint32_t old_state; | |
954 | ||
955 | old_state = ohci->rhstatus; | |
956 | ||
957 | /* write 1 to clear OCIC */ | |
958 | if (val & OHCI_RHS_OCIC) | |
959 | ohci->rhstatus &= ~OHCI_RHS_OCIC; | |
960 | ||
961 | if (val & OHCI_RHS_LPS) { | |
962 | int i; | |
963 | ||
964 | for (i = 0; i < ohci->num_ports; i++) | |
965 | ohci_port_power(ohci, i, 0); | |
966 | dprintf("usb-ohci: powered down all ports\n"); | |
967 | } | |
968 | ||
969 | if (val & OHCI_RHS_LPSC) { | |
970 | int i; | |
971 | ||
972 | for (i = 0; i < ohci->num_ports; i++) | |
973 | ohci_port_power(ohci, i, 1); | |
974 | dprintf("usb-ohci: powered up all ports\n"); | |
975 | } | |
976 | ||
977 | if (val & OHCI_RHS_DRWE) | |
978 | ohci->rhstatus |= OHCI_RHS_DRWE; | |
979 | ||
980 | if (val & OHCI_RHS_CRWE) | |
981 | ohci->rhstatus &= ~OHCI_RHS_DRWE; | |
982 | ||
983 | if (old_state != ohci->rhstatus) | |
984 | ohci_set_interrupt(ohci, OHCI_INTR_RHSC); | |
985 | } | |
986 | ||
987 | /* Set root hub port status */ | |
988 | static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val) | |
989 | { | |
990 | uint32_t old_state; | |
991 | OHCIPort *port; | |
992 | ||
993 | port = &ohci->rhport[portnum]; | |
994 | old_state = port->ctrl; | |
995 | ||
996 | /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */ | |
997 | if (val & OHCI_PORT_WTC) | |
998 | port->ctrl &= ~(val & OHCI_PORT_WTC); | |
999 | ||
1000 | if (val & OHCI_PORT_CCS) | |
1001 | port->ctrl &= ~OHCI_PORT_PES; | |
1002 | ||
1003 | ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES); | |
1004 | ||
1005 | if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) | |
1006 | dprintf("usb-ohci: port %d: SUSPEND\n", portnum); | |
1007 | ||
1008 | if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) { | |
1009 | dprintf("usb-ohci: port %d: RESET\n", portnum); | |
4d611c9a | 1010 | usb_send_msg(port->port.dev, USB_MSG_RESET); |
0d92ed30 PB |
1011 | port->ctrl &= ~OHCI_PORT_PRS; |
1012 | /* ??? Should this also set OHCI_PORT_PESC. */ | |
1013 | port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC; | |
1014 | } | |
1015 | ||
1016 | /* Invert order here to ensure in ambiguous case, device is | |
1017 | * powered up... | |
1018 | */ | |
1019 | if (val & OHCI_PORT_LSDA) | |
1020 | ohci_port_power(ohci, portnum, 0); | |
1021 | if (val & OHCI_PORT_PPS) | |
1022 | ohci_port_power(ohci, portnum, 1); | |
1023 | ||
1024 | if (old_state != port->ctrl) | |
1025 | ohci_set_interrupt(ohci, OHCI_INTR_RHSC); | |
1026 | ||
1027 | return; | |
1028 | } | |
1029 | ||
1030 | static uint32_t ohci_mem_read(void *ptr, target_phys_addr_t addr) | |
1031 | { | |
1032 | OHCIState *ohci = ptr; | |
1033 | ||
1034 | addr -= ohci->mem_base; | |
1035 | ||
1036 | /* Only aligned reads are allowed on OHCI */ | |
1037 | if (addr & 3) { | |
1038 | fprintf(stderr, "usb-ohci: Mis-aligned read\n"); | |
1039 | return 0xffffffff; | |
1040 | } | |
1041 | ||
1042 | if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) { | |
1043 | /* HcRhPortStatus */ | |
1044 | return ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS; | |
1045 | } | |
1046 | ||
1047 | switch (addr >> 2) { | |
1048 | case 0: /* HcRevision */ | |
1049 | return 0x10; | |
1050 | ||
1051 | case 1: /* HcControl */ | |
1052 | return ohci->ctl; | |
1053 | ||
1054 | case 2: /* HcCommandStatus */ | |
1055 | return ohci->status; | |
1056 | ||
1057 | case 3: /* HcInterruptStatus */ | |
1058 | return ohci->intr_status; | |
1059 | ||
1060 | case 4: /* HcInterruptEnable */ | |
1061 | case 5: /* HcInterruptDisable */ | |
1062 | return ohci->intr; | |
1063 | ||
1064 | case 6: /* HcHCCA */ | |
1065 | return ohci->hcca; | |
1066 | ||
1067 | case 7: /* HcPeriodCurrentED */ | |
1068 | return ohci->per_cur; | |
1069 | ||
1070 | case 8: /* HcControlHeadED */ | |
1071 | return ohci->ctrl_head; | |
1072 | ||
1073 | case 9: /* HcControlCurrentED */ | |
1074 | return ohci->ctrl_cur; | |
1075 | ||
1076 | case 10: /* HcBulkHeadED */ | |
1077 | return ohci->bulk_head; | |
1078 | ||
1079 | case 11: /* HcBulkCurrentED */ | |
1080 | return ohci->bulk_cur; | |
1081 | ||
1082 | case 12: /* HcDoneHead */ | |
1083 | return ohci->done; | |
1084 | ||
1085 | case 13: /* HcFmInterval */ | |
1086 | return (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi); | |
1087 | ||
1088 | case 14: /* HcFmRemaining */ | |
1089 | return ohci_get_frame_remaining(ohci); | |
1090 | ||
1091 | case 15: /* HcFmNumber */ | |
1092 | return ohci->frame_number; | |
1093 | ||
1094 | case 16: /* HcPeriodicStart */ | |
1095 | return ohci->pstart; | |
1096 | ||
1097 | case 17: /* HcLSThreshold */ | |
1098 | return ohci->lst; | |
1099 | ||
1100 | case 18: /* HcRhDescriptorA */ | |
1101 | return ohci->rhdesc_a; | |
1102 | ||
1103 | case 19: /* HcRhDescriptorB */ | |
1104 | return ohci->rhdesc_b; | |
1105 | ||
1106 | case 20: /* HcRhStatus */ | |
1107 | return ohci->rhstatus; | |
1108 | ||
e24ad6f1 PB |
1109 | /* PXA27x specific registers */ |
1110 | case 24: /* HcStatus */ | |
1111 | return ohci->hstatus & ohci->hmask; | |
1112 | ||
1113 | case 25: /* HcHReset */ | |
1114 | return ohci->hreset; | |
1115 | ||
1116 | case 26: /* HcHInterruptEnable */ | |
1117 | return ohci->hmask; | |
1118 | ||
1119 | case 27: /* HcHInterruptTest */ | |
1120 | return ohci->htest; | |
1121 | ||
0d92ed30 PB |
1122 | default: |
1123 | fprintf(stderr, "ohci_read: Bad offset %x\n", (int)addr); | |
1124 | return 0xffffffff; | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | static void ohci_mem_write(void *ptr, target_phys_addr_t addr, uint32_t val) | |
1129 | { | |
1130 | OHCIState *ohci = ptr; | |
1131 | ||
1132 | addr -= ohci->mem_base; | |
1133 | ||
1134 | /* Only aligned reads are allowed on OHCI */ | |
1135 | if (addr & 3) { | |
1136 | fprintf(stderr, "usb-ohci: Mis-aligned write\n"); | |
1137 | return; | |
1138 | } | |
1139 | ||
1140 | if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) { | |
1141 | /* HcRhPortStatus */ | |
1142 | ohci_port_set_status(ohci, (addr - 0x54) >> 2, val); | |
1143 | return; | |
1144 | } | |
1145 | ||
1146 | switch (addr >> 2) { | |
1147 | case 1: /* HcControl */ | |
1148 | ohci_set_ctl(ohci, val); | |
1149 | break; | |
1150 | ||
1151 | case 2: /* HcCommandStatus */ | |
1152 | /* SOC is read-only */ | |
1153 | val = (val & ~OHCI_STATUS_SOC); | |
1154 | ||
1155 | /* Bits written as '0' remain unchanged in the register */ | |
1156 | ohci->status |= val; | |
1157 | ||
1158 | if (ohci->status & OHCI_STATUS_HCR) | |
1159 | ohci_reset(ohci); | |
1160 | break; | |
1161 | ||
1162 | case 3: /* HcInterruptStatus */ | |
1163 | ohci->intr_status &= ~val; | |
1164 | ohci_intr_update(ohci); | |
1165 | break; | |
1166 | ||
1167 | case 4: /* HcInterruptEnable */ | |
1168 | ohci->intr |= val; | |
1169 | ohci_intr_update(ohci); | |
1170 | break; | |
1171 | ||
1172 | case 5: /* HcInterruptDisable */ | |
1173 | ohci->intr &= ~val; | |
1174 | ohci_intr_update(ohci); | |
1175 | break; | |
1176 | ||
1177 | case 6: /* HcHCCA */ | |
1178 | ohci->hcca = val & OHCI_HCCA_MASK; | |
1179 | break; | |
1180 | ||
1181 | case 8: /* HcControlHeadED */ | |
1182 | ohci->ctrl_head = val & OHCI_EDPTR_MASK; | |
1183 | break; | |
1184 | ||
1185 | case 9: /* HcControlCurrentED */ | |
1186 | ohci->ctrl_cur = val & OHCI_EDPTR_MASK; | |
1187 | break; | |
1188 | ||
1189 | case 10: /* HcBulkHeadED */ | |
1190 | ohci->bulk_head = val & OHCI_EDPTR_MASK; | |
1191 | break; | |
1192 | ||
1193 | case 11: /* HcBulkCurrentED */ | |
1194 | ohci->bulk_cur = val & OHCI_EDPTR_MASK; | |
1195 | break; | |
1196 | ||
1197 | case 13: /* HcFmInterval */ | |
1198 | ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16; | |
1199 | ohci->fit = (val & OHCI_FMI_FIT) >> 31; | |
1200 | ohci_set_frame_interval(ohci, val); | |
1201 | break; | |
1202 | ||
1203 | case 16: /* HcPeriodicStart */ | |
1204 | ohci->pstart = val & 0xffff; | |
1205 | break; | |
1206 | ||
1207 | case 17: /* HcLSThreshold */ | |
1208 | ohci->lst = val & 0xffff; | |
1209 | break; | |
1210 | ||
1211 | case 18: /* HcRhDescriptorA */ | |
1212 | ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK; | |
1213 | ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK; | |
1214 | break; | |
1215 | ||
1216 | case 19: /* HcRhDescriptorB */ | |
1217 | break; | |
1218 | ||
1219 | case 20: /* HcRhStatus */ | |
1220 | ohci_set_hub_status(ohci, val); | |
1221 | break; | |
1222 | ||
e24ad6f1 PB |
1223 | /* PXA27x specific registers */ |
1224 | case 24: /* HcStatus */ | |
1225 | ohci->hstatus &= ~(val & ohci->hmask); | |
1226 | ||
1227 | case 25: /* HcHReset */ | |
1228 | ohci->hreset = val & ~OHCI_HRESET_FSBIR; | |
1229 | if (val & OHCI_HRESET_FSBIR) | |
1230 | ohci_reset(ohci); | |
1231 | break; | |
1232 | ||
1233 | case 26: /* HcHInterruptEnable */ | |
1234 | ohci->hmask = val; | |
1235 | break; | |
1236 | ||
1237 | case 27: /* HcHInterruptTest */ | |
1238 | ohci->htest = val; | |
1239 | break; | |
1240 | ||
0d92ed30 PB |
1241 | default: |
1242 | fprintf(stderr, "ohci_write: Bad offset %x\n", (int)addr); | |
1243 | break; | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | /* Only dword reads are defined on OHCI register space */ | |
1248 | static CPUReadMemoryFunc *ohci_readfn[3]={ | |
1249 | ohci_mem_read, | |
1250 | ohci_mem_read, | |
1251 | ohci_mem_read | |
1252 | }; | |
1253 | ||
1254 | /* Only dword writes are defined on OHCI register space */ | |
1255 | static CPUWriteMemoryFunc *ohci_writefn[3]={ | |
1256 | ohci_mem_write, | |
1257 | ohci_mem_write, | |
1258 | ohci_mem_write | |
1259 | }; | |
1260 | ||
e24ad6f1 | 1261 | static void usb_ohci_init(OHCIState *ohci, int num_ports, int devfn, |
d537cf6c | 1262 | qemu_irq irq, enum ohci_type type, const char *name) |
0d92ed30 | 1263 | { |
0d92ed30 PB |
1264 | int i; |
1265 | ||
0d92ed30 PB |
1266 | if (usb_frame_time == 0) { |
1267 | #if OHCI_TIME_WARP | |
1268 | usb_frame_time = ticks_per_sec; | |
1269 | usb_bit_time = muldiv64(1, ticks_per_sec, USB_HZ/1000); | |
1270 | #else | |
1271 | usb_frame_time = muldiv64(1, ticks_per_sec, 1000); | |
1272 | if (ticks_per_sec >= USB_HZ) { | |
1273 | usb_bit_time = muldiv64(1, ticks_per_sec, USB_HZ); | |
1274 | } else { | |
1275 | usb_bit_time = 1; | |
1276 | } | |
1277 | #endif | |
1278 | dprintf("usb-ohci: usb_bit_time=%lli usb_frame_time=%lli\n", | |
1279 | usb_frame_time, usb_bit_time); | |
1280 | } | |
1281 | ||
e24ad6f1 PB |
1282 | ohci->mem = cpu_register_io_memory(0, ohci_readfn, ohci_writefn, ohci); |
1283 | ohci->name = name; | |
1284 | ||
e24ad6f1 PB |
1285 | ohci->irq = irq; |
1286 | ohci->type = type; | |
1287 | ||
1288 | ohci->num_ports = num_ports; | |
1289 | for (i = 0; i < num_ports; i++) { | |
1290 | qemu_register_usb_port(&ohci->rhport[i].port, ohci, i, ohci_attach); | |
1291 | } | |
1292 | ||
1293 | ohci->async_td = 0; | |
1294 | ohci_reset(ohci); | |
1295 | } | |
1296 | ||
1297 | typedef struct { | |
1298 | PCIDevice pci_dev; | |
1299 | OHCIState state; | |
1300 | } OHCIPCIState; | |
1301 | ||
1302 | static void ohci_mapfunc(PCIDevice *pci_dev, int i, | |
1303 | uint32_t addr, uint32_t size, int type) | |
1304 | { | |
1305 | OHCIPCIState *ohci = (OHCIPCIState *)pci_dev; | |
1306 | ohci->state.mem_base = addr; | |
1307 | cpu_register_physical_memory(addr, size, ohci->state.mem); | |
1308 | } | |
1309 | ||
1310 | void usb_ohci_init_pci(struct PCIBus *bus, int num_ports, int devfn) | |
1311 | { | |
1312 | OHCIPCIState *ohci; | |
1313 | int vid = 0x106b; | |
1314 | int did = 0x003f; | |
1315 | ||
1316 | ohci = (OHCIPCIState *)pci_register_device(bus, "OHCI USB", sizeof(*ohci), | |
1317 | devfn, NULL, NULL); | |
0d92ed30 PB |
1318 | if (ohci == NULL) { |
1319 | fprintf(stderr, "usb-ohci: Failed to register PCI device\n"); | |
1320 | return; | |
1321 | } | |
1322 | ||
1323 | ohci->pci_dev.config[0x00] = vid & 0xff; | |
1324 | ohci->pci_dev.config[0x01] = (vid >> 8) & 0xff; | |
1325 | ohci->pci_dev.config[0x02] = did & 0xff; | |
1326 | ohci->pci_dev.config[0x03] = (did >> 8) & 0xff; | |
1327 | ohci->pci_dev.config[0x09] = 0x10; /* OHCI */ | |
1328 | ohci->pci_dev.config[0x0a] = 0x3; | |
1329 | ohci->pci_dev.config[0x0b] = 0xc; | |
1330 | ohci->pci_dev.config[0x3d] = 0x01; /* interrupt pin 1 */ | |
1331 | ||
d537cf6c PB |
1332 | usb_ohci_init(&ohci->state, num_ports, devfn, ohci->pci_dev.irq[0], |
1333 | OHCI_TYPE_PCI, ohci->pci_dev.name); | |
0d92ed30 PB |
1334 | |
1335 | pci_register_io_region((struct PCIDevice *)ohci, 0, 256, | |
1336 | PCI_ADDRESS_SPACE_MEM, ohci_mapfunc); | |
e24ad6f1 | 1337 | } |
0d92ed30 | 1338 | |
e24ad6f1 | 1339 | void usb_ohci_init_pxa(target_phys_addr_t base, int num_ports, int devfn, |
d537cf6c | 1340 | qemu_irq irq) |
e24ad6f1 PB |
1341 | { |
1342 | OHCIState *ohci = (OHCIState *)qemu_mallocz(sizeof(OHCIState)); | |
0d92ed30 | 1343 | |
d537cf6c | 1344 | usb_ohci_init(ohci, num_ports, devfn, irq, |
e24ad6f1 PB |
1345 | OHCI_TYPE_PXA, "OHCI USB"); |
1346 | ohci->mem_base = base; | |
1347 | ||
1348 | cpu_register_physical_memory(ohci->mem_base, 0xfff, ohci->mem); | |
0d92ed30 | 1349 | } |