usb_glue_kendryte.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. #include "usbd_core.h"
  28. #include "usbh_core.h"
  29. #include "usb_dwc2_param.h"
  30. #include <riscv_io.h>
  31. #include "sysctl_rst.h"
  32. #include "ioremap.h"
  33. #include "mmu.h"
  34. #include "cache.h"
  35. extern rt_mmu_info mmu_info;
  36. #if defined(ENABLE_CHERRY_USB) || defined(PKG_USING_CHERRYUSB) || defined(RT_USING_CHERRYUSB)
  37. #define DEFAULT_USB_HCLK_FREQ_MHZ 200
  38. uint32_t SystemCoreClock = (DEFAULT_USB_HCLK_FREQ_MHZ * 1000 * 1000);
  39. const uintptr_t usb_dev_addr[2] = { 0x91500000UL, 0x91540000UL };
  40. #define USB_IDPULLUP0 (1 << 4)
  41. #define USB_DMPULLDOWN0 (1 << 8)
  42. #define USB_DPPULLDOWN0 (1 << 9)
  43. const struct dwc2_user_params param_common = {
  44. .phy_type = DWC2_PHY_TYPE_PARAM_UTMI,
  45. #ifdef CONFIG_USB_DWC2_DMA_ENABLE
  46. .device_dma_enable = true,
  47. #else
  48. .device_dma_enable = false,
  49. #endif
  50. .device_dma_desc_enable = false,
  51. .device_rx_fifo_size = (3016 - 16 - 256 * 8),
  52. .device_tx_fifo_size = {
  53. [0] = 16, // 64 byte
  54. [1] = 512, // 1024 byte, double buffer
  55. [2] = 256, // 1024 byte
  56. [3] = 512, // 1024 byte, double buffer
  57. [4] = 256, // 1024 byte
  58. [5] = 256, // 1024 byte
  59. [6] = 256, // 1024 byte
  60. [7] = 0,
  61. [8] = 0,
  62. [9] = 0,
  63. [10] = 0,
  64. [11] = 0,
  65. [12] = 0,
  66. [13] = 0,
  67. [14] = 0,
  68. [15] = 0 },
  69. .host_dma_desc_enable = false,
  70. .host_rx_fifo_size = 3016 - 128 * 2 - 256 * 2,
  71. .host_nperio_tx_fifo_size = 128 * 2, // 512 byte, double buffer
  72. .host_perio_tx_fifo_size = 256 * 2, // 1024 byte, double buffer
  73. .device_gccfg = 0,
  74. .host_gccfg = 0
  75. };
  76. #ifndef CONFIG_USB_DWC2_CUSTOM_PARAM
  77. void dwc2_get_user_params(uint32_t reg_base, struct dwc2_user_params *params)
  78. {
  79. memcpy(params, &param_common, sizeof(struct dwc2_user_params));
  80. #ifdef CONFIG_USB_DWC2_CUSTOM_FIFO
  81. struct usb_dwc2_user_fifo_config s_dwc2_fifo_config;
  82. dwc2_get_user_fifo_config(reg_base, &s_dwc2_fifo_config);
  83. params->device_rx_fifo_size = s_dwc2_fifo_config.device_rx_fifo_size;
  84. for (uint8_t i = 0; i < MAX_EPS_CHANNELS; i++) {
  85. params->device_tx_fifo_size[i] = s_dwc2_fifo_config.device_tx_fifo_size[i];
  86. }
  87. #endif
  88. }
  89. #endif
  90. // USB Host
  91. #if defined(ENABLE_CHERRY_USB_HOST) || defined(PKG_CHERRYUSB_HOST) || defined(RT_CHERRYUSB_HOST)
  92. static void usb_hc_interrupt_cb(int irq, void *arg_pv)
  93. {
  94. USBH_IRQHandler((uint8_t)(uintptr_t)arg_pv);
  95. }
  96. void usb_hc_low_level_init(struct usbh_bus *bus)
  97. {
  98. uint32_t *hs_reg;
  99. uint32_t usb_ctl3;
  100. if ((uintptr_t)rt_hw_mmu_v2p(&mmu_info, (void *)bus->hcd.reg_base) == usb_dev_addr[0]) {
  101. if (!sysctl_reset(SYSCTL_RESET_USB0)) {
  102. USB_LOG_ERR("reset usb0 fail\n");
  103. }
  104. hs_reg = (uint32_t *)rt_ioremap((void *)(0x91585000 + 0x7C), 0x1000);
  105. usb_ctl3 = *hs_reg | USB_IDPULLUP0;
  106. *hs_reg = usb_ctl3 | (USB_DMPULLDOWN0 | USB_DPPULLDOWN0);
  107. rt_iounmap(hs_reg);
  108. rt_hw_interrupt_install(173, usb_hc_interrupt_cb, (void *)(uintptr_t)bus->hcd.hcd_id, "usbh0");
  109. rt_hw_interrupt_umask(173);
  110. } else {
  111. if (!sysctl_reset(SYSCTL_RESET_USB1)) {
  112. USB_LOG_ERR("reset usb1 fail\n");
  113. }
  114. hs_reg = (uint32_t *)rt_ioremap((void *)(0x91585000 + 0x9C), 0x1000);
  115. usb_ctl3 = *hs_reg | USB_IDPULLUP0;
  116. *hs_reg = usb_ctl3 | (USB_DMPULLDOWN0 | USB_DPPULLDOWN0);
  117. rt_iounmap(hs_reg);
  118. rt_hw_interrupt_install(174, usb_hc_interrupt_cb, (void *)(uintptr_t)bus->hcd.hcd_id, "usbh1");
  119. rt_hw_interrupt_umask(174);
  120. }
  121. }
  122. void usb_hc_low_level_deinit(struct usbh_bus *bus)
  123. {
  124. if ((uintptr_t)rt_hw_mmu_v2p(&mmu_info, (void *)bus->hcd.reg_base) == usb_dev_addr[0]) {
  125. rt_hw_interrupt_mask(173);
  126. } else {
  127. rt_hw_interrupt_mask(174);
  128. }
  129. }
  130. #endif // ENABLE_CHERRY_USB_HOST
  131. // USB Device
  132. #if defined(ENABLE_CHERRY_USB_DEVICE) || defined(PKG_CHERRYUSB_DEVICE) || defined(RT_CHERRYUSB_DEVICE)
  133. static void usb_dc_interrupt_cb(int irq, void *arg_pv)
  134. {
  135. USBD_IRQHandler((uint8_t)(uintptr_t)arg_pv);
  136. }
  137. void usb_dc_low_level_init(uint8_t busid)
  138. {
  139. if ((uintptr_t)rt_hw_mmu_v2p(&mmu_info, (void *)g_usbdev_bus[busid].reg_base) == usb_dev_addr[0]) {
  140. if (!sysctl_reset(SYSCTL_RESET_USB0)) {
  141. USB_LOG_ERR("reset usb0 fail\n");
  142. }
  143. uint32_t *hs_reg = (uint32_t *)rt_ioremap((void *)(0x91585000 + 0x7C), 0x1000);
  144. *hs_reg = 0x37;
  145. rt_iounmap(hs_reg);
  146. rt_hw_interrupt_install(173, usb_dc_interrupt_cb, (void *)(uintptr_t)busid, "usbd0");
  147. rt_hw_interrupt_umask(173);
  148. } else {
  149. if (!sysctl_reset(SYSCTL_RESET_USB1)) {
  150. USB_LOG_ERR("reset usb1 fail\n");
  151. }
  152. uint32_t *hs_reg = (uint32_t *)rt_ioremap((void *)(0x91585000 + 0x9C), 0x1000);
  153. *hs_reg = 0x37;
  154. rt_iounmap(hs_reg);
  155. rt_hw_interrupt_install(174, usb_dc_interrupt_cb, (void *)(uintptr_t)busid, "usbd1");
  156. rt_hw_interrupt_umask(174);
  157. }
  158. }
  159. void usb_dc_low_level_deinit(uint8_t busid)
  160. {
  161. if ((uintptr_t)rt_hw_mmu_v2p(&mmu_info, (void *)g_usbdev_bus[busid].reg_base) == usb_dev_addr[0]) {
  162. rt_hw_interrupt_mask(173);
  163. } else {
  164. rt_hw_interrupt_mask(174);
  165. }
  166. }
  167. #endif // ENABLE_CHERRY_USB_DEVICE
  168. void usbd_dwc2_delay_ms(uint8_t ms)
  169. {
  170. rt_thread_mdelay(ms);
  171. }
  172. #ifdef CONFIG_USB_DCACHE_ENABLE
  173. void usb_dcache_clean(uintptr_t addr, size_t size)
  174. {
  175. rt_hw_cpu_dcache_clean((void *)addr, size);
  176. }
  177. void usb_dcache_invalidate(uintptr_t addr, size_t size)
  178. {
  179. rt_hw_cpu_dcache_invalidate((void *)addr, size);
  180. }
  181. void usb_dcache_flush(uintptr_t addr, size_t size)
  182. {
  183. rt_hw_cpu_dcache_clean_flush((void *)addr, size);
  184. }
  185. #endif
  186. #endif // ENABLE_CHERRY_USB