2 * Texas Instruments TUSB6010 emulation.
3 * Based on reverse-engineering of a linux driver.
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, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qemu/module.h"
24 #include "qemu/timer.h"
26 #include "hw/usb/hcd-musb.h"
27 #include "hw/arm/omap.h"
30 #include "hw/sysbus.h"
31 #include "qom/object.h"
33 #define TYPE_TUSB6010 "tusb6010"
34 OBJECT_DECLARE_SIMPLE_TYPE(TUSBState
, TUSB6010
)
37 SysBusDevice parent_obj
;
39 MemoryRegion iomem
[2];
66 uint32_t rx_config
[15];
67 uint32_t tx_config
[15];
70 uint32_t control_config
;
71 uint32_t otg_timer_val
;
74 #define TUSB_DEVCLOCK 60000000 /* 60 MHz */
76 #define TUSB_VLYNQ_CTRL 0x004
78 /* Mentor Graphics OTG core registers. */
79 #define TUSB_BASE_OFFSET 0x400
81 /* FIFO registers, 32-bit. */
82 #define TUSB_FIFO_BASE 0x600
84 /* Device System & Control registers, 32-bit. */
85 #define TUSB_SYS_REG_BASE 0x800
87 #define TUSB_DEV_CONF (TUSB_SYS_REG_BASE + 0x000)
88 #define TUSB_DEV_CONF_USB_HOST_MODE (1 << 16)
89 #define TUSB_DEV_CONF_PROD_TEST_MODE (1 << 15)
90 #define TUSB_DEV_CONF_SOFT_ID (1 << 1)
91 #define TUSB_DEV_CONF_ID_SEL (1 << 0)
93 #define TUSB_PHY_OTG_CTRL_ENABLE (TUSB_SYS_REG_BASE + 0x004)
94 #define TUSB_PHY_OTG_CTRL (TUSB_SYS_REG_BASE + 0x008)
95 #define TUSB_PHY_OTG_CTRL_WRPROTECT (0xa5 << 24)
96 #define TUSB_PHY_OTG_CTRL_O_ID_PULLUP (1 << 23)
97 #define TUSB_PHY_OTG_CTRL_O_VBUS_DET_EN (1 << 19)
98 #define TUSB_PHY_OTG_CTRL_O_SESS_END_EN (1 << 18)
99 #define TUSB_PHY_OTG_CTRL_TESTM2 (1 << 17)
100 #define TUSB_PHY_OTG_CTRL_TESTM1 (1 << 16)
101 #define TUSB_PHY_OTG_CTRL_TESTM0 (1 << 15)
102 #define TUSB_PHY_OTG_CTRL_TX_DATA2 (1 << 14)
103 #define TUSB_PHY_OTG_CTRL_TX_GZ2 (1 << 13)
104 #define TUSB_PHY_OTG_CTRL_TX_ENABLE2 (1 << 12)
105 #define TUSB_PHY_OTG_CTRL_DM_PULLDOWN (1 << 11)
106 #define TUSB_PHY_OTG_CTRL_DP_PULLDOWN (1 << 10)
107 #define TUSB_PHY_OTG_CTRL_OSC_EN (1 << 9)
108 #define TUSB_PHY_OTG_CTRL_PHYREF_CLK(v) (((v) & 3) << 7)
109 #define TUSB_PHY_OTG_CTRL_PD (1 << 6)
110 #define TUSB_PHY_OTG_CTRL_PLL_ON (1 << 5)
111 #define TUSB_PHY_OTG_CTRL_EXT_RPU (1 << 4)
112 #define TUSB_PHY_OTG_CTRL_PWR_GOOD (1 << 3)
113 #define TUSB_PHY_OTG_CTRL_RESET (1 << 2)
114 #define TUSB_PHY_OTG_CTRL_SUSPENDM (1 << 1)
115 #define TUSB_PHY_OTG_CTRL_CLK_MODE (1 << 0)
117 /* OTG status register */
118 #define TUSB_DEV_OTG_STAT (TUSB_SYS_REG_BASE + 0x00c)
119 #define TUSB_DEV_OTG_STAT_PWR_CLK_GOOD (1 << 8)
120 #define TUSB_DEV_OTG_STAT_SESS_END (1 << 7)
121 #define TUSB_DEV_OTG_STAT_SESS_VALID (1 << 6)
122 #define TUSB_DEV_OTG_STAT_VBUS_VALID (1 << 5)
123 #define TUSB_DEV_OTG_STAT_VBUS_SENSE (1 << 4)
124 #define TUSB_DEV_OTG_STAT_ID_STATUS (1 << 3)
125 #define TUSB_DEV_OTG_STAT_HOST_DISCON (1 << 2)
126 #define TUSB_DEV_OTG_STAT_LINE_STATE (3 << 0)
127 #define TUSB_DEV_OTG_STAT_DP_ENABLE (1 << 1)
128 #define TUSB_DEV_OTG_STAT_DM_ENABLE (1 << 0)
130 #define TUSB_DEV_OTG_TIMER (TUSB_SYS_REG_BASE + 0x010)
131 #define TUSB_DEV_OTG_TIMER_ENABLE (1 << 31)
132 #define TUSB_DEV_OTG_TIMER_VAL(v) ((v) & 0x07ffffff)
133 #define TUSB_PRCM_REV (TUSB_SYS_REG_BASE + 0x014)
135 /* PRCM configuration register */
136 #define TUSB_PRCM_CONF (TUSB_SYS_REG_BASE + 0x018)
137 #define TUSB_PRCM_CONF_SFW_CPEN (1 << 24)
138 #define TUSB_PRCM_CONF_SYS_CLKSEL(v) (((v) & 3) << 16)
140 /* PRCM management register */
141 #define TUSB_PRCM_MNGMT (TUSB_SYS_REG_BASE + 0x01c)
142 #define TUSB_PRCM_MNGMT_SRP_FIX_TMR(v) (((v) & 0xf) << 25)
143 #define TUSB_PRCM_MNGMT_SRP_FIX_EN (1 << 24)
144 #define TUSB_PRCM_MNGMT_VBUS_VAL_TMR(v) (((v) & 0xf) << 20)
145 #define TUSB_PRCM_MNGMT_VBUS_VAL_FLT_EN (1 << 19)
146 #define TUSB_PRCM_MNGMT_DFT_CLK_DIS (1 << 18)
147 #define TUSB_PRCM_MNGMT_VLYNQ_CLK_DIS (1 << 17)
148 #define TUSB_PRCM_MNGMT_OTG_SESS_END_EN (1 << 10)
149 #define TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN (1 << 9)
150 #define TUSB_PRCM_MNGMT_OTG_ID_PULLUP (1 << 8)
151 #define TUSB_PRCM_MNGMT_15_SW_EN (1 << 4)
152 #define TUSB_PRCM_MNGMT_33_SW_EN (1 << 3)
153 #define TUSB_PRCM_MNGMT_5V_CPEN (1 << 2)
154 #define TUSB_PRCM_MNGMT_PM_IDLE (1 << 1)
155 #define TUSB_PRCM_MNGMT_DEV_IDLE (1 << 0)
157 /* Wake-up source clear and mask registers */
158 #define TUSB_PRCM_WAKEUP_SOURCE (TUSB_SYS_REG_BASE + 0x020)
159 #define TUSB_PRCM_WAKEUP_CLEAR (TUSB_SYS_REG_BASE + 0x028)
160 #define TUSB_PRCM_WAKEUP_MASK (TUSB_SYS_REG_BASE + 0x02c)
161 #define TUSB_PRCM_WAKEUP_RESERVED_BITS (0xffffe << 13)
162 #define TUSB_PRCM_WGPIO_7 (1 << 12)
163 #define TUSB_PRCM_WGPIO_6 (1 << 11)
164 #define TUSB_PRCM_WGPIO_5 (1 << 10)
165 #define TUSB_PRCM_WGPIO_4 (1 << 9)
166 #define TUSB_PRCM_WGPIO_3 (1 << 8)
167 #define TUSB_PRCM_WGPIO_2 (1 << 7)
168 #define TUSB_PRCM_WGPIO_1 (1 << 6)
169 #define TUSB_PRCM_WGPIO_0 (1 << 5)
170 #define TUSB_PRCM_WHOSTDISCON (1 << 4) /* Host disconnect */
171 #define TUSB_PRCM_WBUS (1 << 3) /* USB bus resume */
172 #define TUSB_PRCM_WNORCS (1 << 2) /* NOR chip select */
173 #define TUSB_PRCM_WVBUS (1 << 1) /* OTG PHY VBUS */
174 #define TUSB_PRCM_WID (1 << 0) /* OTG PHY ID detect */
176 #define TUSB_PULLUP_1_CTRL (TUSB_SYS_REG_BASE + 0x030)
177 #define TUSB_PULLUP_2_CTRL (TUSB_SYS_REG_BASE + 0x034)
178 #define TUSB_INT_CTRL_REV (TUSB_SYS_REG_BASE + 0x038)
179 #define TUSB_INT_CTRL_CONF (TUSB_SYS_REG_BASE + 0x03c)
180 #define TUSB_USBIP_INT_SRC (TUSB_SYS_REG_BASE + 0x040)
181 #define TUSB_USBIP_INT_SET (TUSB_SYS_REG_BASE + 0x044)
182 #define TUSB_USBIP_INT_CLEAR (TUSB_SYS_REG_BASE + 0x048)
183 #define TUSB_USBIP_INT_MASK (TUSB_SYS_REG_BASE + 0x04c)
184 #define TUSB_DMA_INT_SRC (TUSB_SYS_REG_BASE + 0x050)
185 #define TUSB_DMA_INT_SET (TUSB_SYS_REG_BASE + 0x054)
186 #define TUSB_DMA_INT_CLEAR (TUSB_SYS_REG_BASE + 0x058)
187 #define TUSB_DMA_INT_MASK (TUSB_SYS_REG_BASE + 0x05c)
188 #define TUSB_GPIO_INT_SRC (TUSB_SYS_REG_BASE + 0x060)
189 #define TUSB_GPIO_INT_SET (TUSB_SYS_REG_BASE + 0x064)
190 #define TUSB_GPIO_INT_CLEAR (TUSB_SYS_REG_BASE + 0x068)
191 #define TUSB_GPIO_INT_MASK (TUSB_SYS_REG_BASE + 0x06c)
193 /* NOR flash interrupt source registers */
194 #define TUSB_INT_SRC (TUSB_SYS_REG_BASE + 0x070)
195 #define TUSB_INT_SRC_SET (TUSB_SYS_REG_BASE + 0x074)
196 #define TUSB_INT_SRC_CLEAR (TUSB_SYS_REG_BASE + 0x078)
197 #define TUSB_INT_MASK (TUSB_SYS_REG_BASE + 0x07c)
198 #define TUSB_INT_SRC_TXRX_DMA_DONE (1 << 24)
199 #define TUSB_INT_SRC_USB_IP_CORE (1 << 17)
200 #define TUSB_INT_SRC_OTG_TIMEOUT (1 << 16)
201 #define TUSB_INT_SRC_VBUS_SENSE_CHNG (1 << 15)
202 #define TUSB_INT_SRC_ID_STATUS_CHNG (1 << 14)
203 #define TUSB_INT_SRC_DEV_WAKEUP (1 << 13)
204 #define TUSB_INT_SRC_DEV_READY (1 << 12)
205 #define TUSB_INT_SRC_USB_IP_TX (1 << 9)
206 #define TUSB_INT_SRC_USB_IP_RX (1 << 8)
207 #define TUSB_INT_SRC_USB_IP_VBUS_ERR (1 << 7)
208 #define TUSB_INT_SRC_USB_IP_VBUS_REQ (1 << 6)
209 #define TUSB_INT_SRC_USB_IP_DISCON (1 << 5)
210 #define TUSB_INT_SRC_USB_IP_CONN (1 << 4)
211 #define TUSB_INT_SRC_USB_IP_SOF (1 << 3)
212 #define TUSB_INT_SRC_USB_IP_RST_BABBLE (1 << 2)
213 #define TUSB_INT_SRC_USB_IP_RESUME (1 << 1)
214 #define TUSB_INT_SRC_USB_IP_SUSPEND (1 << 0)
216 #define TUSB_GPIO_REV (TUSB_SYS_REG_BASE + 0x080)
217 #define TUSB_GPIO_CONF (TUSB_SYS_REG_BASE + 0x084)
218 #define TUSB_DMA_CTRL_REV (TUSB_SYS_REG_BASE + 0x100)
219 #define TUSB_DMA_REQ_CONF (TUSB_SYS_REG_BASE + 0x104)
220 #define TUSB_EP0_CONF (TUSB_SYS_REG_BASE + 0x108)
221 #define TUSB_EP_IN_SIZE (TUSB_SYS_REG_BASE + 0x10c)
222 #define TUSB_DMA_EP_MAP (TUSB_SYS_REG_BASE + 0x148)
223 #define TUSB_EP_OUT_SIZE (TUSB_SYS_REG_BASE + 0x14c)
224 #define TUSB_EP_MAX_PACKET_SIZE_OFFSET (TUSB_SYS_REG_BASE + 0x188)
225 #define TUSB_SCRATCH_PAD (TUSB_SYS_REG_BASE + 0x1c4)
226 #define TUSB_WAIT_COUNT (TUSB_SYS_REG_BASE + 0x1c8)
227 #define TUSB_PROD_TEST_RESET (TUSB_SYS_REG_BASE + 0x1d8)
229 #define TUSB_DIDR1_LO (TUSB_SYS_REG_BASE + 0x1f8)
230 #define TUSB_DIDR1_HI (TUSB_SYS_REG_BASE + 0x1fc)
232 /* Device System & Control register bitfields */
233 #define TUSB_INT_CTRL_CONF_INT_RLCYC(v) (((v) & 0x7) << 18)
234 #define TUSB_INT_CTRL_CONF_INT_POLARITY (1 << 17)
235 #define TUSB_INT_CTRL_CONF_INT_MODE (1 << 16)
236 #define TUSB_GPIO_CONF_DMAREQ(v) (((v) & 0x3f) << 24)
237 #define TUSB_DMA_REQ_CONF_BURST_SIZE(v) (((v) & 3) << 26)
238 #define TUSB_DMA_REQ_CONF_DMA_RQ_EN(v) (((v) & 0x3f) << 20)
239 #define TUSB_DMA_REQ_CONF_DMA_RQ_ASR(v) (((v) & 0xf) << 16)
240 #define TUSB_EP0_CONFIG_SW_EN (1 << 8)
241 #define TUSB_EP0_CONFIG_DIR_TX (1 << 7)
242 #define TUSB_EP0_CONFIG_XFR_SIZE(v) ((v) & 0x7f)
243 #define TUSB_EP_CONFIG_SW_EN (1 << 31)
244 #define TUSB_EP_CONFIG_XFR_SIZE(v) ((v) & 0x7fffffff)
245 #define TUSB_PROD_TEST_RESET_VAL 0xa596
247 static void tusb_intr_update(TUSBState
*s
)
249 if (s
->control_config
& TUSB_INT_CTRL_CONF_INT_POLARITY
)
250 qemu_set_irq(s
->irq
, s
->intr
& ~s
->mask
& s
->intr_ok
);
252 qemu_set_irq(s
->irq
, (!(s
->intr
& ~s
->mask
)) & s
->intr_ok
);
255 static void tusb_usbip_intr_update(TUSBState
*s
)
257 /* TX interrupt in the MUSB */
258 if (s
->usbip_intr
& 0x0000ffff & ~s
->usbip_mask
)
259 s
->intr
|= TUSB_INT_SRC_USB_IP_TX
;
261 s
->intr
&= ~TUSB_INT_SRC_USB_IP_TX
;
263 /* RX interrupt in the MUSB */
264 if (s
->usbip_intr
& 0xffff0000 & ~s
->usbip_mask
)
265 s
->intr
|= TUSB_INT_SRC_USB_IP_RX
;
267 s
->intr
&= ~TUSB_INT_SRC_USB_IP_RX
;
269 /* XXX: What about TUSB_INT_SRC_USB_IP_CORE? */
274 static void tusb_dma_intr_update(TUSBState
*s
)
276 if (s
->dma_intr
& ~s
->dma_mask
)
277 s
->intr
|= TUSB_INT_SRC_TXRX_DMA_DONE
;
279 s
->intr
&= ~TUSB_INT_SRC_TXRX_DMA_DONE
;
284 static void tusb_gpio_intr_update(TUSBState
*s
)
286 /* TODO: How is this signalled? */
289 static uint32_t tusb_async_readb(void *opaque
, hwaddr addr
)
291 TUSBState
*s
= (TUSBState
*) opaque
;
293 switch (addr
& 0xfff) {
294 case TUSB_BASE_OFFSET
... (TUSB_BASE_OFFSET
| 0x1ff):
295 return musb_read
[0](s
->musb
, addr
& 0x1ff);
297 case TUSB_FIFO_BASE
... (TUSB_FIFO_BASE
| 0x1ff):
298 return musb_read
[0](s
->musb
, 0x20 + ((addr
>> 3) & 0x3c));
301 printf("%s: unknown register at %03x\n",
302 __func__
, (int) (addr
& 0xfff));
306 static uint32_t tusb_async_readh(void *opaque
, hwaddr addr
)
308 TUSBState
*s
= (TUSBState
*) opaque
;
310 switch (addr
& 0xfff) {
311 case TUSB_BASE_OFFSET
... (TUSB_BASE_OFFSET
| 0x1ff):
312 return musb_read
[1](s
->musb
, addr
& 0x1ff);
314 case TUSB_FIFO_BASE
... (TUSB_FIFO_BASE
| 0x1ff):
315 return musb_read
[1](s
->musb
, 0x20 + ((addr
>> 3) & 0x3c));
318 printf("%s: unknown register at %03x\n",
319 __func__
, (int) (addr
& 0xfff));
323 static uint32_t tusb_async_readw(void *opaque
, hwaddr addr
)
325 TUSBState
*s
= (TUSBState
*) opaque
;
326 int offset
= addr
& 0xfff;
332 return s
->dev_config
;
334 case TUSB_BASE_OFFSET
... (TUSB_BASE_OFFSET
| 0x1ff):
335 return musb_read
[2](s
->musb
, offset
& 0x1ff);
337 case TUSB_FIFO_BASE
... (TUSB_FIFO_BASE
| 0x1ff):
338 return musb_read
[2](s
->musb
, 0x20 + ((addr
>> 3) & 0x3c));
340 case TUSB_PHY_OTG_CTRL_ENABLE
:
341 case TUSB_PHY_OTG_CTRL
:
342 return 0x00; /* TODO */
344 case TUSB_DEV_OTG_STAT
:
347 if (!(s
->prcm_mngmt
& TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN
))
348 ret
&= ~TUSB_DEV_OTG_STAT_VBUS_VALID
;
351 case TUSB_DEV_OTG_TIMER
:
352 return s
->otg_timer_val
;
357 return s
->prcm_config
;
358 case TUSB_PRCM_MNGMT
:
359 return s
->prcm_mngmt
;
360 case TUSB_PRCM_WAKEUP_SOURCE
:
361 case TUSB_PRCM_WAKEUP_CLEAR
: /* TODO: What does this one return? */
363 case TUSB_PRCM_WAKEUP_MASK
:
366 case TUSB_PULLUP_1_CTRL
:
368 case TUSB_PULLUP_2_CTRL
:
371 case TUSB_INT_CTRL_REV
:
373 case TUSB_INT_CTRL_CONF
:
374 return s
->control_config
;
376 case TUSB_USBIP_INT_SRC
:
377 case TUSB_USBIP_INT_SET
: /* TODO: What do these two return? */
378 case TUSB_USBIP_INT_CLEAR
:
379 return s
->usbip_intr
;
380 case TUSB_USBIP_INT_MASK
:
381 return s
->usbip_mask
;
383 case TUSB_DMA_INT_SRC
:
384 case TUSB_DMA_INT_SET
: /* TODO: What do these two return? */
385 case TUSB_DMA_INT_CLEAR
:
387 case TUSB_DMA_INT_MASK
:
390 case TUSB_GPIO_INT_SRC
: /* TODO: What do these two return? */
391 case TUSB_GPIO_INT_SET
:
392 case TUSB_GPIO_INT_CLEAR
:
394 case TUSB_GPIO_INT_MASK
:
398 case TUSB_INT_SRC_SET
: /* TODO: What do these two return? */
399 case TUSB_INT_SRC_CLEAR
:
407 return s
->gpio_config
;
409 case TUSB_DMA_CTRL_REV
:
411 case TUSB_DMA_REQ_CONF
:
412 return s
->dma_config
;
414 return s
->ep0_config
;
415 case TUSB_EP_IN_SIZE
... (TUSB_EP_IN_SIZE
+ 0x3b):
416 epnum
= (offset
- TUSB_EP_IN_SIZE
) >> 2;
417 return s
->tx_config
[epnum
];
418 case TUSB_DMA_EP_MAP
:
420 case TUSB_EP_OUT_SIZE
... (TUSB_EP_OUT_SIZE
+ 0x3b):
421 epnum
= (offset
- TUSB_EP_OUT_SIZE
) >> 2;
422 return s
->rx_config
[epnum
];
423 case TUSB_EP_MAX_PACKET_SIZE_OFFSET
...
424 (TUSB_EP_MAX_PACKET_SIZE_OFFSET
+ 0x3b):
425 return 0x00000000; /* TODO */
426 case TUSB_WAIT_COUNT
:
427 return 0x00; /* TODO */
429 case TUSB_SCRATCH_PAD
:
432 case TUSB_PROD_TEST_RESET
:
433 return s
->test_reset
;
442 printf("%s: unknown register at %03x\n", __func__
, offset
);
446 static void tusb_async_writeb(void *opaque
, hwaddr addr
,
449 TUSBState
*s
= (TUSBState
*) opaque
;
451 switch (addr
& 0xfff) {
452 case TUSB_BASE_OFFSET
... (TUSB_BASE_OFFSET
| 0x1ff):
453 musb_write
[0](s
->musb
, addr
& 0x1ff, value
);
456 case TUSB_FIFO_BASE
... (TUSB_FIFO_BASE
| 0x1ff):
457 musb_write
[0](s
->musb
, 0x20 + ((addr
>> 3) & 0x3c), value
);
461 printf("%s: unknown register at %03x\n",
462 __func__
, (int) (addr
& 0xfff));
467 static void tusb_async_writeh(void *opaque
, hwaddr addr
,
470 TUSBState
*s
= (TUSBState
*) opaque
;
472 switch (addr
& 0xfff) {
473 case TUSB_BASE_OFFSET
... (TUSB_BASE_OFFSET
| 0x1ff):
474 musb_write
[1](s
->musb
, addr
& 0x1ff, value
);
477 case TUSB_FIFO_BASE
... (TUSB_FIFO_BASE
| 0x1ff):
478 musb_write
[1](s
->musb
, 0x20 + ((addr
>> 3) & 0x3c), value
);
482 printf("%s: unknown register at %03x\n",
483 __func__
, (int) (addr
& 0xfff));
488 static void tusb_async_writew(void *opaque
, hwaddr addr
,
491 TUSBState
*s
= (TUSBState
*) opaque
;
492 int offset
= addr
& 0xfff;
496 case TUSB_VLYNQ_CTRL
:
499 case TUSB_BASE_OFFSET
... (TUSB_BASE_OFFSET
| 0x1ff):
500 musb_write
[2](s
->musb
, offset
& 0x1ff, value
);
503 case TUSB_FIFO_BASE
... (TUSB_FIFO_BASE
| 0x1ff):
504 musb_write
[2](s
->musb
, 0x20 + ((addr
>> 3) & 0x3c), value
);
508 s
->dev_config
= value
;
509 s
->host_mode
= (value
& TUSB_DEV_CONF_USB_HOST_MODE
);
510 if (value
& TUSB_DEV_CONF_PROD_TEST_MODE
)
511 hw_error("%s: Product Test mode not allowed\n", __func__
);
514 case TUSB_PHY_OTG_CTRL_ENABLE
:
515 case TUSB_PHY_OTG_CTRL
:
517 case TUSB_DEV_OTG_TIMER
:
518 s
->otg_timer_val
= value
;
519 if (value
& TUSB_DEV_OTG_TIMER_ENABLE
)
520 timer_mod(s
->otg_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
521 muldiv64(TUSB_DEV_OTG_TIMER_VAL(value
),
522 NANOSECONDS_PER_SECOND
, TUSB_DEVCLOCK
));
524 timer_del(s
->otg_timer
);
528 s
->prcm_config
= value
;
530 case TUSB_PRCM_MNGMT
:
531 s
->prcm_mngmt
= value
;
533 case TUSB_PRCM_WAKEUP_CLEAR
:
535 case TUSB_PRCM_WAKEUP_MASK
:
536 s
->wkup_mask
= value
;
539 case TUSB_PULLUP_1_CTRL
:
540 s
->pullup
[0] = value
;
542 case TUSB_PULLUP_2_CTRL
:
543 s
->pullup
[1] = value
;
545 case TUSB_INT_CTRL_CONF
:
546 s
->control_config
= value
;
550 case TUSB_USBIP_INT_SET
:
551 s
->usbip_intr
|= value
;
552 tusb_usbip_intr_update(s
);
554 case TUSB_USBIP_INT_CLEAR
:
555 s
->usbip_intr
&= ~value
;
556 tusb_usbip_intr_update(s
);
557 musb_core_intr_clear(s
->musb
, ~value
);
559 case TUSB_USBIP_INT_MASK
:
560 s
->usbip_mask
= value
;
561 tusb_usbip_intr_update(s
);
564 case TUSB_DMA_INT_SET
:
565 s
->dma_intr
|= value
;
566 tusb_dma_intr_update(s
);
568 case TUSB_DMA_INT_CLEAR
:
569 s
->dma_intr
&= ~value
;
570 tusb_dma_intr_update(s
);
572 case TUSB_DMA_INT_MASK
:
574 tusb_dma_intr_update(s
);
577 case TUSB_GPIO_INT_SET
:
578 s
->gpio_intr
|= value
;
579 tusb_gpio_intr_update(s
);
581 case TUSB_GPIO_INT_CLEAR
:
582 s
->gpio_intr
&= ~value
;
583 tusb_gpio_intr_update(s
);
585 case TUSB_GPIO_INT_MASK
:
586 s
->gpio_mask
= value
;
587 tusb_gpio_intr_update(s
);
590 case TUSB_INT_SRC_SET
:
594 case TUSB_INT_SRC_CLEAR
:
604 s
->gpio_config
= value
;
606 case TUSB_DMA_REQ_CONF
:
607 s
->dma_config
= value
;
610 s
->ep0_config
= value
& 0x1ff;
611 musb_set_size(s
->musb
, 0, TUSB_EP0_CONFIG_XFR_SIZE(value
),
612 value
& TUSB_EP0_CONFIG_DIR_TX
);
614 case TUSB_EP_IN_SIZE
... (TUSB_EP_IN_SIZE
+ 0x3b):
615 epnum
= (offset
- TUSB_EP_IN_SIZE
) >> 2;
616 s
->tx_config
[epnum
] = value
;
617 musb_set_size(s
->musb
, epnum
+ 1, TUSB_EP_CONFIG_XFR_SIZE(value
), 1);
619 case TUSB_DMA_EP_MAP
:
622 case TUSB_EP_OUT_SIZE
... (TUSB_EP_OUT_SIZE
+ 0x3b):
623 epnum
= (offset
- TUSB_EP_OUT_SIZE
) >> 2;
624 s
->rx_config
[epnum
] = value
;
625 musb_set_size(s
->musb
, epnum
+ 1, TUSB_EP_CONFIG_XFR_SIZE(value
), 0);
627 case TUSB_EP_MAX_PACKET_SIZE_OFFSET
...
628 (TUSB_EP_MAX_PACKET_SIZE_OFFSET
+ 0x3b):
630 case TUSB_WAIT_COUNT
:
633 case TUSB_SCRATCH_PAD
:
637 case TUSB_PROD_TEST_RESET
:
638 s
->test_reset
= value
;
642 printf("%s: unknown register at %03x\n", __func__
, offset
);
647 static uint64_t tusb_async_readfn(void *opaque
, hwaddr addr
, unsigned size
)
651 return tusb_async_readb(opaque
, addr
);
653 return tusb_async_readh(opaque
, addr
);
655 return tusb_async_readw(opaque
, addr
);
657 g_assert_not_reached();
661 static void tusb_async_writefn(void *opaque
, hwaddr addr
,
662 uint64_t value
, unsigned size
)
666 tusb_async_writeb(opaque
, addr
, value
);
669 tusb_async_writeh(opaque
, addr
, value
);
672 tusb_async_writew(opaque
, addr
, value
);
675 g_assert_not_reached();
679 static const MemoryRegionOps tusb_async_ops
= {
680 .read
= tusb_async_readfn
,
681 .write
= tusb_async_writefn
,
682 .valid
.min_access_size
= 1,
683 .valid
.max_access_size
= 4,
684 .endianness
= DEVICE_NATIVE_ENDIAN
,
687 static void tusb_otg_tick(void *opaque
)
689 TUSBState
*s
= (TUSBState
*) opaque
;
691 s
->otg_timer_val
= 0;
692 s
->intr
|= TUSB_INT_SRC_OTG_TIMEOUT
;
696 static void tusb_power_tick(void *opaque
)
698 TUSBState
*s
= (TUSBState
*) opaque
;
706 static void tusb_musb_core_intr(void *opaque
, int source
, int level
)
708 TUSBState
*s
= (TUSBState
*) opaque
;
709 uint16_t otg_status
= s
->otg_status
;
714 otg_status
|= TUSB_DEV_OTG_STAT_VBUS_VALID
;
716 otg_status
&= ~TUSB_DEV_OTG_STAT_VBUS_VALID
;
718 /* XXX: only if TUSB_PHY_OTG_CTRL_OTG_VBUS_DET_EN set? */
719 /* XXX: only if TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN set? */
720 if (s
->otg_status
!= otg_status
) {
721 s
->otg_status
= otg_status
;
722 s
->intr
|= TUSB_INT_SRC_VBUS_SENSE_CHNG
;
727 case musb_set_session
:
728 /* XXX: only if TUSB_PHY_OTG_CTRL_OTG_SESS_END_EN set? */
729 /* XXX: only if TUSB_PRCM_MNGMT_OTG_SESS_END_EN set? */
731 s
->otg_status
|= TUSB_DEV_OTG_STAT_SESS_VALID
;
732 s
->otg_status
&= ~TUSB_DEV_OTG_STAT_SESS_END
;
734 s
->otg_status
&= ~TUSB_DEV_OTG_STAT_SESS_VALID
;
735 s
->otg_status
|= TUSB_DEV_OTG_STAT_SESS_END
;
738 /* XXX: some IRQ or anything? */
743 s
->usbip_intr
= musb_core_intr_get(s
->musb
);
747 s
->intr
|= 1 << source
;
749 s
->intr
&= ~(1 << source
);
755 static void tusb6010_power(TUSBState
*s
, int on
)
759 } else if (!s
->power
&& on
) {
761 /* Pull the interrupt down after TUSB6010 comes up. */
764 timer_mod(s
->pwr_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
765 NANOSECONDS_PER_SECOND
/ 2);
769 static void tusb6010_irq(void *opaque
, int source
, int level
)
772 tusb_musb_core_intr(opaque
, source
- 1, level
);
774 tusb6010_power(opaque
, level
);
778 static void tusb6010_reset(DeviceState
*dev
)
780 TUSBState
*s
= TUSB6010(dev
);
783 s
->test_reset
= TUSB_PROD_TEST_RESET_VAL
;
786 s
->otg_status
= 0; /* !TUSB_DEV_OTG_STAT_ID_STATUS means host mode */
788 s
->mask
= 0xffffffff;
789 s
->intr
= 0x00000000;
790 s
->otg_timer_val
= 0;
806 s
->pullup
[0] = s
->pullup
[1] = 0;
807 s
->control_config
= 0;
808 for (i
= 0; i
< 15; i
++) {
809 s
->rx_config
[i
] = s
->tx_config
[i
] = 0;
814 static void tusb6010_realize(DeviceState
*dev
, Error
**errp
)
816 TUSBState
*s
= TUSB6010(dev
);
817 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
819 s
->otg_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tusb_otg_tick
, s
);
820 s
->pwr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tusb_power_tick
, s
);
821 memory_region_init_io(&s
->iomem
[1], OBJECT(s
), &tusb_async_ops
, s
,
822 "tusb-async", UINT32_MAX
);
823 sysbus_init_mmio(sbd
, &s
->iomem
[0]);
824 sysbus_init_mmio(sbd
, &s
->iomem
[1]);
825 sysbus_init_irq(sbd
, &s
->irq
);
826 qdev_init_gpio_in(dev
, tusb6010_irq
, musb_irq_max
+ 1);
827 s
->musb
= musb_init(dev
, 1);
830 static void tusb6010_class_init(ObjectClass
*klass
, void *data
)
832 DeviceClass
*dc
= DEVICE_CLASS(klass
);
834 dc
->realize
= tusb6010_realize
;
835 dc
->reset
= tusb6010_reset
;
838 static const TypeInfo tusb6010_info
= {
839 .name
= TYPE_TUSB6010
,
840 .parent
= TYPE_SYS_BUS_DEVICE
,
841 .instance_size
= sizeof(TUSBState
),
842 .class_init
= tusb6010_class_init
,
845 static void tusb6010_register_types(void)
847 type_register_static(&tusb6010_info
);
850 type_init(tusb6010_register_types
)