root / hw / usb-musb.c @ f74ab486
History | View | Annotate | Download (41.3 kB)
1 | 942ac052 | balrog | /*
|
---|---|---|---|
2 | 942ac052 | balrog | * "Inventra" High-speed Dual-Role Controller (MUSB-HDRC), Mentor Graphics,
|
3 | 942ac052 | balrog | * USB2.0 OTG compliant core used in various chips.
|
4 | 942ac052 | balrog | *
|
5 | 942ac052 | balrog | * Copyright (C) 2008 Nokia Corporation
|
6 | 942ac052 | balrog | * Written by Andrzej Zaborowski <andrew@openedhand.com>
|
7 | 942ac052 | balrog | *
|
8 | 942ac052 | balrog | * This program is free software; you can redistribute it and/or
|
9 | 942ac052 | balrog | * modify it under the terms of the GNU General Public License as
|
10 | 942ac052 | balrog | * published by the Free Software Foundation; either version 2 or
|
11 | 942ac052 | balrog | * (at your option) version 3 of the License.
|
12 | 942ac052 | balrog | *
|
13 | 942ac052 | balrog | * This program is distributed in the hope that it will be useful,
|
14 | 942ac052 | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 | 942ac052 | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 | 942ac052 | balrog | * GNU General Public License for more details.
|
17 | 942ac052 | balrog | *
|
18 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
19 | 8167ee88 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
20 | 942ac052 | balrog | *
|
21 | 942ac052 | balrog | * Only host-mode and non-DMA accesses are currently supported.
|
22 | 942ac052 | balrog | */
|
23 | 942ac052 | balrog | #include "qemu-common.h" |
24 | 942ac052 | balrog | #include "qemu-timer.h" |
25 | 942ac052 | balrog | #include "usb.h" |
26 | 942ac052 | balrog | #include "irq.h" |
27 | 942ac052 | balrog | |
28 | 942ac052 | balrog | /* Common USB registers */
|
29 | 942ac052 | balrog | #define MUSB_HDRC_FADDR 0x00 /* 8-bit */ |
30 | 942ac052 | balrog | #define MUSB_HDRC_POWER 0x01 /* 8-bit */ |
31 | 942ac052 | balrog | |
32 | 942ac052 | balrog | #define MUSB_HDRC_INTRTX 0x02 /* 16-bit */ |
33 | 942ac052 | balrog | #define MUSB_HDRC_INTRRX 0x04 |
34 | 942ac052 | balrog | #define MUSB_HDRC_INTRTXE 0x06 |
35 | 942ac052 | balrog | #define MUSB_HDRC_INTRRXE 0x08 |
36 | 942ac052 | balrog | #define MUSB_HDRC_INTRUSB 0x0a /* 8 bit */ |
37 | 942ac052 | balrog | #define MUSB_HDRC_INTRUSBE 0x0b /* 8 bit */ |
38 | 942ac052 | balrog | #define MUSB_HDRC_FRAME 0x0c /* 16-bit */ |
39 | 942ac052 | balrog | #define MUSB_HDRC_INDEX 0x0e /* 8 bit */ |
40 | 942ac052 | balrog | #define MUSB_HDRC_TESTMODE 0x0f /* 8 bit */ |
41 | 942ac052 | balrog | |
42 | 942ac052 | balrog | /* Per-EP registers in indexed mode */
|
43 | 942ac052 | balrog | #define MUSB_HDRC_EP_IDX 0x10 /* 8-bit */ |
44 | 942ac052 | balrog | |
45 | 942ac052 | balrog | /* EP FIFOs */
|
46 | 942ac052 | balrog | #define MUSB_HDRC_FIFO 0x20 |
47 | 942ac052 | balrog | |
48 | 942ac052 | balrog | /* Additional Control Registers */
|
49 | 942ac052 | balrog | #define MUSB_HDRC_DEVCTL 0x60 /* 8 bit */ |
50 | 942ac052 | balrog | |
51 | 942ac052 | balrog | /* These are indexed */
|
52 | 942ac052 | balrog | #define MUSB_HDRC_TXFIFOSZ 0x62 /* 8 bit (see masks) */ |
53 | 942ac052 | balrog | #define MUSB_HDRC_RXFIFOSZ 0x63 /* 8 bit (see masks) */ |
54 | 942ac052 | balrog | #define MUSB_HDRC_TXFIFOADDR 0x64 /* 16 bit offset shifted right 3 */ |
55 | 942ac052 | balrog | #define MUSB_HDRC_RXFIFOADDR 0x66 /* 16 bit offset shifted right 3 */ |
56 | 942ac052 | balrog | |
57 | 942ac052 | balrog | /* Some more registers */
|
58 | 942ac052 | balrog | #define MUSB_HDRC_VCTRL 0x68 /* 8 bit */ |
59 | 942ac052 | balrog | #define MUSB_HDRC_HWVERS 0x6c /* 8 bit */ |
60 | 942ac052 | balrog | |
61 | 942ac052 | balrog | /* Added in HDRC 1.9(?) & MHDRC 1.4 */
|
62 | 942ac052 | balrog | /* ULPI pass-through */
|
63 | 942ac052 | balrog | #define MUSB_HDRC_ULPI_VBUSCTL 0x70 |
64 | 942ac052 | balrog | #define MUSB_HDRC_ULPI_REGDATA 0x74 |
65 | 942ac052 | balrog | #define MUSB_HDRC_ULPI_REGADDR 0x75 |
66 | 942ac052 | balrog | #define MUSB_HDRC_ULPI_REGCTL 0x76 |
67 | 942ac052 | balrog | |
68 | 942ac052 | balrog | /* Extended config & PHY control */
|
69 | 942ac052 | balrog | #define MUSB_HDRC_ENDCOUNT 0x78 /* 8 bit */ |
70 | 942ac052 | balrog | #define MUSB_HDRC_DMARAMCFG 0x79 /* 8 bit */ |
71 | 942ac052 | balrog | #define MUSB_HDRC_PHYWAIT 0x7a /* 8 bit */ |
72 | 942ac052 | balrog | #define MUSB_HDRC_PHYVPLEN 0x7b /* 8 bit */ |
73 | 942ac052 | balrog | #define MUSB_HDRC_HS_EOF1 0x7c /* 8 bit, units of 546.1 us */ |
74 | 942ac052 | balrog | #define MUSB_HDRC_FS_EOF1 0x7d /* 8 bit, units of 533.3 ns */ |
75 | 942ac052 | balrog | #define MUSB_HDRC_LS_EOF1 0x7e /* 8 bit, units of 1.067 us */ |
76 | 942ac052 | balrog | |
77 | 942ac052 | balrog | /* Per-EP BUSCTL registers */
|
78 | 942ac052 | balrog | #define MUSB_HDRC_BUSCTL 0x80 |
79 | 942ac052 | balrog | |
80 | 942ac052 | balrog | /* Per-EP registers in flat mode */
|
81 | 942ac052 | balrog | #define MUSB_HDRC_EP 0x100 |
82 | 942ac052 | balrog | |
83 | 942ac052 | balrog | /* offsets to registers in flat model */
|
84 | 942ac052 | balrog | #define MUSB_HDRC_TXMAXP 0x00 /* 16 bit apparently */ |
85 | 942ac052 | balrog | #define MUSB_HDRC_TXCSR 0x02 /* 16 bit apparently */ |
86 | 942ac052 | balrog | #define MUSB_HDRC_CSR0 MUSB_HDRC_TXCSR /* re-used for EP0 */ |
87 | 942ac052 | balrog | #define MUSB_HDRC_RXMAXP 0x04 /* 16 bit apparently */ |
88 | 942ac052 | balrog | #define MUSB_HDRC_RXCSR 0x06 /* 16 bit apparently */ |
89 | 942ac052 | balrog | #define MUSB_HDRC_RXCOUNT 0x08 /* 16 bit apparently */ |
90 | 942ac052 | balrog | #define MUSB_HDRC_COUNT0 MUSB_HDRC_RXCOUNT /* re-used for EP0 */ |
91 | 942ac052 | balrog | #define MUSB_HDRC_TXTYPE 0x0a /* 8 bit apparently */ |
92 | 942ac052 | balrog | #define MUSB_HDRC_TYPE0 MUSB_HDRC_TXTYPE /* re-used for EP0 */ |
93 | 942ac052 | balrog | #define MUSB_HDRC_TXINTERVAL 0x0b /* 8 bit apparently */ |
94 | 942ac052 | balrog | #define MUSB_HDRC_NAKLIMIT0 MUSB_HDRC_TXINTERVAL /* re-used for EP0 */ |
95 | 942ac052 | balrog | #define MUSB_HDRC_RXTYPE 0x0c /* 8 bit apparently */ |
96 | 942ac052 | balrog | #define MUSB_HDRC_RXINTERVAL 0x0d /* 8 bit apparently */ |
97 | 942ac052 | balrog | #define MUSB_HDRC_FIFOSIZE 0x0f /* 8 bit apparently */ |
98 | 942ac052 | balrog | #define MUSB_HDRC_CONFIGDATA MGC_O_HDRC_FIFOSIZE /* re-used for EP0 */ |
99 | 942ac052 | balrog | |
100 | 942ac052 | balrog | /* "Bus control" registers */
|
101 | 942ac052 | balrog | #define MUSB_HDRC_TXFUNCADDR 0x00 |
102 | 942ac052 | balrog | #define MUSB_HDRC_TXHUBADDR 0x02 |
103 | 942ac052 | balrog | #define MUSB_HDRC_TXHUBPORT 0x03 |
104 | 942ac052 | balrog | |
105 | 942ac052 | balrog | #define MUSB_HDRC_RXFUNCADDR 0x04 |
106 | 942ac052 | balrog | #define MUSB_HDRC_RXHUBADDR 0x06 |
107 | 942ac052 | balrog | #define MUSB_HDRC_RXHUBPORT 0x07 |
108 | 942ac052 | balrog | |
109 | 942ac052 | balrog | /*
|
110 | 942ac052 | balrog | * MUSBHDRC Register bit masks
|
111 | 942ac052 | balrog | */
|
112 | 942ac052 | balrog | |
113 | 942ac052 | balrog | /* POWER */
|
114 | 942ac052 | balrog | #define MGC_M_POWER_ISOUPDATE 0x80 |
115 | 942ac052 | balrog | #define MGC_M_POWER_SOFTCONN 0x40 |
116 | 942ac052 | balrog | #define MGC_M_POWER_HSENAB 0x20 |
117 | 942ac052 | balrog | #define MGC_M_POWER_HSMODE 0x10 |
118 | 942ac052 | balrog | #define MGC_M_POWER_RESET 0x08 |
119 | 942ac052 | balrog | #define MGC_M_POWER_RESUME 0x04 |
120 | 942ac052 | balrog | #define MGC_M_POWER_SUSPENDM 0x02 |
121 | 942ac052 | balrog | #define MGC_M_POWER_ENSUSPEND 0x01 |
122 | 942ac052 | balrog | |
123 | 942ac052 | balrog | /* INTRUSB */
|
124 | 942ac052 | balrog | #define MGC_M_INTR_SUSPEND 0x01 |
125 | 942ac052 | balrog | #define MGC_M_INTR_RESUME 0x02 |
126 | 942ac052 | balrog | #define MGC_M_INTR_RESET 0x04 |
127 | 942ac052 | balrog | #define MGC_M_INTR_BABBLE 0x04 |
128 | 942ac052 | balrog | #define MGC_M_INTR_SOF 0x08 |
129 | 942ac052 | balrog | #define MGC_M_INTR_CONNECT 0x10 |
130 | 942ac052 | balrog | #define MGC_M_INTR_DISCONNECT 0x20 |
131 | 942ac052 | balrog | #define MGC_M_INTR_SESSREQ 0x40 |
132 | 942ac052 | balrog | #define MGC_M_INTR_VBUSERROR 0x80 /* FOR SESSION END */ |
133 | 942ac052 | balrog | #define MGC_M_INTR_EP0 0x01 /* FOR EP0 INTERRUPT */ |
134 | 942ac052 | balrog | |
135 | 942ac052 | balrog | /* DEVCTL */
|
136 | 942ac052 | balrog | #define MGC_M_DEVCTL_BDEVICE 0x80 |
137 | 942ac052 | balrog | #define MGC_M_DEVCTL_FSDEV 0x40 |
138 | 942ac052 | balrog | #define MGC_M_DEVCTL_LSDEV 0x20 |
139 | 942ac052 | balrog | #define MGC_M_DEVCTL_VBUS 0x18 |
140 | 942ac052 | balrog | #define MGC_S_DEVCTL_VBUS 3 |
141 | 942ac052 | balrog | #define MGC_M_DEVCTL_HM 0x04 |
142 | 942ac052 | balrog | #define MGC_M_DEVCTL_HR 0x02 |
143 | 942ac052 | balrog | #define MGC_M_DEVCTL_SESSION 0x01 |
144 | 942ac052 | balrog | |
145 | 942ac052 | balrog | /* TESTMODE */
|
146 | 942ac052 | balrog | #define MGC_M_TEST_FORCE_HOST 0x80 |
147 | 942ac052 | balrog | #define MGC_M_TEST_FIFO_ACCESS 0x40 |
148 | 942ac052 | balrog | #define MGC_M_TEST_FORCE_FS 0x20 |
149 | 942ac052 | balrog | #define MGC_M_TEST_FORCE_HS 0x10 |
150 | 942ac052 | balrog | #define MGC_M_TEST_PACKET 0x08 |
151 | 942ac052 | balrog | #define MGC_M_TEST_K 0x04 |
152 | 942ac052 | balrog | #define MGC_M_TEST_J 0x02 |
153 | 942ac052 | balrog | #define MGC_M_TEST_SE0_NAK 0x01 |
154 | 942ac052 | balrog | |
155 | 942ac052 | balrog | /* CSR0 */
|
156 | 942ac052 | balrog | #define MGC_M_CSR0_FLUSHFIFO 0x0100 |
157 | 942ac052 | balrog | #define MGC_M_CSR0_TXPKTRDY 0x0002 |
158 | 942ac052 | balrog | #define MGC_M_CSR0_RXPKTRDY 0x0001 |
159 | 942ac052 | balrog | |
160 | 942ac052 | balrog | /* CSR0 in Peripheral mode */
|
161 | 942ac052 | balrog | #define MGC_M_CSR0_P_SVDSETUPEND 0x0080 |
162 | 942ac052 | balrog | #define MGC_M_CSR0_P_SVDRXPKTRDY 0x0040 |
163 | 942ac052 | balrog | #define MGC_M_CSR0_P_SENDSTALL 0x0020 |
164 | 942ac052 | balrog | #define MGC_M_CSR0_P_SETUPEND 0x0010 |
165 | 942ac052 | balrog | #define MGC_M_CSR0_P_DATAEND 0x0008 |
166 | 942ac052 | balrog | #define MGC_M_CSR0_P_SENTSTALL 0x0004 |
167 | 942ac052 | balrog | |
168 | 942ac052 | balrog | /* CSR0 in Host mode */
|
169 | 942ac052 | balrog | #define MGC_M_CSR0_H_NO_PING 0x0800 |
170 | 942ac052 | balrog | #define MGC_M_CSR0_H_WR_DATATOGGLE 0x0400 /* set to allow setting: */ |
171 | 942ac052 | balrog | #define MGC_M_CSR0_H_DATATOGGLE 0x0200 /* data toggle control */ |
172 | 942ac052 | balrog | #define MGC_M_CSR0_H_NAKTIMEOUT 0x0080 |
173 | 942ac052 | balrog | #define MGC_M_CSR0_H_STATUSPKT 0x0040 |
174 | 942ac052 | balrog | #define MGC_M_CSR0_H_REQPKT 0x0020 |
175 | 942ac052 | balrog | #define MGC_M_CSR0_H_ERROR 0x0010 |
176 | 942ac052 | balrog | #define MGC_M_CSR0_H_SETUPPKT 0x0008 |
177 | 942ac052 | balrog | #define MGC_M_CSR0_H_RXSTALL 0x0004 |
178 | 942ac052 | balrog | |
179 | 942ac052 | balrog | /* CONFIGDATA */
|
180 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_MPRXE 0x80 /* auto bulk pkt combining */ |
181 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_MPTXE 0x40 /* auto bulk pkt splitting */ |
182 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_BIGENDIAN 0x20 |
183 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_HBRXE 0x10 /* HB-ISO for RX */ |
184 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_HBTXE 0x08 /* HB-ISO for TX */ |
185 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_DYNFIFO 0x04 /* dynamic FIFO sizing */ |
186 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_SOFTCONE 0x02 /* SoftConnect */ |
187 | 942ac052 | balrog | #define MGC_M_CONFIGDATA_UTMIDW 0x01 /* Width, 0 => 8b, 1 => 16b */ |
188 | 942ac052 | balrog | |
189 | 942ac052 | balrog | /* TXCSR in Peripheral and Host mode */
|
190 | 942ac052 | balrog | #define MGC_M_TXCSR_AUTOSET 0x8000 |
191 | 942ac052 | balrog | #define MGC_M_TXCSR_ISO 0x4000 |
192 | 942ac052 | balrog | #define MGC_M_TXCSR_MODE 0x2000 |
193 | 942ac052 | balrog | #define MGC_M_TXCSR_DMAENAB 0x1000 |
194 | 942ac052 | balrog | #define MGC_M_TXCSR_FRCDATATOG 0x0800 |
195 | 942ac052 | balrog | #define MGC_M_TXCSR_DMAMODE 0x0400 |
196 | 942ac052 | balrog | #define MGC_M_TXCSR_CLRDATATOG 0x0040 |
197 | 942ac052 | balrog | #define MGC_M_TXCSR_FLUSHFIFO 0x0008 |
198 | 942ac052 | balrog | #define MGC_M_TXCSR_FIFONOTEMPTY 0x0002 |
199 | 942ac052 | balrog | #define MGC_M_TXCSR_TXPKTRDY 0x0001 |
200 | 942ac052 | balrog | |
201 | 942ac052 | balrog | /* TXCSR in Peripheral mode */
|
202 | 942ac052 | balrog | #define MGC_M_TXCSR_P_INCOMPTX 0x0080 |
203 | 942ac052 | balrog | #define MGC_M_TXCSR_P_SENTSTALL 0x0020 |
204 | 942ac052 | balrog | #define MGC_M_TXCSR_P_SENDSTALL 0x0010 |
205 | 942ac052 | balrog | #define MGC_M_TXCSR_P_UNDERRUN 0x0004 |
206 | 942ac052 | balrog | |
207 | 942ac052 | balrog | /* TXCSR in Host mode */
|
208 | 942ac052 | balrog | #define MGC_M_TXCSR_H_WR_DATATOGGLE 0x0200 |
209 | 942ac052 | balrog | #define MGC_M_TXCSR_H_DATATOGGLE 0x0100 |
210 | 942ac052 | balrog | #define MGC_M_TXCSR_H_NAKTIMEOUT 0x0080 |
211 | 942ac052 | balrog | #define MGC_M_TXCSR_H_RXSTALL 0x0020 |
212 | 942ac052 | balrog | #define MGC_M_TXCSR_H_ERROR 0x0004 |
213 | 942ac052 | balrog | |
214 | 942ac052 | balrog | /* RXCSR in Peripheral and Host mode */
|
215 | 942ac052 | balrog | #define MGC_M_RXCSR_AUTOCLEAR 0x8000 |
216 | 942ac052 | balrog | #define MGC_M_RXCSR_DMAENAB 0x2000 |
217 | 942ac052 | balrog | #define MGC_M_RXCSR_DISNYET 0x1000 |
218 | 942ac052 | balrog | #define MGC_M_RXCSR_DMAMODE 0x0800 |
219 | 942ac052 | balrog | #define MGC_M_RXCSR_INCOMPRX 0x0100 |
220 | 942ac052 | balrog | #define MGC_M_RXCSR_CLRDATATOG 0x0080 |
221 | 942ac052 | balrog | #define MGC_M_RXCSR_FLUSHFIFO 0x0010 |
222 | 942ac052 | balrog | #define MGC_M_RXCSR_DATAERROR 0x0008 |
223 | 942ac052 | balrog | #define MGC_M_RXCSR_FIFOFULL 0x0002 |
224 | 942ac052 | balrog | #define MGC_M_RXCSR_RXPKTRDY 0x0001 |
225 | 942ac052 | balrog | |
226 | 942ac052 | balrog | /* RXCSR in Peripheral mode */
|
227 | 942ac052 | balrog | #define MGC_M_RXCSR_P_ISO 0x4000 |
228 | 942ac052 | balrog | #define MGC_M_RXCSR_P_SENTSTALL 0x0040 |
229 | 942ac052 | balrog | #define MGC_M_RXCSR_P_SENDSTALL 0x0020 |
230 | 942ac052 | balrog | #define MGC_M_RXCSR_P_OVERRUN 0x0004 |
231 | 942ac052 | balrog | |
232 | 942ac052 | balrog | /* RXCSR in Host mode */
|
233 | 942ac052 | balrog | #define MGC_M_RXCSR_H_AUTOREQ 0x4000 |
234 | 942ac052 | balrog | #define MGC_M_RXCSR_H_WR_DATATOGGLE 0x0400 |
235 | 942ac052 | balrog | #define MGC_M_RXCSR_H_DATATOGGLE 0x0200 |
236 | 942ac052 | balrog | #define MGC_M_RXCSR_H_RXSTALL 0x0040 |
237 | 942ac052 | balrog | #define MGC_M_RXCSR_H_REQPKT 0x0020 |
238 | 942ac052 | balrog | #define MGC_M_RXCSR_H_ERROR 0x0004 |
239 | 942ac052 | balrog | |
240 | 942ac052 | balrog | /* HUBADDR */
|
241 | 942ac052 | balrog | #define MGC_M_HUBADDR_MULTI_TT 0x80 |
242 | 942ac052 | balrog | |
243 | 942ac052 | balrog | /* ULPI: Added in HDRC 1.9(?) & MHDRC 1.4 */
|
244 | 942ac052 | balrog | #define MGC_M_ULPI_VBCTL_USEEXTVBUSIND 0x02 |
245 | 942ac052 | balrog | #define MGC_M_ULPI_VBCTL_USEEXTVBUS 0x01 |
246 | 942ac052 | balrog | #define MGC_M_ULPI_REGCTL_INT_ENABLE 0x08 |
247 | 942ac052 | balrog | #define MGC_M_ULPI_REGCTL_READNOTWRITE 0x04 |
248 | 942ac052 | balrog | #define MGC_M_ULPI_REGCTL_COMPLETE 0x02 |
249 | 942ac052 | balrog | #define MGC_M_ULPI_REGCTL_REG 0x01 |
250 | 942ac052 | balrog | |
251 | 942ac052 | balrog | static void musb_attach(USBPort *port, USBDevice *dev); |
252 | 942ac052 | balrog | |
253 | bc24a225 | Paul Brook | typedef struct { |
254 | bc24a225 | Paul Brook | uint16_t faddr[2];
|
255 | bc24a225 | Paul Brook | uint8_t haddr[2];
|
256 | bc24a225 | Paul Brook | uint8_t hport[2];
|
257 | bc24a225 | Paul Brook | uint16_t csr[2];
|
258 | bc24a225 | Paul Brook | uint16_t maxp[2];
|
259 | bc24a225 | Paul Brook | uint16_t rxcount; |
260 | bc24a225 | Paul Brook | uint8_t type[2];
|
261 | bc24a225 | Paul Brook | uint8_t interval[2];
|
262 | bc24a225 | Paul Brook | uint8_t config; |
263 | bc24a225 | Paul Brook | uint8_t fifosize; |
264 | bc24a225 | Paul Brook | int timeout[2]; /* Always in microframes */ |
265 | bc24a225 | Paul Brook | |
266 | bc24a225 | Paul Brook | uint32_t *buf[2];
|
267 | bc24a225 | Paul Brook | int fifolen[2]; |
268 | bc24a225 | Paul Brook | int fifostart[2]; |
269 | bc24a225 | Paul Brook | int fifoaddr[2]; |
270 | bc24a225 | Paul Brook | USBPacket packey[2];
|
271 | bc24a225 | Paul Brook | int status[2]; |
272 | bc24a225 | Paul Brook | int ext_size[2]; |
273 | bc24a225 | Paul Brook | |
274 | bc24a225 | Paul Brook | /* For callbacks' use */
|
275 | bc24a225 | Paul Brook | int epnum;
|
276 | bc24a225 | Paul Brook | int interrupt[2]; |
277 | bc24a225 | Paul Brook | MUSBState *musb; |
278 | bc24a225 | Paul Brook | USBCallback *delayed_cb[2];
|
279 | bc24a225 | Paul Brook | QEMUTimer *intv_timer[2];
|
280 | bc24a225 | Paul Brook | } MUSBEndPoint; |
281 | bc24a225 | Paul Brook | |
282 | bc24a225 | Paul Brook | struct MUSBState {
|
283 | 942ac052 | balrog | qemu_irq *irqs; |
284 | b2317837 | Gerd Hoffmann | USBBus bus; |
285 | 942ac052 | balrog | USBPort port; |
286 | 942ac052 | balrog | |
287 | 942ac052 | balrog | int idx;
|
288 | 942ac052 | balrog | uint8_t devctl; |
289 | 942ac052 | balrog | uint8_t power; |
290 | 942ac052 | balrog | uint8_t faddr; |
291 | 942ac052 | balrog | |
292 | 942ac052 | balrog | uint8_t intr; |
293 | 942ac052 | balrog | uint8_t mask; |
294 | 942ac052 | balrog | uint16_t tx_intr; |
295 | 942ac052 | balrog | uint16_t tx_mask; |
296 | 942ac052 | balrog | uint16_t rx_intr; |
297 | 942ac052 | balrog | uint16_t rx_mask; |
298 | 942ac052 | balrog | |
299 | 942ac052 | balrog | int setup_len;
|
300 | 942ac052 | balrog | int session;
|
301 | 942ac052 | balrog | |
302 | 942ac052 | balrog | uint32_t buf[0x2000];
|
303 | 942ac052 | balrog | |
304 | 942ac052 | balrog | /* Duplicating the world since 2008!... probably we should have 32
|
305 | 942ac052 | balrog | * logical, single endpoints instead. */
|
306 | bc24a225 | Paul Brook | MUSBEndPoint ep[16];
|
307 | 942ac052 | balrog | } *musb_init(qemu_irq *irqs) |
308 | 942ac052 | balrog | { |
309 | bc24a225 | Paul Brook | MUSBState *s = qemu_mallocz(sizeof(*s));
|
310 | 942ac052 | balrog | int i;
|
311 | 942ac052 | balrog | |
312 | 942ac052 | balrog | s->irqs = irqs; |
313 | 942ac052 | balrog | |
314 | 942ac052 | balrog | s->faddr = 0x00;
|
315 | 942ac052 | balrog | s->power = MGC_M_POWER_HSENAB; |
316 | 942ac052 | balrog | s->tx_intr = 0x0000;
|
317 | 942ac052 | balrog | s->rx_intr = 0x0000;
|
318 | 942ac052 | balrog | s->tx_mask = 0xffff;
|
319 | 942ac052 | balrog | s->rx_mask = 0xffff;
|
320 | 942ac052 | balrog | s->intr = 0x00;
|
321 | 942ac052 | balrog | s->mask = 0x06;
|
322 | 942ac052 | balrog | s->idx = 0;
|
323 | 942ac052 | balrog | |
324 | 942ac052 | balrog | /* TODO: _DW */
|
325 | 942ac052 | balrog | s->ep[0].config = MGC_M_CONFIGDATA_SOFTCONE | MGC_M_CONFIGDATA_DYNFIFO;
|
326 | 942ac052 | balrog | for (i = 0; i < 16; i ++) { |
327 | 942ac052 | balrog | s->ep[i].fifosize = 64;
|
328 | 942ac052 | balrog | s->ep[i].maxp[0] = 0x40; |
329 | 942ac052 | balrog | s->ep[i].maxp[1] = 0x40; |
330 | 942ac052 | balrog | s->ep[i].musb = s; |
331 | 942ac052 | balrog | s->ep[i].epnum = i; |
332 | 942ac052 | balrog | } |
333 | 942ac052 | balrog | |
334 | b2317837 | Gerd Hoffmann | usb_bus_new(&s->bus, NULL /* FIXME */); |
335 | b2317837 | Gerd Hoffmann | usb_register_port(&s->bus, &s->port, s, 0, musb_attach);
|
336 | 942ac052 | balrog | |
337 | 942ac052 | balrog | return s;
|
338 | 942ac052 | balrog | } |
339 | 942ac052 | balrog | |
340 | bc24a225 | Paul Brook | static void musb_vbus_set(MUSBState *s, int level) |
341 | 942ac052 | balrog | { |
342 | 942ac052 | balrog | if (level)
|
343 | 942ac052 | balrog | s->devctl |= 3 << MGC_S_DEVCTL_VBUS;
|
344 | 942ac052 | balrog | else
|
345 | 942ac052 | balrog | s->devctl &= ~MGC_M_DEVCTL_VBUS; |
346 | 942ac052 | balrog | |
347 | 942ac052 | balrog | qemu_set_irq(s->irqs[musb_set_vbus], level); |
348 | 942ac052 | balrog | } |
349 | 942ac052 | balrog | |
350 | bc24a225 | Paul Brook | static void musb_intr_set(MUSBState *s, int line, int level) |
351 | 942ac052 | balrog | { |
352 | 942ac052 | balrog | if (!level) {
|
353 | 942ac052 | balrog | s->intr &= ~(1 << line);
|
354 | 942ac052 | balrog | qemu_irq_lower(s->irqs[line]); |
355 | 942ac052 | balrog | } else if (s->mask & (1 << line)) { |
356 | 942ac052 | balrog | s->intr |= 1 << line;
|
357 | 942ac052 | balrog | qemu_irq_raise(s->irqs[line]); |
358 | 942ac052 | balrog | } |
359 | 942ac052 | balrog | } |
360 | 942ac052 | balrog | |
361 | bc24a225 | Paul Brook | static void musb_tx_intr_set(MUSBState *s, int line, int level) |
362 | 942ac052 | balrog | { |
363 | 942ac052 | balrog | if (!level) {
|
364 | 942ac052 | balrog | s->tx_intr &= ~(1 << line);
|
365 | 942ac052 | balrog | if (!s->tx_intr)
|
366 | 942ac052 | balrog | qemu_irq_lower(s->irqs[musb_irq_tx]); |
367 | 942ac052 | balrog | } else if (s->tx_mask & (1 << line)) { |
368 | 942ac052 | balrog | s->tx_intr |= 1 << line;
|
369 | 942ac052 | balrog | qemu_irq_raise(s->irqs[musb_irq_tx]); |
370 | 942ac052 | balrog | } |
371 | 942ac052 | balrog | } |
372 | 942ac052 | balrog | |
373 | bc24a225 | Paul Brook | static void musb_rx_intr_set(MUSBState *s, int line, int level) |
374 | 942ac052 | balrog | { |
375 | 942ac052 | balrog | if (line) {
|
376 | 942ac052 | balrog | if (!level) {
|
377 | 942ac052 | balrog | s->rx_intr &= ~(1 << line);
|
378 | 942ac052 | balrog | if (!s->rx_intr)
|
379 | 942ac052 | balrog | qemu_irq_lower(s->irqs[musb_irq_rx]); |
380 | 942ac052 | balrog | } else if (s->rx_mask & (1 << line)) { |
381 | 942ac052 | balrog | s->rx_intr |= 1 << line;
|
382 | 942ac052 | balrog | qemu_irq_raise(s->irqs[musb_irq_rx]); |
383 | 942ac052 | balrog | } |
384 | 942ac052 | balrog | } else
|
385 | 942ac052 | balrog | musb_tx_intr_set(s, line, level); |
386 | 942ac052 | balrog | } |
387 | 942ac052 | balrog | |
388 | bc24a225 | Paul Brook | uint32_t musb_core_intr_get(MUSBState *s) |
389 | 942ac052 | balrog | { |
390 | 942ac052 | balrog | return (s->rx_intr << 15) | s->tx_intr; |
391 | 942ac052 | balrog | } |
392 | 942ac052 | balrog | |
393 | bc24a225 | Paul Brook | void musb_core_intr_clear(MUSBState *s, uint32_t mask)
|
394 | 942ac052 | balrog | { |
395 | 942ac052 | balrog | if (s->rx_intr) {
|
396 | 942ac052 | balrog | s->rx_intr &= mask >> 15;
|
397 | 942ac052 | balrog | if (!s->rx_intr)
|
398 | 942ac052 | balrog | qemu_irq_lower(s->irqs[musb_irq_rx]); |
399 | 942ac052 | balrog | } |
400 | 942ac052 | balrog | |
401 | 942ac052 | balrog | if (s->tx_intr) {
|
402 | 942ac052 | balrog | s->tx_intr &= mask & 0xffff;
|
403 | 942ac052 | balrog | if (!s->tx_intr)
|
404 | 942ac052 | balrog | qemu_irq_lower(s->irqs[musb_irq_tx]); |
405 | 942ac052 | balrog | } |
406 | 942ac052 | balrog | } |
407 | 942ac052 | balrog | |
408 | bc24a225 | Paul Brook | void musb_set_size(MUSBState *s, int epnum, int size, int is_tx) |
409 | 942ac052 | balrog | { |
410 | 942ac052 | balrog | s->ep[epnum].ext_size[!is_tx] = size; |
411 | 942ac052 | balrog | s->ep[epnum].fifostart[0] = 0; |
412 | 942ac052 | balrog | s->ep[epnum].fifostart[1] = 0; |
413 | 942ac052 | balrog | s->ep[epnum].fifolen[0] = 0; |
414 | 942ac052 | balrog | s->ep[epnum].fifolen[1] = 0; |
415 | 942ac052 | balrog | } |
416 | 942ac052 | balrog | |
417 | bc24a225 | Paul Brook | static void musb_session_update(MUSBState *s, int prev_dev, int prev_sess) |
418 | 942ac052 | balrog | { |
419 | 942ac052 | balrog | int detect_prev = prev_dev && prev_sess;
|
420 | 942ac052 | balrog | int detect = !!s->port.dev && s->session;
|
421 | 942ac052 | balrog | |
422 | 942ac052 | balrog | if (detect && !detect_prev) {
|
423 | 942ac052 | balrog | /* Let's skip the ID pin sense and VBUS sense formalities and
|
424 | 942ac052 | balrog | * and signal a successful SRP directly. This should work at least
|
425 | 942ac052 | balrog | * for the Linux driver stack. */
|
426 | 942ac052 | balrog | musb_intr_set(s, musb_irq_connect, 1);
|
427 | 942ac052 | balrog | |
428 | 942ac052 | balrog | if (s->port.dev->speed == USB_SPEED_LOW) {
|
429 | 942ac052 | balrog | s->devctl &= ~MGC_M_DEVCTL_FSDEV; |
430 | 942ac052 | balrog | s->devctl |= MGC_M_DEVCTL_LSDEV; |
431 | 942ac052 | balrog | } else {
|
432 | 942ac052 | balrog | s->devctl |= MGC_M_DEVCTL_FSDEV; |
433 | 942ac052 | balrog | s->devctl &= ~MGC_M_DEVCTL_LSDEV; |
434 | 942ac052 | balrog | } |
435 | 942ac052 | balrog | |
436 | 942ac052 | balrog | /* A-mode? */
|
437 | 942ac052 | balrog | s->devctl &= ~MGC_M_DEVCTL_BDEVICE; |
438 | 942ac052 | balrog | |
439 | 942ac052 | balrog | /* Host-mode bit? */
|
440 | 942ac052 | balrog | s->devctl |= MGC_M_DEVCTL_HM; |
441 | 942ac052 | balrog | #if 1 |
442 | 942ac052 | balrog | musb_vbus_set(s, 1);
|
443 | 942ac052 | balrog | #endif
|
444 | 942ac052 | balrog | } else if (!detect && detect_prev) { |
445 | 942ac052 | balrog | #if 1 |
446 | 942ac052 | balrog | musb_vbus_set(s, 0);
|
447 | 942ac052 | balrog | #endif
|
448 | 942ac052 | balrog | } |
449 | 942ac052 | balrog | } |
450 | 942ac052 | balrog | |
451 | 942ac052 | balrog | /* Attach or detach a device on our only port. */
|
452 | 942ac052 | balrog | static void musb_attach(USBPort *port, USBDevice *dev) |
453 | 942ac052 | balrog | { |
454 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) port->opaque; |
455 | 942ac052 | balrog | USBDevice *curr; |
456 | 942ac052 | balrog | |
457 | 942ac052 | balrog | port = &s->port; |
458 | 942ac052 | balrog | curr = port->dev; |
459 | 942ac052 | balrog | |
460 | 942ac052 | balrog | if (dev) {
|
461 | 942ac052 | balrog | if (curr) {
|
462 | 942ac052 | balrog | usb_attach(port, NULL);
|
463 | 942ac052 | balrog | /* TODO: signal some interrupts */
|
464 | 942ac052 | balrog | } |
465 | 942ac052 | balrog | |
466 | 942ac052 | balrog | musb_intr_set(s, musb_irq_vbus_request, 1);
|
467 | 942ac052 | balrog | |
468 | 942ac052 | balrog | /* Send the attach message to device */
|
469 | 942ac052 | balrog | usb_send_msg(dev, USB_MSG_ATTACH); |
470 | 942ac052 | balrog | } else if (curr) { |
471 | 942ac052 | balrog | /* Send the detach message */
|
472 | 942ac052 | balrog | usb_send_msg(curr, USB_MSG_DETACH); |
473 | 942ac052 | balrog | |
474 | 942ac052 | balrog | musb_intr_set(s, musb_irq_disconnect, 1);
|
475 | 942ac052 | balrog | } |
476 | 942ac052 | balrog | |
477 | 942ac052 | balrog | port->dev = dev; |
478 | 942ac052 | balrog | |
479 | 942ac052 | balrog | musb_session_update(s, !!curr, s->session); |
480 | 942ac052 | balrog | } |
481 | 942ac052 | balrog | |
482 | 942ac052 | balrog | static inline void musb_cb_tick0(void *opaque) |
483 | 942ac052 | balrog | { |
484 | bc24a225 | Paul Brook | MUSBEndPoint *ep = (MUSBEndPoint *) opaque; |
485 | 942ac052 | balrog | |
486 | 942ac052 | balrog | ep->delayed_cb[0](&ep->packey[0], opaque); |
487 | 942ac052 | balrog | } |
488 | 942ac052 | balrog | |
489 | 942ac052 | balrog | static inline void musb_cb_tick1(void *opaque) |
490 | 942ac052 | balrog | { |
491 | bc24a225 | Paul Brook | MUSBEndPoint *ep = (MUSBEndPoint *) opaque; |
492 | 942ac052 | balrog | |
493 | 942ac052 | balrog | ep->delayed_cb[1](&ep->packey[1], opaque); |
494 | 942ac052 | balrog | } |
495 | 942ac052 | balrog | |
496 | 942ac052 | balrog | #define musb_cb_tick (dir ? musb_cb_tick1 : musb_cb_tick0)
|
497 | 942ac052 | balrog | |
498 | 942ac052 | balrog | static inline void musb_schedule_cb(USBPacket *packey, void *opaque, int dir) |
499 | 942ac052 | balrog | { |
500 | bc24a225 | Paul Brook | MUSBEndPoint *ep = (MUSBEndPoint *) opaque; |
501 | 942ac052 | balrog | int timeout = 0; |
502 | 942ac052 | balrog | |
503 | 942ac052 | balrog | if (ep->status[dir] == USB_RET_NAK)
|
504 | 942ac052 | balrog | timeout = ep->timeout[dir]; |
505 | 942ac052 | balrog | else if (ep->interrupt[dir]) |
506 | 942ac052 | balrog | timeout = 8;
|
507 | 942ac052 | balrog | else
|
508 | 942ac052 | balrog | return musb_cb_tick(opaque);
|
509 | 942ac052 | balrog | |
510 | 942ac052 | balrog | if (!ep->intv_timer[dir])
|
511 | 942ac052 | balrog | ep->intv_timer[dir] = qemu_new_timer(vm_clock, musb_cb_tick, opaque); |
512 | 942ac052 | balrog | |
513 | 942ac052 | balrog | qemu_mod_timer(ep->intv_timer[dir], qemu_get_clock(vm_clock) + |
514 | 6ee093c9 | Juan Quintela | muldiv64(timeout, get_ticks_per_sec(), 8000));
|
515 | 942ac052 | balrog | } |
516 | 942ac052 | balrog | |
517 | 942ac052 | balrog | static void musb_schedule0_cb(USBPacket *packey, void *opaque) |
518 | 942ac052 | balrog | { |
519 | 942ac052 | balrog | return musb_schedule_cb(packey, opaque, 0); |
520 | 942ac052 | balrog | } |
521 | 942ac052 | balrog | |
522 | 942ac052 | balrog | static void musb_schedule1_cb(USBPacket *packey, void *opaque) |
523 | 942ac052 | balrog | { |
524 | 942ac052 | balrog | return musb_schedule_cb(packey, opaque, 1); |
525 | 942ac052 | balrog | } |
526 | 942ac052 | balrog | |
527 | 942ac052 | balrog | static int musb_timeout(int ttype, int speed, int val) |
528 | 942ac052 | balrog | { |
529 | 942ac052 | balrog | #if 1 |
530 | 942ac052 | balrog | return val << 3; |
531 | 942ac052 | balrog | #endif
|
532 | 942ac052 | balrog | |
533 | 942ac052 | balrog | switch (ttype) {
|
534 | 942ac052 | balrog | case USB_ENDPOINT_XFER_CONTROL:
|
535 | 942ac052 | balrog | if (val < 2) |
536 | 942ac052 | balrog | return 0; |
537 | 942ac052 | balrog | else if (speed == USB_SPEED_HIGH) |
538 | 942ac052 | balrog | return 1 << (val - 1); |
539 | 942ac052 | balrog | else
|
540 | 942ac052 | balrog | return 8 << (val - 1); |
541 | 942ac052 | balrog | |
542 | 942ac052 | balrog | case USB_ENDPOINT_XFER_INT:
|
543 | 942ac052 | balrog | if (speed == USB_SPEED_HIGH)
|
544 | 942ac052 | balrog | if (val < 2) |
545 | 942ac052 | balrog | return 0; |
546 | 942ac052 | balrog | else
|
547 | 942ac052 | balrog | return 1 << (val - 1); |
548 | 942ac052 | balrog | else
|
549 | 942ac052 | balrog | return val << 3; |
550 | 942ac052 | balrog | |
551 | 942ac052 | balrog | case USB_ENDPOINT_XFER_BULK:
|
552 | 942ac052 | balrog | case USB_ENDPOINT_XFER_ISOC:
|
553 | 942ac052 | balrog | if (val < 2) |
554 | 942ac052 | balrog | return 0; |
555 | 942ac052 | balrog | else if (speed == USB_SPEED_HIGH) |
556 | 942ac052 | balrog | return 1 << (val - 1); |
557 | 942ac052 | balrog | else
|
558 | 942ac052 | balrog | return 8 << (val - 1); |
559 | 942ac052 | balrog | /* TODO: what with low-speed Bulk and Isochronous? */
|
560 | 942ac052 | balrog | } |
561 | 942ac052 | balrog | |
562 | 2ac71179 | Paul Brook | hw_error("bad interval\n");
|
563 | 942ac052 | balrog | } |
564 | 942ac052 | balrog | |
565 | bc24a225 | Paul Brook | static inline void musb_packet(MUSBState *s, MUSBEndPoint *ep, |
566 | 942ac052 | balrog | int epnum, int pid, int len, USBCallback cb, int dir) |
567 | 942ac052 | balrog | { |
568 | 942ac052 | balrog | int ret;
|
569 | 942ac052 | balrog | int idx = epnum && dir;
|
570 | 942ac052 | balrog | int ttype;
|
571 | 942ac052 | balrog | |
572 | 942ac052 | balrog | /* ep->type[0,1] contains:
|
573 | 942ac052 | balrog | * in bits 7:6 the speed (0 - invalid, 1 - high, 2 - full, 3 - slow)
|
574 | 942ac052 | balrog | * in bits 5:4 the transfer type (BULK / INT)
|
575 | 942ac052 | balrog | * in bits 3:0 the EP num
|
576 | 942ac052 | balrog | */
|
577 | 942ac052 | balrog | ttype = epnum ? (ep->type[idx] >> 4) & 3 : 0; |
578 | 942ac052 | balrog | |
579 | 942ac052 | balrog | ep->timeout[dir] = musb_timeout(ttype, |
580 | 942ac052 | balrog | ep->type[idx] >> 6, ep->interval[idx]);
|
581 | 942ac052 | balrog | ep->interrupt[dir] = ttype == USB_ENDPOINT_XFER_INT; |
582 | 942ac052 | balrog | ep->delayed_cb[dir] = cb; |
583 | 942ac052 | balrog | cb = dir ? musb_schedule1_cb : musb_schedule0_cb; |
584 | 942ac052 | balrog | |
585 | 942ac052 | balrog | ep->packey[dir].pid = pid; |
586 | 942ac052 | balrog | /* A wild guess on the FADDR semantics... */
|
587 | 942ac052 | balrog | ep->packey[dir].devaddr = ep->faddr[idx]; |
588 | 942ac052 | balrog | ep->packey[dir].devep = ep->type[idx] & 0xf;
|
589 | 942ac052 | balrog | ep->packey[dir].data = (void *) ep->buf[idx];
|
590 | 942ac052 | balrog | ep->packey[dir].len = len; |
591 | 942ac052 | balrog | ep->packey[dir].complete_cb = cb; |
592 | 942ac052 | balrog | ep->packey[dir].complete_opaque = ep; |
593 | 942ac052 | balrog | |
594 | 942ac052 | balrog | if (s->port.dev)
|
595 | 806b6024 | Gerd Hoffmann | ret = s->port.dev->info->handle_packet(s->port.dev, &ep->packey[dir]); |
596 | 942ac052 | balrog | else
|
597 | 942ac052 | balrog | ret = USB_RET_NODEV; |
598 | 942ac052 | balrog | |
599 | 942ac052 | balrog | if (ret == USB_RET_ASYNC) {
|
600 | 942ac052 | balrog | ep->status[dir] = len; |
601 | 942ac052 | balrog | return;
|
602 | 942ac052 | balrog | } |
603 | 942ac052 | balrog | |
604 | 942ac052 | balrog | ep->status[dir] = ret; |
605 | 942ac052 | balrog | usb_packet_complete(&ep->packey[dir]); |
606 | 942ac052 | balrog | } |
607 | 942ac052 | balrog | |
608 | 942ac052 | balrog | static void musb_tx_packet_complete(USBPacket *packey, void *opaque) |
609 | 942ac052 | balrog | { |
610 | 942ac052 | balrog | /* Unfortunately we can't use packey->devep because that's the remote
|
611 | 942ac052 | balrog | * endpoint number and may be different than our local. */
|
612 | bc24a225 | Paul Brook | MUSBEndPoint *ep = (MUSBEndPoint *) opaque; |
613 | 942ac052 | balrog | int epnum = ep->epnum;
|
614 | bc24a225 | Paul Brook | MUSBState *s = ep->musb; |
615 | 942ac052 | balrog | |
616 | 942ac052 | balrog | ep->fifostart[0] = 0; |
617 | 942ac052 | balrog | ep->fifolen[0] = 0; |
618 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
619 | 942ac052 | balrog | if (ep->status[0] != USB_RET_NAK) { |
620 | 942ac052 | balrog | #endif
|
621 | 942ac052 | balrog | if (epnum)
|
622 | 942ac052 | balrog | ep->csr[0] &= ~(MGC_M_TXCSR_FIFONOTEMPTY | MGC_M_TXCSR_TXPKTRDY);
|
623 | 942ac052 | balrog | else
|
624 | 942ac052 | balrog | ep->csr[0] &= ~MGC_M_CSR0_TXPKTRDY;
|
625 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
626 | 942ac052 | balrog | } |
627 | 942ac052 | balrog | #endif
|
628 | 942ac052 | balrog | |
629 | 942ac052 | balrog | /* Clear all of the error bits first */
|
630 | 942ac052 | balrog | if (epnum)
|
631 | 942ac052 | balrog | ep->csr[0] &= ~(MGC_M_TXCSR_H_ERROR | MGC_M_TXCSR_H_RXSTALL |
|
632 | 942ac052 | balrog | MGC_M_TXCSR_H_NAKTIMEOUT); |
633 | 942ac052 | balrog | else
|
634 | 942ac052 | balrog | ep->csr[0] &= ~(MGC_M_CSR0_H_ERROR | MGC_M_CSR0_H_RXSTALL |
|
635 | 942ac052 | balrog | MGC_M_CSR0_H_NAKTIMEOUT | MGC_M_CSR0_H_NO_PING); |
636 | 942ac052 | balrog | |
637 | 942ac052 | balrog | if (ep->status[0] == USB_RET_STALL) { |
638 | 942ac052 | balrog | /* Command not supported by target! */
|
639 | 942ac052 | balrog | ep->status[0] = 0; |
640 | 942ac052 | balrog | |
641 | 942ac052 | balrog | if (epnum)
|
642 | 942ac052 | balrog | ep->csr[0] |= MGC_M_TXCSR_H_RXSTALL;
|
643 | 942ac052 | balrog | else
|
644 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_H_RXSTALL;
|
645 | 942ac052 | balrog | } |
646 | 942ac052 | balrog | |
647 | 942ac052 | balrog | if (ep->status[0] == USB_RET_NAK) { |
648 | 942ac052 | balrog | ep->status[0] = 0; |
649 | 942ac052 | balrog | |
650 | 942ac052 | balrog | /* NAK timeouts are only generated in Bulk transfers and
|
651 | 942ac052 | balrog | * Data-errors in Isochronous. */
|
652 | 942ac052 | balrog | if (ep->interrupt[0]) { |
653 | 942ac052 | balrog | return;
|
654 | 942ac052 | balrog | } |
655 | 942ac052 | balrog | |
656 | 942ac052 | balrog | if (epnum)
|
657 | 942ac052 | balrog | ep->csr[0] |= MGC_M_TXCSR_H_NAKTIMEOUT;
|
658 | 942ac052 | balrog | else
|
659 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_H_NAKTIMEOUT;
|
660 | 942ac052 | balrog | } |
661 | 942ac052 | balrog | |
662 | 942ac052 | balrog | if (ep->status[0] < 0) { |
663 | 942ac052 | balrog | if (ep->status[0] == USB_RET_BABBLE) |
664 | 942ac052 | balrog | musb_intr_set(s, musb_irq_rst_babble, 1);
|
665 | 942ac052 | balrog | |
666 | 942ac052 | balrog | /* Pretend we've tried three times already and failed (in
|
667 | 942ac052 | balrog | * case of USB_TOKEN_SETUP). */
|
668 | 942ac052 | balrog | if (epnum)
|
669 | 942ac052 | balrog | ep->csr[0] |= MGC_M_TXCSR_H_ERROR;
|
670 | 942ac052 | balrog | else
|
671 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_H_ERROR;
|
672 | 942ac052 | balrog | |
673 | 942ac052 | balrog | musb_tx_intr_set(s, epnum, 1);
|
674 | 942ac052 | balrog | return;
|
675 | 942ac052 | balrog | } |
676 | 942ac052 | balrog | /* TODO: check len for over/underruns of an OUT packet? */
|
677 | 942ac052 | balrog | |
678 | 942ac052 | balrog | #ifdef SETUPLEN_HACK
|
679 | 942ac052 | balrog | if (!epnum && ep->packey[0].pid == USB_TOKEN_SETUP) |
680 | 942ac052 | balrog | s->setup_len = ep->packey[0].data[6]; |
681 | 942ac052 | balrog | #endif
|
682 | 942ac052 | balrog | |
683 | 942ac052 | balrog | /* In DMA mode: if no error, assert DMA request for this EP,
|
684 | 942ac052 | balrog | * and skip the interrupt. */
|
685 | 942ac052 | balrog | musb_tx_intr_set(s, epnum, 1);
|
686 | 942ac052 | balrog | } |
687 | 942ac052 | balrog | |
688 | 942ac052 | balrog | static void musb_rx_packet_complete(USBPacket *packey, void *opaque) |
689 | 942ac052 | balrog | { |
690 | 942ac052 | balrog | /* Unfortunately we can't use packey->devep because that's the remote
|
691 | 942ac052 | balrog | * endpoint number and may be different than our local. */
|
692 | bc24a225 | Paul Brook | MUSBEndPoint *ep = (MUSBEndPoint *) opaque; |
693 | 942ac052 | balrog | int epnum = ep->epnum;
|
694 | bc24a225 | Paul Brook | MUSBState *s = ep->musb; |
695 | 942ac052 | balrog | |
696 | 942ac052 | balrog | ep->fifostart[1] = 0; |
697 | 942ac052 | balrog | ep->fifolen[1] = 0; |
698 | 942ac052 | balrog | |
699 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
700 | 942ac052 | balrog | if (ep->status[1] != USB_RET_NAK) { |
701 | 942ac052 | balrog | #endif
|
702 | 942ac052 | balrog | ep->csr[1] &= ~MGC_M_RXCSR_H_REQPKT;
|
703 | 942ac052 | balrog | if (!epnum)
|
704 | 942ac052 | balrog | ep->csr[0] &= ~MGC_M_CSR0_H_REQPKT;
|
705 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
706 | 942ac052 | balrog | } |
707 | 942ac052 | balrog | #endif
|
708 | 942ac052 | balrog | |
709 | 942ac052 | balrog | /* Clear all of the imaginable error bits first */
|
710 | 942ac052 | balrog | ep->csr[1] &= ~(MGC_M_RXCSR_H_ERROR | MGC_M_RXCSR_H_RXSTALL |
|
711 | 942ac052 | balrog | MGC_M_RXCSR_DATAERROR); |
712 | 942ac052 | balrog | if (!epnum)
|
713 | 942ac052 | balrog | ep->csr[0] &= ~(MGC_M_CSR0_H_ERROR | MGC_M_CSR0_H_RXSTALL |
|
714 | 942ac052 | balrog | MGC_M_CSR0_H_NAKTIMEOUT | MGC_M_CSR0_H_NO_PING); |
715 | 942ac052 | balrog | |
716 | 942ac052 | balrog | if (ep->status[1] == USB_RET_STALL) { |
717 | 942ac052 | balrog | ep->status[1] = 0; |
718 | 942ac052 | balrog | packey->len = 0;
|
719 | 942ac052 | balrog | |
720 | 942ac052 | balrog | ep->csr[1] |= MGC_M_RXCSR_H_RXSTALL;
|
721 | 942ac052 | balrog | if (!epnum)
|
722 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_H_RXSTALL;
|
723 | 942ac052 | balrog | } |
724 | 942ac052 | balrog | |
725 | 942ac052 | balrog | if (ep->status[1] == USB_RET_NAK) { |
726 | 942ac052 | balrog | ep->status[1] = 0; |
727 | 942ac052 | balrog | |
728 | 942ac052 | balrog | /* NAK timeouts are only generated in Bulk transfers and
|
729 | 942ac052 | balrog | * Data-errors in Isochronous. */
|
730 | 942ac052 | balrog | if (ep->interrupt[1]) |
731 | 942ac052 | balrog | return musb_packet(s, ep, epnum, USB_TOKEN_IN,
|
732 | 942ac052 | balrog | packey->len, musb_rx_packet_complete, 1);
|
733 | 942ac052 | balrog | |
734 | 942ac052 | balrog | ep->csr[1] |= MGC_M_RXCSR_DATAERROR;
|
735 | 942ac052 | balrog | if (!epnum)
|
736 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_H_NAKTIMEOUT;
|
737 | 942ac052 | balrog | } |
738 | 942ac052 | balrog | |
739 | 942ac052 | balrog | if (ep->status[1] < 0) { |
740 | 942ac052 | balrog | if (ep->status[1] == USB_RET_BABBLE) { |
741 | 942ac052 | balrog | musb_intr_set(s, musb_irq_rst_babble, 1);
|
742 | 942ac052 | balrog | return;
|
743 | 942ac052 | balrog | } |
744 | 942ac052 | balrog | |
745 | 942ac052 | balrog | /* Pretend we've tried three times already and failed (in
|
746 | 942ac052 | balrog | * case of a control transfer). */
|
747 | 942ac052 | balrog | ep->csr[1] |= MGC_M_RXCSR_H_ERROR;
|
748 | 942ac052 | balrog | if (!epnum)
|
749 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_H_ERROR;
|
750 | 942ac052 | balrog | |
751 | 942ac052 | balrog | musb_rx_intr_set(s, epnum, 1);
|
752 | 942ac052 | balrog | return;
|
753 | 942ac052 | balrog | } |
754 | 942ac052 | balrog | /* TODO: check len for over/underruns of an OUT packet? */
|
755 | 942ac052 | balrog | /* TODO: perhaps make use of e->ext_size[1] here. */
|
756 | 942ac052 | balrog | |
757 | 942ac052 | balrog | packey->len = ep->status[1];
|
758 | 942ac052 | balrog | |
759 | 942ac052 | balrog | if (!(ep->csr[1] & (MGC_M_RXCSR_H_RXSTALL | MGC_M_RXCSR_DATAERROR))) { |
760 | 942ac052 | balrog | ep->csr[1] |= MGC_M_RXCSR_FIFOFULL | MGC_M_RXCSR_RXPKTRDY;
|
761 | 942ac052 | balrog | if (!epnum)
|
762 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_RXPKTRDY;
|
763 | 942ac052 | balrog | |
764 | 942ac052 | balrog | ep->rxcount = packey->len; /* XXX: MIN(packey->len, ep->maxp[1]); */
|
765 | 942ac052 | balrog | /* In DMA mode: assert DMA request for this EP */
|
766 | 942ac052 | balrog | } |
767 | 942ac052 | balrog | |
768 | 942ac052 | balrog | /* Only if DMA has not been asserted */
|
769 | 942ac052 | balrog | musb_rx_intr_set(s, epnum, 1);
|
770 | 942ac052 | balrog | } |
771 | 942ac052 | balrog | |
772 | bc24a225 | Paul Brook | static void musb_tx_rdy(MUSBState *s, int epnum) |
773 | 942ac052 | balrog | { |
774 | bc24a225 | Paul Brook | MUSBEndPoint *ep = s->ep + epnum; |
775 | 942ac052 | balrog | int pid;
|
776 | 942ac052 | balrog | int total, valid = 0; |
777 | 942ac052 | balrog | |
778 | 942ac052 | balrog | ep->fifostart[0] += ep->fifolen[0]; |
779 | 942ac052 | balrog | ep->fifolen[0] = 0; |
780 | 942ac052 | balrog | |
781 | 942ac052 | balrog | /* XXX: how's the total size of the packet retrieved exactly in
|
782 | 942ac052 | balrog | * the generic case? */
|
783 | 942ac052 | balrog | total = ep->maxp[0] & 0x3ff; |
784 | 942ac052 | balrog | |
785 | 942ac052 | balrog | if (ep->ext_size[0]) { |
786 | 942ac052 | balrog | total = ep->ext_size[0];
|
787 | 942ac052 | balrog | ep->ext_size[0] = 0; |
788 | 942ac052 | balrog | valid = 1;
|
789 | 942ac052 | balrog | } |
790 | 942ac052 | balrog | |
791 | 942ac052 | balrog | /* If the packet is not fully ready yet, wait for a next segment. */
|
792 | 942ac052 | balrog | if (epnum && (ep->fifostart[0] << 2) < total) |
793 | 942ac052 | balrog | return;
|
794 | 942ac052 | balrog | |
795 | 942ac052 | balrog | if (!valid)
|
796 | 942ac052 | balrog | total = ep->fifostart[0] << 2; |
797 | 942ac052 | balrog | |
798 | 942ac052 | balrog | pid = USB_TOKEN_OUT; |
799 | 942ac052 | balrog | if (!epnum && (ep->csr[0] & MGC_M_CSR0_H_SETUPPKT)) { |
800 | 942ac052 | balrog | pid = USB_TOKEN_SETUP; |
801 | 942ac052 | balrog | if (total != 8) |
802 | 942ac052 | balrog | printf("%s: illegal SETUPPKT length of %i bytes\n",
|
803 | 942ac052 | balrog | __FUNCTION__, total); |
804 | 942ac052 | balrog | /* Controller should retry SETUP packets three times on errors
|
805 | 942ac052 | balrog | * but it doesn't make sense for us to do that. */
|
806 | 942ac052 | balrog | } |
807 | 942ac052 | balrog | |
808 | 942ac052 | balrog | return musb_packet(s, ep, epnum, pid,
|
809 | 942ac052 | balrog | total, musb_tx_packet_complete, 0);
|
810 | 942ac052 | balrog | } |
811 | 942ac052 | balrog | |
812 | bc24a225 | Paul Brook | static void musb_rx_req(MUSBState *s, int epnum) |
813 | 942ac052 | balrog | { |
814 | bc24a225 | Paul Brook | MUSBEndPoint *ep = s->ep + epnum; |
815 | 942ac052 | balrog | int total;
|
816 | 942ac052 | balrog | |
817 | 942ac052 | balrog | /* If we already have a packet, which didn't fit into the
|
818 | 942ac052 | balrog | * 64 bytes of the FIFO, only move the FIFO start and return. (Obsolete) */
|
819 | 942ac052 | balrog | if (ep->packey[1].pid == USB_TOKEN_IN && ep->status[1] >= 0 && |
820 | 942ac052 | balrog | (ep->fifostart[1] << 2) + ep->rxcount < |
821 | 942ac052 | balrog | ep->packey[1].len) {
|
822 | 942ac052 | balrog | ep->fifostart[1] += ep->rxcount >> 2; |
823 | 942ac052 | balrog | ep->fifolen[1] = 0; |
824 | 942ac052 | balrog | |
825 | 942ac052 | balrog | ep->rxcount = MIN(ep->packey[0].len - (ep->fifostart[1] << 2), |
826 | 942ac052 | balrog | ep->maxp[1]);
|
827 | 942ac052 | balrog | |
828 | 942ac052 | balrog | ep->csr[1] &= ~MGC_M_RXCSR_H_REQPKT;
|
829 | 942ac052 | balrog | if (!epnum)
|
830 | 942ac052 | balrog | ep->csr[0] &= ~MGC_M_CSR0_H_REQPKT;
|
831 | 942ac052 | balrog | |
832 | 942ac052 | balrog | /* Clear all of the error bits first */
|
833 | 942ac052 | balrog | ep->csr[1] &= ~(MGC_M_RXCSR_H_ERROR | MGC_M_RXCSR_H_RXSTALL |
|
834 | 942ac052 | balrog | MGC_M_RXCSR_DATAERROR); |
835 | 942ac052 | balrog | if (!epnum)
|
836 | 942ac052 | balrog | ep->csr[0] &= ~(MGC_M_CSR0_H_ERROR | MGC_M_CSR0_H_RXSTALL |
|
837 | 942ac052 | balrog | MGC_M_CSR0_H_NAKTIMEOUT | MGC_M_CSR0_H_NO_PING); |
838 | 942ac052 | balrog | |
839 | 942ac052 | balrog | ep->csr[1] |= MGC_M_RXCSR_FIFOFULL | MGC_M_RXCSR_RXPKTRDY;
|
840 | 942ac052 | balrog | if (!epnum)
|
841 | 942ac052 | balrog | ep->csr[0] |= MGC_M_CSR0_RXPKTRDY;
|
842 | 942ac052 | balrog | musb_rx_intr_set(s, epnum, 1);
|
843 | 942ac052 | balrog | return;
|
844 | 942ac052 | balrog | } |
845 | 942ac052 | balrog | |
846 | 942ac052 | balrog | /* The driver sets maxp[1] to 64 or less because it knows the hardware
|
847 | 942ac052 | balrog | * FIFO is this deep. Bigger packets get split in
|
848 | 942ac052 | balrog | * usb_generic_handle_packet but we can also do the splitting locally
|
849 | 942ac052 | balrog | * for performance. It turns out we can also have a bigger FIFO and
|
850 | 942ac052 | balrog | * ignore the limit set in ep->maxp[1]. The Linux MUSB driver deals
|
851 | 942ac052 | balrog | * OK with single packets of even 32KB and we avoid splitting, however
|
852 | 942ac052 | balrog | * usb_msd.c sometimes sends a packet bigger than what Linux expects
|
853 | 942ac052 | balrog | * (e.g. 8192 bytes instead of 4096) and we get an OVERRUN. Splitting
|
854 | 942ac052 | balrog | * hides this overrun from Linux. Up to 4096 everything is fine
|
855 | 942ac052 | balrog | * though. Currently this is disabled.
|
856 | 942ac052 | balrog | *
|
857 | 942ac052 | balrog | * XXX: mind ep->fifosize. */
|
858 | 942ac052 | balrog | total = MIN(ep->maxp[1] & 0x3ff, sizeof(s->buf)); |
859 | 942ac052 | balrog | |
860 | 942ac052 | balrog | #ifdef SETUPLEN_HACK
|
861 | 942ac052 | balrog | /* Why should *we* do that instead of Linux? */
|
862 | 942ac052 | balrog | if (!epnum) {
|
863 | 942ac052 | balrog | if (ep->packey[0].devaddr == 2) |
864 | 942ac052 | balrog | total = MIN(s->setup_len, 8);
|
865 | 942ac052 | balrog | else
|
866 | 942ac052 | balrog | total = MIN(s->setup_len, 64);
|
867 | 942ac052 | balrog | s->setup_len -= total; |
868 | 942ac052 | balrog | } |
869 | 942ac052 | balrog | #endif
|
870 | 942ac052 | balrog | |
871 | 942ac052 | balrog | return musb_packet(s, ep, epnum, USB_TOKEN_IN,
|
872 | 942ac052 | balrog | total, musb_rx_packet_complete, 1);
|
873 | 942ac052 | balrog | } |
874 | 942ac052 | balrog | |
875 | bc24a225 | Paul Brook | static void musb_ep_frame_cancel(MUSBEndPoint *ep, int dir) |
876 | 942ac052 | balrog | { |
877 | 942ac052 | balrog | if (ep->intv_timer[dir])
|
878 | 942ac052 | balrog | qemu_del_timer(ep->intv_timer[dir]); |
879 | 942ac052 | balrog | } |
880 | 942ac052 | balrog | |
881 | 942ac052 | balrog | /* Bus control */
|
882 | 942ac052 | balrog | static uint8_t musb_busctl_readb(void *opaque, int ep, int addr) |
883 | 942ac052 | balrog | { |
884 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
885 | 942ac052 | balrog | |
886 | 942ac052 | balrog | switch (addr) {
|
887 | 942ac052 | balrog | /* For USB2.0 HS hubs only */
|
888 | 942ac052 | balrog | case MUSB_HDRC_TXHUBADDR:
|
889 | 942ac052 | balrog | return s->ep[ep].haddr[0]; |
890 | 942ac052 | balrog | case MUSB_HDRC_TXHUBPORT:
|
891 | 942ac052 | balrog | return s->ep[ep].hport[0]; |
892 | 942ac052 | balrog | case MUSB_HDRC_RXHUBADDR:
|
893 | 942ac052 | balrog | return s->ep[ep].haddr[1]; |
894 | 942ac052 | balrog | case MUSB_HDRC_RXHUBPORT:
|
895 | 942ac052 | balrog | return s->ep[ep].hport[1]; |
896 | 942ac052 | balrog | |
897 | 942ac052 | balrog | default:
|
898 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
|
899 | 942ac052 | balrog | return 0x00; |
900 | 942ac052 | balrog | }; |
901 | 942ac052 | balrog | } |
902 | 942ac052 | balrog | |
903 | 942ac052 | balrog | static void musb_busctl_writeb(void *opaque, int ep, int addr, uint8_t value) |
904 | 942ac052 | balrog | { |
905 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
906 | 942ac052 | balrog | |
907 | 942ac052 | balrog | switch (addr) {
|
908 | 942ac052 | balrog | case MUSB_HDRC_TXHUBADDR:
|
909 | 942ac052 | balrog | s->ep[ep].haddr[0] = value;
|
910 | 942ac052 | balrog | break;
|
911 | 942ac052 | balrog | case MUSB_HDRC_TXHUBPORT:
|
912 | 942ac052 | balrog | s->ep[ep].hport[0] = value;
|
913 | 942ac052 | balrog | break;
|
914 | 942ac052 | balrog | case MUSB_HDRC_RXHUBADDR:
|
915 | 942ac052 | balrog | s->ep[ep].haddr[1] = value;
|
916 | 942ac052 | balrog | break;
|
917 | 942ac052 | balrog | case MUSB_HDRC_RXHUBPORT:
|
918 | 942ac052 | balrog | s->ep[ep].hport[1] = value;
|
919 | 942ac052 | balrog | break;
|
920 | 942ac052 | balrog | |
921 | 942ac052 | balrog | default:
|
922 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
|
923 | 942ac052 | balrog | }; |
924 | 942ac052 | balrog | } |
925 | 942ac052 | balrog | |
926 | 942ac052 | balrog | static uint16_t musb_busctl_readh(void *opaque, int ep, int addr) |
927 | 942ac052 | balrog | { |
928 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
929 | 942ac052 | balrog | |
930 | 942ac052 | balrog | switch (addr) {
|
931 | 942ac052 | balrog | case MUSB_HDRC_TXFUNCADDR:
|
932 | 942ac052 | balrog | return s->ep[ep].faddr[0]; |
933 | 942ac052 | balrog | case MUSB_HDRC_RXFUNCADDR:
|
934 | 942ac052 | balrog | return s->ep[ep].faddr[1]; |
935 | 942ac052 | balrog | |
936 | 942ac052 | balrog | default:
|
937 | 942ac052 | balrog | return musb_busctl_readb(s, ep, addr) |
|
938 | 942ac052 | balrog | (musb_busctl_readb(s, ep, addr | 1) << 8); |
939 | 942ac052 | balrog | }; |
940 | 942ac052 | balrog | } |
941 | 942ac052 | balrog | |
942 | 942ac052 | balrog | static void musb_busctl_writeh(void *opaque, int ep, int addr, uint16_t value) |
943 | 942ac052 | balrog | { |
944 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
945 | 942ac052 | balrog | |
946 | 942ac052 | balrog | switch (addr) {
|
947 | 942ac052 | balrog | case MUSB_HDRC_TXFUNCADDR:
|
948 | 942ac052 | balrog | s->ep[ep].faddr[0] = value;
|
949 | 942ac052 | balrog | break;
|
950 | 942ac052 | balrog | case MUSB_HDRC_RXFUNCADDR:
|
951 | 942ac052 | balrog | s->ep[ep].faddr[1] = value;
|
952 | 942ac052 | balrog | break;
|
953 | 942ac052 | balrog | |
954 | 942ac052 | balrog | default:
|
955 | 942ac052 | balrog | musb_busctl_writeb(s, ep, addr, value & 0xff);
|
956 | 942ac052 | balrog | musb_busctl_writeb(s, ep, addr | 1, value >> 8); |
957 | 942ac052 | balrog | }; |
958 | 942ac052 | balrog | } |
959 | 942ac052 | balrog | |
960 | 942ac052 | balrog | /* Endpoint control */
|
961 | 942ac052 | balrog | static uint8_t musb_ep_readb(void *opaque, int ep, int addr) |
962 | 942ac052 | balrog | { |
963 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
964 | 942ac052 | balrog | |
965 | 942ac052 | balrog | switch (addr) {
|
966 | 942ac052 | balrog | case MUSB_HDRC_TXTYPE:
|
967 | 942ac052 | balrog | return s->ep[ep].type[0]; |
968 | 942ac052 | balrog | case MUSB_HDRC_TXINTERVAL:
|
969 | 942ac052 | balrog | return s->ep[ep].interval[0]; |
970 | 942ac052 | balrog | case MUSB_HDRC_RXTYPE:
|
971 | 942ac052 | balrog | return s->ep[ep].type[1]; |
972 | 942ac052 | balrog | case MUSB_HDRC_RXINTERVAL:
|
973 | 942ac052 | balrog | return s->ep[ep].interval[1]; |
974 | 942ac052 | balrog | case (MUSB_HDRC_FIFOSIZE & ~1): |
975 | 942ac052 | balrog | return 0x00; |
976 | 942ac052 | balrog | case MUSB_HDRC_FIFOSIZE:
|
977 | 942ac052 | balrog | return ep ? s->ep[ep].fifosize : s->ep[ep].config;
|
978 | 942ac052 | balrog | |
979 | 942ac052 | balrog | default:
|
980 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
|
981 | 942ac052 | balrog | return 0x00; |
982 | 942ac052 | balrog | }; |
983 | 942ac052 | balrog | } |
984 | 942ac052 | balrog | |
985 | 942ac052 | balrog | static void musb_ep_writeb(void *opaque, int ep, int addr, uint8_t value) |
986 | 942ac052 | balrog | { |
987 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
988 | 942ac052 | balrog | |
989 | 942ac052 | balrog | switch (addr) {
|
990 | 942ac052 | balrog | case MUSB_HDRC_TXTYPE:
|
991 | 942ac052 | balrog | s->ep[ep].type[0] = value;
|
992 | 942ac052 | balrog | break;
|
993 | 942ac052 | balrog | case MUSB_HDRC_TXINTERVAL:
|
994 | 942ac052 | balrog | s->ep[ep].interval[0] = value;
|
995 | 942ac052 | balrog | musb_ep_frame_cancel(&s->ep[ep], 0);
|
996 | 942ac052 | balrog | break;
|
997 | 942ac052 | balrog | case MUSB_HDRC_RXTYPE:
|
998 | 942ac052 | balrog | s->ep[ep].type[1] = value;
|
999 | 942ac052 | balrog | break;
|
1000 | 942ac052 | balrog | case MUSB_HDRC_RXINTERVAL:
|
1001 | 942ac052 | balrog | s->ep[ep].interval[1] = value;
|
1002 | 942ac052 | balrog | musb_ep_frame_cancel(&s->ep[ep], 1);
|
1003 | 942ac052 | balrog | break;
|
1004 | 942ac052 | balrog | case (MUSB_HDRC_FIFOSIZE & ~1): |
1005 | 942ac052 | balrog | break;
|
1006 | 942ac052 | balrog | case MUSB_HDRC_FIFOSIZE:
|
1007 | 942ac052 | balrog | printf("%s: somebody messes with fifosize (now %i bytes)\n",
|
1008 | 942ac052 | balrog | __FUNCTION__, value); |
1009 | 942ac052 | balrog | s->ep[ep].fifosize = value; |
1010 | 942ac052 | balrog | break;
|
1011 | 942ac052 | balrog | |
1012 | 942ac052 | balrog | default:
|
1013 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, addr);
|
1014 | 942ac052 | balrog | }; |
1015 | 942ac052 | balrog | } |
1016 | 942ac052 | balrog | |
1017 | 942ac052 | balrog | static uint16_t musb_ep_readh(void *opaque, int ep, int addr) |
1018 | 942ac052 | balrog | { |
1019 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1020 | 942ac052 | balrog | uint16_t ret; |
1021 | 942ac052 | balrog | |
1022 | 942ac052 | balrog | switch (addr) {
|
1023 | 942ac052 | balrog | case MUSB_HDRC_TXMAXP:
|
1024 | 942ac052 | balrog | return s->ep[ep].maxp[0]; |
1025 | 942ac052 | balrog | case MUSB_HDRC_TXCSR:
|
1026 | 942ac052 | balrog | return s->ep[ep].csr[0]; |
1027 | 942ac052 | balrog | case MUSB_HDRC_RXMAXP:
|
1028 | 942ac052 | balrog | return s->ep[ep].maxp[1]; |
1029 | 942ac052 | balrog | case MUSB_HDRC_RXCSR:
|
1030 | 942ac052 | balrog | ret = s->ep[ep].csr[1];
|
1031 | 942ac052 | balrog | |
1032 | 942ac052 | balrog | /* TODO: This and other bits probably depend on
|
1033 | 942ac052 | balrog | * ep->csr[1] & MGC_M_RXCSR_AUTOCLEAR. */
|
1034 | 942ac052 | balrog | if (s->ep[ep].csr[1] & MGC_M_RXCSR_AUTOCLEAR) |
1035 | 942ac052 | balrog | s->ep[ep].csr[1] &= ~MGC_M_RXCSR_RXPKTRDY;
|
1036 | 942ac052 | balrog | |
1037 | 942ac052 | balrog | return ret;
|
1038 | 942ac052 | balrog | case MUSB_HDRC_RXCOUNT:
|
1039 | 942ac052 | balrog | return s->ep[ep].rxcount;
|
1040 | 942ac052 | balrog | |
1041 | 942ac052 | balrog | default:
|
1042 | 942ac052 | balrog | return musb_ep_readb(s, ep, addr) |
|
1043 | 942ac052 | balrog | (musb_ep_readb(s, ep, addr | 1) << 8); |
1044 | 942ac052 | balrog | }; |
1045 | 942ac052 | balrog | } |
1046 | 942ac052 | balrog | |
1047 | 942ac052 | balrog | static void musb_ep_writeh(void *opaque, int ep, int addr, uint16_t value) |
1048 | 942ac052 | balrog | { |
1049 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1050 | 942ac052 | balrog | |
1051 | 942ac052 | balrog | switch (addr) {
|
1052 | 942ac052 | balrog | case MUSB_HDRC_TXMAXP:
|
1053 | 942ac052 | balrog | s->ep[ep].maxp[0] = value;
|
1054 | 942ac052 | balrog | break;
|
1055 | 942ac052 | balrog | case MUSB_HDRC_TXCSR:
|
1056 | 942ac052 | balrog | if (ep) {
|
1057 | 942ac052 | balrog | s->ep[ep].csr[0] &= value & 0xa6; |
1058 | 942ac052 | balrog | s->ep[ep].csr[0] |= value & 0xff59; |
1059 | 942ac052 | balrog | } else {
|
1060 | 942ac052 | balrog | s->ep[ep].csr[0] &= value & 0x85; |
1061 | 942ac052 | balrog | s->ep[ep].csr[0] |= value & 0xf7a; |
1062 | 942ac052 | balrog | } |
1063 | 942ac052 | balrog | |
1064 | 942ac052 | balrog | musb_ep_frame_cancel(&s->ep[ep], 0);
|
1065 | 942ac052 | balrog | |
1066 | 942ac052 | balrog | if ((ep && (value & MGC_M_TXCSR_FLUSHFIFO)) ||
|
1067 | 942ac052 | balrog | (!ep && (value & MGC_M_CSR0_FLUSHFIFO))) { |
1068 | 942ac052 | balrog | s->ep[ep].fifolen[0] = 0; |
1069 | 942ac052 | balrog | s->ep[ep].fifostart[0] = 0; |
1070 | 942ac052 | balrog | if (ep)
|
1071 | 942ac052 | balrog | s->ep[ep].csr[0] &=
|
1072 | 942ac052 | balrog | ~(MGC_M_TXCSR_FIFONOTEMPTY | MGC_M_TXCSR_TXPKTRDY); |
1073 | 942ac052 | balrog | else
|
1074 | 942ac052 | balrog | s->ep[ep].csr[0] &=
|
1075 | 942ac052 | balrog | ~(MGC_M_CSR0_TXPKTRDY | MGC_M_CSR0_RXPKTRDY); |
1076 | 942ac052 | balrog | } |
1077 | 942ac052 | balrog | if (
|
1078 | 942ac052 | balrog | (ep && |
1079 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
1080 | 942ac052 | balrog | (value & MGC_M_TXCSR_TXPKTRDY) && |
1081 | 942ac052 | balrog | !(value & MGC_M_TXCSR_H_NAKTIMEOUT)) || |
1082 | 942ac052 | balrog | #else
|
1083 | 942ac052 | balrog | (value & MGC_M_TXCSR_TXPKTRDY)) || |
1084 | 942ac052 | balrog | #endif
|
1085 | 942ac052 | balrog | (!ep && |
1086 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
1087 | 942ac052 | balrog | (value & MGC_M_CSR0_TXPKTRDY) && |
1088 | 942ac052 | balrog | !(value & MGC_M_CSR0_H_NAKTIMEOUT))) |
1089 | 942ac052 | balrog | #else
|
1090 | 942ac052 | balrog | (value & MGC_M_CSR0_TXPKTRDY))) |
1091 | 942ac052 | balrog | #endif
|
1092 | 942ac052 | balrog | musb_tx_rdy(s, ep); |
1093 | 942ac052 | balrog | if (!ep &&
|
1094 | 942ac052 | balrog | (value & MGC_M_CSR0_H_REQPKT) && |
1095 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
1096 | 942ac052 | balrog | !(value & (MGC_M_CSR0_H_NAKTIMEOUT | |
1097 | 942ac052 | balrog | MGC_M_CSR0_RXPKTRDY))) |
1098 | 942ac052 | balrog | #else
|
1099 | 942ac052 | balrog | !(value & MGC_M_CSR0_RXPKTRDY)) |
1100 | 942ac052 | balrog | #endif
|
1101 | 942ac052 | balrog | musb_rx_req(s, ep); |
1102 | 942ac052 | balrog | break;
|
1103 | 942ac052 | balrog | |
1104 | 942ac052 | balrog | case MUSB_HDRC_RXMAXP:
|
1105 | 942ac052 | balrog | s->ep[ep].maxp[1] = value;
|
1106 | 942ac052 | balrog | break;
|
1107 | 942ac052 | balrog | case MUSB_HDRC_RXCSR:
|
1108 | 942ac052 | balrog | /* (DMA mode only) */
|
1109 | 942ac052 | balrog | if (
|
1110 | 942ac052 | balrog | (value & MGC_M_RXCSR_H_AUTOREQ) && |
1111 | 942ac052 | balrog | !(value & MGC_M_RXCSR_RXPKTRDY) && |
1112 | 942ac052 | balrog | (s->ep[ep].csr[1] & MGC_M_RXCSR_RXPKTRDY))
|
1113 | 942ac052 | balrog | value |= MGC_M_RXCSR_H_REQPKT; |
1114 | 942ac052 | balrog | |
1115 | 942ac052 | balrog | s->ep[ep].csr[1] &= 0x102 | (value & 0x4d); |
1116 | 942ac052 | balrog | s->ep[ep].csr[1] |= value & 0xfeb0; |
1117 | 942ac052 | balrog | |
1118 | 942ac052 | balrog | musb_ep_frame_cancel(&s->ep[ep], 1);
|
1119 | 942ac052 | balrog | |
1120 | 942ac052 | balrog | if (value & MGC_M_RXCSR_FLUSHFIFO) {
|
1121 | 942ac052 | balrog | s->ep[ep].fifolen[1] = 0; |
1122 | 942ac052 | balrog | s->ep[ep].fifostart[1] = 0; |
1123 | 942ac052 | balrog | s->ep[ep].csr[1] &= ~(MGC_M_RXCSR_FIFOFULL | MGC_M_RXCSR_RXPKTRDY);
|
1124 | 942ac052 | balrog | /* If double buffering and we have two packets ready, flush
|
1125 | 942ac052 | balrog | * only the first one and set up the fifo at the second packet. */
|
1126 | 942ac052 | balrog | } |
1127 | 942ac052 | balrog | #ifdef CLEAR_NAK
|
1128 | 942ac052 | balrog | if ((value & MGC_M_RXCSR_H_REQPKT) && !(value & MGC_M_RXCSR_DATAERROR))
|
1129 | 942ac052 | balrog | #else
|
1130 | 942ac052 | balrog | if (value & MGC_M_RXCSR_H_REQPKT)
|
1131 | 942ac052 | balrog | #endif
|
1132 | 942ac052 | balrog | musb_rx_req(s, ep); |
1133 | 942ac052 | balrog | break;
|
1134 | 942ac052 | balrog | case MUSB_HDRC_RXCOUNT:
|
1135 | 942ac052 | balrog | s->ep[ep].rxcount = value; |
1136 | 942ac052 | balrog | break;
|
1137 | 942ac052 | balrog | |
1138 | 942ac052 | balrog | default:
|
1139 | 942ac052 | balrog | musb_ep_writeb(s, ep, addr, value & 0xff);
|
1140 | 942ac052 | balrog | musb_ep_writeb(s, ep, addr | 1, value >> 8); |
1141 | 942ac052 | balrog | }; |
1142 | 942ac052 | balrog | } |
1143 | 942ac052 | balrog | |
1144 | 942ac052 | balrog | /* Generic control */
|
1145 | c227f099 | Anthony Liguori | static uint32_t musb_readb(void *opaque, target_phys_addr_t addr) |
1146 | 942ac052 | balrog | { |
1147 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1148 | 942ac052 | balrog | int ep, i;
|
1149 | 942ac052 | balrog | uint8_t ret; |
1150 | 942ac052 | balrog | |
1151 | 942ac052 | balrog | switch (addr) {
|
1152 | 942ac052 | balrog | case MUSB_HDRC_FADDR:
|
1153 | 942ac052 | balrog | return s->faddr;
|
1154 | 942ac052 | balrog | case MUSB_HDRC_POWER:
|
1155 | 942ac052 | balrog | return s->power;
|
1156 | 942ac052 | balrog | case MUSB_HDRC_INTRUSB:
|
1157 | 942ac052 | balrog | ret = s->intr; |
1158 | 942ac052 | balrog | for (i = 0; i < sizeof(ret) * 8; i ++) |
1159 | 942ac052 | balrog | if (ret & (1 << i)) |
1160 | 942ac052 | balrog | musb_intr_set(s, i, 0);
|
1161 | 942ac052 | balrog | return ret;
|
1162 | 942ac052 | balrog | case MUSB_HDRC_INTRUSBE:
|
1163 | 942ac052 | balrog | return s->mask;
|
1164 | 942ac052 | balrog | case MUSB_HDRC_INDEX:
|
1165 | 942ac052 | balrog | return s->idx;
|
1166 | 942ac052 | balrog | case MUSB_HDRC_TESTMODE:
|
1167 | 942ac052 | balrog | return 0x00; |
1168 | 942ac052 | balrog | |
1169 | 942ac052 | balrog | case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf): |
1170 | 942ac052 | balrog | return musb_ep_readb(s, s->idx, addr & 0xf); |
1171 | 942ac052 | balrog | |
1172 | 942ac052 | balrog | case MUSB_HDRC_DEVCTL:
|
1173 | 942ac052 | balrog | return s->devctl;
|
1174 | 942ac052 | balrog | |
1175 | 942ac052 | balrog | case MUSB_HDRC_TXFIFOSZ:
|
1176 | 942ac052 | balrog | case MUSB_HDRC_RXFIFOSZ:
|
1177 | 942ac052 | balrog | case MUSB_HDRC_VCTRL:
|
1178 | 942ac052 | balrog | /* TODO */
|
1179 | 942ac052 | balrog | return 0x00; |
1180 | 942ac052 | balrog | |
1181 | 942ac052 | balrog | case MUSB_HDRC_HWVERS:
|
1182 | 942ac052 | balrog | return (1 << 10) | 400; |
1183 | 942ac052 | balrog | |
1184 | 942ac052 | balrog | case (MUSB_HDRC_VCTRL | 1): |
1185 | 942ac052 | balrog | case (MUSB_HDRC_HWVERS | 1): |
1186 | 942ac052 | balrog | case (MUSB_HDRC_DEVCTL | 1): |
1187 | 942ac052 | balrog | return 0x00; |
1188 | 942ac052 | balrog | |
1189 | 942ac052 | balrog | case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f): |
1190 | 942ac052 | balrog | ep = (addr >> 3) & 0xf; |
1191 | 942ac052 | balrog | return musb_busctl_readb(s, ep, addr & 0x7); |
1192 | 942ac052 | balrog | |
1193 | 942ac052 | balrog | case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff): |
1194 | 942ac052 | balrog | ep = (addr >> 4) & 0xf; |
1195 | 942ac052 | balrog | return musb_ep_readb(s, ep, addr & 0xf); |
1196 | 942ac052 | balrog | |
1197 | 942ac052 | balrog | default:
|
1198 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr); |
1199 | 942ac052 | balrog | return 0x00; |
1200 | 942ac052 | balrog | }; |
1201 | 942ac052 | balrog | } |
1202 | 942ac052 | balrog | |
1203 | c227f099 | Anthony Liguori | static void musb_writeb(void *opaque, target_phys_addr_t addr, uint32_t value) |
1204 | 942ac052 | balrog | { |
1205 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1206 | 942ac052 | balrog | int ep;
|
1207 | 942ac052 | balrog | |
1208 | 942ac052 | balrog | switch (addr) {
|
1209 | 942ac052 | balrog | case MUSB_HDRC_FADDR:
|
1210 | 942ac052 | balrog | s->faddr = value & 0x7f;
|
1211 | 942ac052 | balrog | break;
|
1212 | 942ac052 | balrog | case MUSB_HDRC_POWER:
|
1213 | 942ac052 | balrog | s->power = (value & 0xef) | (s->power & 0x10); |
1214 | 942ac052 | balrog | /* MGC_M_POWER_RESET is also read-only in Peripheral Mode */
|
1215 | 942ac052 | balrog | if ((value & MGC_M_POWER_RESET) && s->port.dev) {
|
1216 | 942ac052 | balrog | usb_send_msg(s->port.dev, USB_MSG_RESET); |
1217 | 942ac052 | balrog | /* Negotiate high-speed operation if MGC_M_POWER_HSENAB is set. */
|
1218 | 942ac052 | balrog | if ((value & MGC_M_POWER_HSENAB) &&
|
1219 | 942ac052 | balrog | s->port.dev->speed == USB_SPEED_HIGH) |
1220 | 942ac052 | balrog | s->power |= MGC_M_POWER_HSMODE; /* Success */
|
1221 | 942ac052 | balrog | /* Restart frame counting. */
|
1222 | 942ac052 | balrog | } |
1223 | 942ac052 | balrog | if (value & MGC_M_POWER_SUSPENDM) {
|
1224 | 942ac052 | balrog | /* When all transfers finish, suspend and if MGC_M_POWER_ENSUSPEND
|
1225 | 942ac052 | balrog | * is set, also go into low power mode. Frame counting stops. */
|
1226 | 942ac052 | balrog | /* XXX: Cleared when the interrupt register is read */
|
1227 | 942ac052 | balrog | } |
1228 | 942ac052 | balrog | if (value & MGC_M_POWER_RESUME) {
|
1229 | 942ac052 | balrog | /* Wait 20ms and signal resuming on the bus. Frame counting
|
1230 | 942ac052 | balrog | * restarts. */
|
1231 | 942ac052 | balrog | } |
1232 | 942ac052 | balrog | break;
|
1233 | 942ac052 | balrog | case MUSB_HDRC_INTRUSB:
|
1234 | 942ac052 | balrog | break;
|
1235 | 942ac052 | balrog | case MUSB_HDRC_INTRUSBE:
|
1236 | 942ac052 | balrog | s->mask = value & 0xff;
|
1237 | 942ac052 | balrog | break;
|
1238 | 942ac052 | balrog | case MUSB_HDRC_INDEX:
|
1239 | 942ac052 | balrog | s->idx = value & 0xf;
|
1240 | 942ac052 | balrog | break;
|
1241 | 942ac052 | balrog | case MUSB_HDRC_TESTMODE:
|
1242 | 942ac052 | balrog | break;
|
1243 | 942ac052 | balrog | |
1244 | 942ac052 | balrog | case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf): |
1245 | 942ac052 | balrog | musb_ep_writeb(s, s->idx, addr & 0xf, value);
|
1246 | 942ac052 | balrog | break;
|
1247 | 942ac052 | balrog | |
1248 | 942ac052 | balrog | case MUSB_HDRC_DEVCTL:
|
1249 | 942ac052 | balrog | s->session = !!(value & MGC_M_DEVCTL_SESSION); |
1250 | 942ac052 | balrog | musb_session_update(s, |
1251 | 942ac052 | balrog | !!s->port.dev, |
1252 | 942ac052 | balrog | !!(s->devctl & MGC_M_DEVCTL_SESSION)); |
1253 | 942ac052 | balrog | |
1254 | 942ac052 | balrog | /* It seems this is the only R/W bit in this register? */
|
1255 | 942ac052 | balrog | s->devctl &= ~MGC_M_DEVCTL_SESSION; |
1256 | 942ac052 | balrog | s->devctl |= value & MGC_M_DEVCTL_SESSION; |
1257 | 942ac052 | balrog | break;
|
1258 | 942ac052 | balrog | |
1259 | 942ac052 | balrog | case MUSB_HDRC_TXFIFOSZ:
|
1260 | 942ac052 | balrog | case MUSB_HDRC_RXFIFOSZ:
|
1261 | 942ac052 | balrog | case MUSB_HDRC_VCTRL:
|
1262 | 942ac052 | balrog | /* TODO */
|
1263 | 942ac052 | balrog | break;
|
1264 | 942ac052 | balrog | |
1265 | 942ac052 | balrog | case (MUSB_HDRC_VCTRL | 1): |
1266 | 942ac052 | balrog | case (MUSB_HDRC_DEVCTL | 1): |
1267 | 942ac052 | balrog | break;
|
1268 | 942ac052 | balrog | |
1269 | 942ac052 | balrog | case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f): |
1270 | 942ac052 | balrog | ep = (addr >> 3) & 0xf; |
1271 | 942ac052 | balrog | musb_busctl_writeb(s, ep, addr & 0x7, value);
|
1272 | 942ac052 | balrog | break;
|
1273 | 942ac052 | balrog | |
1274 | 942ac052 | balrog | case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff): |
1275 | 942ac052 | balrog | ep = (addr >> 4) & 0xf; |
1276 | 942ac052 | balrog | musb_ep_writeb(s, ep, addr & 0xf, value);
|
1277 | 942ac052 | balrog | break;
|
1278 | 942ac052 | balrog | |
1279 | 942ac052 | balrog | default:
|
1280 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr); |
1281 | 942ac052 | balrog | }; |
1282 | 942ac052 | balrog | } |
1283 | 942ac052 | balrog | |
1284 | c227f099 | Anthony Liguori | static uint32_t musb_readh(void *opaque, target_phys_addr_t addr) |
1285 | 942ac052 | balrog | { |
1286 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1287 | 942ac052 | balrog | int ep, i;
|
1288 | 942ac052 | balrog | uint16_t ret; |
1289 | 942ac052 | balrog | |
1290 | 942ac052 | balrog | switch (addr) {
|
1291 | 942ac052 | balrog | case MUSB_HDRC_INTRTX:
|
1292 | 942ac052 | balrog | ret = s->tx_intr; |
1293 | 942ac052 | balrog | /* Auto clear */
|
1294 | 942ac052 | balrog | for (i = 0; i < sizeof(ret) * 8; i ++) |
1295 | 942ac052 | balrog | if (ret & (1 << i)) |
1296 | 942ac052 | balrog | musb_tx_intr_set(s, i, 0);
|
1297 | 942ac052 | balrog | return ret;
|
1298 | 942ac052 | balrog | case MUSB_HDRC_INTRRX:
|
1299 | 942ac052 | balrog | ret = s->rx_intr; |
1300 | 942ac052 | balrog | /* Auto clear */
|
1301 | 942ac052 | balrog | for (i = 0; i < sizeof(ret) * 8; i ++) |
1302 | 942ac052 | balrog | if (ret & (1 << i)) |
1303 | 942ac052 | balrog | musb_rx_intr_set(s, i, 0);
|
1304 | 942ac052 | balrog | return ret;
|
1305 | 942ac052 | balrog | case MUSB_HDRC_INTRTXE:
|
1306 | 942ac052 | balrog | return s->tx_mask;
|
1307 | 942ac052 | balrog | case MUSB_HDRC_INTRRXE:
|
1308 | 942ac052 | balrog | return s->rx_mask;
|
1309 | 942ac052 | balrog | |
1310 | 942ac052 | balrog | case MUSB_HDRC_FRAME:
|
1311 | 942ac052 | balrog | /* TODO */
|
1312 | 942ac052 | balrog | return 0x0000; |
1313 | 942ac052 | balrog | case MUSB_HDRC_TXFIFOADDR:
|
1314 | 942ac052 | balrog | return s->ep[s->idx].fifoaddr[0]; |
1315 | 942ac052 | balrog | case MUSB_HDRC_RXFIFOADDR:
|
1316 | 942ac052 | balrog | return s->ep[s->idx].fifoaddr[1]; |
1317 | 942ac052 | balrog | |
1318 | 942ac052 | balrog | case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf): |
1319 | 942ac052 | balrog | return musb_ep_readh(s, s->idx, addr & 0xf); |
1320 | 942ac052 | balrog | |
1321 | 942ac052 | balrog | case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f): |
1322 | 942ac052 | balrog | ep = (addr >> 3) & 0xf; |
1323 | 942ac052 | balrog | return musb_busctl_readh(s, ep, addr & 0x7); |
1324 | 942ac052 | balrog | |
1325 | 942ac052 | balrog | case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff): |
1326 | 942ac052 | balrog | ep = (addr >> 4) & 0xf; |
1327 | 942ac052 | balrog | return musb_ep_readh(s, ep, addr & 0xf); |
1328 | 942ac052 | balrog | |
1329 | 942ac052 | balrog | default:
|
1330 | 942ac052 | balrog | return musb_readb(s, addr) | (musb_readb(s, addr | 1) << 8); |
1331 | 942ac052 | balrog | }; |
1332 | 942ac052 | balrog | } |
1333 | 942ac052 | balrog | |
1334 | c227f099 | Anthony Liguori | static void musb_writeh(void *opaque, target_phys_addr_t addr, uint32_t value) |
1335 | 942ac052 | balrog | { |
1336 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1337 | 942ac052 | balrog | int ep;
|
1338 | 942ac052 | balrog | |
1339 | 942ac052 | balrog | switch (addr) {
|
1340 | 942ac052 | balrog | case MUSB_HDRC_INTRTXE:
|
1341 | 942ac052 | balrog | s->tx_mask = value; |
1342 | 942ac052 | balrog | /* XXX: the masks seem to apply on the raising edge like with
|
1343 | 942ac052 | balrog | * edge-triggered interrupts, thus no need to update. I may be
|
1344 | 942ac052 | balrog | * wrong though. */
|
1345 | 942ac052 | balrog | break;
|
1346 | 942ac052 | balrog | case MUSB_HDRC_INTRRXE:
|
1347 | 942ac052 | balrog | s->rx_mask = value; |
1348 | 942ac052 | balrog | break;
|
1349 | 942ac052 | balrog | |
1350 | 942ac052 | balrog | case MUSB_HDRC_FRAME:
|
1351 | 942ac052 | balrog | /* TODO */
|
1352 | 942ac052 | balrog | break;
|
1353 | 942ac052 | balrog | case MUSB_HDRC_TXFIFOADDR:
|
1354 | 942ac052 | balrog | s->ep[s->idx].fifoaddr[0] = value;
|
1355 | 942ac052 | balrog | s->ep[s->idx].buf[0] =
|
1356 | 942ac052 | balrog | s->buf + ((value << 1) & (sizeof(s->buf) / 4 - 1)); |
1357 | 942ac052 | balrog | break;
|
1358 | 942ac052 | balrog | case MUSB_HDRC_RXFIFOADDR:
|
1359 | 942ac052 | balrog | s->ep[s->idx].fifoaddr[1] = value;
|
1360 | 942ac052 | balrog | s->ep[s->idx].buf[1] =
|
1361 | 942ac052 | balrog | s->buf + ((value << 1) & (sizeof(s->buf) / 4 - 1)); |
1362 | 942ac052 | balrog | break;
|
1363 | 942ac052 | balrog | |
1364 | 942ac052 | balrog | case MUSB_HDRC_EP_IDX ... (MUSB_HDRC_EP_IDX + 0xf): |
1365 | 942ac052 | balrog | musb_ep_writeh(s, s->idx, addr & 0xf, value);
|
1366 | 942ac052 | balrog | break;
|
1367 | 942ac052 | balrog | |
1368 | 942ac052 | balrog | case MUSB_HDRC_BUSCTL ... (MUSB_HDRC_BUSCTL + 0x7f): |
1369 | 942ac052 | balrog | ep = (addr >> 3) & 0xf; |
1370 | 942ac052 | balrog | musb_busctl_writeh(s, ep, addr & 0x7, value);
|
1371 | 942ac052 | balrog | break;
|
1372 | 942ac052 | balrog | |
1373 | 942ac052 | balrog | case MUSB_HDRC_EP ... (MUSB_HDRC_EP + 0xff): |
1374 | 942ac052 | balrog | ep = (addr >> 4) & 0xf; |
1375 | 942ac052 | balrog | musb_ep_writeh(s, ep, addr & 0xf, value);
|
1376 | 942ac052 | balrog | break;
|
1377 | 942ac052 | balrog | |
1378 | 942ac052 | balrog | default:
|
1379 | 942ac052 | balrog | musb_writeb(s, addr, value & 0xff);
|
1380 | 942ac052 | balrog | musb_writeb(s, addr | 1, value >> 8); |
1381 | 942ac052 | balrog | }; |
1382 | 942ac052 | balrog | } |
1383 | 942ac052 | balrog | |
1384 | c227f099 | Anthony Liguori | static uint32_t musb_readw(void *opaque, target_phys_addr_t addr) |
1385 | 942ac052 | balrog | { |
1386 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1387 | bc24a225 | Paul Brook | MUSBEndPoint *ep; |
1388 | 942ac052 | balrog | int epnum;
|
1389 | 942ac052 | balrog | |
1390 | 942ac052 | balrog | switch (addr) {
|
1391 | 942ac052 | balrog | case MUSB_HDRC_FIFO ... (MUSB_HDRC_FIFO + 0x3f): |
1392 | 942ac052 | balrog | epnum = ((addr - MUSB_HDRC_FIFO) >> 2) & 0xf; |
1393 | 942ac052 | balrog | ep = s->ep + epnum; |
1394 | 942ac052 | balrog | |
1395 | 942ac052 | balrog | if (ep->fifolen[1] >= 16) { |
1396 | 942ac052 | balrog | /* We have a FIFO underrun */
|
1397 | 942ac052 | balrog | printf("%s: EP%i FIFO is now empty, stop reading\n",
|
1398 | 942ac052 | balrog | __FUNCTION__, epnum); |
1399 | 942ac052 | balrog | return 0x00000000; |
1400 | 942ac052 | balrog | } |
1401 | 942ac052 | balrog | /* In DMA mode clear RXPKTRDY and set REQPKT automatically
|
1402 | 942ac052 | balrog | * (if AUTOREQ is set) */
|
1403 | 942ac052 | balrog | |
1404 | 942ac052 | balrog | ep->csr[1] &= ~MGC_M_RXCSR_FIFOFULL;
|
1405 | 942ac052 | balrog | return ep->buf[1][ep->fifostart[1] + ep->fifolen[1] ++]; |
1406 | 942ac052 | balrog | |
1407 | 942ac052 | balrog | default:
|
1408 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr); |
1409 | 942ac052 | balrog | return 0x00000000; |
1410 | 942ac052 | balrog | }; |
1411 | 942ac052 | balrog | } |
1412 | 942ac052 | balrog | |
1413 | c227f099 | Anthony Liguori | static void musb_writew(void *opaque, target_phys_addr_t addr, uint32_t value) |
1414 | 942ac052 | balrog | { |
1415 | bc24a225 | Paul Brook | MUSBState *s = (MUSBState *) opaque; |
1416 | bc24a225 | Paul Brook | MUSBEndPoint *ep; |
1417 | 942ac052 | balrog | int epnum;
|
1418 | 942ac052 | balrog | |
1419 | 942ac052 | balrog | switch (addr) {
|
1420 | 942ac052 | balrog | case MUSB_HDRC_FIFO ... (MUSB_HDRC_FIFO + 0x3f): |
1421 | 942ac052 | balrog | epnum = ((addr - MUSB_HDRC_FIFO) >> 2) & 0xf; |
1422 | 942ac052 | balrog | ep = s->ep + epnum; |
1423 | 942ac052 | balrog | |
1424 | 942ac052 | balrog | if (ep->fifolen[0] >= 16) { |
1425 | 942ac052 | balrog | /* We have a FIFO overrun */
|
1426 | 942ac052 | balrog | printf("%s: EP%i FIFO exceeded 64 bytes, stop feeding data\n",
|
1427 | 942ac052 | balrog | __FUNCTION__, epnum); |
1428 | 942ac052 | balrog | break;
|
1429 | 942ac052 | balrog | } |
1430 | 942ac052 | balrog | |
1431 | 942ac052 | balrog | ep->buf[0][ep->fifostart[0] + ep->fifolen[0] ++] = value; |
1432 | 942ac052 | balrog | if (epnum)
|
1433 | 942ac052 | balrog | ep->csr[0] |= MGC_M_TXCSR_FIFONOTEMPTY;
|
1434 | 942ac052 | balrog | break;
|
1435 | 942ac052 | balrog | |
1436 | 942ac052 | balrog | default:
|
1437 | 942ac052 | balrog | printf("%s: unknown register at %02x\n", __FUNCTION__, (int) addr); |
1438 | 942ac052 | balrog | }; |
1439 | 942ac052 | balrog | } |
1440 | 942ac052 | balrog | |
1441 | d60efc6b | Blue Swirl | CPUReadMemoryFunc * const musb_read[] = {
|
1442 | 942ac052 | balrog | musb_readb, |
1443 | 942ac052 | balrog | musb_readh, |
1444 | 942ac052 | balrog | musb_readw, |
1445 | 942ac052 | balrog | }; |
1446 | 942ac052 | balrog | |
1447 | d60efc6b | Blue Swirl | CPUWriteMemoryFunc * const musb_write[] = {
|
1448 | 942ac052 | balrog | musb_writeb, |
1449 | 942ac052 | balrog | musb_writeh, |
1450 | 942ac052 | balrog | musb_writew, |
1451 | 942ac052 | balrog | }; |