usb_glue_hpm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2022-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include "usbh_core.h"
  8. #include "hpm_common.h"
  9. #include "hpm_soc.h"
  10. #include "hpm_usb_drv.h"
  11. #if !defined(CONFIG_USB_EHCI_HPMICRO) || !CONFIG_USB_EHCI_HPMICRO
  12. #error "hpm ehci must set CONFIG_USB_EHCI_HPMICRO=1"
  13. #endif
  14. #if !defined(CONFIG_USB_EHCI_HCCR_OFFSET) || CONFIG_USB_EHCI_HCCR_OFFSET != 0x100
  15. #error "hpm ehci must config CONFIG_USB_EHCI_HCCR_OFFSET to 0x100"
  16. #endif
  17. static uint32_t _hcd_irqnum[CONFIG_USBHOST_MAX_BUS];
  18. static uint8_t _hcd_busid[CONFIG_USBHOST_MAX_BUS];
  19. static void usb_host_mode_init(USB_Type *ptr)
  20. {
  21. /* Set mode to host, must be set immediately after reset */
  22. ptr->USBMODE &= ~USB_USBMODE_CM_MASK;
  23. ptr->USBMODE |= USB_USBMODE_CM_SET(3);
  24. /* Set the endian */
  25. ptr->USBMODE &= ~USB_USBMODE_ES_MASK;
  26. /* Set parallel interface signal */
  27. ptr->PORTSC1 &= ~USB_PORTSC1_STS_MASK;
  28. /* Set parallel transceiver width */
  29. ptr->PORTSC1 &= ~USB_PORTSC1_PTW_MASK;
  30. #ifdef CONFIG_USB_HOST_FORCE_FULL_SPEED
  31. /* Set usb forced to full speed mode */
  32. ptr->PORTSC1 |= USB_PORTSC1_PFSC_MASK;
  33. #endif
  34. /* Not use interrupt threshold. */
  35. ptr->USBCMD &= ~USB_USBCMD_ITC_MASK;
  36. }
  37. void usb_hc_low_level_init(struct usbh_bus *bus)
  38. {
  39. if (bus->hcd.reg_base == HPM_USB0_BASE) {
  40. _hcd_irqnum[bus->hcd.hcd_id] = IRQn_USB0;
  41. _hcd_busid[0] = bus->hcd.hcd_id;
  42. } else {
  43. #ifdef HPM_USB1_BASE
  44. if (bus->hcd.reg_base == HPM_USB1_BASE) {
  45. _hcd_irqnum[bus->hcd.hcd_id] = IRQn_USB1;
  46. _hcd_busid[1] = bus->hcd.hcd_id;
  47. }
  48. #endif
  49. }
  50. usb_phy_init((USB_Type *)(bus->hcd.reg_base), true);
  51. intc_m_enable_irq(_hcd_irqnum[bus->hcd.hcd_id]);
  52. }
  53. void usb_hc_low_level2_init(struct usbh_bus *bus)
  54. {
  55. usb_host_mode_init((USB_Type *)(bus->hcd.reg_base));
  56. }
  57. uint8_t usbh_get_port_speed(struct usbh_bus *bus, const uint8_t port)
  58. {
  59. (void)port;
  60. uint8_t speed;
  61. speed = usb_get_port_speed((USB_Type *)(bus->hcd.reg_base));
  62. if (speed == 0x00) {
  63. return USB_SPEED_FULL;
  64. }
  65. if (speed == 0x01) {
  66. return USB_SPEED_LOW;
  67. }
  68. if (speed == 0x02) {
  69. return USB_SPEED_HIGH;
  70. }
  71. return 0;
  72. }
  73. #if !defined(USBH_USE_CUSTOM_ISR) || !USBH_USE_CUSTOM_ISR
  74. extern void USBH_IRQHandler(uint8_t busid);
  75. SDK_DECLARE_EXT_ISR_M(IRQn_USB0, isr_usbh0)
  76. void isr_usbh0(void)
  77. {
  78. USBH_IRQHandler(_hcd_busid[0]);
  79. }
  80. #ifdef HPM_USB1_BASE
  81. SDK_DECLARE_EXT_ISR_M(IRQn_USB1, isr_usbh1)
  82. void isr_usbh1(void)
  83. {
  84. USBH_IRQHandler(_hcd_busid[1]);
  85. }
  86. #endif
  87. #endif