2
0

hcd-dwc2.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * dwc-hsotg (dwc2) USB host controller state definitions
  3. *
  4. * Based on hw/usb/hcd-ehci.h
  5. *
  6. * Copyright (c) 2020 Paul Zimmerman <pauldzim@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  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.
  17. */
  18. #ifndef HW_USB_DWC2_H
  19. #define HW_USB_DWC2_H
  20. #include "qemu/timer.h"
  21. #include "hw/irq.h"
  22. #include "hw/sysbus.h"
  23. #include "hw/usb.h"
  24. #include "sysemu/dma.h"
  25. #define DWC2_MMIO_SIZE 0x11000
  26. #define DWC2_NB_CHAN 8 /* Number of host channels */
  27. #define DWC2_MAX_XFER_SIZE 65536 /* Max transfer size expected in HCTSIZ */
  28. typedef struct DWC2Packet DWC2Packet;
  29. typedef struct DWC2State DWC2State;
  30. typedef struct DWC2Class DWC2Class;
  31. enum async_state {
  32. DWC2_ASYNC_NONE = 0,
  33. DWC2_ASYNC_INITIALIZED,
  34. DWC2_ASYNC_INFLIGHT,
  35. DWC2_ASYNC_FINISHED,
  36. };
  37. struct DWC2Packet {
  38. USBPacket packet;
  39. uint32_t devadr;
  40. uint32_t epnum;
  41. uint32_t epdir;
  42. uint32_t mps;
  43. uint32_t pid;
  44. uint32_t index;
  45. uint32_t pcnt;
  46. uint32_t len;
  47. int32_t async;
  48. bool small;
  49. bool needs_service;
  50. };
  51. struct DWC2State {
  52. /*< private >*/
  53. SysBusDevice parent_obj;
  54. /*< public >*/
  55. USBBus bus;
  56. qemu_irq irq;
  57. MemoryRegion *dma_mr;
  58. AddressSpace dma_as;
  59. MemoryRegion container;
  60. MemoryRegion hsotg;
  61. MemoryRegion fifos;
  62. union {
  63. #define DWC2_GLBREG_SIZE 0x70
  64. uint32_t glbreg[DWC2_GLBREG_SIZE / sizeof(uint32_t)];
  65. struct {
  66. uint32_t gotgctl; /* 00 */
  67. uint32_t gotgint; /* 04 */
  68. uint32_t gahbcfg; /* 08 */
  69. uint32_t gusbcfg; /* 0c */
  70. uint32_t grstctl; /* 10 */
  71. uint32_t gintsts; /* 14 */
  72. uint32_t gintmsk; /* 18 */
  73. uint32_t grxstsr; /* 1c */
  74. uint32_t grxstsp; /* 20 */
  75. uint32_t grxfsiz; /* 24 */
  76. uint32_t gnptxfsiz; /* 28 */
  77. uint32_t gnptxsts; /* 2c */
  78. uint32_t gi2cctl; /* 30 */
  79. uint32_t gpvndctl; /* 34 */
  80. uint32_t ggpio; /* 38 */
  81. uint32_t guid; /* 3c */
  82. uint32_t gsnpsid; /* 40 */
  83. uint32_t ghwcfg1; /* 44 */
  84. uint32_t ghwcfg2; /* 48 */
  85. uint32_t ghwcfg3; /* 4c */
  86. uint32_t ghwcfg4; /* 50 */
  87. uint32_t glpmcfg; /* 54 */
  88. uint32_t gpwrdn; /* 58 */
  89. uint32_t gdfifocfg; /* 5c */
  90. uint32_t gadpctl; /* 60 */
  91. uint32_t grefclk; /* 64 */
  92. uint32_t gintmsk2; /* 68 */
  93. uint32_t gintsts2; /* 6c */
  94. };
  95. };
  96. union {
  97. #define DWC2_FSZREG_SIZE 0x04
  98. uint32_t fszreg[DWC2_FSZREG_SIZE / sizeof(uint32_t)];
  99. struct {
  100. uint32_t hptxfsiz; /* 100 */
  101. };
  102. };
  103. union {
  104. #define DWC2_HREG0_SIZE 0x44
  105. uint32_t hreg0[DWC2_HREG0_SIZE / sizeof(uint32_t)];
  106. struct {
  107. uint32_t hcfg; /* 400 */
  108. uint32_t hfir; /* 404 */
  109. uint32_t hfnum; /* 408 */
  110. uint32_t rsvd0; /* 40c */
  111. uint32_t hptxsts; /* 410 */
  112. uint32_t haint; /* 414 */
  113. uint32_t haintmsk; /* 418 */
  114. uint32_t hflbaddr; /* 41c */
  115. uint32_t rsvd1[8]; /* 420-43c */
  116. uint32_t hprt0; /* 440 */
  117. };
  118. };
  119. #define DWC2_HREG1_SIZE (0x20 * DWC2_NB_CHAN)
  120. uint32_t hreg1[DWC2_HREG1_SIZE / sizeof(uint32_t)];
  121. #define hcchar(_ch) hreg1[((_ch) << 3) + 0] /* 500, 520, ... */
  122. #define hcsplt(_ch) hreg1[((_ch) << 3) + 1] /* 504, 524, ... */
  123. #define hcint(_ch) hreg1[((_ch) << 3) + 2] /* 508, 528, ... */
  124. #define hcintmsk(_ch) hreg1[((_ch) << 3) + 3] /* 50c, 52c, ... */
  125. #define hctsiz(_ch) hreg1[((_ch) << 3) + 4] /* 510, 530, ... */
  126. #define hcdma(_ch) hreg1[((_ch) << 3) + 5] /* 514, 534, ... */
  127. #define hcdmab(_ch) hreg1[((_ch) << 3) + 7] /* 51c, 53c, ... */
  128. union {
  129. #define DWC2_PCGREG_SIZE 0x08
  130. uint32_t pcgreg[DWC2_PCGREG_SIZE / sizeof(uint32_t)];
  131. struct {
  132. uint32_t pcgctl; /* e00 */
  133. uint32_t pcgcctl1; /* e04 */
  134. };
  135. };
  136. /* TODO - implement FIFO registers for slave mode */
  137. #define DWC2_HFIFO_SIZE (0x1000 * DWC2_NB_CHAN)
  138. /*
  139. * Internal state
  140. */
  141. QEMUTimer *eof_timer;
  142. QEMUTimer *frame_timer;
  143. QEMUBH *async_bh;
  144. int64_t sof_time;
  145. int64_t usb_frame_time;
  146. int64_t usb_bit_time;
  147. uint32_t usb_version;
  148. uint16_t frame_number;
  149. uint16_t fi;
  150. uint16_t next_chan;
  151. bool working;
  152. USBPort uport;
  153. DWC2Packet packet[DWC2_NB_CHAN]; /* one packet per chan */
  154. uint8_t usb_buf[DWC2_NB_CHAN][DWC2_MAX_XFER_SIZE]; /* one buffer per chan */
  155. };
  156. struct DWC2Class {
  157. /*< private >*/
  158. SysBusDeviceClass parent_class;
  159. ResettablePhases parent_phases;
  160. /*< public >*/
  161. };
  162. #define TYPE_DWC2_USB "dwc2-usb"
  163. #define DWC2_USB(obj) \
  164. OBJECT_CHECK(DWC2State, (obj), TYPE_DWC2_USB)
  165. #define DWC2_CLASS(klass) \
  166. OBJECT_CLASS_CHECK(DWC2Class, (klass), TYPE_DWC2_USB)
  167. #define DWC2_GET_CLASS(obj) \
  168. OBJECT_GET_CLASS(DWC2Class, (obj), TYPE_DWC2_USB)
  169. #endif