usb_device_ehci.c 70 KB

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