usb_device_ehci.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. /*
  2. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016 - 2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <usb/include/usb_device_config.h>
  31. #include "fsl_device_registers.h"
  32. #include <usb/include/usb.h>
  33. #include "usb_device.h"
  34. #if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
  35. #include "usb_device_dci.h"
  36. #include "usb_device_ehci.h"
  37. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  38. #include "usb_phy.h"
  39. #endif
  40. #include "drv_common.h"
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. #if defined(USB_STACK_USE_DEDICATED_RAM) && (USB_STACK_USE_DEDICATED_RAM > 0U)
  45. #error The SOC does not suppoort dedicated RAM case.
  46. #endif
  47. #define virtual_to_physical(v) ((void *)((size_t)v + PV_OFFSET))
  48. #define physical_to_virtual(p) ((void *)((size_t)p - PV_OFFSET))
  49. /*******************************************************************************
  50. * Prototypes
  51. ******************************************************************************/
  52. static void USB_DeviceEhciSetDefaultState(usb_device_ehci_state_struct_t *ehciState);
  53. static usb_status_t USB_DeviceEhciEndpointInit(usb_device_ehci_state_struct_t *ehciState,
  54. usb_device_endpoint_init_struct_t *epInit);
  55. static usb_status_t USB_DeviceEhciEndpointDeinit(usb_device_ehci_state_struct_t *ehciState, uint8_t ep);
  56. static usb_status_t USB_DeviceEhciEndpointStall(usb_device_ehci_state_struct_t *ehciState, uint8_t ep);
  57. static usb_status_t USB_DeviceEhciEndpointUnstall(usb_device_ehci_state_struct_t *ehciState, uint8_t ep);
  58. static void USB_DeviceEhciFillSetupBuffer(usb_device_ehci_state_struct_t *ehciState, uint8_t ep);
  59. static void USB_DeviceEhciCancelControlPipe(usb_device_ehci_state_struct_t *ehciState,
  60. uint8_t endpoint,
  61. uint8_t direction);
  62. static void USB_DeviceEhciInterruptTokenDone(usb_device_ehci_state_struct_t *ehciState);
  63. static void USB_DeviceEhciInterruptPortChange(usb_device_ehci_state_struct_t *ehciState);
  64. static void USB_DeviceEhciInterruptReset(usb_device_ehci_state_struct_t *ehciState);
  65. static void USB_DeviceEhciInterruptSof(usb_device_ehci_state_struct_t *ehciState);
  66. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  67. static void USB_DeviceEhciInterruptSuspend(usb_device_ehci_state_struct_t *ehciState);
  68. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  69. static usb_status_t USB_DeviceEhciTransfer(usb_device_ehci_state_struct_t *ehciState,
  70. uint8_t endpointAddress,
  71. uint8_t *buffer,
  72. uint32_t length);
  73. extern usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg);
  74. /*******************************************************************************
  75. * Variables
  76. ******************************************************************************/
  77. /* Apply for QH buffer, 2048-byte alignment */
  78. void *qh_buffer_vir = NULL;
  79. void *qh_buffer_phy = NULL;
  80. /* Apply for DTD buffer, 32-byte alignment */
  81. void *dtd_buffer_vir = NULL;
  82. void *dtd_buffer_phy = NULL;
  83. /* Apply for ehci device state structure */
  84. static usb_device_ehci_state_struct_t g_UsbDeviceEhciSate[USB_DEVICE_CONFIG_EHCI];
  85. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  86. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
  87. /* Apply for device dcd state structure */
  88. static usb_device_dcd_state_struct_t s_UsbDeviceDcdHSState[USB_DEVICE_CONFIG_EHCI];
  89. #endif
  90. /*******************************************************************************
  91. * Code
  92. ******************************************************************************/
  93. /*!
  94. * @brief EHCI NC get USB NC bass address.
  95. *
  96. * This function is used to get USB NC bass address.
  97. *
  98. * @param[in] controllerId EHCI controller ID; See the #usb_controller_index_t.
  99. *
  100. * @retval USB NC bass address.
  101. */
  102. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  103. #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
  104. void *USB_EhciNCGetBase(uint8_t controllerId)
  105. {
  106. void *usbNCBase = NULL;
  107. #if ((defined FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
  108. uint32_t instance;
  109. uint32_t newinstance = 0;
  110. uint32_t usbnc_base_temp[] = USBNC_BASE_ADDRS;
  111. uint32_t usbnc_base[] = USBNC_BASE_ADDRS;
  112. if (controllerId < kUSB_ControllerEhci0)
  113. {
  114. return NULL;
  115. }
  116. controllerId = controllerId - kUSB_ControllerEhci0;
  117. for (instance = 0; instance < (sizeof(usbnc_base_temp) / sizeof(usbnc_base_temp[0])); instance++)
  118. {
  119. if (usbnc_base_temp[instance])
  120. {
  121. usbnc_base[newinstance++] = usbnc_base_temp[instance];
  122. }
  123. }
  124. if (controllerId > newinstance)
  125. {
  126. return NULL;
  127. }
  128. usbNCBase = (void *)usbnc_base[controllerId];
  129. #endif
  130. return usbNCBase;
  131. }
  132. #endif
  133. #endif
  134. /*!
  135. * @brief Set device controller state to default state.
  136. *
  137. * The function is used to set device controller state to default state.
  138. * The function will be called when USB_DeviceEhciInit called or the control type kUSB_DeviceControlGetEndpointStatus
  139. * received in USB_DeviceEhciControl.
  140. *
  141. * @param ehciState Pointer of the device EHCI state structure.
  142. *
  143. */
  144. static void USB_DeviceEhciSetDefaultState(usb_device_ehci_state_struct_t *ehciState)
  145. {
  146. usb_device_ehci_dtd_struct_t *p;
  147. /* Initialize the dtd free queue */
  148. ehciState->dtdFree = ehciState->dtd;
  149. p = ehciState->dtdFree;
  150. for (uint32_t i = 1U; i < USB_DEVICE_CONFIG_EHCI_MAX_DTD; i++)
  151. {
  152. p->nextDtdPointer = (uint32_t)&ehciState->dtd[i];
  153. p = (usb_device_ehci_dtd_struct_t *)p->nextDtdPointer;
  154. }
  155. p->nextDtdPointer = 0U;
  156. ehciState->dtdCount = USB_DEVICE_CONFIG_EHCI_MAX_DTD;
  157. /* Not use interrupt threshold. */
  158. ehciState->registerBase->USBCMD &= ~USBHS_USBCMD_ITC_MASK;
  159. ehciState->registerBase->USBCMD |= USBHS_USBCMD_ITC(0U);
  160. /* Disable setup lockout, please refer to "Control Endpoint Operation" section in RM. */
  161. ehciState->registerBase->USBMODE |= USBHS_USBMODE_SLOM_MASK;
  162. /* Set the endian by using CPU's endian */
  163. //#if (ENDIANNESS == USB_BIG_ENDIAN)
  164. #if 0
  165. ehciState->registerBase->USBMODE |= USBHS_USBMODE_ES_MASK;
  166. #else
  167. ehciState->registerBase->USBMODE &= ~USBHS_USBMODE_ES_MASK;
  168. #endif
  169. /* Initialize the QHs of endpoint. */
  170. for (uint32_t i = 0U; i < (USB_DEVICE_CONFIG_ENDPOINTS * 2U); i++)
  171. {
  172. ehciState->qh[i].nextDtdPointer = USB_DEVICE_ECHI_DTD_TERMINATE_MASK;
  173. ehciState->qh[i].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.maxPacketSize =
  174. USB_CONTROL_MAX_PACKET_SIZE;
  175. ehciState->dtdHard[i] = NULL;
  176. ehciState->dtdTail[i] = NULL;
  177. ehciState->qh[i].endpointStatusUnion.endpointStatusBitmap.isOpened = 0U;
  178. }
  179. /* Add QH buffer address to USBHS_EPLISTADDR_REG */
  180. //ehciState->registerBase->EPLISTADDR = (uint32_t)ehciState->qh;
  181. ehciState->registerBase->EPLISTADDR = (uint32_t)qh_buffer_phy;
  182. /* Clear device address */
  183. ehciState->registerBase->DEVICEADDR = 0U;
  184. #if defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE > 0U)
  185. ehciState->registerBase->OTGSC = ehciState->registerBase->OTGSC & 0x0000FFFF;
  186. ehciState->registerBase->OTGSC |= USBHS_OTGSC_BSVIE_MASK;
  187. #endif /* USB_DEVICE_CONFIG_DETACH_ENABLE */
  188. /* Enable reset, sof, token, stall interrupt */
  189. ehciState->registerBase->USBINTR =
  190. (USBHS_USBINTR_UE_MASK | USBHS_USBINTR_UEE_MASK | USBHS_USBINTR_PCE_MASK | USBHS_USBINTR_URE_MASK
  191. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  192. | USBHS_USBINTR_SLE_MASK
  193. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  194. );
  195. /* Clear reset flag */
  196. ehciState->isResetting = 0U;
  197. }
  198. /*!
  199. * @brief Initialize a specified endpoint.
  200. *
  201. * The function is used to initialize a specified endpoint.
  202. *
  203. * @param ehciState Pointer of the device EHCI state structure.
  204. * @param epInit The endpoint initialization structure pointer.
  205. *
  206. * @return A USB error code or kStatus_USB_Success.
  207. */
  208. static usb_status_t USB_DeviceEhciEndpointInit(usb_device_ehci_state_struct_t *ehciState,
  209. usb_device_endpoint_init_struct_t *epInit)
  210. {
  211. uint32_t primeBit = 1U << ((epInit->endpointAddress & USB_ENDPOINT_NUMBER_MASK) +
  212. ((epInit->endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> 0x03U));
  213. uint16_t maxPacketSize = epInit->maxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_SIZE_MASK;
  214. uint8_t endpoint = (epInit->endpointAddress & USB_ENDPOINT_NUMBER_MASK);
  215. uint8_t direction = (epInit->endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
  216. USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  217. uint8_t index = ((uint8_t)((uint32_t)endpoint << 1U)) | direction;
  218. uint8_t transferType = epInit->transferType & USB_DESCRIPTOR_ENDPOINT_ATTRIBUTE_TYPE_MASK;
  219. /* Cancel pending transfer of the endpoint */
  220. USB_DeviceEhciCancel(ehciState, epInit->endpointAddress);
  221. if ((ehciState->registerBase->EPPRIME & primeBit) || (ehciState->registerBase->EPSR & primeBit))
  222. {
  223. return kStatus_USB_Busy;
  224. }
  225. /* Make the endpoint max packet size align with USB Specification 2.0. */
  226. if (USB_ENDPOINT_ISOCHRONOUS == transferType)
  227. {
  228. if (maxPacketSize > USB_DEVICE_MAX_HS_ISO_MAX_PACKET_SIZE)
  229. {
  230. maxPacketSize = USB_DEVICE_MAX_HS_ISO_MAX_PACKET_SIZE;
  231. }
  232. ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.mult =
  233. 1U + ((maxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_MASK) >>
  234. USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_SHFIT);
  235. }
  236. else
  237. {
  238. ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.mult = 0U;
  239. }
  240. /* Save the max packet size of the endpoint */
  241. ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.maxPacketSize =
  242. maxPacketSize;
  243. /* Set ZLT bit. */
  244. ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.zlt = !epInit->zlt;
  245. /* Enable the endpoint. */
  246. if (USB_ENDPOINT_CONTROL == transferType)
  247. {
  248. ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.ios = 1U;
  249. ehciState->registerBase->EPCR0 |=
  250. (direction ?
  251. (USBHS_EPCR_TXE_MASK | USBHS_EPCR_TXR_MASK | ((uint32_t)transferType << USBHS_EPCR_TXT_SHIFT)) :
  252. (USBHS_EPCR_RXE_MASK | USBHS_EPCR_RXR_MASK | ((uint32_t)transferType << USBHS_EPCR_RXT_SHIFT)));
  253. }
  254. else
  255. {
  256. ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.ios = 0U;
  257. ehciState->registerBase->EPCR[endpoint - 1U] |=
  258. (direction ?
  259. (USBHS_EPCR_TXE_MASK | USBHS_EPCR_TXR_MASK | ((uint32_t)transferType << USBHS_EPCR_TXT_SHIFT)) :
  260. (USBHS_EPCR_RXE_MASK | USBHS_EPCR_RXR_MASK | ((uint32_t)transferType << USBHS_EPCR_RXT_SHIFT)));
  261. }
  262. ehciState->qh[index].endpointStatusUnion.endpointStatusBitmap.isOpened = 1U;
  263. return kStatus_USB_Success;
  264. }
  265. /*!
  266. * @brief De-initialize a specified endpoint.
  267. *
  268. * The function is used to de-initialize a specified endpoint.
  269. * Current transfer of the endpoint will be cancelled and the specified endpoint will be disabled.
  270. *
  271. * @param ehciState Pointer of the device EHCI state structure.
  272. * @param ep The endpoint address, Bit7, 0U - USB_OUT, 1U - USB_IN.
  273. *
  274. * @return A USB error code or kStatus_USB_Success.
  275. */
  276. static usb_status_t USB_DeviceEhciEndpointDeinit(usb_device_ehci_state_struct_t *ehciState, uint8_t ep)
  277. {
  278. uint32_t primeBit =
  279. 1U << ((ep & USB_ENDPOINT_NUMBER_MASK) + ((ep & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> 0x03U));
  280. uint8_t endpoint = (ep & USB_ENDPOINT_NUMBER_MASK);
  281. uint8_t direction =
  282. (ep & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  283. uint8_t index = ((uint8_t)((uint32_t)endpoint << 1U)) | direction;
  284. ehciState->qh[index].endpointStatusUnion.endpointStatusBitmap.isOpened = 0U;
  285. /* Cancel the transfer of the endpoint */
  286. USB_DeviceEhciCancel(ehciState, ep);
  287. if ((ehciState->registerBase->EPPRIME & primeBit) || (ehciState->registerBase->EPSR & primeBit))
  288. {
  289. return kStatus_USB_Busy;
  290. }
  291. /* Clear endpoint state */
  292. ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristics = 0U;
  293. /* Disable the endpoint */
  294. if (!endpoint)
  295. {
  296. ehciState->registerBase->EPCR0 &=
  297. ~(direction ? (USBHS_EPCR_TXE_MASK | USBHS_EPCR_TXT_MASK) : (USBHS_EPCR_RXE_MASK | USBHS_EPCR_RXT_MASK));
  298. }
  299. else
  300. {
  301. ehciState->registerBase->EPCR[endpoint - 1U] &=
  302. ~(direction ? (USBHS_EPCR_TXE_MASK | USBHS_EPCR_TXT_MASK) : (USBHS_EPCR_RXE_MASK | USBHS_EPCR_RXT_MASK));
  303. }
  304. return kStatus_USB_Success;
  305. }
  306. /*!
  307. * @brief Stall a specified endpoint.
  308. *
  309. * The function is used to stall a specified endpoint.
  310. * Current transfer of the endpoint will be cancelled and the specified endpoint will be stalled.
  311. *
  312. * @param ehciState Pointer of the device EHCI state structure.
  313. * @param ep The endpoint address, Bit7, 0U - USB_OUT, 1U - USB_IN.
  314. *
  315. * @return A USB error code or kStatus_USB_Success.
  316. */
  317. static usb_status_t USB_DeviceEhciEndpointStall(usb_device_ehci_state_struct_t *ehciState, uint8_t ep)
  318. {
  319. uint8_t endpoint = ep & USB_ENDPOINT_NUMBER_MASK;
  320. uint8_t direction =
  321. (ep & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  322. uint8_t index = ((uint8_t)((uint32_t)endpoint << 1U)) | direction;
  323. /* Cancel the transfer of the endpoint */
  324. USB_DeviceEhciCancel(ehciState, ep);
  325. /* Set endpoint stall flag. */
  326. if (ehciState->qh[index].capabilttiesCharacteristicsUnion.capabilttiesCharacteristicsBitmap.ios)
  327. {
  328. if (!endpoint)
  329. {
  330. ehciState->registerBase->EPCR0 |= (USBHS_EPCR_TXS_MASK | USBHS_EPCR_RXS_MASK);
  331. }
  332. else
  333. {
  334. ehciState->registerBase->EPCR[endpoint - 1U] |= (USBHS_EPCR_TXS_MASK | USBHS_EPCR_RXS_MASK);
  335. }
  336. }
  337. else
  338. {
  339. if (!endpoint)
  340. {
  341. ehciState->registerBase->EPCR0 |= (direction ? USBHS_EPCR_TXS_MASK : USBHS_EPCR_RXS_MASK);
  342. }
  343. else
  344. {
  345. ehciState->registerBase->EPCR[endpoint - 1U] |= (direction ? USBHS_EPCR_TXS_MASK : USBHS_EPCR_RXS_MASK);
  346. }
  347. }
  348. return kStatus_USB_Success;
  349. }
  350. /*!
  351. * @brief Un-stall a specified endpoint.
  352. *
  353. * The function is used to un-stall a specified endpoint.
  354. * Current transfer of the endpoint will be cancelled and the specified endpoint will be un-stalled.
  355. *
  356. * @param ehciState Pointer of the device EHCI state structure.
  357. * @param ep The endpoint address, Bit7, 0U - USB_OUT, 1U - USB_IN.
  358. *
  359. * @return A USB error code or kStatus_USB_Success.
  360. */
  361. static usb_status_t USB_DeviceEhciEndpointUnstall(usb_device_ehci_state_struct_t *ehciState, uint8_t ep)
  362. {
  363. uint8_t endpoint = ep & USB_ENDPOINT_NUMBER_MASK;
  364. uint8_t direction =
  365. (ep & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  366. /* Clear the endpoint stall state */
  367. if (!endpoint)
  368. {
  369. ehciState->registerBase->EPCR0 &= ~(direction ? USBHS_EPCR_TXS_MASK : USBHS_EPCR_RXS_MASK);
  370. }
  371. else
  372. {
  373. ehciState->registerBase->EPCR[endpoint - 1U] &= ~(direction ? USBHS_EPCR_TXS_MASK : USBHS_EPCR_RXS_MASK);
  374. ehciState->registerBase->EPCR[endpoint - 1U] |= (direction ? USBHS_EPCR_TXR_MASK : USBHS_EPCR_RXR_MASK);
  375. }
  376. return kStatus_USB_Success;
  377. }
  378. /*!
  379. * @brief Get setup packet data.
  380. *
  381. * The function is used to get setup packet data and copy to a backup buffer.
  382. *
  383. * @param ehciState Pointer of the device EHCI state structure.
  384. * @param ep The endpoint number.
  385. *
  386. */
  387. static void USB_DeviceEhciFillSetupBuffer(usb_device_ehci_state_struct_t *ehciState, uint8_t ep)
  388. {
  389. uint8_t waitingSafelyAccess = 1U;
  390. uint8_t index = (ep * 2U) | USB_OUT;
  391. /* Write 1U to clear corresponding bit in EPSETUPSR. */
  392. ehciState->registerBase->EPSETUPSR = 1U << ep;
  393. while (waitingSafelyAccess)
  394. {
  395. /* Set the setup tripwire bit. */
  396. ehciState->registerBase->USBCMD |= USBHS_USBCMD_SUTW_MASK;
  397. /* Copy setup packet data to backup buffer */
  398. ehciState->qh[index].setupBufferBack[0] = ehciState->qh[index].setupBuffer[0];
  399. ehciState->qh[index].setupBufferBack[1] = ehciState->qh[index].setupBuffer[1];
  400. /* Read the USBCMD[SUTW] bit. If set, jump out from the while loop; if cleared continue */
  401. if (ehciState->registerBase->USBCMD & USBHS_USBCMD_SUTW_MASK)
  402. {
  403. waitingSafelyAccess = 0U;
  404. }
  405. }
  406. /* Clear the setup tripwire bit */
  407. ehciState->registerBase->USBCMD &= ~USBHS_USBCMD_SUTW_MASK;
  408. /* Poll until the EPSETUPSR bit clearred */
  409. while (ehciState->registerBase->EPSETUPSR & (1U << ep))
  410. {
  411. }
  412. }
  413. /*!
  414. * @brief Cancel the transfer of the control pipe.
  415. *
  416. * The function is used to cancel the transfer of the control pipe.
  417. *
  418. * @param ehciState Pointer of the device EHCI state structure.
  419. * @param endpoint The endpoint number.
  420. * @param direction The direction of the endpoint.
  421. *
  422. */
  423. static void USB_DeviceEhciCancelControlPipe(usb_device_ehci_state_struct_t *ehciState,
  424. uint8_t endpoint,
  425. uint8_t direction)
  426. {
  427. usb_device_ehci_dtd_struct_t *currentDtd;
  428. uint32_t index = ((uint32_t)endpoint << 1U) + (uint32_t)direction;
  429. usb_device_callback_message_struct_t message;
  430. message.buffer = NULL;
  431. message.length = 0U;
  432. /* Get the dtd of the control pipe */
  433. currentDtd =
  434. (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK);
  435. while (currentDtd)
  436. {
  437. /* Pass the transfer buffer address */
  438. if (NULL == message.buffer)
  439. {
  440. uint32_t bufferAddress = currentDtd->bufferPointerPage[0];
  441. message.buffer = (uint8_t *)((bufferAddress & USB_DEVICE_ECHI_DTD_PAGE_MASK) |
  442. (currentDtd->reservedUnion.originalBufferInfo.originalBufferOffest));
  443. }
  444. /* If the dtd is active, set the message length to USB_UNINITIALIZED_VAL_32. Or set the length by using finished
  445. * length. */
  446. if (currentDtd->dtdTokenUnion.dtdTokenBitmap.status & USB_DEVICE_ECHI_DTD_STATUS_ACTIVE)
  447. {
  448. message.length = USB_UNINITIALIZED_VAL_32;
  449. }
  450. else
  451. {
  452. message.length += (currentDtd->reservedUnion.originalBufferInfo.originalBufferLength -
  453. currentDtd->dtdTokenUnion.dtdTokenBitmap.totalBytes);
  454. }
  455. /* Move the dtd head pointer to next. */
  456. /* If the pointer of the head equals to the tail, set the dtd queue to null. */
  457. if (ehciState->dtdHard[index] == ehciState->dtdTail[index])
  458. {
  459. ehciState->dtdHard[index] = NULL;
  460. ehciState->dtdTail[index] = NULL;
  461. ehciState->qh[index].nextDtdPointer = USB_DEVICE_ECHI_DTD_TERMINATE_MASK;
  462. ehciState->qh[index].dtdTokenUnion.dtdToken = 0U;
  463. }
  464. else
  465. {
  466. ehciState->dtdHard[index] = (usb_device_ehci_dtd_struct_t *)ehciState->dtdHard[index]->nextDtdPointer;
  467. }
  468. /* When the ioc is set or the dtd queue is empty, the up layer will be notified. */
  469. if ((currentDtd->dtdTokenUnion.dtdTokenBitmap.ioc) ||
  470. (0 == ((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK)))
  471. {
  472. message.code = endpoint | (uint8_t)((uint32_t)direction << 0x07U);
  473. message.isSetup = 0U;
  474. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  475. message.buffer = NULL;
  476. message.length = 0U;
  477. }
  478. /* Clear the token field of the dtd. */
  479. currentDtd->dtdTokenUnion.dtdToken = 0U;
  480. /* Add the dtd to the free dtd queue. */
  481. currentDtd->nextDtdPointer = (uint32_t)ehciState->dtdFree;
  482. ehciState->dtdFree = currentDtd;
  483. ehciState->dtdCount++;
  484. /* Get the next in-used dtd. */
  485. currentDtd =
  486. (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK);
  487. }
  488. }
  489. /*!
  490. * @brief Handle the endpoint token done interrupt.
  491. *
  492. * The function is used to handle the endpoint token done interrupt.
  493. *
  494. * @param ehciState Pointer of the device EHCI state structure.
  495. *
  496. */
  497. static void USB_DeviceEhciInterruptTokenDone(usb_device_ehci_state_struct_t *ehciState)
  498. {
  499. uint32_t status;
  500. uint32_t primeBit;
  501. usb_device_ehci_dtd_struct_t *currentDtd;
  502. usb_device_callback_message_struct_t message;
  503. uint8_t endpoint;
  504. uint8_t direction;
  505. uint8_t count;
  506. uint8_t index;
  507. /* Get the EPSETUPSR to check the setup packect received in which one endpoint. */
  508. status = ehciState->registerBase->EPSETUPSR;
  509. if (status)
  510. {
  511. for (endpoint = 0U; endpoint < USB_DEVICE_CONFIG_ENDPOINTS; endpoint++)
  512. {
  513. /* Check the endpoint receive the setup packet. */
  514. if (status & (1U << endpoint))
  515. {
  516. /* Get last setup packet */
  517. usb_setup_struct_t *deviceSetup =
  518. (usb_setup_struct_t *)&ehciState->qh[(uint8_t)((uint32_t)endpoint << 1U) + USB_OUT].setupBufferBack;
  519. /* Check the direction of the data phase. */
  520. direction = (deviceSetup->bmRequestType & USB_REQUEST_TYPE_DIR_IN) >> USB_REQUEST_TYPE_DIR_SHIFT;
  521. /* Cancel the data phase transfer */
  522. USB_DeviceEhciCancelControlPipe(ehciState, endpoint, direction);
  523. /* Cancel the status phase transfer */
  524. USB_DeviceEhciCancelControlPipe(ehciState, endpoint, 1U ^ direction);
  525. message.code = (endpoint) | (USB_OUT << 0x07U);
  526. message.buffer = (uint8_t *)deviceSetup;
  527. message.length = USB_SETUP_PACKET_SIZE;
  528. message.isSetup = 1U;
  529. /* Fill the setup packet to the backup buffer */
  530. USB_DeviceEhciFillSetupBuffer(ehciState, endpoint);
  531. /* Notify the up layer the EHCI status changed. */
  532. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  533. }
  534. }
  535. }
  536. /* Read the USBHS_EPCOMPLETE_REG to get the endpoint transfer done status */
  537. status = ehciState->registerBase->EPCOMPLETE;
  538. /* Clear the endpoint transfer done status */
  539. ehciState->registerBase->EPCOMPLETE = status;
  540. if (status)
  541. {
  542. for (count = 0U; count < 32U; count++)
  543. {
  544. /* Check the transfer is done or not in the specified endpoint. */
  545. if (status & ((uint32_t)(1U << count)))
  546. {
  547. if (count > 15U)
  548. {
  549. endpoint = count - 16U;
  550. direction = USB_IN;
  551. }
  552. else
  553. {
  554. endpoint = count;
  555. direction = USB_OUT;
  556. }
  557. if (endpoint >= USB_DEVICE_CONFIG_ENDPOINTS)
  558. {
  559. continue;
  560. }
  561. index = (endpoint << 1U) + direction;
  562. message.buffer = NULL;
  563. message.length = 0U;
  564. /* Get the in-used dtd of the specified endpoint. */
  565. currentDtd = (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] &
  566. USB_DEVICE_ECHI_DTD_POINTER_MASK);
  567. while (currentDtd)
  568. {
  569. uint8_t isTokenDone = 0;
  570. /* Get the in-used dtd of the specified endpoint. */
  571. currentDtd = (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] &
  572. USB_DEVICE_ECHI_DTD_POINTER_MASK);
  573. while (currentDtd)
  574. {
  575. /* Don't handle the active dtd. */
  576. if ((currentDtd->dtdTokenUnion.dtdTokenBitmap.status & USB_DEVICE_ECHI_DTD_STATUS_ACTIVE) ||
  577. (currentDtd->dtdTokenUnion.dtdTokenBitmap.ioc))
  578. {
  579. if ((!(currentDtd->dtdTokenUnion.dtdTokenBitmap.status &
  580. USB_DEVICE_ECHI_DTD_STATUS_ACTIVE)) &&
  581. (currentDtd->dtdTokenUnion.dtdTokenBitmap.ioc))
  582. {
  583. isTokenDone = 1U;
  584. }
  585. break;
  586. }
  587. currentDtd = (usb_device_ehci_dtd_struct_t *)(currentDtd->nextDtdPointer &
  588. USB_DEVICE_ECHI_DTD_POINTER_MASK);
  589. }
  590. if ((0 == isTokenDone) && (currentDtd))
  591. {
  592. break;
  593. }
  594. /* Get the in-used dtd of the specified endpoint. */
  595. currentDtd = (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] &
  596. USB_DEVICE_ECHI_DTD_POINTER_MASK);
  597. while (currentDtd)
  598. {
  599. /* Don't handle the active dtd. */
  600. if (currentDtd->dtdTokenUnion.dtdTokenBitmap.status & USB_DEVICE_ECHI_DTD_STATUS_ACTIVE)
  601. {
  602. break;
  603. }
  604. /* Save the transfer buffer address */
  605. if (NULL == message.buffer)
  606. {
  607. message.buffer =
  608. (uint8_t *)((currentDtd->bufferPointerPage[0] & USB_DEVICE_ECHI_DTD_PAGE_MASK) |
  609. (currentDtd->reservedUnion.originalBufferInfo.originalBufferOffest));
  610. }
  611. /* Save the transferred data length */
  612. message.length += (currentDtd->reservedUnion.originalBufferInfo.originalBufferLength -
  613. currentDtd->dtdTokenUnion.dtdTokenBitmap.totalBytes);
  614. /* Move the dtd queue head pointer to next */
  615. if (ehciState->dtdHard[index] == ehciState->dtdTail[index])
  616. {
  617. ehciState->dtdHard[index] = NULL;
  618. ehciState->dtdTail[index] = NULL;
  619. ehciState->qh[index].nextDtdPointer = USB_DEVICE_ECHI_DTD_TERMINATE_MASK;
  620. ehciState->qh[index].dtdTokenUnion.dtdToken = 0U;
  621. }
  622. else
  623. {
  624. ehciState->dtdHard[index] =
  625. (usb_device_ehci_dtd_struct_t *)ehciState->dtdHard[index]->nextDtdPointer;
  626. }
  627. /* When the ioc is set or the dtd queue is empty, the up layer will be notified. */
  628. if ((currentDtd->dtdTokenUnion.dtdTokenBitmap.ioc) ||
  629. (0 == ((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK)))
  630. {
  631. message.code = endpoint | (uint8_t)((uint32_t)direction << 0x07U);
  632. message.isSetup = 0U;
  633. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  634. message.buffer = NULL;
  635. message.length = 0U;
  636. }
  637. /* Clear the token field of the dtd */
  638. currentDtd->dtdTokenUnion.dtdToken = 0U;
  639. currentDtd->nextDtdPointer = (uint32_t)ehciState->dtdFree;
  640. ehciState->dtdFree = currentDtd;
  641. ehciState->dtdCount++;
  642. /* Get the next in-used dtd */
  643. currentDtd = (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] &
  644. USB_DEVICE_ECHI_DTD_POINTER_MASK);
  645. if ((NULL != currentDtd) &&
  646. (currentDtd->dtdTokenUnion.dtdTokenBitmap.status & USB_DEVICE_ECHI_DTD_STATUS_ACTIVE))
  647. {
  648. primeBit = 1U << (endpoint + 16U * direction);
  649. /* Try to prime the next dtd. */
  650. ehciState->registerBase->EPPRIME = primeBit;
  651. /* Whether the endpoint transmit/receive buffer is ready or not. If not, wait for prime bit
  652. * cleared and prime the next dtd. */
  653. if (!(ehciState->registerBase->EPSR & primeBit))
  654. {
  655. /* Wait for the endpoint prime bit cleared by HW */
  656. while (ehciState->registerBase->EPPRIME & primeBit)
  657. {
  658. }
  659. /* If the endpoint transmit/receive buffer is not ready */
  660. if (!(ehciState->registerBase->EPSR & primeBit))
  661. {
  662. /* Prime next dtd and prime the transfer */
  663. ehciState->qh[index].nextDtdPointer = (uint32_t)currentDtd;
  664. ehciState->qh[index].dtdTokenUnion.dtdToken = 0U;
  665. ehciState->registerBase->EPPRIME = primeBit;
  666. }
  667. }
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. }
  675. /*!
  676. * @brief Handle the port status change interrupt.
  677. *
  678. * The function is used to handle the port status change interrupt.
  679. *
  680. * @param ehciState Pointer of the device EHCI state structure.
  681. *
  682. */
  683. static void USB_DeviceEhciInterruptPortChange(usb_device_ehci_state_struct_t *ehciState)
  684. {
  685. usb_device_callback_message_struct_t message;
  686. message.buffer = (uint8_t *)NULL;
  687. message.length = 0U;
  688. message.isSetup = 0U;
  689. /* Whether the port is doing reset. */
  690. if (!(ehciState->registerBase->PORTSC1 & USBHS_PORTSC1_PR_MASK))
  691. {
  692. /* If not, update the USB speed. */
  693. if (ehciState->registerBase->PORTSC1 & USBHS_PORTSC1_HSP_MASK)
  694. {
  695. ehciState->speed = USB_SPEED_HIGH;
  696. }
  697. else
  698. {
  699. ehciState->speed = USB_SPEED_FULL;
  700. }
  701. /* If the device reset flag is non-zero, notify the up layer the device reset finished. */
  702. if (ehciState->isResetting)
  703. {
  704. message.code = kUSB_DeviceNotifyBusReset;
  705. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  706. ehciState->isResetting = 0U;
  707. }
  708. }
  709. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  710. if ((ehciState->isSuspending) && (!(ehciState->registerBase->PORTSC1 & USBHS_PORTSC1_SUSP_MASK)))
  711. {
  712. /* Set the resume flag */
  713. ehciState->isSuspending = 0U;
  714. message.code = kUSB_DeviceNotifyResume;
  715. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  716. }
  717. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  718. }
  719. /*!
  720. * @brief Handle the reset interrupt.
  721. *
  722. * The function is used to handle the reset interrupt.
  723. *
  724. * @param ehciState Pointer of the device EHCI state structure.
  725. *
  726. */
  727. static void USB_DeviceEhciInterruptReset(usb_device_ehci_state_struct_t *ehciState)
  728. {
  729. uint32_t status = 0U;
  730. /* Clear the setup flag */
  731. status = ehciState->registerBase->EPSETUPSR;
  732. ehciState->registerBase->EPSETUPSR = status;
  733. /* Clear the endpoint complete flag */
  734. status = ehciState->registerBase->EPCOMPLETE;
  735. ehciState->registerBase->EPCOMPLETE = status;
  736. do
  737. {
  738. /* Flush the pending transfers */
  739. ehciState->registerBase->EPFLUSH = USBHS_EPFLUSH_FERB_MASK | USBHS_EPFLUSH_FETB_MASK;
  740. } while (ehciState->registerBase->EPPRIME & (USBHS_EPPRIME_PERB_MASK | USBHS_EPPRIME_PETB_MASK));
  741. /* Whether is the port reset. If yes, set the isResetting flag. Or, notify the up layer. */
  742. if (ehciState->registerBase->PORTSC1 & USBHS_PORTSC1_PR_MASK)
  743. {
  744. ehciState->isResetting = 1U;
  745. }
  746. else
  747. {
  748. usb_device_callback_message_struct_t message;
  749. message.buffer = (uint8_t *)NULL;
  750. message.code = kUSB_DeviceNotifyBusReset;
  751. message.length = 0U;
  752. message.isSetup = 0U;
  753. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  754. }
  755. }
  756. /*!
  757. * @brief Handle the sof interrupt.
  758. *
  759. * The function is used to handle the sof interrupt.
  760. *
  761. * @param ehciState Pointer of the device EHCI state structure.
  762. *
  763. */
  764. static void USB_DeviceEhciInterruptSof(usb_device_ehci_state_struct_t *ehciState)
  765. {
  766. }
  767. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  768. /*!
  769. * @brief Handle the suspend interrupt.
  770. *
  771. * The function is used to handle the suspend interrupt.
  772. *
  773. * @param ehciState Pointer of the device EHCI state structure.
  774. *
  775. */
  776. static void USB_DeviceEhciInterruptSuspend(usb_device_ehci_state_struct_t *ehciState)
  777. {
  778. /* If the port is in suspend state, notify the up layer */
  779. if (ehciState->registerBase->PORTSC1 & USBHS_PORTSC1_SUSP_MASK)
  780. {
  781. #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
  782. #else
  783. if (ehciState->registerPhyBase->USB1_VBUS_DET_STAT & USBPHY_USB1_VBUS_DET_STAT_VBUS_VALID_3V_MASK)
  784. #endif
  785. {
  786. usb_device_callback_message_struct_t message;
  787. message.buffer = (uint8_t *)NULL;
  788. message.length = 0U;
  789. message.isSetup = 0U;
  790. message.code = kUSB_DeviceNotifySuspend;
  791. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  792. }
  793. }
  794. }
  795. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  796. /*!
  797. * @brief Get dtds and link to QH.
  798. *
  799. * The function is used to get dtds and link to QH.
  800. *
  801. * @param ehciState Pointer of the device EHCI state structure.
  802. * @param endpointAddress The endpoint address, Bit7, 0U - USB_OUT, 1U - USB_IN.
  803. * @param buffer The memory address needed to be transferred.
  804. * @param length Data length.
  805. *
  806. * @return A USB error code or kStatus_USB_Success.
  807. */
  808. static usb_status_t USB_DeviceEhciTransfer(usb_device_ehci_state_struct_t *ehciState,
  809. uint8_t endpointAddress,
  810. uint8_t *buffer,
  811. uint32_t length)
  812. {
  813. usb_device_ehci_dtd_struct_t *dtd;
  814. usb_device_ehci_dtd_struct_t *dtdHard;
  815. uint32_t index = ((endpointAddress & USB_ENDPOINT_NUMBER_MASK) << 1U) |
  816. ((endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
  817. USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
  818. uint32_t primeBit = 1U << ((endpointAddress & USB_ENDPOINT_NUMBER_MASK) +
  819. ((endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> 0x03U));
  820. uint8_t epStatus = primeBit;
  821. uint32_t sendLength;
  822. uint32_t currentIndex = 0U;
  823. uint32_t dtdRequestCount = (length + USB_DEVICE_ECHI_DTD_TOTAL_BYTES - 1U) / USB_DEVICE_ECHI_DTD_TOTAL_BYTES;
  824. uint8_t qhIdle = 0U;
  825. uint8_t waitingSafelyAccess = 1U;
  826. USB_OSA_SR_ALLOC();
  827. if (!ehciState)
  828. {
  829. return kStatus_USB_InvalidHandle;
  830. }
  831. if (0U == ehciState->qh[index].endpointStatusUnion.endpointStatusBitmap.isOpened)
  832. {
  833. return kStatus_USB_Error;
  834. }
  835. /* Return error when ehci is doing reset */
  836. if (ehciState->isResetting)
  837. {
  838. return kStatus_USB_Error;
  839. }
  840. if (!dtdRequestCount)
  841. {
  842. dtdRequestCount = 1U;
  843. }
  844. USB_OSA_ENTER_CRITICAL();
  845. /* The free dtd count need to not less than the transfer requests. */
  846. if (dtdRequestCount > (uint32_t)ehciState->dtdCount)
  847. {
  848. USB_OSA_EXIT_CRITICAL();
  849. return kStatus_USB_Busy;
  850. }
  851. do
  852. {
  853. /* The transfer length need to not more than USB_DEVICE_ECHI_DTD_TOTAL_BYTES for each dtd. */
  854. if (length > USB_DEVICE_ECHI_DTD_TOTAL_BYTES)
  855. {
  856. sendLength = USB_DEVICE_ECHI_DTD_TOTAL_BYTES;
  857. }
  858. else
  859. {
  860. sendLength = length;
  861. }
  862. length -= sendLength;
  863. /* Get a free dtd */
  864. dtd = ehciState->dtdFree;
  865. ehciState->dtdFree = (usb_device_ehci_dtd_struct_t *)dtd->nextDtdPointer;
  866. ehciState->dtdCount--;
  867. /* Save the dtd head when current active buffer offset is zero. */
  868. if (!currentIndex)
  869. {
  870. dtdHard = dtd;
  871. }
  872. /* Set the dtd field */
  873. dtd->nextDtdPointer = USB_DEVICE_ECHI_DTD_TERMINATE_MASK;
  874. dtd->dtdTokenUnion.dtdToken = 0U;
  875. dtd->bufferPointerPage[0] = (uint32_t)(virtual_to_physical(buffer) + currentIndex);
  876. dtd->bufferPointerPage[1] =
  877. (dtd->bufferPointerPage[0] + USB_DEVICE_ECHI_DTD_PAGE_BLOCK) & USB_DEVICE_ECHI_DTD_PAGE_MASK;
  878. dtd->bufferPointerPage[2] = dtd->bufferPointerPage[1] + USB_DEVICE_ECHI_DTD_PAGE_BLOCK;
  879. dtd->bufferPointerPage[3] = dtd->bufferPointerPage[2] + USB_DEVICE_ECHI_DTD_PAGE_BLOCK;
  880. dtd->bufferPointerPage[4] = dtd->bufferPointerPage[3] + USB_DEVICE_ECHI_DTD_PAGE_BLOCK;
  881. dtd->dtdTokenUnion.dtdTokenBitmap.totalBytes = sendLength;
  882. /* Save the data length needed to be transferred. */
  883. dtd->reservedUnion.originalBufferInfo.originalBufferLength = sendLength;
  884. /* Save the original buffer address */
  885. dtd->reservedUnion.originalBufferInfo.originalBufferOffest =
  886. dtd->bufferPointerPage[0] & USB_DEVICE_ECHI_DTD_PAGE_OFFSET_MASK;
  887. dtd->reservedUnion.originalBufferInfo.dtdInvalid = 0U;
  888. /* Set the IOC field in last dtd. */
  889. if (!length)
  890. {
  891. dtd->dtdTokenUnion.dtdTokenBitmap.ioc = 1U;
  892. }
  893. /* Set dtd active */
  894. dtd->dtdTokenUnion.dtdTokenBitmap.status = USB_DEVICE_ECHI_DTD_STATUS_ACTIVE;
  895. /* Move the buffer offset index */
  896. currentIndex += sendLength;
  897. /* Add dtd to the in-used dtd queue */
  898. if (ehciState->dtdTail[index])
  899. {
  900. ehciState->dtdTail[index]->nextDtdPointer = (uint32_t)dtd;
  901. ehciState->dtdTail[index] = dtd;
  902. }
  903. else
  904. {
  905. ehciState->dtdHard[index] = dtd;
  906. ehciState->dtdTail[index] = dtd;
  907. qhIdle = 1U;
  908. }
  909. } while (length);
  910. /* If the QH is not empty */
  911. if (!qhIdle)
  912. {
  913. /* If the prime bit is set, nothing need to do. */
  914. if (ehciState->registerBase->EPPRIME & primeBit)
  915. {
  916. USB_OSA_EXIT_CRITICAL();
  917. return kStatus_USB_Success;
  918. }
  919. /* To safely a dtd */
  920. while (waitingSafelyAccess)
  921. {
  922. /* set the ATDTW flag to USBHS_USBCMD_REG. */
  923. ehciState->registerBase->USBCMD |= USBHS_USBCMD_ATDTW_MASK;
  924. /* Read EPSR */
  925. epStatus = ehciState->registerBase->EPSR;
  926. /* Wait the ATDTW bit set */
  927. if (ehciState->registerBase->USBCMD & USBHS_USBCMD_ATDTW_MASK)
  928. {
  929. waitingSafelyAccess = 0U;
  930. }
  931. }
  932. /* Clear the ATDTW bit */
  933. ehciState->registerBase->USBCMD &= ~USBHS_USBCMD_ATDTW_MASK;
  934. }
  935. void *dtdhard_phy = NULL;
  936. /* If QH is empty or the endpoint is not primed, need to link current dtd head to the QH. */
  937. /* When the endpoint is not primed if qhIdle is zero, it means the QH is empty. */
  938. if ((qhIdle) || (!(epStatus & primeBit)))
  939. {
  940. //ehciState->qh[index].nextDtdPointer = (uint32_t)dtdHard;
  941. dtdhard_phy = imx6ull_get_periph_paddr((uint32_t)dtdHard);
  942. ehciState->qh[index].nextDtdPointer = (uint32_t)dtdhard_phy;
  943. ehciState->qh[index].dtdTokenUnion.dtdToken = 0U;
  944. ehciState->registerBase->EPPRIME = primeBit;
  945. while (!(ehciState->registerBase->EPSR & primeBit))
  946. {
  947. if (ehciState->registerBase->EPCOMPLETE & primeBit)
  948. {
  949. break;
  950. }
  951. else
  952. {
  953. ehciState->registerBase->EPPRIME = primeBit;
  954. }
  955. }
  956. }
  957. USB_OSA_EXIT_CRITICAL();
  958. return kStatus_USB_Success;
  959. }
  960. /*!
  961. * @brief Initialize the USB device EHCI instance.
  962. *
  963. * This function initializes the USB device EHCI module specified by the controllerId.
  964. *
  965. * @param controllerId The controller id of the USB IP. Please refer to enumeration type usb_controller_index_t.
  966. * @param handle Pointer of the device handle, used to identify the device object is belonged to.
  967. * @param ehciHandle It is out parameter, is used to return pointer of the device EHCI handle to the caller.
  968. *
  969. * @return A USB error code or kStatus_USB_Success.
  970. */
  971. usb_status_t USB_DeviceEhciInit(uint8_t controllerId,
  972. usb_device_handle handle,
  973. usb_device_controller_handle *ehciHandle)
  974. {
  975. usb_device_ehci_state_struct_t *ehciState;
  976. uint32_t ehci_base[] = USBHS_BASE_ADDRS;
  977. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  978. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
  979. usb_device_dcd_state_struct_t *dcdHSState;
  980. uint32_t dcd_base[] = USBHSDCD_BASE_ADDRS;
  981. usb_device_callback_message_struct_t message;
  982. #endif
  983. if ((controllerId < kUSB_ControllerEhci0) ||
  984. ((uint32_t)(controllerId - kUSB_ControllerEhci0) >= USB_DEVICE_CONFIG_EHCI) ||
  985. ((uint32_t)(controllerId - kUSB_ControllerEhci0) >= (sizeof(ehci_base) / sizeof(uint32_t))))
  986. {
  987. return kStatus_USB_ControllerNotFound;
  988. }
  989. ehciState = &g_UsbDeviceEhciSate[controllerId - kUSB_ControllerEhci0];
  990. int num = 1;
  991. int page_size = 4096;
  992. qh_buffer_vir = (void *)rt_pages_alloc(num);
  993. if(!qh_buffer_vir)
  994. {
  995. rt_kprintf("ERROR: qh buff page alloc failed\n");
  996. return kStatus_USB_Error;
  997. }
  998. qh_buffer_phy = virtual_to_physical(qh_buffer_vir);
  999. qh_buffer_vir = rt_ioremap_nocache(virtual_to_physical(qh_buffer_vir), (page_size<<num));
  1000. dtd_buffer_vir = (void *)rt_pages_alloc(num);
  1001. if(!dtd_buffer_vir)
  1002. {
  1003. rt_kprintf("ERROR: dtd buff page alloc failed\n");
  1004. return kStatus_USB_Error;
  1005. }
  1006. dtd_buffer_phy = virtual_to_physical(dtd_buffer_vir);
  1007. dtd_buffer_vir = rt_ioremap_nocache(virtual_to_physical(dtd_buffer_vir), (page_size<<num));
  1008. //ehciState->dtd = s_UsbDeviceEhciDtd[controllerId - kUSB_ControllerEhci0];
  1009. //ehciState->qh = (usb_device_ehci_qh_struct_t *)&qh_buffer[(controllerId - kUSB_ControllerEhci0) * 2048];
  1010. ehciState->dtd = (usb_device_ehci_dtd_struct_t *)dtd_buffer_vir;
  1011. ehciState->qh = (usb_device_ehci_qh_struct_t *)qh_buffer_vir;
  1012. ehciState->controllerId = controllerId;
  1013. ehciState->registerBase = (USBHS_Type *)ehci_base[controllerId - kUSB_ControllerEhci0];
  1014. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  1015. ehciState->registerPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  1016. #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
  1017. ehciState->registerNcBase = (USBNC_Type *)USB_EhciNCGetBase(controllerId);
  1018. #endif
  1019. #endif
  1020. /* Get the HW's endpoint count */
  1021. ehciState->endpointCount =
  1022. (uint8_t)((ehciState->registerBase->DCCPARAMS & USBHS_DCCPARAMS_DEN_MASK) >> USBHS_DCCPARAMS_DEN_SHIFT);
  1023. if (ehciState->endpointCount < USB_DEVICE_CONFIG_ENDPOINTS)
  1024. {
  1025. return kStatus_USB_Error;
  1026. }
  1027. ehciState->deviceHandle = (usb_device_struct_t *)handle;
  1028. /* Clear the controller mode field and set to device mode. */
  1029. ehciState->registerBase->USBMODE &= ~USBHS_USBMODE_CM_MASK;
  1030. ehciState->registerBase->USBMODE |= USBHS_USBMODE_CM(0x02U);
  1031. /* Set the EHCI to default status. */
  1032. USB_DeviceEhciSetDefaultState(ehciState);
  1033. *ehciHandle = (usb_device_controller_handle)ehciState;
  1034. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  1035. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
  1036. dcdHSState = &s_UsbDeviceDcdHSState[controllerId - kUSB_ControllerEhci0];
  1037. dcdHSState->controllerId = controllerId;
  1038. dcdHSState->dcdRegisterBase = (USBHSDCD_Type *)dcd_base[controllerId - kUSB_ControllerEhci0];
  1039. dcdHSState->deviceHandle = (usb_device_struct_t *)handle;
  1040. message.buffer = (uint8_t *)NULL;
  1041. message.length = 0U;
  1042. message.isSetup = 0U;
  1043. if (ehciState->registerBase->OTGSC & USBHS_OTGSC_BSV_MASK)
  1044. {
  1045. /* Device is connected to a host. */
  1046. message.code = kUSB_DeviceNotifyAttach;
  1047. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  1048. }
  1049. #endif
  1050. return kStatus_USB_Success;
  1051. }
  1052. /*!
  1053. * @brief De-initialize the USB device EHCI instance.
  1054. *
  1055. * This function de-initializes the USB device EHCI module.
  1056. *
  1057. * @param ehciHandle Pointer of the device EHCI handle.
  1058. *
  1059. * @return A USB error code or kStatus_USB_Success.
  1060. */
  1061. usb_status_t USB_DeviceEhciDeinit(usb_device_controller_handle ehciHandle)
  1062. {
  1063. usb_device_ehci_state_struct_t *ehciState = (usb_device_ehci_state_struct_t *)ehciHandle;
  1064. if (!ehciHandle)
  1065. {
  1066. return kStatus_USB_InvalidHandle;
  1067. }
  1068. /* Disable all interrupt. */
  1069. ehciState->registerBase->USBINTR = 0U;
  1070. /* Stop the device functionality. */
  1071. ehciState->registerBase->USBCMD &= ~USBHS_USBCMD_RS_MASK;
  1072. /* Reset the controller. */
  1073. ehciState->registerBase->USBCMD |= USBHS_USBCMD_RST_MASK;
  1074. return kStatus_USB_Success;
  1075. }
  1076. /*!
  1077. * @brief Send data through a specified endpoint.
  1078. *
  1079. * This function sends data through a specified endpoint.
  1080. *
  1081. * @param ehciHandle Pointer of the device EHCI handle.
  1082. * @param endpointAddress Endpoint index.
  1083. * @param buffer The memory address to hold the data need to be sent.
  1084. * @param length The data length need to be sent.
  1085. *
  1086. * @return A USB error code or kStatus_USB_Success.
  1087. *
  1088. * @note The return value just means if the sending request is successful or not; the transfer done is notified by the
  1089. * corresponding callback function.
  1090. * Currently, only one transfer request can be supported for one specific endpoint.
  1091. * If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
  1092. * should implement a queue in the application level.
  1093. * The subsequent transfer could begin only when the previous transfer is done (get notification through the endpoint
  1094. * callback).
  1095. */
  1096. usb_status_t USB_DeviceEhciSend(usb_device_controller_handle ehciHandle,
  1097. uint8_t endpointAddress,
  1098. uint8_t *buffer,
  1099. uint32_t length)
  1100. {
  1101. /* Add dtd to the QH */
  1102. return USB_DeviceEhciTransfer(
  1103. (usb_device_ehci_state_struct_t *)ehciHandle,
  1104. (endpointAddress & USB_ENDPOINT_NUMBER_MASK) | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
  1105. buffer, length);
  1106. }
  1107. /*!
  1108. * @brief Receive data through a specified endpoint.
  1109. *
  1110. * This function Receives data through a specified endpoint.
  1111. *
  1112. * @param ehciHandle Pointer of the device EHCI handle.
  1113. * @param endpointAddress Endpoint index.
  1114. * @param buffer The memory address to save the received data.
  1115. * @param length The data length want to be received.
  1116. *
  1117. * @return A USB error code or kStatus_USB_Success.
  1118. *
  1119. * @note The return value just means if the receiving request is successful or not; the transfer done is notified by the
  1120. * corresponding callback function.
  1121. * Currently, only one transfer request can be supported for one specific endpoint.
  1122. * If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
  1123. * should implement a queue in the application level.
  1124. * The subsequent transfer could begin only when the previous transfer is done (get notification through the endpoint
  1125. * callback).
  1126. */
  1127. usb_status_t USB_DeviceEhciRecv(usb_device_controller_handle ehciHandle,
  1128. uint8_t endpointAddress,
  1129. uint8_t *buffer,
  1130. uint32_t length)
  1131. {
  1132. /* Add dtd to the QH */
  1133. return USB_DeviceEhciTransfer(
  1134. (usb_device_ehci_state_struct_t *)ehciHandle,
  1135. (endpointAddress & USB_ENDPOINT_NUMBER_MASK) | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
  1136. buffer, length);
  1137. }
  1138. /*!
  1139. * @brief Cancel the pending transfer in a specified endpoint.
  1140. *
  1141. * The function is used to cancel the pending transfer in a specified endpoint.
  1142. *
  1143. * @param ehciHandle Pointer of the device EHCI handle.
  1144. * @param ep Endpoint address, bit7 is the direction of endpoint, 1U - IN, 0U - OUT.
  1145. *
  1146. * @return A USB error code or kStatus_USB_Success.
  1147. */
  1148. usb_status_t USB_DeviceEhciCancel(usb_device_controller_handle ehciHandle, uint8_t ep)
  1149. {
  1150. usb_device_ehci_state_struct_t *ehciState = (usb_device_ehci_state_struct_t *)ehciHandle;
  1151. usb_device_callback_message_struct_t message;
  1152. usb_device_ehci_dtd_struct_t *currentDtd;
  1153. uint32_t primeBit =
  1154. 1U << ((ep & USB_ENDPOINT_NUMBER_MASK) + ((ep & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> 0x03U));
  1155. uint8_t index =
  1156. ((ep & USB_ENDPOINT_NUMBER_MASK) << 1U) | ((ep & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >> 0x07U);
  1157. USB_OSA_SR_ALLOC();
  1158. if (!ehciHandle)
  1159. {
  1160. return kStatus_USB_InvalidHandle;
  1161. }
  1162. USB_OSA_ENTER_CRITICAL();
  1163. message.buffer = NULL;
  1164. message.length = USB_UNINITIALIZED_VAL_32;
  1165. /* Get the first dtd */
  1166. currentDtd =
  1167. (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK);
  1168. while (currentDtd)
  1169. {
  1170. currentDtd->reservedUnion.originalBufferInfo.dtdInvalid = 1U;
  1171. currentDtd = (usb_device_ehci_dtd_struct_t *)(currentDtd->nextDtdPointer & USB_DEVICE_ECHI_DTD_POINTER_MASK);
  1172. }
  1173. /* Get the first dtd */
  1174. currentDtd =
  1175. (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK);
  1176. while (currentDtd)
  1177. {
  1178. if (!currentDtd->reservedUnion.originalBufferInfo.dtdInvalid)
  1179. {
  1180. break;
  1181. }
  1182. else
  1183. {
  1184. if (currentDtd->dtdTokenUnion.dtdTokenBitmap.status & USB_DEVICE_ECHI_DTD_STATUS_ACTIVE)
  1185. {
  1186. /* Flush the endpoint to stop a transfer. */
  1187. do
  1188. {
  1189. /* Set the corresponding bit(s) in the EPFLUSH register */
  1190. ehciState->registerBase->EPFLUSH |= primeBit;
  1191. /* Wait until all bits in the EPFLUSH register are cleared. */
  1192. while (ehciState->registerBase->EPFLUSH & primeBit)
  1193. {
  1194. }
  1195. /*
  1196. * Read the EPSR register to ensure that for all endpoints
  1197. * commanded to be flushed, that the corresponding bits
  1198. * are now cleared.
  1199. */
  1200. } while (ehciState->registerBase->EPSR & primeBit);
  1201. }
  1202. /* Save the original buffer address. */
  1203. if (NULL == message.buffer)
  1204. {
  1205. message.buffer = (uint8_t *)((currentDtd->bufferPointerPage[0] & USB_DEVICE_ECHI_DTD_PAGE_MASK) |
  1206. (currentDtd->reservedUnion.originalBufferInfo.originalBufferOffest));
  1207. }
  1208. /* Remove the dtd from the dtd in-used queue. */
  1209. if (ehciState->dtdHard[index] == ehciState->dtdTail[index])
  1210. {
  1211. ehciState->dtdHard[index] = NULL;
  1212. ehciState->dtdTail[index] = NULL;
  1213. }
  1214. else
  1215. {
  1216. ehciState->dtdHard[index] = (usb_device_ehci_dtd_struct_t *)ehciState->dtdHard[index]->nextDtdPointer;
  1217. }
  1218. /* When the ioc is set or the dtd queue is empty, the up layer will be notified. */
  1219. if ((currentDtd->dtdTokenUnion.dtdTokenBitmap.ioc) ||
  1220. (0 == ((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK)))
  1221. {
  1222. message.code = ep;
  1223. message.isSetup = 0U;
  1224. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  1225. message.buffer = NULL;
  1226. }
  1227. /* Clear the token field. */
  1228. currentDtd->dtdTokenUnion.dtdToken = 0U;
  1229. /* Save the dtd to the free queue. */
  1230. currentDtd->nextDtdPointer = (uint32_t)ehciState->dtdFree;
  1231. ehciState->dtdFree = currentDtd;
  1232. ehciState->dtdCount++;
  1233. }
  1234. /* Get the next dtd. */
  1235. currentDtd =
  1236. (usb_device_ehci_dtd_struct_t *)((uint32_t)ehciState->dtdHard[index] & USB_DEVICE_ECHI_DTD_POINTER_MASK);
  1237. }
  1238. if (!currentDtd)
  1239. {
  1240. /* Set the QH to empty. */
  1241. ehciState->qh[index].nextDtdPointer = USB_DEVICE_ECHI_DTD_TERMINATE_MASK;
  1242. ehciState->qh[index].dtdTokenUnion.dtdToken = 0U;
  1243. }
  1244. USB_OSA_EXIT_CRITICAL();
  1245. return kStatus_USB_Success;
  1246. }
  1247. /*!
  1248. * @brief Control the status of the selected item.
  1249. *
  1250. * The function is used to control the status of the selected item.
  1251. *
  1252. * @param ehciHandle Pointer of the device EHCI handle.
  1253. * @param type The selected item. Please refer to enumeration type usb_device_control_type_t.
  1254. * @param param The param type is determined by the selected item.
  1255. *
  1256. * @return A USB error code or kStatus_USB_Success.
  1257. */
  1258. usb_status_t USB_DeviceEhciControl(usb_device_controller_handle ehciHandle, usb_device_control_type_t type, void *param)
  1259. {
  1260. usb_device_ehci_state_struct_t *ehciState = (usb_device_ehci_state_struct_t *)ehciHandle;
  1261. usb_status_t error = kStatus_USB_Error;
  1262. uint16_t *temp16;
  1263. uint8_t *temp8;
  1264. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  1265. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
  1266. usb_device_dcd_state_struct_t *dcdHSState;
  1267. dcdHSState =
  1268. &s_UsbDeviceDcdHSState[ehciState->controllerId - kUSB_ControllerEhci0]; /*The hard code should be replaced*/
  1269. usb_device_dcd_charging_time_t *deviceDcdTimingConfig = (usb_device_dcd_charging_time_t *)param;
  1270. #endif
  1271. #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
  1272. usb_device_struct_t *deviceHandle;
  1273. uint64_t startTick;
  1274. #endif
  1275. if (!ehciHandle)
  1276. {
  1277. return kStatus_USB_InvalidHandle;
  1278. }
  1279. #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
  1280. deviceHandle = (usb_device_struct_t *)ehciState->deviceHandle;
  1281. #endif
  1282. switch (type)
  1283. {
  1284. case kUSB_DeviceControlRun:
  1285. ehciState->registerBase->USBCMD |= USBHS_USBCMD_RS_MASK;
  1286. error = kStatus_USB_Success;
  1287. break;
  1288. case kUSB_DeviceControlStop:
  1289. ehciState->registerBase->USBCMD &= ~USBHS_USBCMD_RS_MASK;
  1290. error = kStatus_USB_Success;
  1291. break;
  1292. case kUSB_DeviceControlEndpointInit:
  1293. if (param)
  1294. {
  1295. error = USB_DeviceEhciEndpointInit(ehciState, (usb_device_endpoint_init_struct_t *)param);
  1296. }
  1297. break;
  1298. case kUSB_DeviceControlEndpointDeinit:
  1299. if (param)
  1300. {
  1301. temp8 = (uint8_t *)param;
  1302. error = USB_DeviceEhciEndpointDeinit(ehciState, *temp8);
  1303. }
  1304. break;
  1305. case kUSB_DeviceControlEndpointStall:
  1306. if (param)
  1307. {
  1308. temp8 = (uint8_t *)param;
  1309. error = USB_DeviceEhciEndpointStall(ehciState, *temp8);
  1310. }
  1311. break;
  1312. case kUSB_DeviceControlEndpointUnstall:
  1313. if (param)
  1314. {
  1315. temp8 = (uint8_t *)param;
  1316. error = USB_DeviceEhciEndpointUnstall(ehciState, *temp8);
  1317. }
  1318. break;
  1319. case kUSB_DeviceControlGetDeviceStatus:
  1320. if (param)
  1321. {
  1322. temp16 = (uint16_t *)param;
  1323. *temp16 = (USB_DEVICE_CONFIG_SELF_POWER << (USB_REQUEST_STANDARD_GET_STATUS_DEVICE_SELF_POWERED_SHIFT))
  1324. #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
  1325. | (deviceHandle->remotewakeup << (USB_REQUEST_STANDARD_GET_STATUS_DEVICE_REMOTE_WARKUP_SHIFT))
  1326. #endif
  1327. ;
  1328. error = kStatus_USB_Success;
  1329. }
  1330. break;
  1331. case kUSB_DeviceControlGetEndpointStatus:
  1332. if (param)
  1333. {
  1334. usb_device_endpoint_status_struct_t *endpointStatus = (usb_device_endpoint_status_struct_t *)param;
  1335. uint8_t ep = (endpointStatus->endpointAddress) & USB_ENDPOINT_NUMBER_MASK;
  1336. uint8_t direction =
  1337. ((endpointStatus->endpointAddress) & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
  1338. USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
  1339. if (ep < USB_DEVICE_CONFIG_ENDPOINTS)
  1340. {
  1341. if (ep)
  1342. {
  1343. endpointStatus->endpointStatus = (ehciState->registerBase->EPCR[ep - 1U] &
  1344. (direction ? USBHS_EPCR_TXS_MASK : USBHS_EPCR_RXS_MASK)) ?
  1345. kUSB_DeviceEndpointStateStalled :
  1346. kUSB_DeviceEndpointStateIdle;
  1347. }
  1348. else
  1349. {
  1350. endpointStatus->endpointStatus =
  1351. (ehciState->registerBase->EPCR0 & (direction ? USBHS_EPCR_TXS_MASK : USBHS_EPCR_RXS_MASK)) ?
  1352. kUSB_DeviceEndpointStateStalled :
  1353. kUSB_DeviceEndpointStateIdle;
  1354. }
  1355. error = kStatus_USB_Success;
  1356. }
  1357. }
  1358. break;
  1359. case kUSB_DeviceControlSetDeviceAddress:
  1360. if (param)
  1361. {
  1362. temp8 = (uint8_t *)param;
  1363. ehciState->registerBase->DEVICEADDR = (((uint32_t)(*temp8)) << USBHS_DEVICEADDR_USBADR_SHIFT);
  1364. error = kStatus_USB_Success;
  1365. }
  1366. break;
  1367. case kUSB_DeviceControlGetSynchFrame:
  1368. break;
  1369. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  1370. #if defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)
  1371. case kUSB_DeviceControlResume:
  1372. #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
  1373. ehciState->registerNcBase->USB_OTGn_CTRL &= ~USBNC_USB_OTGn_CTRL_WIE_MASK;
  1374. #else
  1375. ehciState->registerBase->USBGENCTRL &= ~USBHS_USBGENCTRL_WU_IE_MASK;
  1376. #endif
  1377. ehciState->registerBase->PORTSC1 &= ~USBHS_PORTSC1_PHCD_MASK;
  1378. ehciState->registerBase->PORTSC1 |= USBHS_PORTSC1_FPR_MASK;
  1379. startTick = deviceHandle->hwTick;
  1380. while ((deviceHandle->hwTick - startTick) < 10)
  1381. {
  1382. __ASM("nop");
  1383. }
  1384. ehciState->registerBase->PORTSC1 &= ~USBHS_PORTSC1_FPR_MASK;
  1385. error = kStatus_USB_Success;
  1386. break;
  1387. #endif /* USB_DEVICE_CONFIG_REMOTE_WAKEUP */
  1388. case kUSB_DeviceControlSuspend:
  1389. ehciState->registerBase->OTGSC |= 0x007F0000U;
  1390. ehciState->registerPhyBase->PWD = 0xFFFFFFFF;
  1391. /* ehciState->registerBase->OTGCTL |= ((1U<<10) | (1U<<17) | (1U<<16)); */
  1392. while (ehciState->registerPhyBase->CTRL & (USBPHY_CTRL_UTMI_SUSPENDM_MASK))
  1393. {
  1394. __ASM("nop");
  1395. }
  1396. /* ehciState->registerPhyBase->CTRL |= ((1U << 21) | (1U << 22) | (1U << 23)); */
  1397. ehciState->registerBase->USBSTS |= USBHS_USBSTS_SRI_MASK;
  1398. ehciState->registerBase->PORTSC1 |= USBHS_PORTSC1_PHCD_MASK;
  1399. #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
  1400. ehciState->registerPhyBase->CTRL |= USBPHY_CTRL_ENVBUSCHG_WKUP_MASK | USBPHY_CTRL_ENIDCHG_WKUP_MASK |
  1401. USBPHY_CTRL_ENDPDMCHG_WKUP_MASK | USBPHY_CTRL_ENIRQRESUMEDETECT_MASK;
  1402. ehciState->registerNcBase->USB_OTGn_CTRL |= USBNC_USB_OTGn_CTRL_WKUP_ID_EN_MASK |
  1403. USBNC_USB_OTGn_CTRL_WKUP_VBUS_EN_MASK |
  1404. USBNC_USB_OTGn_CTRL_WKUP_DPDM_EN_MASK;
  1405. ehciState->registerNcBase->USB_OTGn_CTRL |= USBNC_USB_OTGn_CTRL_WIE_MASK;
  1406. #else
  1407. ehciState->registerBase->USBGENCTRL = USBHS_USBGENCTRL_WU_IE_MASK;
  1408. #endif
  1409. ehciState->registerPhyBase->CTRL |= USBPHY_CTRL_CLKGATE_MASK;
  1410. ehciState->isSuspending = 1U;
  1411. error = kStatus_USB_Success;
  1412. break;
  1413. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  1414. case kUSB_DeviceControlSetDefaultStatus:
  1415. for (uint8_t count = 0U; count < USB_DEVICE_CONFIG_ENDPOINTS; count++)
  1416. {
  1417. USB_DeviceEhciEndpointDeinit(ehciState, (count | (USB_IN << 0x07U)));
  1418. USB_DeviceEhciEndpointDeinit(ehciState, (count | (USB_OUT << 0x07U)));
  1419. }
  1420. USB_DeviceEhciSetDefaultState(ehciState);
  1421. error = kStatus_USB_Success;
  1422. break;
  1423. case kUSB_DeviceControlGetSpeed:
  1424. if (param)
  1425. {
  1426. temp8 = (uint8_t *)param;
  1427. *temp8 = ehciState->speed;
  1428. error = kStatus_USB_Success;
  1429. }
  1430. break;
  1431. case kUSB_DeviceControlGetOtgStatus:
  1432. break;
  1433. case kUSB_DeviceControlSetOtgStatus:
  1434. break;
  1435. #if (defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
  1436. case kUSB_DeviceControlSetTestMode:
  1437. if (param)
  1438. {
  1439. temp8 = (uint8_t *)param;
  1440. ehciState->registerBase->PORTSC1 |= ((uint32_t)(*temp8) << 16U);
  1441. error = kStatus_USB_Success;
  1442. }
  1443. break;
  1444. #endif
  1445. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  1446. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
  1447. case kUSB_DeviceControlDcdInitModule:
  1448. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_SR_MASK;
  1449. dcdHSState->dcdRegisterBase->TIMER0 = USBDCD_TIMER0_TSEQ_INIT(deviceDcdTimingConfig->dcdSeqInitTime);
  1450. dcdHSState->dcdRegisterBase->TIMER1 = USBDCD_TIMER1_TDCD_DBNC(deviceDcdTimingConfig->dcdDbncTime);
  1451. dcdHSState->dcdRegisterBase->TIMER1 |= USBDCD_TIMER1_TVDPSRC_ON(deviceDcdTimingConfig->dcdDpSrcOnTime);
  1452. dcdHSState->dcdRegisterBase->TIMER2_BC12 =
  1453. USBDCD_TIMER2_BC12_TWAIT_AFTER_PRD(deviceDcdTimingConfig->dcdTimeWaitAfterPrD);
  1454. dcdHSState->dcdRegisterBase->TIMER2_BC12 |=
  1455. USBDCD_TIMER2_BC12_TVDMSRC_ON(deviceDcdTimingConfig->dcdTimeDMSrcOn);
  1456. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_IE_MASK;
  1457. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_BC12_MASK;
  1458. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_START_MASK;
  1459. break;
  1460. case kUSB_DeviceControlDcdDeinitModule:
  1461. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_SR_MASK;
  1462. break;
  1463. #endif
  1464. default:
  1465. break;
  1466. }
  1467. return error;
  1468. }
  1469. /*!
  1470. * @brief Handle the EHCI device interrupt.
  1471. *
  1472. * The function is used to handle the EHCI device interrupt.
  1473. *
  1474. * @param deviceHandle The device handle got from USB_DeviceInit.
  1475. *
  1476. */
  1477. void USB_DeviceEhciIsrFunction(void *deviceHandle)
  1478. {
  1479. usb_device_struct_t *handle = (usb_device_struct_t *)deviceHandle;
  1480. usb_device_ehci_state_struct_t *ehciState;
  1481. uint32_t status;
  1482. if (NULL == deviceHandle)
  1483. {
  1484. return;
  1485. }
  1486. ehciState = (usb_device_ehci_state_struct_t *)(handle->controllerHandle);
  1487. #if ((defined(USB_DEVICE_CONFIG_LOW_POWER_MODE)) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  1488. #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
  1489. if (ehciState->registerNcBase->USB_OTGn_CTRL & USBNC_USB_OTGn_CTRL_WIE_MASK)
  1490. {
  1491. if (ehciState->registerNcBase->USB_OTGn_CTRL & USBNC_USB_OTGn_CTRL_WIR_MASK)
  1492. {
  1493. ehciState->registerBase->PORTSC1 &= ~USBHS_PORTSC1_PHCD_MASK;
  1494. ehciState->registerNcBase->USB_OTGn_CTRL &= ~USBNC_USB_OTGn_CTRL_WIE_MASK;
  1495. }
  1496. }
  1497. else
  1498. {
  1499. }
  1500. #else
  1501. if (ehciState->registerBase->USBGENCTRL & USBHS_USBGENCTRL_WU_IE_MASK)
  1502. {
  1503. if (ehciState->registerBase->USBGENCTRL & (1U << 8))
  1504. {
  1505. ehciState->registerBase->USBGENCTRL &= ~(1U << 8);
  1506. ehciState->registerBase->USBGENCTRL |= USBHS_USBGENCTRL_WU_INT_CLR_MASK;
  1507. ehciState->registerBase->PORTSC1 &= ~USBHS_PORTSC1_PHCD_MASK;
  1508. ehciState->registerBase->USBGENCTRL &= ~USBHS_USBGENCTRL_WU_IE_MASK;
  1509. }
  1510. }
  1511. else
  1512. {
  1513. }
  1514. #endif
  1515. #endif
  1516. #if defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE > 0U)
  1517. if (ehciState->registerBase->OTGSC & USBHS_OTGSC_BSVIS_MASK)
  1518. {
  1519. usb_device_callback_message_struct_t message;
  1520. ehciState->registerBase->OTGSC |= USBHS_OTGSC_BSVIS_MASK;
  1521. message.buffer = (uint8_t *)NULL;
  1522. message.length = 0U;
  1523. message.isSetup = 0U;
  1524. if (ehciState->registerBase->OTGSC & USBHS_OTGSC_BSV_MASK)
  1525. {
  1526. /* Device is connected to a host. */
  1527. message.code = kUSB_DeviceNotifyAttach;
  1528. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  1529. }
  1530. else
  1531. {
  1532. /* Device is disconnected from a host. */
  1533. message.code = kUSB_DeviceNotifyDetach;
  1534. USB_DeviceNotificationTrigger(ehciState->deviceHandle, &message);
  1535. }
  1536. }
  1537. #endif /* USB_DEVICE_CONFIG_DETACH_ENABLE */
  1538. status = ehciState->registerBase->USBSTS;
  1539. status &= ehciState->registerBase->USBINTR;
  1540. ehciState->registerBase->USBSTS = status;
  1541. #if defined(USB_DEVICE_CONFIG_ERROR_HANDLING) && (USB_DEVICE_CONFIG_ERROR_HANDLING > 0U)
  1542. if (status & USBHS_USBSTS_UEI_MASK)
  1543. {
  1544. /* Error interrupt */
  1545. USB_DeviceEhciInterruptError(ehciState);
  1546. }
  1547. #endif /* USB_DEVICE_CONFIG_ERROR_HANDLING */
  1548. if (status & USBHS_USBSTS_URI_MASK)
  1549. {
  1550. /* Reset interrupt */
  1551. USB_DeviceEhciInterruptReset(ehciState);
  1552. }
  1553. if (status & USBHS_USBSTS_UI_MASK)
  1554. {
  1555. /* Token done interrupt */
  1556. USB_DeviceEhciInterruptTokenDone(ehciState);
  1557. }
  1558. if (status & USBHS_USBSTS_PCI_MASK)
  1559. {
  1560. /* Port status change interrupt */
  1561. USB_DeviceEhciInterruptPortChange(ehciState);
  1562. }
  1563. #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
  1564. if (status & USBHS_USBSTS_SLI_MASK)
  1565. {
  1566. /* Suspend interrupt */
  1567. USB_DeviceEhciInterruptSuspend(ehciState);
  1568. }
  1569. #endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
  1570. if (status & USBHS_USBSTS_SRI_MASK)
  1571. {
  1572. /* Sof interrupt */
  1573. USB_DeviceEhciInterruptSof(ehciState);
  1574. }
  1575. }
  1576. #if (defined(USB_DEVICE_CHARGER_DETECT_ENABLE) && (USB_DEVICE_CHARGER_DETECT_ENABLE > 0U)) && \
  1577. (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
  1578. void USB_DeviceDcdHSIsrFunction(void *deviceHandle)
  1579. {
  1580. usb_device_struct_t *handle = (usb_device_struct_t *)deviceHandle;
  1581. usb_device_ehci_state_struct_t *ehciState;
  1582. usb_device_dcd_state_struct_t *dcdHSState;
  1583. uint32_t status;
  1584. uint32_t chargerType;
  1585. usb_device_callback_message_struct_t message;
  1586. if (NULL == deviceHandle)
  1587. {
  1588. return;
  1589. }
  1590. ehciState = (usb_device_ehci_state_struct_t *)(handle->controllerHandle);
  1591. dcdHSState = &s_UsbDeviceDcdHSState[ehciState->controllerId - kUSB_ControllerEhci0];
  1592. status = dcdHSState->dcdRegisterBase->STATUS;
  1593. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_IACK_MASK;
  1594. message.buffer = (uint8_t *)NULL;
  1595. message.length = 0U;
  1596. message.isSetup = 0U;
  1597. if (status & USBDCD_STATUS_ERR_MASK)
  1598. {
  1599. if (status & USBDCD_STATUS_TO_MASK)
  1600. {
  1601. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_SR_MASK;
  1602. message.code = kUSB_DeviceNotifyDcdTimeOut;
  1603. USB_DeviceNotificationTrigger(dcdHSState->deviceHandle, &message);
  1604. }
  1605. else
  1606. {
  1607. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_SR_MASK;
  1608. message.code = kUSB_DeviceNotifyDcdUnknownPortType;
  1609. USB_DeviceNotificationTrigger(dcdHSState->deviceHandle, &message);
  1610. }
  1611. }
  1612. else
  1613. {
  1614. switch (status & USBDCD_STATUS_SEQ_STAT_MASK)
  1615. {
  1616. case USBDCD_STATUS_SEQ_STAT(kUSB_DcdChargingPortDetectionCompleted):
  1617. chargerType = status & USBDCD_STATUS_SEQ_RES_MASK;
  1618. if (chargerType == USBDCD_STATUS_SEQ_RES(kUSB_DcdDetectionStandardHost))
  1619. {
  1620. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_SR_MASK;
  1621. message.code = kUSB_DeviceNotifySDPDetected;
  1622. USB_DeviceNotificationTrigger(dcdHSState->deviceHandle, &message);
  1623. }
  1624. else if (chargerType == USBDCD_STATUS_SEQ_RES(kUSB_DcdDetectionChargingPort))
  1625. {
  1626. message.code = kUSB_DeviceNotifyChargingPortDetected;
  1627. USB_DeviceNotificationTrigger(dcdHSState->deviceHandle, &message);
  1628. }
  1629. break;
  1630. case USBDCD_STATUS_SEQ_STAT(kUSB_DcdChargerTypeDetectionCompleted):
  1631. chargerType = status & USBDCD_STATUS_SEQ_RES_MASK;
  1632. if (chargerType == USBDCD_STATUS_SEQ_RES(kUSB_DcdDetectionChargingPort))
  1633. {
  1634. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_SR_MASK;
  1635. message.code = kUSB_DeviceNotifyChargingHostDetected;
  1636. USB_DeviceNotificationTrigger(dcdHSState->deviceHandle, &message);
  1637. }
  1638. else if (chargerType == USBDCD_STATUS_SEQ_RES(kUSB_DcdDetectionDedicatedCharger))
  1639. {
  1640. dcdHSState->dcdRegisterBase->CONTROL |= USBDCD_CONTROL_SR_MASK;
  1641. message.code = kUSB_DeviceNotifyDedicatedChargerDetected;
  1642. USB_DeviceNotificationTrigger(dcdHSState->deviceHandle, &message);
  1643. }
  1644. break;
  1645. default:
  1646. break;
  1647. }
  1648. }
  1649. }
  1650. #endif
  1651. #endif /* USB_DEVICE_CONFIG_EHCI */