imx-usb-phy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = (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. value = s->usbphy[index];
  87. break;
  88. }
  89. return (uint64_t)value;
  90. }
  91. static void imx_usbphy_write(void *opaque, hwaddr offset, uint64_t value,
  92. unsigned size)
  93. {
  94. IMXUSBPHYState *s = (IMXUSBPHYState *)opaque;
  95. uint32_t index = offset >> 2;
  96. switch (index) {
  97. case USBPHY_CTRL:
  98. s->usbphy[index] = value;
  99. if (value & USBPHY_CTRL_SFTRST) {
  100. imx_usbphy_softreset(s);
  101. }
  102. break;
  103. case USBPHY_PWD:
  104. case USBPHY_TX:
  105. case USBPHY_RX:
  106. case USBPHY_STATUS:
  107. case USBPHY_DEBUG:
  108. case USBPHY_DEBUG1:
  109. s->usbphy[index] = value;
  110. break;
  111. case USBPHY_CTRL_SET:
  112. s->usbphy[index - 1] |= value;
  113. if (value & USBPHY_CTRL_SFTRST) {
  114. imx_usbphy_softreset(s);
  115. }
  116. break;
  117. case USBPHY_PWD_SET:
  118. case USBPHY_TX_SET:
  119. case USBPHY_RX_SET:
  120. case USBPHY_DEBUG_SET:
  121. case USBPHY_DEBUG1_SET:
  122. /*
  123. * All REG_NAME_SET register access are in fact targeting the
  124. * REG_NAME register. So we change the value of the REG_NAME
  125. * register, setting bits passed in the value.
  126. */
  127. s->usbphy[index - 1] |= value;
  128. break;
  129. case USBPHY_PWD_CLR:
  130. case USBPHY_TX_CLR:
  131. case USBPHY_RX_CLR:
  132. case USBPHY_CTRL_CLR:
  133. case USBPHY_DEBUG_CLR:
  134. case USBPHY_DEBUG1_CLR:
  135. /*
  136. * All REG_NAME_CLR register access are in fact targeting the
  137. * REG_NAME register. So we change the value of the REG_NAME
  138. * register, unsetting bits passed in the value.
  139. */
  140. s->usbphy[index - 2] &= ~value;
  141. break;
  142. case USBPHY_CTRL_TOG:
  143. s->usbphy[index - 3] ^= value;
  144. if ((value & USBPHY_CTRL_SFTRST) &&
  145. (s->usbphy[index - 3] & USBPHY_CTRL_SFTRST)) {
  146. imx_usbphy_softreset(s);
  147. }
  148. break;
  149. case USBPHY_PWD_TOG:
  150. case USBPHY_TX_TOG:
  151. case USBPHY_RX_TOG:
  152. case USBPHY_DEBUG_TOG:
  153. case USBPHY_DEBUG1_TOG:
  154. /*
  155. * All REG_NAME_TOG register access are in fact targeting the
  156. * REG_NAME register. So we change the value of the REG_NAME
  157. * register, toggling bits passed in the value.
  158. */
  159. s->usbphy[index - 3] ^= value;
  160. break;
  161. default:
  162. /* Other registers are read-only */
  163. break;
  164. }
  165. }
  166. static const struct MemoryRegionOps imx_usbphy_ops = {
  167. .read = imx_usbphy_read,
  168. .write = imx_usbphy_write,
  169. .endianness = DEVICE_NATIVE_ENDIAN,
  170. .valid = {
  171. /*
  172. * Our device would not work correctly if the guest was doing
  173. * unaligned access. This might not be a limitation on the real
  174. * device but in practice there is no reason for a guest to access
  175. * this device unaligned.
  176. */
  177. .min_access_size = 4,
  178. .max_access_size = 4,
  179. .unaligned = false,
  180. },
  181. };
  182. static void imx_usbphy_realize(DeviceState *dev, Error **errp)
  183. {
  184. IMXUSBPHYState *s = IMX_USBPHY(dev);
  185. memory_region_init_io(&s->iomem, OBJECT(s), &imx_usbphy_ops, s,
  186. "imx-usbphy", 0x1000);
  187. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
  188. }
  189. static void imx_usbphy_class_init(ObjectClass *klass, void *data)
  190. {
  191. DeviceClass *dc = DEVICE_CLASS(klass);
  192. dc->reset = imx_usbphy_reset;
  193. dc->vmsd = &vmstate_imx_usbphy;
  194. dc->desc = "i.MX USB PHY Module";
  195. dc->realize = imx_usbphy_realize;
  196. }
  197. static const TypeInfo imx_usbphy_info = {
  198. .name = TYPE_IMX_USBPHY,
  199. .parent = TYPE_SYS_BUS_DEVICE,
  200. .instance_size = sizeof(IMXUSBPHYState),
  201. .class_init = imx_usbphy_class_init,
  202. };
  203. static void imx_usbphy_register_types(void)
  204. {
  205. type_register_static(&imx_usbphy_info);
  206. }
  207. type_init(imx_usbphy_register_types)