usb_glue_t113.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2025, YC113
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usb_hc_ehci.h"
  8. #ifdef CONFIG_USB_EHCI_WITH_OHCI
  9. #include "usb_hc_ohci.h"
  10. #endif
  11. #include "interrupt.h"
  12. #include "drv_reg_base.h"
  13. #include "drv_clock.h"
  14. #if CONFIG_USBHOST_MAX_BUS != 2
  15. #error "t113 has 2 usb host controller"
  16. #endif
  17. #if CONFIG_USB_OHCI_HCOR_OFFSET != 0x400
  18. #error "t113 CONFIG_USB_OHCI_HCOR_OFFSET must be 0x400"
  19. #endif
  20. #if defined(CONFIG_USB_EHCI_HCOR_RESERVED_DISABLE)
  21. #error "t113 usb ehci register need reserved"
  22. #endif
  23. #if !defined(CONFIG_USB_EHCI_CONFIGFLAG)
  24. #error "t113 usb ehci has configflag register"
  25. #endif
  26. #if defined(CONFIG_USB_EHCI_ISO)
  27. #error "t113 usb ehci no iso register"
  28. #endif
  29. void usb_select_phyTohci(void)
  30. {
  31. *(volatile rt_uint32_t *)(USB0_OTG_BASE_ADDR + 0x420) &= ~(1 << 0);
  32. }
  33. void usb_gate_open(rt_uint8_t busid)
  34. {
  35. rt_uint32_t addr;
  36. addr = (rt_uint32_t)&CCU->usb0_clk + busid * 4;
  37. if(busid == 0) {
  38. /* otg gate open*/
  39. CCU->usb_bgr |= 1 << 8;
  40. /* otg bus reset */
  41. CCU->usb_bgr &= ~(1 << 24);
  42. sdelay(10);
  43. CCU->usb_bgr |= (1 << 24);
  44. sdelay(10);
  45. }
  46. /* ehci gate open */
  47. CCU->usb_bgr |= (1 << 4) << busid;
  48. /* ehci bus reset */
  49. CCU->usb_bgr &= ~((1 << 20) << busid);
  50. sdelay(10);
  51. CCU->usb_bgr |= (1 << 20) << busid;
  52. sdelay(10);
  53. /* ohci gate open */
  54. CCU->usb_bgr |= 1 << busid;
  55. /* ohci bus reset */
  56. CCU->usb_bgr &= ~((1 << 16) << busid);
  57. sdelay(10);
  58. CCU->usb_bgr |= (1 << 16) << busid;
  59. sdelay(10);
  60. sdelay(10);
  61. /* clock enable */
  62. *(volatile rt_uint32_t *)addr &= ~(3 << 24);
  63. *(volatile rt_uint32_t *)addr |= (1 << 31) | (1 << 24);
  64. /* reset phy */
  65. *(volatile rt_uint32_t *)addr &= ~(1 << 30);
  66. sdelay(10);
  67. *(volatile rt_uint32_t *)addr |= 1 << 30;
  68. sdelay(10);
  69. /* otg phy select */
  70. if(busid == 0) usb_select_phyTohci();
  71. USB_LOG_DBG("usb%d gate : %X, clock : %X\n", busid, CCU->usb_bgr, *(volatile rt_uint32_t *)addr);
  72. }
  73. void usb_clean_siddp(struct usbh_bus *bus)
  74. {
  75. *(volatile rt_uint32_t *)(bus->hcd.reg_base + 0x810) &= ~(1 << 3);
  76. }
  77. void usb_hci_set_passby(struct usbh_bus *bus)
  78. {
  79. /* AHB Master interface INCR16 enable */
  80. /* AHB Master interface INCR8 enable */
  81. /* AHB Master interface burst type INCR4 enable */
  82. /* AHB Master interface INCRX align enable */
  83. /* ULPI bypass enable */
  84. *(volatile rt_uint32_t *)(bus->hcd.reg_base + 0x800) |= (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 0);
  85. }
  86. void t113_ehci_isr(int vector, void *arg)
  87. {
  88. rt_interrupt_enter();
  89. struct usbh_bus *bus = (struct usbh_bus *)arg;
  90. USB_LOG_DBG("t113_ehci_isr");
  91. extern void USBH_IRQHandler(uint8_t busid);
  92. USBH_IRQHandler(bus->hcd.hcd_id);
  93. rt_interrupt_leave();
  94. }
  95. #ifdef CONFIG_USB_EHCI_WITH_OHCI
  96. void t113_ohci_isr(int vector, void *arg)
  97. {
  98. rt_interrupt_enter();
  99. struct usbh_bus *bus = (struct usbh_bus *)arg;
  100. USB_LOG_DBG("t113_ohci_isr");
  101. extern void OHCI_IRQHandler(uint8_t busid);
  102. OHCI_IRQHandler(bus->hcd.hcd_id);
  103. rt_interrupt_leave();
  104. }
  105. #endif
  106. void usb_hc_low_level_init(struct usbh_bus *bus)
  107. {
  108. int vector;
  109. RT_ASSERT(bus->busid <= 1);
  110. usb_gate_open(bus->busid);
  111. usb_clean_siddp(bus);
  112. usb_hci_set_passby(bus);
  113. /* register EHCI interrupt callback */
  114. vector = T113_IRQ_USB0_EHCI + (bus->busid > 0 ? 3 : 0);
  115. rt_hw_interrupt_install(vector, t113_ehci_isr, bus, RT_NULL);
  116. rt_hw_interrupt_umask(vector);
  117. /* register OHCI interrupt callback */
  118. #ifdef CONFIG_USB_EHCI_WITH_OHCI
  119. rt_hw_interrupt_install(vector + 1, t113_ohci_isr, bus, RT_NULL);
  120. rt_hw_interrupt_set_priority(vector, 11 << 4);
  121. rt_hw_interrupt_umask(vector + 1);
  122. #endif
  123. USB_LOG_DBG("usb%d vector : %d, phy : %X\n", bus->busid, vector, *(volatile rt_uint32_t *)(bus->hcd.reg_base + 0x810));
  124. USB_LOG_DBG("usb%d hc low level init success\n", bus->busid);
  125. }
  126. uint8_t usbh_get_port_speed(struct usbh_bus *bus, const uint8_t port)
  127. {
  128. /* Defined by individual manufacturers */
  129. uint32_t regval;
  130. regval = EHCI_HCOR->portsc[port - 1];
  131. if ((regval & EHCI_PORTSC_LSTATUS_MASK) == EHCI_PORTSC_LSTATUS_KSTATE)
  132. return USB_SPEED_LOW;
  133. if (regval & EHCI_PORTSC_PE)
  134. return USB_SPEED_HIGH;
  135. else
  136. return USB_SPEED_FULL;
  137. }
  138. int __usbh_init(void)
  139. {
  140. #ifdef T113_USING_USB0_HOST
  141. /* USB0 MSC test OK */
  142. usbh_initialize(0, USB0_BASE_ADDR);
  143. #endif
  144. #ifdef T113_USING_USB1_HOST
  145. /* USB1 MSC test OK */
  146. usbh_initialize(1, USB1_BASE_ADDR);
  147. #endif
  148. return 0;
  149. }
  150. #ifdef PKG_CHERRYUSB_HOST
  151. #include <rtthread.h>
  152. #include <rtdevice.h>
  153. INIT_ENV_EXPORT(__usbh_init);
  154. #endif