usb_glue_hpm.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /* Not use interrupt threshold. */
  31. ptr->USBCMD &= ~USB_USBCMD_ITC_MASK;
  32. }
  33. void usb_hc_low_level_init(struct usbh_bus *bus)
  34. {
  35. if (bus->hcd.reg_base == HPM_USB0_BASE) {
  36. _hcd_irqnum[bus->hcd.hcd_id] = IRQn_USB0;
  37. _hcd_busid[0] = bus->hcd.hcd_id;
  38. } else {
  39. #ifdef HPM_USB1_BASE
  40. if (bus->hcd.reg_base == HPM_USB1_BASE) {
  41. _hcd_irqnum[bus->hcd.hcd_id] = IRQn_USB1;
  42. _hcd_busid[1] = bus->hcd.hcd_id;
  43. }
  44. #endif
  45. }
  46. usb_phy_init((USB_Type *)(bus->hcd.reg_base));
  47. intc_m_enable_irq(_hcd_irqnum[bus->hcd.hcd_id]);
  48. }
  49. void usb_hc_low_level2_init(struct usbh_bus *bus)
  50. {
  51. usb_host_mode_init((USB_Type *)(bus->hcd.reg_base));
  52. }
  53. uint8_t usbh_get_port_speed(struct usbh_bus *bus, const uint8_t port)
  54. {
  55. (void)port;
  56. uint8_t speed;
  57. speed = usb_get_port_speed((USB_Type *)(bus->hcd.reg_base));
  58. if (speed == 0x00) {
  59. return USB_SPEED_FULL;
  60. }
  61. if (speed == 0x01) {
  62. return USB_SPEED_LOW;
  63. }
  64. if (speed == 0x02) {
  65. return USB_SPEED_HIGH;
  66. }
  67. return 0;
  68. }
  69. extern void USBH_IRQHandler(uint8_t busid);
  70. void isr_usbh0(void)
  71. {
  72. USBH_IRQHandler(_hcd_busid[0]);
  73. }
  74. SDK_DECLARE_EXT_ISR_M(IRQn_USB0, isr_usbh0)
  75. #ifdef HPM_USB1_BASE
  76. void isr_usbh1(void)
  77. {
  78. USBH_IRQHandler(_hcd_busid[1]);
  79. }
  80. SDK_DECLARE_EXT_ISR_M(IRQn_USB1, isr_usbh1)
  81. #endif