2 * "Inventra" High-speed Dual-Role Controller (MUSB-HDRC), Mentor Graphics,
3 * USB2.0 OTG compliant core used in various chips.
5 * Copyright (C) 2008 Nokia Corporation
6 * Written by Andrzej Zaborowski <andrew@openedhand.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 or
11 * (at your option) version 3 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Only host-mode and non-DMA accesses are currently supported.
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
29 /* Common USB registers */
30 #define MUSB_HDRC_FADDR 0x00 /* 8-bit */
31 #define MUSB_HDRC_POWER 0x01 /* 8-bit */
33 #define MUSB_HDRC_INTRTX 0x02 /* 16-bit */
34 #define MUSB_HDRC_INTRRX 0x04
35 #define MUSB_HDRC_INTRTXE 0x06
36 #define MUSB_HDRC_INTRRXE 0x08
37 #define MUSB_HDRC_INTRUSB 0x0a /* 8 bit */
38 #define MUSB_HDRC_INTRUSBE 0x0b /* 8 bit */
39 #define MUSB_HDRC_FRAME 0x0c /* 16-bit */
40 #define MUSB_HDRC_INDEX 0x0e /* 8 bit */
41 #define MUSB_HDRC_TESTMODE 0x0f /* 8 bit */
43 /* Per-EP registers in indexed mode */
44 #define MUSB_HDRC_EP_IDX 0x10 /* 8-bit */
47 #define MUSB_HDRC_FIFO 0x20
49 /* Additional Control Registers */
50 #define MUSB_HDRC_DEVCTL 0x60 /* 8 bit */
52 /* These are indexed */
53 #define MUSB_HDRC_TXFIFOSZ 0x62 /* 8 bit (see masks) */
54 #define MUSB_HDRC_RXFIFOSZ 0x63 /* 8 bit (see masks) */
55 #define MUSB_HDRC_TXFIFOADDR 0x64 /* 16 bit offset shifted right 3 */
56 #define MUSB_HDRC_RXFIFOADDR 0x66 /* 16 bit offset shifted right 3 */
58 /* Some more registers */
59 #define MUSB_HDRC_VCTRL 0x68 /* 8 bit */
60 #define MUSB_HDRC_HWVERS 0x6c /* 8 bit */
62 /* Added in HDRC 1.9(?) & MHDRC 1.4 */
63 /* ULPI pass-through */
64 #define MUSB_HDRC_ULPI_VBUSCTL 0x70
65 #define MUSB_HDRC_ULPI_REGDATA 0x74
66 #define MUSB_HDRC_ULPI_REGADDR 0x75
67 #define MUSB_HDRC_ULPI_REGCTL 0x76
69 /* Extended config & PHY control */
70 #define MUSB_HDRC_ENDCOUNT 0x78 /* 8 bit */
71 #define MUSB_HDRC_DMARAMCFG 0x79 /* 8 bit */
72 #define MUSB_HDRC_PHYWAIT 0x7a /* 8 bit */
73 #define MUSB_HDRC_PHYVPLEN 0x7b /* 8 bit */
74 #define MUSB_HDRC_HS_EOF1 0x7c /* 8 bit, units of 546.1 us */
75 #define MUSB_HDRC_FS_EOF1 0x7d /* 8 bit, units of 533.3 ns */
76 #define MUSB_HDRC_LS_EOF1 0x7e /* 8 bit, units of 1.067 us */
78 /* Per-EP BUSCTL registers */
79 #define MUSB_HDRC_BUSCTL 0x80
81 /* Per-EP registers in flat mode */
82 #define MUSB_HDRC_EP 0x100
84 /* offsets to registers in flat model */
85 #define MUSB_HDRC_TXMAXP 0x00 /* 16 bit apparently */
86 #define MUSB_HDRC_TXCSR 0x02 /* 16 bit apparently */
87 #define MUSB_HDRC_CSR0 MUSB_HDRC_TXCSR /* re-used for EP0 */
88 #define MUSB_HDRC_RXMAXP 0x04 /* 16 bit apparently */
89 #define MUSB_HDRC_RXCSR 0x06 /* 16 bit apparently */
90 #define MUSB_HDRC_RXCOUNT 0x08 /* 16 bit apparently */
91 #define MUSB_HDRC_COUNT0 MUSB_HDRC_RXCOUNT /* re-used for EP0 */
92 #define MUSB_HDRC_TXTYPE 0x0a /* 8 bit apparently */
93 #define MUSB_HDRC_TYPE0 MUSB_HDRC_TXTYPE /* re-used for EP0 */
94 #define MUSB_HDRC_TXINTERVAL 0x0b /* 8 bit apparently */
95 #define MUSB_HDRC_NAKLIMIT0 MUSB_HDRC_TXINTERVAL /* re-used for EP0 */
96 #define MUSB_HDRC_RXTYPE 0x0c /* 8 bit apparently */
97 #define MUSB_HDRC_RXINTERVAL 0x0d /* 8 bit apparently */
98 #define MUSB_HDRC_FIFOSIZE 0x0f /* 8 bit apparently */
99 #define MUSB_HDRC_CONFIGDATA MGC_O_HDRC_FIFOSIZE /* re-used for EP0 */
101 /* "Bus control" registers */
102 #define MUSB_HDRC_TXFUNCADDR 0x00
103 #define MUSB_HDRC_TXHUBADDR 0x02
104 #define MUSB_HDRC_TXHUBPORT 0x03
106 #define MUSB_HDRC_RXFUNCADDR 0x04
107 #define MUSB_HDRC_RXHUBADDR 0x06
108 #define MUSB_HDRC_RXHUBPORT 0x07
111 * MUSBHDRC Register bit masks
115 #define MGC_M_POWER_ISOUPDATE 0x80
116 #define MGC_M_POWER_SOFTCONN 0x40
117 #define MGC_M_POWER_HSENAB 0x20
118 #define MGC_M_POWER_HSMODE 0x10
119 #define MGC_M_POWER_RESET 0x08
120 #define MGC_M_POWER_RESUME 0x04
121 #define MGC_M_POWER_SUSPENDM 0x02
122 #define MGC_M_POWER_ENSUSPEND 0x01
125 #define MGC_M_INTR_SUSPEND 0x01
126 #define MGC_M_INTR_RESUME 0x02
127 #define MGC_M_INTR_RESET 0x04
128 #define MGC_M_INTR_BABBLE 0x04
129 #define MGC_M_INTR_SOF 0x08
130 #define MGC_M_INTR_CONNECT 0x10
131 #define MGC_M_INTR_DISCONNECT 0x20
132 #define MGC_M_INTR_SESSREQ 0x40
133 #define MGC_M_INTR_VBUSERROR 0x80 /* FOR SESSION END */
134 #define MGC_M_INTR_EP0 0x01 /* FOR EP0 INTERRUPT */
137 #define MGC_M_DEVCTL_BDEVICE 0x80
138 #define MGC_M_DEVCTL_FSDEV 0x40
139 #define MGC_M_DEVCTL_LSDEV 0x20
140 #define MGC_M_DEVCTL_VBUS 0x18
141 #define MGC_S_DEVCTL_VBUS 3
142 #define MGC_M_DEVCTL_HM 0x04
143 #define MGC_M_DEVCTL_HR 0x02
144 #define MGC_M_DEVCTL_SESSION 0x01
147 #define MGC_M_TEST_FORCE_HOST 0x80
148 #define MGC_M_TEST_FIFO_ACCESS 0x40
149 #define MGC_M_TEST_FORCE_FS 0x20
150 #define MGC_M_TEST_FORCE_HS 0x10
151 #define MGC_M_TEST_PACKET 0x08
152 #define MGC_M_TEST_K 0x04
153 #define MGC_M_TEST_J 0x02
154 #define MGC_M_TEST_SE0_NAK 0x01
157 #define MGC_M_CSR0_FLUSHFIFO 0x0100
158 #define MGC_M_CSR0_TXPKTRDY 0x0002
159 #define MGC_M_CSR0_RXPKTRDY 0x0001
161 /* CSR0 in Peripheral mode */
162 #define MGC_M_CSR0_P_SVDSETUPEND 0x0080
163 #define MGC_M_CSR0_P_SVDRXPKTRDY 0x0040
164 #define MGC_M_CSR0_P_SENDSTALL 0x0020
165 #define MGC_M_CSR0_P_SETUPEND 0x0010
166 #define MGC_M_CSR0_P_DATAEND 0x0008
167 #define MGC_M_CSR0_P_SENTSTALL 0x0004
169 /* CSR0 in Host mode */
170 #define MGC_M_CSR0_H_NO_PING 0x0800
171 #define MGC_M_CSR0_H_WR_DATATOGGLE 0x0400 /* set to allow setting: */
172 #define MGC_M_CSR0_H_DATATOGGLE 0x0200 /* data toggle control */
173 #define MGC_M_CSR0_H_NAKTIMEOUT 0x0080
174 #define MGC_M_CSR0_H_STATUSPKT 0x0040
175 #define MGC_M_CSR0_H_REQPKT 0x0020
176 #define MGC_M_CSR0_H_ERROR 0x0010
177 #define MGC_M_CSR0_H_SETUPPKT 0x0008
178 #define MGC_M_CSR0_H_RXSTALL 0x0004
181 #define MGC_M_CONFIGDATA_MPRXE 0x80 /* auto bulk pkt combining */
182 #define MGC_M_CONFIGDATA_MPTXE 0x40 /* auto bulk pkt splitting */
183 #define MGC_M_CONFIGDATA_BIGENDIAN 0x20
184 #define MGC_M_CONFIGDATA_HBRXE 0x10 /* HB-ISO for RX */
185 #define MGC_M_CONFIGDATA_HBTXE 0x08 /* HB-ISO for TX */
186 #define MGC_M_CONFIGDATA_DYNFIFO 0x04 /* dynamic FIFO sizing */
187 #define MGC_M_CONFIGDATA_SOFTCONE 0x02 /* SoftConnect */
188 #define MGC_M_CONFIGDATA_UTMIDW 0x01 /* Width, 0 => 8b, 1 => 16b */
190 /* TXCSR in Peripheral and Host mode */
191 #define MGC_M_TXCSR_AUTOSET 0x8000
192 #define MGC_M_TXCSR_ISO 0x4000
193 #define MGC_M_TXCSR_MODE 0x2000
194 #define MGC_M_TXCSR_DMAENAB 0x1000
195 #define MGC_M_TXCSR_FRCDATATOG 0x0800
196 #define MGC_M_TXCSR_DMAMODE 0x0400
197 #define MGC_M_TXCSR_CLRDATATOG 0x0040
198 #define MGC_M_TXCSR_FLUSHFIFO 0x0008
199 #define MGC_M_TXCSR_FIFONOTEMPTY 0x0002
200 #define MGC_M_TXCSR_TXPKTRDY 0x0001
202 /* TXCSR in Peripheral mode */
203 #define MGC_M_TXCSR_P_INCOMPTX 0x0080
204 #define MGC_M_TXCSR_P_SENTSTALL 0x0020
205 #define MGC_M_TXCSR_P_SENDSTALL 0x0010
206 #define MGC_M_TXCSR_P_UNDERRUN 0x0004
208 /* TXCSR in Host mode */
209 #define MGC_M_TXCSR_H_WR_DATATOGGLE 0x0200
210 #define MGC_M_TXCSR_H_DATATOGGLE 0x0100
211 #define MGC_M_TXCSR_H_NAKTIMEOUT 0x0080
212 #define MGC_M_TXCSR_H_RXSTALL 0x0020
213 #define MGC_M_TXCSR_H_ERROR 0x0004
215 /* RXCSR in Peripheral and Host mode */
216 #define MGC_M_RXCSR_AUTOCLEAR 0x8000
217 #define MGC_M_RXCSR_DMAENAB 0x2000
218 #define MGC_M_RXCSR_DISNYET 0x1000
219 #define MGC_M_RXCSR_DMAMODE 0x0800
220 #define MGC_M_RXCSR_INCOMPRX 0x0100
221 #define MGC_M_RXCSR_CLRDATATOG 0x0080
222 #define MGC_M_RXCSR_FLUSHFIFO 0x0010
223 #define MGC_M_RXCSR_DATAERROR 0x0008
224 #define MGC_M_RXCSR_FIFOFULL 0x0002
225 #define MGC_M_RXCSR_RXPKTRDY 0x0001
227 /* RXCSR in Peripheral mode */
228 #define MGC_M_RXCSR_P_ISO 0x4000
229 #define MGC_M_RXCSR_P_SENTSTALL 0x0040
230 #define MGC_M_RXCSR_P_SENDSTALL 0x0020
231 #define MGC_M_RXCSR_P_OVERRUN 0x0004
233 /* RXCSR in Host mode */
234 #define MGC_M_RXCSR_H_AUTOREQ 0x4000
235 #define MGC_M_RXCSR_H_WR_DATATOGGLE 0x0400
236 #define MGC_M_RXCSR_H_DATATOGGLE 0x0200
237 #define MGC_M_RXCSR_H_RXSTALL 0x0040
238 #define MGC_M_RXCSR_H_REQPKT 0x0020
239 #define MGC_M_RXCSR_H_ERROR 0x0004
242 #define MGC_M_HUBADDR_MULTI_TT 0x80
244 /* ULPI: Added in HDRC 1.9(?) & MHDRC 1.4 */
245 #define MGC_M_ULPI_VBCTL_USEEXTVBUSIND 0x02
246 #define MGC_M_ULPI_VBCTL_USEEXTVBUS 0x01
247 #define MGC_M_ULPI_REGCTL_INT_ENABLE 0x08
248 #define MGC_M_ULPI_REGCTL_READNOTWRITE 0x04
249 #define MGC_M_ULPI_REGCTL_COMPLETE 0x02
250 #define MGC_M_ULPI_REGCTL_REG 0x01
252 static void musb_attach(USBPort *port, USBDevice *dev);
273 uint32_t buf[0x2000];
286 int timeout[2]; /* Always in microframes */
296 /* For callbacks' use */
300 USBCallback *delayed_cb[2];
301 QEMUTimer *intv_timer[2];
302 /* Duplicating the world since 2008!... probably we should have 32
303 * logical, single endpoints instead. */
305 } *musb_init(qemu_irq *irqs)
307 struct musb_s *s = qemu_mallocz(sizeof(*s));
313 s->power = MGC_M_POWER_HSENAB;
323 s->ep[0].config = MGC_M_CONFIGDATA_SOFTCONE | MGC_M_CONFIGDATA_DYNFIFO;
324 for (i = 0; i < 16; i ++) {
325 s->ep[i].fifosize = 64;
326 s->ep[i].maxp[0] = 0x40;
327 s->ep[i].maxp[1] = 0x40;
332 qemu_register_usb_port(&s->port, s, 0, musb_attach);
337 static void musb_vbus_set(struct musb_s *s, int level)
340 s->devctl |= 3 << MGC_S_DEVCTL_VBUS;
342 s->devctl &= ~MGC_M_DEVCTL_VBUS;
344 qemu_set_irq(s->irqs[musb_set_vbus], level);
347 static void musb_intr_set(struct musb_s *s, int line, int level)
350 s->intr &= ~(1 << line);
351 qemu_irq_lower(s->irqs[line]);
352 } else if (s->mask & (1 << line)) {
353 s->intr |= 1 << line;
354 qemu_irq_raise(s->irqs[line]);
358 static void musb_tx_intr_set(struct musb_s *s, int line, int level)
361 s->tx_intr &= ~(1 << line);
363 qemu_irq_lower(s->irqs[musb_irq_tx]);
364 } else if (s->tx_mask & (1 << line)) {
365 s->tx_intr |= 1 << line;
366 qemu_irq_raise(s->irqs[musb_irq_tx]);
370 static void musb_rx_intr_set(struct musb_s *s, int line, int level)
374 s->rx_intr &= ~(1 << line);
376 qemu_irq_lower(s->irqs[musb_irq_rx]);
377 } else if (s->rx_mask & (1 << line)) {
378 s->rx_intr |= 1 << line;
379 qemu_irq_raise(s->irqs[musb_irq_rx]);
382 musb_tx_intr_set(s, line, level);
385 uint32_t musb_core_intr_get(struct musb_s *s)
387 return (s->rx_intr << 15) | s->tx_intr;
390 void musb_core_intr_clear(struct musb_s *s, uint32_t mask)
393 s->rx_intr &= mask >> 15;
395 qemu_irq_lower(s->irqs[musb_irq_rx]);
399 s->tx_intr &= mask & 0xffff;
401 qemu_irq_lower(s->irqs[musb_irq_tx]);
405 void musb_set_size(struct musb_s *s, int epnum, int size, int is_tx)
407 s->ep[epnum].ext_size[!is_tx] = size;
408 s->ep[epnum].fifostart[0] = 0;
409 s->ep[epnum].fifostart[1] = 0;
410 s->ep[epnum].fifolen[0] = 0;
411 s->ep[epnum].fifolen[1] = 0;
414 static void musb_session_update(struct musb_s *s, int prev_dev, int prev_sess)
416 int detect_prev = prev_dev && prev_sess;
417 int detect = !!s->port.dev && s->session;
419 if (detect && !detect_prev) {
420 /* Let's skip the ID pin sense and VBUS sense formalities and
421 * and signal a successful SRP directly. This should work at least
422 * for the Linux driver stack. */
423 musb_intr_set(s, musb_irq_connect, 1);
425 if (s->port.dev->speed == USB_SPEED_LOW) {
426 s->devctl &= ~MGC_M_DEVCTL_FSDEV;
427 s->devctl |= MGC_M_DEVCTL_LSDEV;
429 s->devctl |= MGC_M_DEVCTL_FSDEV;
430 s->devctl &= ~MGC_M_DEVCTL_LSDEV;
434 s->devctl &= ~MGC_M_DEVCTL_BDEVICE;
437 s->devctl |= MGC_M_DEVCTL_HM;
441 } else if (!detect && detect_prev) {
448 /* Attach or detach a device on our only port. */
449 static void musb_attach(USBPort *port, USBDevice *dev)
451 struct musb_s *s = (struct musb_s *) port->opaque;
459 usb_attach(port, NULL);
460 /* TODO: signal some interrupts */
463 musb_intr_set(s, musb_irq_vbus_request, 1);
465 /* Send the attach message to device */
466 usb_send_msg(dev, USB_MSG_ATTACH);
468 /* Send the detach message */
469 usb_send_msg(curr, USB_MSG_DETACH);
471 musb_intr_set(s, musb_irq_disconnect, 1);
476 musb_session_update(s, !!curr, s->session);
479 static inline void musb_cb_tick0(void *opaque)
481 struct musb_ep_s *ep = (struct musb_ep_s *) opaque;
483 ep->delayed_cb[0](&ep->packey[0], opaque);
486 static inline void musb_cb_tick1(void *opaque)
488 struct musb_ep_s *ep = (struct musb_ep_s *) opaque;
490 ep->delayed_cb[1](&ep->packey[1], opaque);
493 #define musb_cb_tick (dir ? musb_cb_tick1 : musb_cb_tick0)
495 static inline void musb_schedule_cb(USBPacket *packey, void *opaque, int dir)
497 struct musb_ep_s *ep = (struct musb_ep_s *) opaque;
500 if (ep->status[dir] == USB_RET_NAK)
501 timeout = ep->timeout[dir];
502 else if (ep->interrupt[dir])
505 return musb_cb_tick(opaque);
507 if (!ep->intv_timer[dir])
508 ep->intv_timer[dir] = qemu_new_timer(vm_clock, musb_cb_tick, opaque);
510 qemu_mod_timer(ep->intv_timer[dir], qemu_get_clock(vm_clock) +
511 muldiv64(timeout, ticks_per_sec, 8000));
514 static void musb_schedule0_cb(USBPacket *packey, void *opaque)
516 return musb_schedule_cb(packey, opaque, 0);
519 static void musb_schedule1_cb(USBPacket *packey, void *opaque)
521 return musb_schedule_cb(packey, opaque, 1);
524 static int musb_timeout(int ttype, int speed, int val)
531 case USB_ENDPOINT_XFER_CONTROL:
534 else if (speed == USB_SPEED_HIGH)
535 return 1 << (val - 1);
537 return 8 << (val - 1);
539 case USB_ENDPOINT_XFER_INT:
540 if (speed == USB_SPEED_HIGH)
544 return 1 << (val - 1);
548 case USB_ENDPOINT_XFER_BULK:
549 case USB_ENDPOINT_XFER_ISOC:
552 else if (speed == USB_SPEED_HIGH)
553 return 1 << (val - 1);
555 return 8 << (val - 1);
556 /* TODO: what with low-speed Bulk and Isochronous? */
559 cpu_abort(cpu_single_env, "bad interval\n");
562 static inline void musb_packet(struct musb_s *s, struct musb_ep_s *ep,
563 int epnum, int pid, int len, USBCallback cb, int dir)
566 int idx = epnum && dir;
569 /* ep->type[0,1] contains:
570 * in bits 7:6 the speed (0 - invalid, 1 - high, 2 - full, 3 - slow)
571 * in bits 5:4 the transfer type (BULK / INT)
572 * in bits 3:0 the EP num
574 ttype = epnum ? (ep->type[idx] >> 4) & 3 : 0;
576 ep->timeout[dir] = musb_timeout(ttype,
577 ep->type[idx] >> 6, ep->interval[idx]);
578 ep->interrupt[dir] = ttype == USB_ENDPOINT_XFER_INT;
579 ep->delayed_cb[dir] = cb;
580 cb = dir ? musb_schedule1_cb : musb_schedule0_cb;
582 ep->packey[dir].pid = pid;
583 /* A wild guess on the FADDR semantics... */
584 ep->packey[dir].devaddr = ep->faddr[idx];
585 ep->packey[dir].devep = ep->type[idx] & 0xf;
586 ep->packey[dir].data = (void *) ep->buf[idx];
587 ep->packey[dir].len = len;
588 ep->packey[dir].complete_cb = cb;
589 ep->packey[dir].complete_opaque = ep;
592 ret = s->port.dev->handle_packet(s->port.dev, &ep->packey[dir]);
596 if (ret == USB_RET_ASYNC) {
597 ep->status[dir] = len;
601 ep->status[dir] = ret;
602 usb_packet_complete(&ep->packey[dir]);
605 static void musb_tx_packet_complete(USBPacket *packey, void *opaque)
607 /* Unfortunately we can't use packey->devep because that's the remote
608 * endpoint number and may be different than our local. */
609 struct musb_ep_s *ep = (struct musb_ep_s *) opaque;
610 int epnum = ep->epnum;
611 struct musb_s *s = ep->musb;
613 ep->fifostart[0] = 0;
616 if (ep->status[0] != USB_RET_NAK) {
619 ep->csr[0] &= ~(MGC_M_TXCSR_FIFONOTEMPTY | MGC_M_TXCSR_TXPKTRDY);
621 ep->csr[0] &= ~MGC_M_CSR0_TXPKTRDY;
626 /* Clear all of the error bits first */
628 ep->csr[0] &= ~(MGC_M_TXCSR_H_ERROR | MGC_M_TXCSR_H_RXSTALL |
629 MGC_M_TXCSR_H_NAKTIMEOUT);
631 ep->csr[0] &= ~(MGC_M_CSR0_H_ERROR | MGC_M_CSR0_H_RXSTALL |
632 MGC_M_CSR0_H_NAKTIMEOUT | MGC_M_CSR0_H_NO_PING);
634 if (ep->status[0] == USB_RET_STALL) {
635 /* Command not supported by target! */
639 ep->csr[0] |= MGC_M_TXCSR_H_RXSTALL;
641 ep->csr[0] |= MGC_M_CSR0_H_RXSTALL;
644 if (ep->status[0] == USB_RET_NAK) {
647 /* NAK timeouts are only generated in Bulk transfers and
648 * Data-errors in Isochronous. */
649 if (ep->interrupt[0]) {
654 ep->csr[0] |= MGC_M_TXCSR_H_NAKTIMEOUT;
656 ep->csr[0] |= MGC_M_CSR0_H_NAKTIMEOUT;
659 if (ep->status[0] < 0) {
660 if (ep->status[0] == USB_RET_BABBLE)
661 musb_intr_set(s, musb_irq_rst_babble, 1);
663 /* Pretend we've tried three times already and failed (in
664 * case of USB_TOKEN_SETUP). */
666 ep->csr[0] |= MGC_M_TXCSR_H_ERROR;
668 ep->csr[0] |= MGC_M_CSR0_H_ERROR;
670 musb_tx_intr_set(s, epnum, 1);
673 /* TODO: check len for over/underruns of an OUT packet? */
676 if (!epnum && ep->packey[0].pid == USB_TOKEN_SETUP)
677 s->setup_len = ep->packey[0].data[6];
680 /* In DMA mode: if no error, assert DMA request for this EP,
681 * and skip the interrupt. */
682 musb_tx_intr_set(s, epnum, 1);
685 static void musb_rx_packet_complete(USBPacket *packey, void *opaque)
687 /* Unfortunately we can't use packey->devep because that's the remote
688 * endpoint number and may be different than our local. */
689 struct musb_ep_s *ep = (struct musb_ep_s *) opaque;
690 int epnum = ep->epnum;
691 struct musb_s *s = ep->musb;
693 ep->fifostart[1] = 0;
697 if (ep->status[1] != USB_RET_NAK) {
699 ep->csr[1] &= ~MGC_M_RXCSR_H_REQPKT;
701 ep->csr[0] &= ~MGC_M_CSR0_H_REQPKT;
706 /* Clear all of the imaginable error bits first */
707 ep->csr[1] &= ~(MGC_M_RXCSR_H_ERROR | MGC_M_RXCSR_H_RXSTALL |
708 MGC_M_RXCSR_DATAERROR);
710 ep->csr[0] &= ~(MGC_M_CSR0_H_ERROR | MGC_M_CSR0_H_RXSTALL |
711 MGC_M_CSR0_H_NAKTIMEOUT | MGC_M_CSR0_H_NO_PING);
713 if (ep->status[1] == USB_RET_STALL) {
717 ep->csr[1] |= MGC_M_RXCSR_H_RXSTALL;
719 ep->csr[0] |= MGC_M_CSR0_H_RXSTALL;
722 if (ep->status[1] == USB_RET_NAK) {
725 /* NAK timeouts are only generated in Bulk transfers and
726 * Data-errors in Isochronous. */
727 if (ep->interrupt[1])
728 return musb_packet(s, ep, epnum, USB_TOKEN_IN,
729 packey->len, musb_rx_packet_complete, 1);
731 ep->csr[1] |= MGC_M_RXCSR_DATAERROR;
733 ep->csr[0] |= MGC_M_CSR0_H_NAKTIMEOUT;
736 if (ep->status[1] < 0) {
737 if (ep->status[1] == USB_RET_BABBLE) {
738 musb_intr_set(s, musb_irq_rst_babble, 1);
742 /* Pretend we've tried three times already and failed (in
743 * case of a control transfer). */
744 ep->csr[1] |= MGC_M_RXCSR_H_ERROR;
746 ep->csr[0] |= MGC_M_CSR0_H_ERROR;
748 musb_rx_intr_set(s, epnum, 1);
751 /* TODO: check len for over/underruns of an OUT packet? */
752 /* TODO: perhaps make use of e->ext_size[1] here. */
754 packey->len = ep->status[1];
756 if (!(ep->csr[1] & (MGC_M_RXCSR_H_RXSTALL | MGC_M_RXCSR_DATAERROR))) {
757 ep->csr[1] |= MGC_M_RXCSR_FIFOFULL | MGC_M_RXCSR_RXPKTRDY;
759 ep->csr[0] |= MGC_M_CSR0_RXPKTRDY;
761 ep->rxcount = packey->len; /* XXX: MIN(packey->len, ep->maxp[1]); */
762 /* In DMA mode: assert DMA request for this EP */
765 /* Only if DMA has not been asserted */
766 musb_rx_intr_set(s, epnum, 1);
769 static void musb_tx_rdy(struct musb_s *s, int epnum)
771 struct musb_ep_s *ep = s->ep + epnum;
773 int total, valid = 0;
775 ep->fifostart[0] += ep->fifolen[0];
778 /* XXX: how's the total size of the packet retrieved exactly in
779 * the generic case? */
780 total = ep->maxp[0] & 0x3ff;
782 if (ep->ext_size[0]) {
783 total = ep->ext_size[0];
788 /* If the packet is not fully ready yet, wait for a next segment. */
789 if (epnum && (ep->fifostart[0] << 2) < total)
793 total = ep->fifostart[0] << 2;
796 if (!epnum && (ep->csr[0] & MGC_M_CSR0_H_SETUPPKT)) {
797 pid = USB_TOKEN_SETUP;
799 printf("%s: illegal SETUPPKT length of %i bytes\n",
800 __FUNCTION__, total);
801 /* Controller should retry SETUP packets three times on errors
802 * but it doesn't make sense for us to do that. */
805 return musb_packet(s, ep, epnum, pid,
806 total, musb_tx_packet_complete, 0);
809 static void musb_rx_req(struct musb_s *s, int epnum)
811 struct musb_ep_s *ep = s->ep + epnum;
814 /* If we already have a packet, which didn't fit into the
815 * 64 bytes of the FIFO, only move the FIFO start and return. (Obsolete) */
816 if (ep->packey[1].pid == USB_TOKEN_IN && ep->status[1] >= 0 &&
817 (ep->fifostart[1] << 2) + ep->rxcount <
819 ep->fifostart[1] += ep->rxcount >> 2;
822 ep->rxcount = MIN(ep->packey[0].len - (ep->fifostart[1] << 2),
825 ep->csr[1] &= ~MGC_M_RXCSR_H_REQPKT;
827 ep->csr[0] &= ~MGC_M_CSR0_H_REQPKT;
829 /* Clear all of the error bits first */
830 ep->csr[1] &= ~(MGC_M_RXCSR_H_ERROR | MGC_M_RXCSR_H_RXSTALL |
831 MGC_M_RXCSR_DATAERROR);
833 ep->csr[0] &= ~(MGC_M_CSR0_H_ERROR | MGC_M_CSR0_H_RXSTALL |
834 MGC_M_CSR0_H_NAKTIMEOUT | MGC_M_CSR0_H_NO_PING);
836 ep->csr[1] |= MGC_M_RXCSR_FIFOFULL | MGC_M_RXCSR_RXPKTRDY;
838 ep->csr[0] |= MGC_M_CSR0_RXPKTRDY;
839 musb_rx_intr_set(s, epnum, 1);
843 /* The driver sets maxp[1] to 64 or less because it knows the hardware
844 * FIFO is this deep. Bigger packets get split in
845 * usb_generic_handle_packet but we can also do the splitting locally
846 * for performance. It turns out we can also have a bigger FIFO and
847 * ignore the limit set in ep->maxp[1]. The Linux MUSB driver deals
848 * OK with single packets of even 32KB and we avoid splitting, however
849 * usb_msd.c sometimes sends a packet bigger than what Linux expects
850 * (e.g. 8192 bytes instead of 4096) and we get an OVERRUN. Splitting
851 * hides this overrun from Linux. Up to 4096 everything is fine
852 * though. Currently this is disabled.
854 * XXX: mind ep->fifosize. */
855 total = MIN(ep->maxp[1] & 0x3ff, sizeof(s->buf));
858 /* Why should *we* do that instead of Linux? */
860 if (ep->packey[0].devaddr == 2)
861 total = MIN(s->setup_len, 8);
863 total = MIN(s->setup_len, 64);
864 s->setup_len -= total;
868 return musb_packet(s, ep, epnum, USB_TOKEN_IN,
869 total, musb_rx_packet_complete, 1);
872 static void musb_ep_frame_cancel(struct musb_ep_s *ep, int dir)
874 if (ep->intv_timer[dir])
875 qemu_del_timer(ep->intv_timer[dir]);
879 static uint8_t musb_busctl_readb(void *opaque, int ep, int addr)
881 struct musb_s *s = (struct musb_s *) opaque;
884 /* For USB2.0 HS hubs only */
885 case MUSB_HDRC_TXHUBADDR:
886 return s->ep[ep].haddr[0];
887 case MUSB_HDRC_TXHUBPORT:
888 return s->ep[ep].hport[0];
889 case MUSB_HDRC_RXHUBADDR:
890 return s->ep[ep].haddr[1];
891 case MUSB_HDRC_RXHUBPORT:
892 return s->ep[ep].hport[1];
895 printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
900 static void musb_busctl_writeb(void *opaque, int ep, int addr, uint8_t value)
902 struct musb_s *s = (struct musb_s *) opaque;
905 case MUSB_HDRC_TXHUBADDR:
906 s->ep[ep].haddr[0] = value;
908 case MUSB_HDRC_TXHUBPORT:
909 s->ep[ep].hport[0] = value;
911 case MUSB_HDRC_RXHUBADDR:
912 s->ep[ep].haddr[1] = value;
914 case MUSB_HDRC_RXHUBPORT:
915 s->ep[ep].hport[1] = value;
919 printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
923 static uint16_t musb_busctl_readh(void *opaque, int ep, int addr)
925 struct musb_s *s = (struct musb_s *) opaque;
928 case MUSB_HDRC_TXFUNCADDR:
929 return s->ep[ep].faddr[0];
930 case MUSB_HDRC_RXFUNCADDR:
931 return s->ep[ep].faddr[1];
934 return musb_busctl_readb(s, ep, addr) |
935 (musb_busctl_readb(s, ep, addr | 1) << 8);
939 static void musb_busctl_writeh(void *opaque, int ep, int addr, uint16_t value)
941 struct musb_s *s = (struct musb_s *) opaque;
944 case MUSB_HDRC_TXFUNCADDR:
945 s->ep[ep].faddr[0] = value;
947 case MUSB_HDRC_RXFUNCADDR:
948 s->ep[ep].faddr[1] = value;
952 musb_busctl_writeb(s, ep, addr, value & 0xff);
953 musb_busctl_writeb(s, ep, addr | 1, value >> 8);
957 /* Endpoint control */
958 static uint8_t musb_ep_readb(void *opaque, int ep, int addr)
960 struct musb_s *s = (struct musb_s *) opaque;
963 case MUSB_HDRC_TXTYPE:
964 return s->ep[ep].type[0];
965 case MUSB_HDRC_TXINTERVAL:
966 return s->ep[ep].interval[0];
967 case MUSB_HDRC_RXTYPE:
968 return s->ep[ep].type[1];
969 case MUSB_HDRC_RXINTERVAL:
970 return s->ep[ep].interval[1];
971 case (MUSB_HDRC_FIFOSIZE & ~1):
973 case MUSB_HDRC_FIFOSIZE:
974 return ep ? s->ep[ep].fifosize : s->ep[ep].config;
977 printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
982 static void musb_ep_writeb(void *opaque, int ep, int addr, uint8_t value)
984 struct musb_s *s = (struct musb_s *) opaque;
987 case MUSB_HDRC_TXTYPE:
988 s->ep[ep].type[0] = value;
990 case MUSB_HDRC_TXINTERVAL:
991 s->ep[ep].interval[0] = value;
992 musb_ep_frame_cancel(&s->ep[ep], 0);
994 case MUSB_HDRC_RXTYPE:
995 s->ep[ep].type[1] = value;
997 case MUSB_HDRC_RXINTERVAL:
998 s->ep[ep].interval[1] = value;
999 musb_ep_frame_cancel(&s->ep[ep], 1);
1001 case (MUSB_HDRC_FIFOSIZE & ~1):
1003 case MUSB_HDRC_FIFOSIZE:
1004 printf("%s: somebody messes with fifosize (now %i bytes)\n",
1005 __FUNCTION__, value);
1006 s->ep[ep].fifosize = value;
1010 printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
1014 static uint16_t musb_ep_readh(void *opaque, int ep, int addr)
1016 struct musb_s *s = (struct musb_s *) opaque;
1020 case MUSB_HDRC_TXMAXP:
1021 return s->ep[ep].maxp[0];
1022 case MUSB_HDRC_TXCSR:
1023 return s->ep[ep].csr[0];
1024 case MUSB_HDRC_RXMAXP:
1025 return s->ep[ep].maxp[1];
1026 case MUSB_HDRC_RXCSR:
1027 ret = s->ep[ep].csr[1];
1029 /* TODO: This and other bits probably depend on
1030 * ep->csr[1] & MGC_M_RXCSR_AUTOCLEAR. */
1031 if (s->ep[ep].csr[1] & MGC_M_RXCSR_AUTOCLEAR)
1032 s->ep[ep].csr[1] &= ~MGC_M_RXCSR_RXPKTRDY;
1035 case MUSB_HDRC_RXCOUNT:
1036 return s->ep[ep].rxcount;
1039 return musb_ep_readb(s, ep, addr) |
1040 (musb_ep_readb(s, ep, addr | 1) << 8);
1044 static void musb_ep_writeh(void *opaque, int ep, int addr, uint16_t value)
1046 struct musb_s *s = (struct musb_s *) opaque;
1049 case MUSB_HDRC_TXMAXP:
1050 s->ep[ep].maxp[0] = value;
1052 case MUSB_HDRC_TXCSR:
1054 s->ep[ep].csr[0] &= value & 0xa6;
1055 s->ep[ep].csr[0] |= value & 0xff59;
1057 s->ep[ep].csr[0] &= value & 0x85;
1058 s->ep[ep].csr[0] |= value & 0xf7a;
1061 musb_ep_frame_cancel(&s->ep[ep], 0);
1063 if ((ep && (value & MGC_M_TXCSR_FLUSHFIFO)) ||
1064 (!ep && (value & MGC_M_CSR0_FLUSHFIFO))) {
1065 s->ep[ep].fifolen[0] = 0;
1066 s->ep[ep].fifostart[0] = 0;
1069 ~(MGC_M_TXCSR_FIFONOTEMPTY | MGC_M_TXCSR_TXPKTRDY);
1072 ~(MGC_M_CSR0_TXPKTRDY | MGC_M_CSR0_RXPKTRDY);
1077 (value & MGC_M_TXCSR_TXPKTRDY) &&
1078 !(value & MGC_M_TXCSR_H_NAKTIMEOUT)) ||
1080 (value & MGC_M_TXCSR_TXPKTRDY)) ||
1084 (value & MGC_M_CSR0_TXPKTRDY) &&
1085 !(value & MGC_M_CSR0_H_NAKTIMEOUT)))
1087 (value & MGC_M_CSR0_TXPKTRDY)))
1091 (value & MGC_M_CSR0_H_REQPKT) &&
1093 !(value & (MGC_M_CSR0_H_NAKTIMEOUT |
1094 MGC_M_CSR0_RXPKTRDY)))
1096 !(value & MGC_M_CSR0_RXPKTRDY))
1101 case MUSB_HDRC_RXMAXP:
1102 s->ep[ep].maxp[1] = value;
1104 case MUSB_HDRC_RXCSR:
1105 /* (DMA mode only) */
1107 (value & MGC_M_RXCSR_H_AUTOREQ) &&
1108 !(value & MGC_M_RXCSR_RXPKTRDY) &&
1109 (s->ep[ep].csr[1] & MGC_M_RXCSR_RXPKTRDY))
1110 value |= MGC_M_RXCSR_H_REQPKT;
1112 s->ep[ep].csr[1] &= 0x102 | (value & 0x4d);
1113 s->ep[ep].csr[1] |= value & 0xfeb0;
1115 musb_ep_frame_cancel(&s->ep[ep], 1);
1117 if (value & MGC_M_RXCSR_FLUSHFIFO) {
1118 s->ep[ep].fifolen[1] = 0;
1119 s->ep[ep].fifostart[1] = 0;
1120 s->ep[ep].csr[1] &= ~(MGC_M_RXCSR_FIFOFULL | MGC_M_RXCSR_RXPKTRDY);
1121 /* If double buffering and we have two packets ready, flush
1122 * only the first one and set up the fifo at the second packet. */
1125 if ((value & MGC_M_RXCSR_H_REQPKT) && !(value & MGC_M_RXCSR_DATAERROR))
1127 if (value & MGC_M_RXCSR_H_REQPKT)
1131 case MUSB_HDRC_RXCOUNT:
1132 s->ep[ep].rxcount = value;
1136 musb_ep_writeb(s, ep, addr, value & 0xff);
1137 musb_ep_writeb(s, ep, addr | 1, value >> 8);
1141 /* Generic control */
1142 static uint32_t musb_readb(void *opaque, target_phys_addr_t addr)
1144 struct musb_s *s = (struct musb_s *) opaque;
1149 case MUSB_HDRC_FADDR:
1151 case MUSB_HDRC_POWER:
1153 case MUSB_HDRC_INTRUSB:
1155 for (i = 0; i < sizeof(ret) * 8; i ++)
1157 musb_intr_set(s, i, 0);
1159 case MUSB_HDRC_INTRUSBE:
1161 case MUSB_HDRC_INDEX:
1163 case MUSB_HDRC_TESTMODE:
1166 case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf):
1167 return musb_ep_readb(s, s->idx, addr & 0xf);
1169 case MUSB_HDRC_DEVCTL:
1172 case MUSB_HDRC_TXFIFOSZ:
1173 case MUSB_HDRC_RXFIFOSZ:
1174 case MUSB_HDRC_VCTRL:
1178 case MUSB_HDRC_HWVERS:
1179 return (1 << 10) | 400;
1181 case (MUSB_HDRC_VCTRL | 1):
1182 case (MUSB_HDRC_HWVERS | 1):
1183 case (MUSB_HDRC_DEVCTL | 1):
1186 case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f):
1187 ep = (addr >> 3) & 0xf;
1188 return musb_busctl_readb(s, ep, addr & 0x7);
1190 case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff):
1191 ep = (addr >> 4) & 0xf;
1192 return musb_ep_readb(s, ep, addr & 0xf);
1195 printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr);
1200 static void musb_writeb(void *opaque, target_phys_addr_t addr, uint32_t value)
1202 struct musb_s *s = (struct musb_s *) opaque;
1206 case MUSB_HDRC_FADDR:
1207 s->faddr = value & 0x7f;
1209 case MUSB_HDRC_POWER:
1210 s->power = (value & 0xef) | (s->power & 0x10);
1211 /* MGC_M_POWER_RESET is also read-only in Peripheral Mode */
1212 if ((value & MGC_M_POWER_RESET) && s->port.dev) {
1213 usb_send_msg(s->port.dev, USB_MSG_RESET);
1214 /* Negotiate high-speed operation if MGC_M_POWER_HSENAB is set. */
1215 if ((value & MGC_M_POWER_HSENAB) &&
1216 s->port.dev->speed == USB_SPEED_HIGH)
1217 s->power |= MGC_M_POWER_HSMODE; /* Success */
1218 /* Restart frame counting. */
1220 if (value & MGC_M_POWER_SUSPENDM) {
1221 /* When all transfers finish, suspend and if MGC_M_POWER_ENSUSPEND
1222 * is set, also go into low power mode. Frame counting stops. */
1223 /* XXX: Cleared when the interrupt register is read */
1225 if (value & MGC_M_POWER_RESUME) {
1226 /* Wait 20ms and signal resuming on the bus. Frame counting
1230 case MUSB_HDRC_INTRUSB:
1232 case MUSB_HDRC_INTRUSBE:
1233 s->mask = value & 0xff;
1235 case MUSB_HDRC_INDEX:
1236 s->idx = value & 0xf;
1238 case MUSB_HDRC_TESTMODE:
1241 case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf):
1242 musb_ep_writeb(s, s->idx, addr & 0xf, value);
1245 case MUSB_HDRC_DEVCTL:
1246 s->session = !!(value & MGC_M_DEVCTL_SESSION);
1247 musb_session_update(s,
1249 !!(s->devctl & MGC_M_DEVCTL_SESSION));
1251 /* It seems this is the only R/W bit in this register? */
1252 s->devctl &= ~MGC_M_DEVCTL_SESSION;
1253 s->devctl |= value & MGC_M_DEVCTL_SESSION;
1256 case MUSB_HDRC_TXFIFOSZ:
1257 case MUSB_HDRC_RXFIFOSZ:
1258 case MUSB_HDRC_VCTRL:
1262 case (MUSB_HDRC_VCTRL | 1):
1263 case (MUSB_HDRC_DEVCTL | 1):
1266 case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f):
1267 ep = (addr >> 3) & 0xf;
1268 musb_busctl_writeb(s, ep, addr & 0x7, value);
1271 case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff):
1272 ep = (addr >> 4) & 0xf;
1273 musb_ep_writeb(s, ep, addr & 0xf, value);
1277 printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr);
1281 static uint32_t musb_readh(void *opaque, target_phys_addr_t addr)
1283 struct musb_s *s = (struct musb_s *) opaque;
1288 case MUSB_HDRC_INTRTX:
1291 for (i = 0; i < sizeof(ret) * 8; i ++)
1293 musb_tx_intr_set(s, i, 0);
1295 case MUSB_HDRC_INTRRX:
1298 for (i = 0; i < sizeof(ret) * 8; i ++)
1300 musb_rx_intr_set(s, i, 0);
1302 case MUSB_HDRC_INTRTXE:
1304 case MUSB_HDRC_INTRRXE:
1307 case MUSB_HDRC_FRAME:
1310 case MUSB_HDRC_TXFIFOADDR:
1311 return s->ep[s->idx].fifoaddr[0];
1312 case MUSB_HDRC_RXFIFOADDR:
1313 return s->ep[s->idx].fifoaddr[1];
1315 case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf):
1316 return musb_ep_readh(s, s->idx, addr & 0xf);
1318 case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f):
1319 ep = (addr >> 3) & 0xf;
1320 return musb_busctl_readh(s, ep, addr & 0x7);
1322 case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff):
1323 ep = (addr >> 4) & 0xf;
1324 return musb_ep_readh(s, ep, addr & 0xf);
1327 return musb_readb(s, addr) | (musb_readb(s, addr | 1) << 8);
1331 static void musb_writeh(void *opaque, target_phys_addr_t addr, uint32_t value)
1333 struct musb_s *s = (struct musb_s *) opaque;
1337 case MUSB_HDRC_INTRTXE:
1339 /* XXX: the masks seem to apply on the raising edge like with
1340 * edge-triggered interrupts, thus no need to update. I may be
1343 case MUSB_HDRC_INTRRXE:
1347 case MUSB_HDRC_FRAME:
1350 case MUSB_HDRC_TXFIFOADDR:
1351 s->ep[s->idx].fifoaddr[0] = value;
1352 s->ep[s->idx].buf[0] =
1353 s->buf + ((value << 1) & (sizeof(s->buf) / 4 - 1));
1355 case MUSB_HDRC_RXFIFOADDR:
1356 s->ep[s->idx].fifoaddr[1] = value;
1357 s->ep[s->idx].buf[1] =
1358 s->buf + ((value << 1) & (sizeof(s->buf) / 4 - 1));
1361 case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf):
1362 musb_ep_writeh(s, s->idx, addr & 0xf, value);
1365 case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f):
1366 ep = (addr >> 3) & 0xf;
1367 musb_busctl_writeh(s, ep, addr & 0x7, value);
1370 case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff):
1371 ep = (addr >> 4) & 0xf;
1372 musb_ep_writeh(s, ep, addr & 0xf, value);
1376 musb_writeb(s, addr, value & 0xff);
1377 musb_writeb(s, addr | 1, value >> 8);
1381 static uint32_t musb_readw(void *opaque, target_phys_addr_t addr)
1383 struct musb_s *s = (struct musb_s *) opaque;
1384 struct musb_ep_s *ep;
1388 case MUSB_HDRC_FIFO ... (MUSB_HDRC_FIFO + 0x3f):
1389 epnum = ((addr - MUSB_HDRC_FIFO) >> 2) & 0xf;
1392 if (ep->fifolen[1] >= 16) {
1393 /* We have a FIFO underrun */
1394 printf("%s: EP%i FIFO is now empty, stop reading\n",
1395 __FUNCTION__, epnum);
1398 /* In DMA mode clear RXPKTRDY and set REQPKT automatically
1399 * (if AUTOREQ is set) */
1401 ep->csr[1] &= ~MGC_M_RXCSR_FIFOFULL;
1402 return ep->buf[1][ep->fifostart[1] + ep->fifolen[1] ++];
1405 printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr);
1410 static void musb_writew(void *opaque, target_phys_addr_t addr, uint32_t value)
1412 struct musb_s *s = (struct musb_s *) opaque;
1413 struct musb_ep_s *ep;
1417 case MUSB_HDRC_FIFO ... (MUSB_HDRC_FIFO + 0x3f):
1418 epnum = ((addr - MUSB_HDRC_FIFO) >> 2) & 0xf;
1421 if (ep->fifolen[0] >= 16) {
1422 /* We have a FIFO overrun */
1423 printf("%s: EP%i FIFO exceeded 64 bytes, stop feeding data\n",
1424 __FUNCTION__, epnum);
1428 ep->buf[0][ep->fifostart[0] + ep->fifolen[0] ++] = value;
1430 ep->csr[0] |= MGC_M_TXCSR_FIFONOTEMPTY;
1434 printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr);
1438 CPUReadMemoryFunc *musb_read[] = {
1444 CPUWriteMemoryFunc *musb_write[] = {