usb_hc_hpm.c 2.6 KB

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