2
0

hcd-dwc2.h 5.5 KB

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