imx-usb-phy.c 6.2 KB

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