diff options
| author | sakumisu <[email protected]> | 2026-11-10 21:25:31 +0800 |
|---|---|---|
| committer | sakumisu <[email protected]> | 2026-11-10 21:25:31 +0800 |
| commit | a0054795c44037ace8a2b77b6e610adbff2257ea (patch) | |
| tree | f36dac4d25d67f9553cdd68c27a74be70f33a9d7 | |
| parent | 692e0697c5a60534fb2b8a2edff9a4a4c2f6edba (diff) | |
update(port/aic): sync code from luban lite
Signed-off-by: sakumisu <[email protected]>
| -rw-r--r-- | port/aic/usb_dc_aic.c | 472 | ||||
| -rw-r--r-- | port/aic/usb_dc_aic_ll.c | 58 | ||||
| -rw-r--r-- | port/aic/usb_dc_aic_reg.h | 88 | ||||
| -rw-r--r-- | port/aic/usb_glue_aic.c | 188 |
4 files changed, 669 insertions, 137 deletions
diff --git a/port/aic/usb_dc_aic.c b/port/aic/usb_dc_aic.c index fafa6118..1e0204ae 100644 --- a/port/aic/usb_dc_aic.c +++ b/port/aic/usb_dc_aic.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024, ArtInChip Technology Co., Ltd + * Copyright (c) 2023-2026, ArtInChip Technology Co., Ltd * * SPDX-License-Identifier: Apache-2.0 */ @@ -8,6 +8,15 @@ #include "usbd_core.h" #include "usb_dc_aic_reg.h" +#ifdef RT_USING_PM +#include <rtthread.h> +#endif + +#if defined(KERNEL_RTTHREAD) && defined(LPKG_CHERRYUSB_DEVICE_PLUG_CHECK) +#include <rtdevice.h> +#include "aic_hal.h" +#endif + // clang-format off #ifndef __UNALIGNED_UINT32_WRITE #pragma GCC diagnostic push @@ -58,6 +67,8 @@ #define AIC_UDC_REG ((AIC_UDC_RegDef *)(USB_BASE)) #define AIC_EP_FIFO(i) *(__IO uint32_t *)(USB_BASE + AIC_EP_FIFO_BASE + ((i)*AIC_EP_FIFO_SIZE)) +#define AIC_EP_READ_TIMEOUT 1000000U +#define AIC_EP_FIFOCTL_TIMEOUT 200000U /* Endpoint state */ struct aic_ep_state { uint16_t ep_mps; /* Endpoint max packet size */ @@ -82,8 +93,13 @@ extern uint32_t usbd_clk; struct usb_endpoint_descriptor in_ep_desc[USB_NUM_BIDIR_ENDPOINTS]; uint32_t p_txfifo_map; /* map which periodic_txfifo is used */ uint32_t np_txfifo_map; /* map no-periodic_txfifo is used */ + uint8_t test_mode; } g_aic_udc; +#ifndef AIC_USB_DEVICE_DRV_V10 +static uint32_t g_tx_fifo_sz_array[USB_NUM_BIDIR_ENDPOINTS] = {0}; +#endif + #define AIC_EP0_SETUP 1 #define AIC_EP0_DATA_IN 2 #define AIC_EP0_STATUS_OUT 3 @@ -91,6 +107,11 @@ extern uint32_t usbd_clk; #define AIC_EP0_STATUS_IN 13 uint8_t ep0_ctrl_stage = 0; /* 1 = setup stage, 2 = data stage, 3 = status stage */ +static int setup_handler_immediately = 0; + +#ifdef RT_USING_PM +static int usbd_pm_init(void); +#endif #ifdef CONFIG_USB_AIC_DMA_ENABLE static uint8_t g_aic_udc_ibuf[USB_RAM_SIZE] __ALIGNED(CACHE_LINE_SIZE); @@ -177,17 +198,27 @@ static void aic_udc_obuf_free(struct aic_ep_state *ep) static void aic_set_dma_nextep(void) { - uint32_t i; - uint32_t inepcfg = 0; + uint32_t i, j; + uint32_t inepcfg = 0, next = 0; + +#ifndef AIC_USB_DEVICE_DRV_V10 + return; +#endif /* dma to set the next-endpoint pointer. */ for (i = 0; i < USB_NUM_BIDIR_ENDPOINTS; i++) { - uint32_t next = ((i + 1) % USB_NUM_BIDIR_ENDPOINTS) << DEPCTL_NEXT_EP_BIT; + for (j = i + 1; j < USB_NUM_BIDIR_ENDPOINTS; j++) { + if ((g_aic_udc.np_txfifo_map & (1 << j)) == 0U) + continue; + next = (j << DEPCTL_NEXT_EP_BIT); + break; + } inepcfg = readl(&AIC_UDC_REG->inepcfg[i]); inepcfg &= ~DEPCTL_NEXT_EP_MASK; inepcfg |= next; writel(inepcfg, &AIC_UDC_REG->inepcfg[i]); + next = 0; } } @@ -198,7 +229,7 @@ static inline int aic_reset(void) /* Wait for AHB master IDLE state. */ do { - if (++count > 200000U) { + if (++count > AIC_EP_FIFOCTL_TIMEOUT) { return -1; } tmpreg = readl(&AIC_UDC_REG->ahbbasic); @@ -211,7 +242,7 @@ static inline int aic_reset(void) writel(tmpreg, &AIC_UDC_REG->usbdevinit); do { - if (++count > 200000U) { + if (++count > AIC_EP_FIFOCTL_TIMEOUT) { return -1; } tmpreg = readl(&AIC_UDC_REG->usbdevinit); @@ -227,7 +258,11 @@ static inline int aic_core_init(void) 0 << 19 /* ULPI Clock SuspendM */ | 0 << 18 /* ULPI Phy Auto Resume */ | 0 << 15 /* PHY Low Power Clock sel */ +#ifdef FPGA_BOARD_ARTINCHIP + | 0x9 << 10 /* USB Turnaround time (0x5 for HS phy) */ +#else | 0x5 << 10 /* USB Turnaround time (0x5 for HS phy) */ +#endif | 0 << 7 /* ULPI DDR sel 0:single 8bit, 1:double 4bit */ /*| 0 << 6 0: high speed utmi+, 1: full speed serial*/ #ifdef FPGA_BOARD_ARTINCHIP @@ -239,11 +274,12 @@ static inline int aic_core_init(void) | 0x7 << 0; /* HS/FS timeout calibration**/ /* Activate the USB Transceiver */ - writel(usb_gusbcfg, &AIC_UDC_REG->usbphyif); /* Reset after a PHY select */ ret = aic_reset(); + writel(usb_gusbcfg, &AIC_UDC_REG->usbphyif); + aic_set_dma_nextep(); return ret; @@ -259,7 +295,7 @@ static inline int aic_flush_rxfifo(void) writel(tmpreg, &AIC_UDC_REG->usbdevinit); do { - if (++count > 200000U) { + if (++count > AIC_EP_FIFOCTL_TIMEOUT) { return -1; } tmpreg = readl(&AIC_UDC_REG->usbdevinit); @@ -280,7 +316,7 @@ static inline int aic_flush_txfifo(uint32_t num) writel(tmpreg, &AIC_UDC_REG->usbdevinit); do { - if (++count > 200000U) { + if (++count > AIC_EP_FIFOCTL_TIMEOUT) { return -1; } tmpreg = readl(&AIC_UDC_REG->usbdevinit); @@ -293,7 +329,9 @@ static void aic_set_turnaroundtime(uint32_t hclk, uint8_t speed) { uint32_t UsbTrd; uint32_t tmpreg = 0; - +#ifdef FPGA_BOARD_ARTINCHIP + return; +#endif /* The USBTRD is configured according to the tables below, depending on AHB frequency used by application. In the low AHB frequency range it is used to stretch enough the USB response time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access @@ -393,10 +431,10 @@ static uint8_t aic_get_devspeed(void) return speed; } -static void aic_ep0_start_read_setup(uint8_t *psetup) +void aic_ep0_start_read_setup(void) { uint32_t outepcfg = 0; - + uint8_t *psetup = (uint8_t *)&g_aic_udc.setup; g_aic_udc.out_ep[0].xfer_buf = psetup; g_aic_udc.out_ep[0].xfer_len = 8; ep0_ctrl_stage = AIC_EP0_SETUP; @@ -406,7 +444,7 @@ static void aic_ep0_start_read_setup(uint8_t *psetup) aic_udc_dcache_invalidate((uintptr_t)psetup, CACHE_LINE_SIZE); - writel((1<<29) | (DXEPTSIZ_PKT_CNT_MASK & (1U << 19)) | (8U), &AIC_UDC_REG->outeptsfsiz[0]); + writel((1<<29) | (DXEPTSIZ_PKT_CNT_MASK & (3U << 19)) | (24U), &AIC_UDC_REG->outeptsfsiz[0]); #ifdef CONFIG_USB_AIC_DMA_ENABLE writel((uint32_t)(uintptr_t)psetup, &AIC_UDC_REG->outepdmaaddr[0]); @@ -419,6 +457,11 @@ static void aic_ep0_start_read_setup(uint8_t *psetup) writel(outepcfg, &AIC_UDC_REG->outepcfg[0]); } +void usbd_ep0_start_read_zlp(void) +{ + usbd_ep_start_read(0, USB_CONTROL_OUT_EP0, (uint8_t *)&g_aic_udc.setup, 0); +} + void aic_ep_write(uint8_t ep_idx, uint8_t *src, uint16_t len) { uint32_t *pSrc = (uint32_t *)src; @@ -444,20 +487,41 @@ void aic_ep_read(uint8_t *dest, uint16_t len) } #ifndef CONFIG_USB_AIC_DMA_ENABLE -static void aic_tx_fifo_empty_procecss(uint8_t ep_idx) +static int aic_tx_fifo_is_ready(uint8_t ep_idx) { uint32_t len; - //uint32_t len32b; + uint32_t len32b; len = g_aic_udc.in_ep[ep_idx].xfer_len - g_aic_udc.in_ep[ep_idx].actual_xfer_len; if (len > g_aic_udc.in_ep[ep_idx].ep_mps) { len = g_aic_udc.in_ep[ep_idx].ep_mps; } - //len32b = (len + 3U) / 4U; + len32b = (len + 3U) / 4U; + + if (AIC_UDC_REG->inepcfg[ep_idx] & DEPCTL_EPENA + && (g_aic_udc.in_ep[ep_idx].actual_xfer_len < g_aic_udc.in_ep[ep_idx].xfer_len) + && (g_aic_udc.in_ep[ep_idx].xfer_len != 0U) +#ifdef AIC_USB_DEVICE_DRV_V10 + &&((AIC_UDC_REG->nptxfifosts & INEPTXSTS_IN_EP_TXFIFO_STS) >= len32b) + && ((AIC_UDC_REG->nptxfifosts & INEPTXSTS_IN_EP_TXQUEUE_STS) != 0) +#else + &&((AIC_UDC_REG->ineptxsts[ep_idx] & INEPTXSTS_IN_EP_TXFIFO_STS) >= len32b) +#endif + ) { + return 0; + } else { + return -1; + } +} + +static void aic_tx_fifo_empty_procecss(uint8_t ep_idx) +{ + uint32_t len; + + len = g_aic_udc.in_ep[ep_idx].xfer_len - g_aic_udc.in_ep[ep_idx].actual_xfer_len; - while (/*((AIC_UDC_REG->ineptxsts[ep_idx] & INEPTXSTS_IN_EP_TXFIFO_STS) >= len32b) &&*/ - (g_aic_udc.in_ep[ep_idx].actual_xfer_len < g_aic_udc.in_ep[ep_idx].xfer_len) && (g_aic_udc.in_ep[ep_idx].xfer_len != 0U)) { + while (aic_tx_fifo_is_ready(ep_idx) == 0) { /* Write the FIFO */ len = g_aic_udc.in_ep[ep_idx].xfer_len - g_aic_udc.in_ep[ep_idx].actual_xfer_len; if (len > g_aic_udc.in_ep[ep_idx].ep_mps) { @@ -567,7 +631,7 @@ int usb_dc_rst(void) USB_LOG_DBG("usb_dc_rst.\n"); - usbd_set_address(0); + usbd_set_address(0, 0); aic_flush_txfifo(0x10U); aic_flush_rxfifo(); @@ -577,8 +641,8 @@ int usb_dc_rst(void) writel(DEPCTL_SNAK, &AIC_UDC_REG->inepcfg[i]); writel(DEPCTL_SNAK, &AIC_UDC_REG->outepcfg[i]); } else { - usbd_ep_close(i); - usbd_ep_close(i | 0x80); + usbd_ep_close(0, i); + usbd_ep_close(0, i | 0x80); } writel(0U, &AIC_UDC_REG->ineptsfsiz[i]); @@ -609,11 +673,23 @@ int usb_dc_rst(void) memset(&g_aic_udc, 0, sizeof(struct aic_udc)); usbd_event_reset_handler(0); /* Start reading setup */ - aic_ep0_start_read_setup((uint8_t *)&g_aic_udc.setup); + aic_ep0_start_read_setup(); return 0; } +static int _set_tx_fifo_size(void) +{ + #ifndef AIC_USB_DEVICE_DRV_V10 + g_tx_fifo_sz_array[0] = AIC_DEDICATED_TX_FIFO0_SIZE; + g_tx_fifo_sz_array[1] = AIC_DEDICATED_TX_FIFO1_SIZE; + g_tx_fifo_sz_array[2] = AIC_DEDICATED_TX_FIFO2_SIZE; + g_tx_fifo_sz_array[3] = AIC_DEDICATED_TX_FIFO3_SIZE; + g_tx_fifo_sz_array[4] = AIC_DEDICATED_TX_FIFO4_SIZE; + #endif + return 0; +} + int usb_dc_init(uint8_t busid) { int ret; @@ -635,10 +711,12 @@ int usb_dc_init(uint8_t busid) ret = aic_core_init(); - for (uint8_t i = 0U; i < 2U; i++) { + for (uint8_t i = 0U; i < (USB_NUM_BIDIR_ENDPOINTS - 1); i++) { writel(0, &AIC_UDC_REG->txfifosiz[i]); } + _set_tx_fifo_size(); + /* Device mode configuration */ tmpreg = readl(&AIC_UDC_REG->usbdevconf); tmpreg |= PERIOD_FRAME_INTERVAL_80; @@ -667,7 +745,7 @@ int usb_dc_init(uint8_t busid) /* Enable interrupts matching to the Device mode ONLY */ tmpreg = INT_RESET | INT_ENUMDONE | INT_OUT_EP | INT_IN_EP | - INT_INCOMP_ISO_IN_INT | INT_INCOMP_ISO_OUT_INT | INT_SUSPEND; + INT_INCOMP_ISO_IN_INT | INT_INCOMP_ISO_OUT_INT | INT_SUSPEND | INT_RESUME; writel(tmpreg, &AIC_UDC_REG->usbintmsk); #ifdef CONFIG_USB_AIC_DMA_ENABLE @@ -685,11 +763,25 @@ int usb_dc_init(uint8_t busid) base = 0; writel(AIC_RX_FIFO_SIZE, &AIC_UDC_REG->rxfifosiz); base += AIC_RX_FIFO_SIZE; +#ifdef AIC_USB_DEVICE_DRV_V10 writel((AIC_NP_TX_FIFO_SIZE << 16) | base, &AIC_UDC_REG->nptxfifosiz); base += AIC_NP_TX_FIFO_SIZE; writel((AIC_PERIOD_TX_FIFO1_SIZE << 16) | base, &AIC_UDC_REG->txfifosiz[0]); base += AIC_PERIOD_TX_FIFO1_SIZE; writel((AIC_PERIOD_TX_FIFO2_SIZE << 16) | base, &AIC_UDC_REG->txfifosiz[1]); +#else + AIC_UDC_REG->ietxfifosiz = (g_tx_fifo_sz_array[0] << 16) | base; + base += g_tx_fifo_sz_array[0]; + + for (int i = 1; i < USB_NUM_BIDIR_ENDPOINTS; i++) { + /* txfifo is configuared separately, starting from txfifo 1 here. */ + + writel((g_tx_fifo_sz_array[i] << 16) | base, &AIC_UDC_REG->txfifosiz[i -1]); + base += g_tx_fifo_sz_array[i]; + if (g_tx_fifo_sz_array[i] == 0) + writel((g_tx_fifo_sz_array[i] << 16) | 0, &AIC_UDC_REG->txfifosiz[i -1]); + } +#endif usb_dc_rst(); @@ -701,7 +793,12 @@ int usb_dc_init(uint8_t busid) tmpreg = readl(&AIC_UDC_REG->usbdevfunc); tmpreg &= ~USBDEVFUNC_SFTDISCON; writel(tmpreg, &AIC_UDC_REG->usbdevfunc); - +#if defined(KERNEL_RTTHREAD) && defined(LPKG_CHERRYUSB_DEVICE_PLUG_CHECK) + ret = usbd_plug_irq_init(); +#endif +#ifdef RT_USING_PM + ret = usbd_pm_init(); +#endif return ret; } @@ -709,7 +806,7 @@ int usb_dc_deinit(uint8_t busid) { uint32_t tmpreg = 0; - usbd_event_suspend_handler(); + usbd_event_suspend_handler(busid); /* Clear Pending interrupt */ for (uint8_t i = 0U; i < USB_NUM_BIDIR_ENDPOINTS; i++) { @@ -746,7 +843,21 @@ int usbd_set_address(uint8_t busid, const uint8_t addr) return 0; } -uint8_t usbd_get_port_speed(uint8_t busid, const uint8_t port) +int usbd_set_remote_wakeup(uint8_t busid) +{ + uint32_t tmpreg = 0; + + tmpreg |= USBDEVFUNC_RMTWKUPSIG; + writel(tmpreg, &AIC_UDC_REG->usbdevfunc); + aicos_mdelay(7); + tmpreg &= ~USBDEVFUNC_RMTWKUPSIG; + writel(tmpreg, &AIC_UDC_REG->usbdevfunc); + aicos_mdelay(7); + + return 0; +} + +uint8_t usbd_get_port_speed(uint8_t busid) { uint8_t speed; uint32_t DevEnumSpeed = readl(&AIC_UDC_REG->usblinests); @@ -764,12 +875,22 @@ uint8_t usbd_get_port_speed(uint8_t busid, const uint8_t port) return speed; } +void usbd_iso_threshold_cfg(void) +{ +#ifndef AIC_USB_DEVICE_DRV_V10 + uint32_t reg = readl(&AIC_UDC_REG->thr_ctl); + + reg |= ISO_THR_EN | TX_THR_LEN(128); + + writel(reg, &AIC_UDC_REG->thr_ctl); +#endif +} + int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep) { uint8_t ep_idx = USB_EP_GET_IDX(ep->bEndpointAddress); uint16_t ep_mps; uint8_t tx_fifo_num = 0; - uint32_t i; uint32_t usbepintmsk = 0; uint32_t epcfg = 0; @@ -816,11 +937,11 @@ int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep) usbepintmsk |= DAINT_IN_MASK & (uint32_t)(1UL << ep_idx); writel(usbepintmsk, &AIC_UDC_REG->usbepintmsk); - +#ifdef AIC_USB_DEVICE_DRV_V10 /* Period IN EP alloc fifo num */ if ((USB_GET_ENDPOINT_TYPE(ep->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) || (USB_GET_ENDPOINT_TYPE(ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS)) { - for (i = 1; i <= USB_P_TXFIFO_NUM; i++) { + for (int i = 1; i <= USB_P_TXFIFO_NUM; i++) { if (g_aic_udc.p_txfifo_map & (1 << i)) continue; g_aic_udc.p_txfifo_map |= (1 << i); @@ -835,6 +956,14 @@ int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep) } else { g_aic_udc.np_txfifo_map |= (1 << ep_idx); } + aic_set_dma_nextep(); +#else + tx_fifo_num = ep_idx; + aic_flush_txfifo(tx_fifo_num); + if ((USB_GET_ENDPOINT_TYPE(ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS)) { + usbd_iso_threshold_cfg(); + } +#endif epcfg = readl(&AIC_UDC_REG->inepcfg[ep_idx]); epcfg |= (ep_mps & DEPCTL_MPS_MASK) | @@ -976,10 +1105,12 @@ int usbd_ep_close(uint8_t busid, const uint8_t ep) aic_flush_txfifo(tx_fifo_num); /* Period IN EP free fifo num */ + #ifdef AIC_USB_DEVICE_DRV_V10 if (tx_fifo_num > 0) g_aic_udc.p_txfifo_map &= ~(1 << tx_fifo_num); else g_aic_udc.np_txfifo_map &= ~(1 << ep_idx); + #endif /* (4) Clear Global In NP NAK in Shared FIFO for non periodic ep */ if (!tx_fifo_num) { @@ -1103,11 +1234,11 @@ int usbd_npinep_rewrite(const uint8_t ep) writel(tmpreg, &AIC_UDC_REG->usbdevfunc); /* (2) reopen current ep */ - usbd_ep_open(&g_aic_udc.in_ep_desc[ep]); + usbd_ep_open(0, &g_aic_udc.in_ep_desc[ep]); /* (3) rewrite current ep */ if (pending_map & (1 << ep)) { - usbd_ep_start_write(ep, g_aic_udc.in_ep[ep].xfer_buf, + usbd_ep_start_write(0, ep, g_aic_udc.in_ep[ep].xfer_buf, g_aic_udc.in_ep[ep].xfer_len); } @@ -1116,8 +1247,8 @@ int usbd_npinep_rewrite(const uint8_t ep) if (!(pending_map & (1 << i)) || (i == ep)) continue; - usbd_ep_open(&g_aic_udc.in_ep_desc[i]); - usbd_ep_start_write(i, g_aic_udc.in_ep[i].xfer_buf, + usbd_ep_open(0, &g_aic_udc.in_ep_desc[i]); + usbd_ep_start_write(0, i, g_aic_udc.in_ep[i].xfer_buf, g_aic_udc.in_ep[i].xfer_len); } @@ -1147,7 +1278,7 @@ int usbd_ep_set_stall(uint8_t busid, const uint8_t ep) #ifdef CONFIG_USB_AIC_DMA_ENABLE if (ep_idx == 0) { - aic_ep0_start_read_setup((uint8_t *)&g_aic_udc.setup); + aic_ep0_start_read_setup(); } #endif return 0; @@ -1241,6 +1372,10 @@ int usbd_ep_start_write_raw(const uint8_t ep, const uint8_t *data, uint32_t data ineptsfsiz &= ~(DXEPTSIZ_XFER_SIZE_MASK); if (data_len == 0) { +#ifdef CONFIG_USB_AIC_DMA_ENABLE + if (data) + writel((uint32_t)(uintptr_t)data, &AIC_UDC_REG->inepdmaaddr[ep_idx]); +#endif ineptsfsiz |= (DXEPTSIZ_PKT_CNT_MASK & (1U << 19)); writel(ineptsfsiz, &AIC_UDC_REG->ineptsfsiz[ep_idx]); inepcfg |= (DEPCTL_CNAK | DEPCTL_EPENA); @@ -1272,7 +1407,7 @@ int usbd_ep_start_write_raw(const uint8_t ep, const uint8_t *data, uint32_t data inepcfg |= DEPCTL_SETD0PID; } ineptsfsiz &= ~(DXEPTSIZ_MULCNT_MASK); - ineptsfsiz |= ((pktcnt << DXEPTSIZ_MULCNT_SHIFT) & DXEPTSIZ_MULCNT_MASK); + ineptsfsiz |= ((MIN(3, pktcnt) << DXEPTSIZ_MULCNT_SHIFT) & DXEPTSIZ_MULCNT_MASK); } else if (g_aic_udc.in_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_INTERRUPT) { ineptsfsiz &= ~(DXEPTSIZ_MULCNT_MASK); ineptsfsiz |= (1U << DXEPTSIZ_MULCNT_SHIFT); @@ -1301,7 +1436,6 @@ int usbd_ep_start_read_raw(const uint8_t ep, uint8_t *data, uint32_t data_len, u { uint8_t ep_idx = USB_EP_GET_IDX(ep); uint32_t pktcnt = 0; - uint32_t i = 0; uint32_t outepcfg = 0; uint32_t outeptsfsiz = 0; uint32_t usblinests = 0; @@ -1311,12 +1445,13 @@ int usbd_ep_start_read_raw(const uint8_t ep, uint8_t *data, uint32_t data_len, u } outepcfg = readl(&AIC_UDC_REG->outepcfg[ep_idx]); if (outepcfg & DEPCTL_EPENA) { - for (i = 0; i < 100000; i++) { + uint32_t i = 0; + for (i = 0; i < AIC_EP_READ_TIMEOUT; i++) { outepcfg = readl(&AIC_UDC_REG->outepcfg[ep_idx]); if (!(outepcfg & DEPCTL_EPENA)) break; } - if (i == 1000000) { + if (i == AIC_EP_READ_TIMEOUT) { g_aic_udc.out_ep[ep_idx].xfer_len = data_len; USB_LOG_ERR("ep%d AIC_UDC_REG->outepcfg[ep_idx] = 0x%x.\n", (unsigned int)ep_idx, (unsigned int)outepcfg); @@ -1365,6 +1500,10 @@ int usbd_ep_start_read_raw(const uint8_t ep, uint8_t *data, uint32_t data_len, u outeptsfsiz |= (DXEPTSIZ_PKT_CNT_MASK & (1 << 19)); outeptsfsiz |= (DXEPTSIZ_XFER_SIZE_MASK & g_aic_udc.out_ep[ep_idx].ep_mps); writel(outeptsfsiz, &AIC_UDC_REG->outeptsfsiz[ep_idx]); + #ifdef CONFIG_USB_AIC_DMA_ENABLE + if (data) + writel((uint32_t)(uintptr_t)data, &AIC_UDC_REG->outepdmaaddr[ep_idx]); + #endif outepcfg |= (DEPCTL_CNAK | DEPCTL_EPENA); writel(outepcfg, &AIC_UDC_REG->outepcfg[ep_idx]); return 0; @@ -1412,7 +1551,100 @@ int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t return usbd_ep_start_read_raw(ep, data, data_len, 1); } -void USBD_IRQHandler(void) +#ifdef CONFIG_USBDEV_TEST_MODE + +void usbd_req_test_mode(uint8_t test_mode) +{ + g_aic_udc.test_mode = test_mode; +} + +void usbd_execute_test_mode(uint8_t test_mode) +{ + uint32_t regval; + uint32_t test_mode_val; + + switch (test_mode) { + case TEST_SE0_NAK_MODE: + test_mode_val = TEST_SE0_NAK_MODE; + USB_LOG_INFO("TEST_SE0_NAK_MODE\n"); + break; + case TEST_J_MODE: + test_mode_val = TEST_J_MODE; + USB_LOG_INFO("TEST_J_MODE\n"); + break; + case TEST_K_MODE: + test_mode_val = TEST_K_MODE; + USB_LOG_INFO("TEST_K_MODE\n"); + break; + case TEST_PACKET_MODE: + test_mode_val = TEST_PACKET_MODE; + USB_LOG_INFO("TEST_PACKET_MODE\n"); + break; + case TEST_FORCE_MODE: + test_mode_val = TEST_FORCE_MODE; + USB_LOG_INFO("TEST_FORCE_MODE\n"); + break; + default: + return; + } + + regval = readl(&AIC_UDC_REG->usbdevfunc); + regval |= test_mode_val << USBDEVFUNC_TSTCTL_SHIFT; + writel(regval, &AIC_UDC_REG->usbdevfunc); +} +#endif + +static int usbd_ep0_setup_handler(void) +{ + uint32_t outepdmaaddr = readl(&AIC_UDC_REG->outepdmaaddr[0]); + struct usb_setup_packet *setup = (struct usb_setup_packet *)(uintptr_t)outepdmaaddr; + setup--; + if (setup == NULL) { + USB_LOG_ERR("outepdmaaddr is null.\n"); + return -1; + } + + if (g_aic_udc.setup.wLength) { + if ((g_aic_udc.setup.bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) { + ep0_ctrl_stage = AIC_EP0_DATA_OUT; + // USB_LOG_DBG("out cb -> out data start\n"); + } else { + ep0_ctrl_stage = AIC_EP0_DATA_IN; + } + } else { + ep0_ctrl_stage = AIC_EP0_STATUS_IN; + } + + usbd_event_ep0_setup_complete_handler(0, (uint8_t *)setup); + + return 0; +} + +static int usbd_ep0_setup_handler_immediately(void) +{ + uint32_t count = 0; + + aic_udc_dcache_invalidate((uintptr_t)&g_aic_udc, CACHE_LINE_SIZE); + + USB_LOG_DBG("out cb -> out setup + done %#x %#x %#x ", + readl(&AIC_UDC_REG->inepcfg[0]), + readl(&AIC_UDC_REG->outepcfg[0]), + *(uint16_t *)&g_aic_udc.setup); + + setup_handler_immediately = 1; + do { + if (++count > AIC_EP_FIFOCTL_TIMEOUT) { + USB_LOG_ERR("(E) setup busy timeout. -> %#x %#x\n", + readl(&AIC_UDC_REG->inepcfg[0]), readl(&AIC_UDC_REG->outepcfg[0])); + break; + } + } while ((readl(&AIC_UDC_REG->outepcfg[0]) & DEPCTL_NAK_STATUS) == 0); + + return 0; +} + + +void usbd_irq_handler(void) { uint32_t gint_status, ep_idx, ep_intr, epint, daintmask, tmpreg, tmpreg01; gint_status = aic_get_glb_intstatus(); @@ -1422,6 +1654,11 @@ void USBD_IRQHandler(void) return; } +#if defined(RT_USING_PM) && defined(AIC_USING_PM) && defined(AIC_USB_DISP_PM_SUSPEND) + #define USB_DEV_SUSPEND_DELAY_MS 2000 + rt_pm_module_delay_sleep(PM_USB_DEV_ID, rt_tick_from_millisecond(USB_DEV_SUSPEND_DELAY_MS)); +#endif + #ifndef CONFIG_USB_AIC_DMA_ENABLE uint32_t temp; uint32_t read_count; @@ -1474,27 +1711,44 @@ void USBD_IRQHandler(void) /* Setup data-out will send STATUS_PHASE_RCVD interrupt when next setup. Attemp to re-read setup data. */ if (epint & CTRL_OUT_EP_STATUS_PHASE_RCVD) { - aic_ep0_start_read_setup((uint8_t *)&g_aic_udc.setup); + if (epint & TRANSFER_DONE) { + /* case E*/ + aic_ep0_start_read_setup(); + USB_LOG_DBG("(E)out cb -> out status %d \n", epint); + } else { + /* case D */ + usbd_ep_start_read(0, USB_CONTROL_OUT_EP0, (uint8_t *)&g_aic_udc.setup, 0); + USB_LOG_DBG("(D)out cb -> out status %d \n", epint); + } } /* Normal setup handle */ if (epint & CTRL_OUT_EP_SETUP_PHASE_DONE) { - if (g_aic_udc.setup.wLength) - if ((g_aic_udc.setup.bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) - ep0_ctrl_stage = AIC_EP0_DATA_OUT; - else - ep0_ctrl_stage = AIC_EP0_DATA_IN; - else - ep0_ctrl_stage = AIC_EP0_STATUS_IN; - usbd_event_ep0_setup_complete_handler(0, (uint8_t *)&g_aic_udc.setup); + USB_LOG_DBG("out cb -> out setup %#x %#x %#x \n", + readl(&AIC_UDC_REG->inepcfg[0]), + readl(&AIC_UDC_REG->outepcfg[0]), + *(uint16_t *)&g_aic_udc.setup); + + usbd_ep0_setup_handler(); } } else { + if (epint & CTRL_OUT_EP_SETUP_PHASE_DONE) { + usbd_ep0_setup_handler_immediately(); + } + if ((epint & TRANSFER_DONE) == TRANSFER_DONE) { if (ep->xfer_len == 0) { + USB_LOG_DBG("out cb -> out status\n"); /* Out status, start reading setup */ - aic_ep0_start_read_setup((uint8_t *)&g_aic_udc.setup); + if (setup_handler_immediately) { + usbd_ep0_setup_handler(); + setup_handler_immediately = 0; + } else { + aic_ep0_start_read_setup(); + } } else { - //ep->actual_xfer_len = ep->xfer_len - ((AIC_UDC_REG->outeptsfsiz[ep_idx]) & DXEPTSIZ_XFER_SIZE_MASK); + USB_LOG_DBG("out cb -> out data %#x %#x \n", readl(&AIC_UDC_REG->inepcfg[0]), readl(&AIC_UDC_REG->outepcfg[0])); + // ep->actual_xfer_len = ep->xfer_len - ((AIC_UDC_REG->outeptsfsiz[ep_idx]) & DXEPTSIZ_XFER_SIZE_MASK); ep->actual_xfer_len = ep->xfer_len; ep->xfer_len = 0; usbd_event_ep_out_complete_handler(0, 0x00, ep->actual_xfer_len); @@ -1554,15 +1808,24 @@ void USBD_IRQHandler(void) if (ep_idx == 0) { if (g_aic_udc.setup.wLength && ((g_aic_udc.setup.bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT)) { /* In status, start reading setup */ - aic_ep0_start_read_setup((uint8_t *)&g_aic_udc.setup); + aic_ep0_start_read_setup(); } else if (g_aic_udc.setup.wLength == 0) { /* In status, start reading setup */ - aic_ep0_start_read_setup((uint8_t *)&g_aic_udc.setup); + USB_LOG_DBG("in cb -> zlp %d \n", setup_handler_immediately); + aic_ep0_start_read_setup(); } else { + USB_LOG_DBG("in cb -> data %d\n", g_aic_udc.in_ep[ep_idx].xfer_len); g_aic_udc.in_ep[ep_idx].actual_xfer_len = g_aic_udc.in_ep[ep_idx].xfer_len - ((AIC_UDC_REG->ineptsfsiz[ep_idx]) & DXEPTSIZ_XFER_SIZE_MASK); g_aic_udc.in_ep[ep_idx].xfer_len = 0; usbd_event_ep_in_complete_handler(0, 0x80, g_aic_udc.in_ep[ep_idx].actual_xfer_len); } +#ifdef CONFIG_USBDEV_TEST_MODE + if (g_aic_udc.test_mode) { + usbd_execute_test_mode(g_aic_udc.test_mode); + g_aic_udc.test_mode = 0; + return; + } +#endif } else { tmpreg = readl(&AIC_UDC_REG->ineptsfsiz[ep_idx]); g_aic_udc.in_ep[ep_idx].actual_xfer_len = g_aic_udc.in_ep[ep_idx].xfer_len - (tmpreg & DXEPTSIZ_XFER_SIZE_MASK); @@ -1659,3 +1922,106 @@ void USBD_IRQHandler(void) writel(INT_RESUME, &AIC_UDC_REG->usbintsts); } } + +/*add usb plug/unplug check function*/ +#if defined(KERNEL_RTTHREAD) && defined(LPKG_CHERRYUSB_DEVICE_PLUG_CHECK) +void usb_plug_irq(void *args) +{ + unsigned int ret; + int pin = *((int *)(args)); + + hal_gpio_get_value(GPIO_GROUP(pin), GPIO_GROUP_PIN(pin), &ret); + if (ret) { + usbd_event_connect_handler(0); + } else { + usbd_event_disconnect_handler(0); + } +} +int usbd_plug_irq_init(void) +{ + static rt_base_t pin = 0; + unsigned int g, p; + + pin = rt_pin_get(LPKG_PLUG_CHECK_GPIO); + if (pin < 0) { + USB_LOG_ERR("get id gpio err.\n"); + return -1; + } + + g = GPIO_GROUP(pin); + p = GPIO_GROUP_PIN(pin); + hal_gpio_set_debounce(g, p, 0xFFF); + rt_pin_mode(pin, PIN_MODE_INPUT); + + rt_pin_attach_irq(pin, PIN_IRQ_MODE_RISING_FALLING, usb_plug_irq, &pin); + rt_pin_irq_enable(pin, 1); + return 0; +} +#endif + +#ifdef RT_USING_PM +static int aic_usbd_suspend(const struct rt_device *device, rt_uint8_t mode) +{ + switch (mode) { + case PM_SLEEP_MODE_IDLE: + break; + case PM_SLEEP_MODE_LIGHT: + case PM_SLEEP_MODE_DEEP: + case PM_SLEEP_MODE_STANDBY: + case PM_SLEEP_MODE_SHUTDOWN: + usb_dc_deinit(0); + break; + default: + break; + } + + return 0; +} + +static void aic_usbd_resume(const struct rt_device *device, rt_uint8_t mode) +{ + switch (mode) { + case PM_SLEEP_MODE_IDLE: + break; + case PM_SLEEP_MODE_LIGHT: + case PM_SLEEP_MODE_DEEP: + case PM_SLEEP_MODE_STANDBY: + case PM_SLEEP_MODE_SHUTDOWN: + usb_dc_init(0); + break; + default: + break; + } +} + +static struct rt_device_pm_ops aic_usbd_pm_ops = +{ + SET_DEVICE_PM_OPS(aic_usbd_suspend, aic_usbd_resume) + NULL, +}; + +struct rt_device *usbd_device = RT_NULL; + +static int usbd_pm_init(void) +{ + if (usbd_device) + return 0; + + usbd_device = (struct rt_device *)rt_malloc(sizeof(struct rt_device)); + if (usbd_device) { + rt_memset(usbd_device, 0, sizeof(struct rt_device)); + usbd_device->user_data = &g_aic_udc; + if (rt_device_register(usbd_device, "usbd", RT_DEVICE_FLAG_RDWR) != RT_EOK) { + USB_LOG_WRN("Failed to register usbd device\n"); + rt_free(usbd_device); + usbd_device = RT_NULL; + return -1; + } else { + rt_pm_device_register(usbd_device, &aic_usbd_pm_ops); + } + } + + return 0; +} + +#endif diff --git a/port/aic/usb_dc_aic_ll.c b/port/aic/usb_dc_aic_ll.c index e9b5b692..59079c38 100644 --- a/port/aic/usb_dc_aic_ll.c +++ b/port/aic/usb_dc_aic_ll.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024, ArtInChip Technology Co., Ltd + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,16 +10,56 @@ #include "usbd_core.h" #include "usb_dc_aic_reg.h" -extern irqreturn_t USBD_IRQHandler(int irq, void * data); +extern irqreturn_t usbd_irq_handler(int irq, void * data); uint32_t usbd_clk; static unsigned char dma_sync_buffer[CACHE_LINE_SIZE] __attribute__((aligned(CACHE_LINE_SIZE))); +#define USB_PHY_TUNE_REG 0x8018 +#define USB_PHY_TUNE_TXREF_OFFSET 8 +#define USB_PHY_TUNE_TXREF_MASK (0xF << USB_PHY_TUNE_TXREF_OFFSET) +#define USB_PHY_TUNE_TXPREEMPAMP_OFFSET 0 +#define USB_PHY_TUNE_TXPREEMPAMP_MASK 3 void usb_dc_sync_dma(void) { asm volatile("sw t0, (%0)" : : "r"(dma_sync_buffer)); csi_dcache_clean_range((phy_addr_t)(ptr_t)dma_sync_buffer, CACHE_LINE_SIZE); } +#ifndef AIC_USB_DEVICE_DRV_V10 +int usbd_wait_pll_locked(void) +{ + uint32_t i = 0; + + while((readl(PLL_COM) & PLL_LOCKED) == 0) { + i++; + if (i >= 20000) { + return -1; + } + } + return 0; +} +#endif + +void usbd_dc_set_phytune(void) +{ + uint32_t reg_val; + + /* Register default value is 0x8 */ + if (AIC_USB_PHY_TXREF != 0x8) { + reg_val = readl((volatile void *)(USB_DEV_BASE + USB_PHY_TUNE_REG)); + reg_val &= ~USB_PHY_TUNE_TXREF_MASK; + reg_val |= AIC_USB_PHY_TXREF << USB_PHY_TUNE_TXREF_OFFSET; + writel(reg_val, (volatile void *)(USB_DEV_BASE + USB_PHY_TUNE_REG)); + } + + /* Register default value is 0 */ + if (AIC_USB_TXPREEMPAMP != 0) { + reg_val = readl((volatile void *)(USB_DEV_BASE + USB_PHY_TUNE_REG)); + reg_val &= ~USB_PHY_TUNE_TXPREEMPAMP_MASK; + reg_val |= AIC_USB_TXPREEMPAMP << USB_PHY_TUNE_TXPREEMPAMP_OFFSET; + writel(reg_val, (volatile void *)(USB_DEV_BASE + USB_PHY_TUNE_REG)); + } +} void usb_dc_low_level_init(void) { @@ -27,6 +67,10 @@ void usb_dc_low_level_init(void) #if defined(AIC_USING_USB0_DEVICE) || defined(AIC_USING_USB0_OTG) hal_syscfg_usb_phy0_sw_host(0); #endif + +#ifndef AIC_USB_DEVICE_DRV_V10 + hal_syscfg_usb_pll_en(); +#endif /* set pin-mux */ /* enable clock */ @@ -39,11 +83,19 @@ void usb_dc_low_level_init(void) hal_reset_deassert(CONFIG_USB_AIC_DC_PHY_RESET); hal_reset_deassert(CONFIG_USB_AIC_DC_RESET); aicos_udelay(300); +#ifndef AIC_USB_DEVICE_DRV_V10 + if (usbd_wait_pll_locked() < 0) + USB_LOG_ERR("usb pll unlock :%#lx \n", (long)(readl(PLL_COM))); + +#endif + + /* Set USB PHY tune, default params are usually sufficient */ + usbd_dc_set_phytune(); usbd_clk = hal_clk_get_freq(CONFIG_USB_AIC_DC_CLK); /* register interrupt callback */ - aicos_request_irq(CONFIG_USB_AIC_DC_IRQ_NUM, USBD_IRQHandler, + aicos_request_irq(CONFIG_USB_AIC_DC_IRQ_NUM, usbd_irq_handler, 0, "usb_device", NULL); aicos_irq_enable(CONFIG_USB_AIC_DC_IRQ_NUM); } diff --git a/port/aic/usb_dc_aic_reg.h b/port/aic/usb_dc_aic_reg.h index b35b8d4f..ad814b14 100644 --- a/port/aic/usb_dc_aic_reg.h +++ b/port/aic/usb_dc_aic_reg.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024, Artinchip Technology Co., Ltd + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,6 +9,8 @@ #define __IO volatile /*!< Defines 'read / write' permissions */ +#if defined(AIC_USB_DEVICE_DRV_V10) +#define TXFIFO_NUM 3 typedef struct { __IO uint32_t ahbbasic; /* 0x0000: AHBBASIC */ __IO uint32_t usbdevinit; /* 0x0004: USBDEVINIT */ @@ -54,6 +56,50 @@ typedef struct { __IO uint32_t dtknqr3; /* 0x0368: DTKNQR3 */ __IO uint32_t dtknqr4; /* 0x036C: DTKNQR4 */ }AIC_UDC_RegDef; +#elif defined(AIC_USB_DEVICE_DRV_V30) +#define USB_NUM_BIDIR_ENDPOINTS 5U +typedef struct { + __IO uint32_t ahbbasic; /* 0x0000: AHBBASIC */ + __IO uint32_t usbdevinit; /* 0x0004: USBDEVINIT */ + __IO uint32_t usbphyif; /* 0x0008: USBPHYIF */ + __IO uint32_t usbulpiphy; /* 0x000C: USBULPIPHY */ + __IO uint32_t usbintsts; /* 0x0010: USBINTSTS */ + __IO uint32_t usbintmsk; /* 0x0014: USBINTMSK */ + __IO uint32_t rxfifosiz; /* 0x0018: RXFIFOSIZ */ + __IO uint32_t rxfifosts_pop; /* 0x001C: RXFIFOSTS */ + __IO uint32_t ietxfifosiz; /* 0x0020: IETXFIFO0_SIZ */ + __IO uint32_t thr_ctl; /* 0x0024: THR_CTL */ + uint8_t res0[0x8]; + __IO uint32_t rxfifosts_dbg; /* 0x0030: RXFIFOSTS_DBG */ + uint8_t res1[0xc]; + __IO uint32_t phyclkctl; /* 0x0040: RXFIFOSTS_DBG */ + uint8_t res2[0x1c]; + __IO uint32_t txfifosiz[16 - 1]; /* 0x0060 - 0x0098: TXFIFOSIZ() */ + uint8_t res3[0x164]; + __IO uint32_t usbdevconf; /* 0x0200: USBDEVCONF */ + __IO uint32_t usbdevfunc; /* 0x0204: USBDEVFUNC */ + __IO uint32_t usblinests; /* 0x0208: USBLINESTS */ + __IO uint32_t inepintmsk; /* 0x020C: INEPINTMSK */ + __IO uint32_t outepintmsk; /* 0x0210: OUTEPINTMSK */ + __IO uint32_t usbepint; /* 0x0214: USBEPINT */ + __IO uint32_t usbepintmsk; /* 0x0218: USBEPINTMSK */ + uint8_t res4[0x4]; + __IO uint32_t inepcfg[16]; /* 0x0220 - 0x0260: INEPCFG() */ + __IO uint32_t outepcfg[16]; /* 0x0260 - 0x02A0: OUTEPCFG() */ + __IO uint32_t inepint[16]; /* 0x02A0 - 0x02E0: INEPINT() */ + __IO uint32_t outepint[16]; /* 0x02E0 - 0x0320: OUTEPINT() */ + __IO uint32_t ineptsfsiz[16]; /* 0x0320 - 0x0360: INEPTSFSIZ() */ + __IO uint32_t outeptsfsiz[16]; /* 0x0360 - 0x03A0: OUTEPTSFSIZ() */ + __IO uint32_t inepdmaaddr[16]; /* 0x03A0 - 0x03E0: INEPDMAADDR() */ + __IO uint32_t outepdmaaddr[16]; /* 0x03E0 - 0x0420: OUTEPDMAADDR() */ + __IO uint32_t ineptxsts[16]; /* 0x0420 - 0x0460: INEPTXSTS() */ + __IO uint32_t dtknqr1; /* 0x0360: DTKNQR1 */ + __IO uint32_t dtknqr2; /* 0x0364: DTKNQR2 */ + __IO uint32_t dtknqr3; /* 0x0368: DTKNQR3 */ + __IO uint32_t dtknqr4; /* 0x036C: DTKNQR4 */ +}AIC_UDC_RegDef; +#endif + /*===================================================================== */ /*definitions related to CSR setting */ @@ -114,6 +160,7 @@ typedef struct { /* USBINTSTS/USBINTMSK interrupt register */ #define INT_RESUME (1u << 31) +#define INT_DATA_FET_STOP_INT (0x1 << 21) #define INT_INCOMP_ISO_OUT_INT (0x1 << 21) #define INT_INCOMP_ISO_IN_INT (0x1 << 20) #define INT_OUT_EP (0x1 << 19) @@ -128,6 +175,14 @@ typedef struct { #define INT_RX_FIFO_NOT_EMPTY (0x1 << 4) #define INT_SOF (0x1 << 3) +/* USB THR CTL */ +#define TX_THR_LEN(x) (((x) & 0x7FE) << 2) +#define ISO_THR_EN (0x1 << 1) +#define NON_ISO_THR_EN (0x1 << 0) + +#define PLL_COM (CONFIG_USB_AIC_DC_BASE + 0x8078) +#define PLL_LOCKED BIT(23) + #define FULL_SPEED_CONTROL_PKT_SIZE 8 #define FULL_SPEED_BULK_PKT_SIZE 64 @@ -142,6 +197,7 @@ typedef struct { #define EPS_NUM 5 #define PERIOD_IN_EP_NUM 2 #define TOTAL_FIFO_SIZE 0x3f6 +#if defined(AIC_USB_DEVICE_DRV_V10) #ifdef LPKG_CHERRYUSB_DEVICE_VIDEO_DVP_TEMPLATE /* video_dvp_template must use 2K Bytes period_tx_fifo */ #define AIC_RX_FIFO_SIZE 0x99 @@ -155,7 +211,24 @@ typedef struct { #define AIC_PERIOD_TX_FIFO1_SIZE 0x100 #define AIC_PERIOD_TX_FIFO2_SIZE 0xDD #endif - +#else +#ifdef LPKG_CHERRYUSB_DEVICE_VIDEO_DVP_TEMPLATE +/* using fifo with threshold */ +#define AIC_RX_FIFO_SIZE 0x119 /* rx fifo must be greater than the MPS of out ep */ +#define AIC_DEDICATED_TX_FIFO0_SIZE 0x10 +#define AIC_DEDICATED_TX_FIFO1_SIZE 0x180 /* high-bandwith 2k/3k & AHB bus needs to operate at 240Mhz */ +#define AIC_DEDICATED_TX_FIFO2_SIZE 0x80 +#define AIC_DEDICATED_TX_FIFO3_SIZE 0x80 +#define AIC_DEDICATED_TX_FIFO4_SIZE 0x00 +#else +#define AIC_RX_FIFO_SIZE 0x119 +#define AIC_DEDICATED_TX_FIFO0_SIZE 0x20 +#define AIC_DEDICATED_TX_FIFO1_SIZE 0x80 /* to achieve the highest performance, it is best for FIFO to be twice the MPS */ +#define AIC_DEDICATED_TX_FIFO2_SIZE 0x80 +#define AIC_DEDICATED_TX_FIFO3_SIZE 0x80 +#define AIC_DEDICATED_TX_FIFO4_SIZE 0x80 +#endif +#endif #define DEPCTL_TXFNUM_0 (0x0 << 22) #define DEPCTL_TXFNUM_1 (0x1 << 22) #define DEPCTL_TXFNUM_2 (0x2 << 22) @@ -204,6 +277,14 @@ typedef struct { #define USBDEVFUNC_SFTDISCON (1 << 1) #define USBDEVFUNC_RMTWKUPSIG (1 << 0) +/* USBDEVFUNC TEST MODE */ +#define TEST_MODE_DISABLE (0) +#define TEST_J_MODE (1) +#define TEST_K_MODE (2) +#define TEST_SE0_NAK_MODE (3) +#define TEST_PACKET_MODE (4) +#define TEST_FORCE_MODE (5) + /* USBLINESTS: Enumeration speed */ #define USB_ENUM_SPEED_SHIFT 1 #define USB_ENUM_SPEED_MASK (0x3 << USB_ENUM_SPEED_SHIFT) @@ -237,6 +318,7 @@ typedef struct { #define DEPCTL_ISO_TYPE (0x1 << 18) #define DEPCTL_BULK_TYPE (0x2 << 18) #define DEPCTL_INTR_TYPE (0x3 << 18) +#define DEPCTL_NAK_STATUS (0x1 << 17) #define DEPCTL_USBACTEP (0x1 << 15) #define DEPCTL_NEXT_EP_BIT (11) #define DEPCTL_NEXT_EP_MASK (0xFU << DEPCTL_NEXT_EP_BIT) @@ -307,6 +389,6 @@ typedef struct { /* In EPn Txfifo Status (INEPTXSTS) */ #define INEPTXSTS_IN_EP_TXFIFO_STS (0xFFFFU << 0) - +#define INEPTXSTS_IN_EP_TXQUEUE_STS (0xFFU << 15) #endif diff --git a/port/aic/usb_glue_aic.c b/port/aic/usb_glue_aic.c index 30a6beb2..c4e24946 100644 --- a/port/aic/usb_glue_aic.c +++ b/port/aic/usb_glue_aic.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Artinchip Technology Co., Ltd + * Copyright (c) 2022-2026, ArtInChip Technology Co., Ltd * * SPDX-License-Identifier: Apache-2.0 */ @@ -8,35 +8,24 @@ #include <aic_hal.h> #include <hal_syscfg.h> #include "usbh_core.h" -#include "usb_hc_ehci.h" -#include "usb_hc_ohci.h" +#include "usb_ehci_priv.h" -#if !defined(CONFIG_USB_EHCI_CONFIGFLAG) -#error "aic ehci must define CONFIG_USB_EHCI_CONFIGFLAG" -#endif - -#if !defined(CONFIG_USB_EHCI_WITH_OHCI) -#error "aic must define CONFIG_USB_EHCI_WITH_OHCI for ls/fs device" -#endif +#if defined(LPKG_CHERRYUSB_HOST_OHCI) +#include "../ohci/usb_hc_ohci.h" -#if CONFIG_USB_OHCI_HCOR_OFFSET != 0x400 -#error "aic CONFIG_USB_OHCI_HCOR_OFFSET must be 0x400" -#endif - -extern void USBH_IRQHandler(uint8_t busid); - -static void aic_ehci_isr(int vector, void *arg) +static void aic_ohci_isr(int vector, void *arg) { struct usbh_bus *bus = (struct usbh_bus *)arg; - extern void USBH_IRQHandler(uint8_t busid); - USBH_IRQHandler(bus->hcd.hcd_id); + extern void OHCI_IRQHandler(struct usbh_bus *bus); + OHCI_IRQHandler(bus); } +#endif -static void aic_ohci_isr(int vector, void *arg) +static void aic_ehci_isr(int vector, void *arg) { struct usbh_bus *bus = (struct usbh_bus *)arg; - extern void OHCI_IRQHandler(uint8_t busid); - OHCI_IRQHandler(bus->hcd.hcd_id); + extern void usbh_irq_handler(struct usbh_bus *bus); + usbh_irq_handler(bus); } typedef struct aic_ehci_config { @@ -46,50 +35,77 @@ typedef struct aic_ehci_config { uint32_t phy_clk_id; uint32_t phy_rst_id; uint32_t irq_num; -} aic_ehci_config_t; +}aic_ehci_config_t; aic_ehci_config_t config[] = { -#ifdef AIC_USING_USB0_HOST - { USB_HOST0_BASE, - CLK_USBH0, - RESET_USBH0, - CLK_USB_PHY0, - RESET_USBPHY0, - USB_HOST0_EHCI_IRQn }, +#if defined(AIC_USING_USB0_HOST) || defined(AIC_USING_USB0_OTG) + { + USB_HOST0_BASE, + CLK_USBH0, + RESET_USBH0, + CLK_USB_PHY0, + RESET_USBPHY0, + USB_HOST0_EHCI_IRQn + }, #else - { 0xFFFFFFFF, - 0xFFFFFFFF, - 0xFFFFFFFF, - 0xFFFFFFFF, - 0xFFFFFFFF, - 0xFFFFFFFF }, + { + 0xFFFFFFFF, + 0xFFFFFFFF, + 0xFFFFFFFF, + 0xFFFFFFFF, + 0xFFFFFFFF, + 0xFFFFFFFF + }, #endif #ifdef AIC_USING_USB1_HOST - { USB_HOST1_BASE, - CLK_USBH1, - RESET_USBH1, - CLK_USB_PHY1, - RESET_USBPHY1, - USB_HOST1_EHCI_IRQn } + { + USB_HOST1_BASE, + CLK_USBH1, + RESET_USBH1, + CLK_USB_PHY1, + RESET_USBPHY1, + USB_HOST1_EHCI_IRQn + } #endif }; +#if defined(AIC_SYSCFG_DRV_V16) +#define PLL_COM (USB_HOST0_BASE + 0x878) +#define PLL_LOCKED BIT(23) + +int usbh_wait_pll_locked(void) +{ + uint32_t i = 0; + + while((readl(PLL_COM) & PLL_LOCKED) == 0) { + i++; + if (i >= 20000) { + return -1; + } + } + return 0; +} +#endif void usb_hc_low_level_init(struct usbh_bus *bus) { uint32_t val; int i = 0; - for (i = 0; i < sizeof(config) / sizeof(aic_ehci_config_t); i++) { +#if defined(AIC_SYSCFG_DRV_V16) + hal_syscfg_usb_pll_en(); +#endif + + for (i=0; i<sizeof(config)/sizeof(aic_ehci_config_t); i++) { if (bus->hcd.reg_base == config[i].base_addr) break; } - if (i == sizeof(config) / sizeof(aic_ehci_config_t)) + if (i == sizeof(config)/sizeof(aic_ehci_config_t)) return; /* set usb0 phy switch: Host/Device */ if (i == 0) - syscfg_usb_phy0_sw_host(1); + hal_syscfg_usb_phy0_sw_host(1); /* enable clock */ hal_clk_enable(config[i].phy_clk_id); @@ -102,72 +118,88 @@ void usb_hc_low_level_init(struct usbh_bus *bus) hal_reset_deassert(config[i].rst_id); aicos_udelay(300); +#if defined(AIC_SYSCFG_DRV_V16) + if (usbh_wait_pll_locked() < 0) + USB_LOG_ERR("usb pll unlock :%#lx \n", (long)(readl(PLL_COM))); +#endif + /* set phy type: UTMI/ULPI */ - val = readl((volatile void *)(unsigned long)(config[i].base_addr + 0x800)); + val = readl((volatile void *)(unsigned long)(config[i].base_addr+0x800)); #ifdef FPGA_BOARD_ARTINCHIP /* fpga phy type = ULPI */ - writel((val & ~0x1U), (volatile void *)(unsigned long)(config[i].base_addr + 0x800)); + writel((val & ~0x1U), (volatile void *)(unsigned long)(config[i].base_addr+0x800)); #else /* board phy type = UTMI */ - writel((val | 0x1), (volatile void *)(unsigned long)(config[i].base_addr + 0x800)); + writel((val | 0x1 | 0xe << 8), (volatile void *)(unsigned long)(config[i].base_addr+0x800)); #endif /* Set AHB2STBUS_INSREG01 Set EHCI packet buffer IN/OUT threshold (in DWORDs) Must increase the OUT threshold to avoid underrun. (FIFO size - 4) */ - writel((32 | (127 << 16)), (volatile void *)(unsigned long)(config[i].base_addr + 0x94)); + writel((32 | (127 << 16)), (volatile void *)(unsigned long)(config[i].base_addr+0x94)); /* register interrupt callback */ aicos_request_irq(config[i].irq_num, (irq_handler_t)aic_ehci_isr, - 0, "usb_host_ehci", bus); + 0, "usb_host_ehci", bus); + aicos_irq_enable(config[i].irq_num); +#if defined(LPKG_CHERRYUSB_HOST_OHCI) aicos_request_irq(config[i].irq_num + 1, (irq_handler_t)aic_ohci_isr, 0, "usb_host_ohci", bus); - aicos_irq_enable(config[i].irq_num); aicos_irq_enable(config[i].irq_num + 1); +#endif +} + +void usb_hc_low_level_deinit(struct usbh_bus *bus) +{ + int i = 0; + + for (i=0; i<sizeof(config)/sizeof(aic_ehci_config_t); i++) { + if (bus->hcd.reg_base == config[i].base_addr) + break; + } + + if (i == sizeof(config)/sizeof(aic_ehci_config_t)) + return; + + aicos_irq_disable(config[i].irq_num); +#if defined(LPKG_CHERRYUSB_HOST_OHCI) + aicos_irq_disable(config[i].irq_num + 1); +#endif + hal_reset_assert(config[i].phy_rst_id); + hal_reset_assert(config[i].rst_id); + hal_clk_disable(config[i].phy_clk_id); + hal_clk_disable(config[i].clk_id); } uint8_t usbh_get_port_speed(struct usbh_bus *bus, const uint8_t port) { - return USB_SPEED_HIGH; + /* Defined by individual manufacturers */ + uint32_t regval; + + regval = EHCI_HCOR->portsc[port-1]; + if ((regval & EHCI_PORTSC_LSTATUS_MASK) == EHCI_PORTSC_LSTATUS_KSTATE) + return USB_SPEED_LOW; + + if (regval & EHCI_PORTSC_PE) + return USB_SPEED_HIGH; + else + return USB_SPEED_FULL; } -void usb_ehci_dcache_clean(uintptr_t addr, uint32_t len) +void usb_dcache_clean(uintptr_t addr, uint32_t len) { aicos_dcache_clean_range((size_t *)addr, len); } -void usb_ehci_dcache_invalidate(uintptr_t addr, uint32_t len) +void usb_dcache_invalidate(uintptr_t addr, uint32_t len) { aicos_dcache_invalid_range((size_t *)addr, len); } -void usb_ehci_dcache_clean_invalidate(uintptr_t addr, uint32_t len) +void usb_dcache_clean_invalidate(uintptr_t addr, uint32_t len) { aicos_dcache_clean_invalid_range((size_t *)addr, len); } -int __usbh_init(void) -{ -#if defined(AIC_USING_USB0_HOST) || defined(AIC_USING_USB1_HOST) - int bus_id = 0; -#endif -#ifdef AIC_USING_USB0_HOST - usbh_initialize(bus_id, USB_HOST0_BASE, NULL); - bus_id++; -#endif - -#ifdef AIC_USING_USB1_HOST - usbh_initialize(bus_id, USB_HOST1_BASE, NULL); - bus_id++; -#endif - return 0; -} - -#if defined(KERNEL_RTTHREAD) -#include <rtthread.h> -#include <rtdevice.h> - -INIT_ENV_EXPORT(__usbh_init); -#endif |
