2
0

imx-usb-phy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * i.MX USB PHY
  3. *
  4. * Copyright (c) 2020 Guenter Roeck <linux@roeck-us.net>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. * We need to implement basic reset control in the PHY control register.
  10. * For everything else, it is sufficient to set whatever is written.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "hw/usb/imx-usb-phy.h"
  14. #include "migration/vmstate.h"
  15. #include "qemu/log.h"
  16. #include "qemu/module.h"
  17. static const VMStateDescription vmstate_imx_usbphy = {
  18. .name = TYPE_IMX_USBPHY,
  19. .version_id = 1,
  20. .minimum_version_id = 1,
  21. .fields = (const VMStateField[]) {
  22. VMSTATE_UINT32_ARRAY(usbphy, IMXUSBPHYState, USBPHY_MAX),
  23. VMSTATE_END_OF_LIST()
  24. },
  25. };
  26. static void imx_usbphy_softreset(IMXUSBPHYState *s)
  27. {
  28. s->usbphy[USBPHY_PWD] = 0x001e1c00;
  29. s->usbphy[USBPHY_TX] = 0x10060607;
  30. s->usbphy[USBPHY_RX] = 0x00000000;
  31. s->usbphy[USBPHY_CTRL] = 0xc0200000;
  32. }
  33. static void imx_usbphy_reset(DeviceState *dev)
  34. {
  35. IMXUSBPHYState *s = IMX_USBPHY(dev);
  36. s->usbphy[USBPHY_STATUS] = 0x00000000;
  37. s->usbphy[USBPHY_DEBUG] = 0x7f180000;
  38. s->usbphy[USBPHY_DEBUG0_STATUS] = 0x00000000;
  39. s->usbphy[USBPHY_DEBUG1] = 0x00001000;
  40. s->usbphy[USBPHY_VERSION] = 0x04020000;
  41. imx_usbphy_softreset(s);
  42. }
  43. static uint64_t imx_usbphy_read(void *opaque, hwaddr offset, unsigned size)
  44. {
  45. IMXUSBPHYState *s = (IMXUSBPHYState *)opaque;
  46. uint32_t index = offset >> 2;
  47. uint32_t value;
  48. switch (index) {
  49. case USBPHY_PWD_SET:
  50. case USBPHY_TX_SET:
  51. case USBPHY_RX_SET:
  52. case USBPHY_CTRL_SET:
  53. case USBPHY_DEBUG_SET:
  54. case USBPHY_DEBUG1_SET:
  55. /*
  56. * All REG_NAME_SET register access are in fact targeting the
  57. * REG_NAME register.
  58. */
  59. value = s->usbphy[index - 1];
  60. break;
  61. case USBPHY_PWD_CLR:
  62. case USBPHY_TX_CLR:
  63. case USBPHY_RX_CLR:
  64. case USBPHY_CTRL_CLR:
  65. case USBPHY_DEBUG_CLR:
  66. case USBPHY_DEBUG1_CLR:
  67. /*
  68. * All REG_NAME_CLR register access are in fact targeting the
  69. * REG_NAME register.
  70. */
  71. value = s->usbphy[index - 2];
  72. break;
  73. case USBPHY_PWD_TOG:
  74. case USBPHY_TX_TOG:
  75. case USBPHY_RX_TOG:
  76. case USBPHY_CTRL_TOG:
  77. case USBPHY_DEBUG_TOG:
  78. case USBPHY_DEBUG1_TOG:
  79. /*
  80. * All REG_NAME_TOG register access are in fact targeting the
  81. * REG_NAME register.
  82. */
  83. value = s->usbphy[index - 3];
  84. break;
  85. default:
  86. if (index < USBPHY_MAX) {
  87. value = s->usbphy[index];
  88. } else {
  89. qemu_log_mask(LOG_GUEST_ERROR,
  90. "%s: Read from non-existing USB PHY register 0x%"
  91. HWADDR_PRIx "\n",
  92. __func__, offset);
  93. value = 0;
  94. }
  95. break;
  96. }
  97. return (uint64_t)value;
  98. }
  99. static void imx_usbphy_write(void *opaque, hwaddr offset, uint64_t value,
  100. unsigned size)
  101. {
  102. IMXUSBPHYState *s = (IMXUSBPHYState *)opaque;
  103. uint32_t index = offset >> 2;
  104. switch (index) {
  105. case USBPHY_CTRL:
  106. s->usbphy[index] = value;
  107. if (value & USBPHY_CTRL_SFTRST) {
  108. imx_usbphy_softreset(s);
  109. }
  110. break;
  111. case USBPHY_PWD:
  112. case USBPHY_TX:
  113. case USBPHY_RX:
  114. case USBPHY_STATUS:
  115. case USBPHY_DEBUG:
  116. case USBPHY_DEBUG1:
  117. s->usbphy[index] = value;
  118. break;
  119. case USBPHY_CTRL_SET:
  120. s->usbphy[index - 1] |= value;
  121. if (value & USBPHY_CTRL_SFTRST) {
  122. imx_usbphy_softreset(s);
  123. }
  124. break;
  125. case USBPHY_PWD_SET:
  126. case USBPHY_TX_SET:
  127. case USBPHY_RX_SET:
  128. case USBPHY_DEBUG_SET:
  129. case USBPHY_DEBUG1_SET:
  130. /*
  131. * All REG_NAME_SET register access are in fact targeting the
  132. * REG_NAME register. So we change the value of the REG_NAME
  133. * register, setting bits passed in the value.
  134. */
  135. s->usbphy[index - 1] |= value;
  136. break;
  137. case USBPHY_PWD_CLR:
  138. case USBPHY_TX_CLR:
  139. case USBPHY_RX_CLR:
  140. case USBPHY_CTRL_CLR:
  141. case USBPHY_DEBUG_CLR:
  142. case USBPHY_DEBUG1_CLR:
  143. /*
  144. * All REG_NAME_CLR register access are in fact targeting the
  145. * REG_NAME register. So we change the value of the REG_NAME
  146. * register, unsetting bits passed in the value.
  147. */
  148. s->usbphy[index - 2] &= ~value;
  149. break;
  150. case USBPHY_CTRL_TOG:
  151. s->usbphy[index - 3] ^= value;
  152. if ((value & USBPHY_CTRL_SFTRST) &&
  153. (s->usbphy[index - 3] & USBPHY_CTRL_SFTRST)) {
  154. imx_usbphy_softreset(s);
  155. }
  156. break;
  157. case USBPHY_PWD_TOG:
  158. case USBPHY_TX_TOG:
  159. case USBPHY_RX_TOG:
  160. case USBPHY_DEBUG_TOG:
  161. case USBPHY_DEBUG1_TOG:
  162. /*
  163. * All REG_NAME_TOG register access are in fact targeting the
  164. * REG_NAME register. So we change the value of the REG_NAME
  165. * register, toggling bits passed in the value.
  166. */
  167. s->usbphy[index - 3] ^= value;
  168. break;
  169. default:
  170. /* Other registers are read-only or do not exist */
  171. qemu_log_mask(LOG_GUEST_ERROR,
  172. "%s: Write to %s USB PHY register 0x%"
  173. HWADDR_PRIx "\n",
  174. __func__,
  175. index >= USBPHY_MAX ? "non-existing" : "read-only",
  176. offset);
  177. break;
  178. }
  179. }
  180. static const struct MemoryRegionOps imx_usbphy_ops = {
  181. .read = imx_usbphy_read,
  182. .write = imx_usbphy_write,
  183. .endianness = DEVICE_NATIVE_ENDIAN,
  184. .valid = {
  185. /*
  186. * Our device would not work correctly if the guest was doing
  187. * unaligned access. This might not be a limitation on the real
  188. * device but in practice there is no reason for a guest to access
  189. * this device unaligned.
  190. */
  191. .min_access_size = 4,
  192. .max_access_size = 4,
  193. .unaligned = false,
  194. },
  195. };
  196. static void imx_usbphy_realize(DeviceState *dev, Error **errp)
  197. {
  198. IMXUSBPHYState *s = IMX_USBPHY(dev);
  199. memory_region_init_io(&s->iomem, OBJECT(s), &imx_usbphy_ops, s,
  200. "imx-usbphy", 0x1000);
  201. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
  202. }
  203. static void imx_usbphy_class_init(ObjectClass *klass, void *data)
  204. {
  205. DeviceClass *dc = DEVICE_CLASS(klass);
  206. device_class_set_legacy_reset(dc, imx_usbphy_reset);
  207. dc->vmsd = &vmstate_imx_usbphy;
  208. dc->desc = "i.MX USB PHY Module";
  209. dc->realize = imx_usbphy_realize;
  210. }
  211. static const TypeInfo imx_usbphy_info = {
  212. .name = TYPE_IMX_USBPHY,
  213. .parent = TYPE_SYS_BUS_DEVICE,
  214. .instance_size = sizeof(IMXUSBPHYState),
  215. .class_init = imx_usbphy_class_init,
  216. };
  217. static void imx_usbphy_register_types(void)
  218. {
  219. type_register_static(&imx_usbphy_info);
  220. }
  221. type_init(imx_usbphy_register_types)