drv_usbd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-12-04 ZYH first implementation
  9. * 2023-10-11 ChuShicheng change rt_size_t to rt_ssize_t
  10. */
  11. #include <usb/include/usb_device_config.h>
  12. #include <usb/include/usb.h>
  13. #include <rtthread.h>
  14. #include <usb/phy/usb_phy.h>
  15. #include <usb/device/usb_device.h>
  16. #include <usb/device/usb_device_dci.h>
  17. #include <rtdevice.h>
  18. #include <imx6ull.h>
  19. #define USB0_IRQNUM 75
  20. /* USB PHY condfiguration */
  21. #define BOARD_USB_PHY_D_CAL (0x0CU)
  22. #define BOARD_USB_PHY_TXCAL45DP (0x06U)
  23. #define BOARD_USB_PHY_TXCAL45DM (0x06U)
  24. #ifdef BSP_USING_USB_DEVICE
  25. static usb_device_handle ehci0_handle;
  26. static struct udcd _fsl_udc_0;
  27. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam);
  28. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam);
  29. static void USB_DeviceIsrEnable(uint8_t controllerId)
  30. {
  31. uint8_t irqNumber;
  32. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  33. uint8_t usbDeviceEhciIrq[] = USBHS_IRQS;
  34. irqNumber = usbDeviceEhciIrq[controllerId - kUSB_ControllerEhci0];
  35. #endif
  36. /* Install isr, set priority, and enable IRQ. */
  37. #if defined(__GIC_PRIO_BITS)
  38. GIC_SetPriority((IRQn_Type)irqNumber, 3);
  39. #else
  40. NVIC_SetPriority((IRQn_Type)irqNumber, 3);
  41. #endif
  42. EnableIRQ((IRQn_Type)irqNumber);
  43. }
  44. /*!
  45. * @brief Initializes USB specific setting that was not set by the Clocks tool.
  46. */
  47. static void USB_DeviceClockInit(uint8_t controllerId)
  48. {
  49. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  50. usb_phy_config_struct_t phyConfig = {
  51. BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
  52. };
  53. #endif
  54. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  55. if (controllerId == kUSB_ControllerEhci0)
  56. {
  57. CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  58. CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
  59. }
  60. else
  61. {
  62. CLOCK_EnableUsbhs1PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  63. CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U);
  64. }
  65. USB_EhciPhyInit(controllerId, 0, &phyConfig);
  66. #endif
  67. }
  68. static struct ep_id _ehci0_ep_pool[] =
  69. {
  70. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  71. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  72. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  73. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  74. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  75. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  76. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  77. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  78. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  79. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  80. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  81. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  82. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  83. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  84. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  85. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  86. };
  87. /*!
  88. * @brief USB Interrupt service routine.
  89. *
  90. * This function serves as the USB interrupt service routine.
  91. *
  92. * @return None.
  93. */
  94. static struct rt_workqueue *usb0_wq = NULL;
  95. static struct rt_work usb0_work;
  96. void ehci0_work(struct rt_work *work, void *work_data)
  97. {
  98. USB_DeviceEhciIsrFunction(ehci0_handle);
  99. rt_hw_interrupt_umask(USB0_IRQNUM);
  100. }
  101. void USB_OTG1_IRQHandler(int irq, void *base)
  102. {
  103. // USB_DeviceEhciIsrFunction(ehci0_handle);
  104. rt_hw_interrupt_mask(USB0_IRQNUM);
  105. rt_workqueue_dowork(usb0_wq, &usb0_work);
  106. }
  107. static rt_err_t _ehci0_ep_set_stall(rt_uint8_t address)
  108. {
  109. USB_DeviceStallEndpoint(ehci0_handle, address);
  110. return RT_EOK;
  111. }
  112. static rt_err_t _ehci0_ep_clear_stall(rt_uint8_t address)
  113. {
  114. USB_DeviceUnstallEndpoint(ehci0_handle, address);
  115. return RT_EOK;
  116. }
  117. static rt_err_t _ehci0_set_address(rt_uint8_t address)
  118. {
  119. USB_DeviceSetStatus(ehci0_handle, kUSB_DeviceStatusAddress, &address);
  120. return RT_EOK;
  121. }
  122. static rt_err_t _ehci0_set_config(rt_uint8_t address)
  123. {
  124. return RT_EOK;
  125. }
  126. static rt_err_t _ehci0_ep_enable(uep_t ep)
  127. {
  128. usb_device_endpoint_init_struct_t ep_init;
  129. usb_device_endpoint_callback_struct_t ep_callback;
  130. rt_uint32_t param = ep->ep_desc->bEndpointAddress;
  131. RT_ASSERT(ep != RT_NULL);
  132. RT_ASSERT(ep->ep_desc != RT_NULL);
  133. ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
  134. ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
  135. ep_init.transferType = ep->ep_desc->bmAttributes;
  136. ep_init.zlt = 0;
  137. ep_callback.callbackFn = usb_device_endpoint_callback;
  138. ep_callback.callbackParam = (void *)param;
  139. ep_callback.isBusy = 0;
  140. USB_DeviceInitEndpoint(ehci0_handle, &ep_init, &ep_callback);
  141. return RT_EOK;
  142. }
  143. static rt_err_t _ehci0_ep_disable(uep_t ep)
  144. {
  145. RT_ASSERT(ep != RT_NULL);
  146. RT_ASSERT(ep->ep_desc != RT_NULL);
  147. USB_DeviceDeinitEndpoint(ehci0_handle, ep->ep_desc->bEndpointAddress);
  148. return RT_EOK;
  149. }
  150. static rt_ssize_t _ehci0_ep_read(rt_uint8_t address, void *buffer)
  151. {
  152. rt_size_t size = 0;
  153. RT_ASSERT(buffer != RT_NULL);
  154. return size;
  155. }
  156. static rt_ssize_t _ehci0_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  157. {
  158. USB_DeviceRecvRequest(ehci0_handle, address, buffer, size);
  159. return size;
  160. }
  161. static rt_ssize_t _ehci0_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  162. {
  163. USB_DeviceSendRequest(ehci0_handle, address, buffer, size);
  164. return size;
  165. }
  166. static rt_err_t _ehci0_ep0_send_status(void)
  167. {
  168. _ehci0_ep_write(0x00, NULL, 0);
  169. return RT_EOK;
  170. }
  171. static rt_err_t _ehci0_suspend(void)
  172. {
  173. return RT_EOK;
  174. }
  175. static rt_err_t _ehci0_wakeup(void)
  176. {
  177. return RT_EOK;
  178. }
  179. const static struct udcd_ops _ehci0_udc_ops =
  180. {
  181. _ehci0_set_address,
  182. _ehci0_set_config,
  183. _ehci0_ep_set_stall,
  184. _ehci0_ep_clear_stall,
  185. _ehci0_ep_enable,
  186. _ehci0_ep_disable,
  187. _ehci0_ep_read_prepare,
  188. _ehci0_ep_read,
  189. _ehci0_ep_write,
  190. _ehci0_ep0_send_status,
  191. _ehci0_suspend,
  192. _ehci0_wakeup,
  193. };
  194. extern void rt_hw_interrupt_umask(int vector);
  195. static rt_err_t drv_ehci0_usbd_init(rt_device_t device)
  196. {
  197. usb_status_t result;
  198. USB_DeviceClockInit(kUSB_ControllerEhci0);
  199. result = USB_DeviceInit(kUSB_ControllerEhci0, usb_device_callback, &ehci0_handle);
  200. RT_ASSERT(ehci0_handle);
  201. if(result == kStatus_USB_Success)
  202. {
  203. usb0_wq = rt_workqueue_create("u0wq", 4096, 3);
  204. rt_work_init(&usb0_work, ehci0_work, NULL);
  205. rt_hw_interrupt_install(USB0_IRQNUM, USB_OTG1_IRQHandler, (void *)ehci0_handle, "usb1_intr");
  206. rt_hw_interrupt_umask(USB0_IRQNUM);
  207. USB_DeviceRun(ehci0_handle);
  208. }
  209. else
  210. {
  211. rt_kprintf("USB_DeviceInit ehci0 error\r\n");
  212. return RT_ERROR;
  213. }
  214. return RT_EOK;
  215. }
  216. struct rt_device_ops imx6ull_usb_ops =
  217. {
  218. drv_ehci0_usbd_init,
  219. RT_NULL,
  220. RT_NULL,
  221. RT_NULL,
  222. RT_NULL,
  223. RT_NULL,
  224. };
  225. static int rt_usbd_init(void)
  226. {
  227. rt_memset((void *)&_fsl_udc_0, 0, sizeof(struct udcd));
  228. _fsl_udc_0.parent.type = RT_Device_Class_USBDevice;
  229. _fsl_udc_0.parent.ops = &imx6ull_usb_ops;
  230. _fsl_udc_0.ops = &_ehci0_udc_ops;
  231. /* Register endpoint infomation */
  232. _fsl_udc_0.ep_pool = _ehci0_ep_pool;
  233. _fsl_udc_0.ep0.id = &_ehci0_ep_pool[0];
  234. _fsl_udc_0.device_is_hs = RT_FALSE;
  235. rt_device_register((rt_device_t)&_fsl_udc_0, "usbd", 0);
  236. rt_usb_device_init();
  237. return 0;
  238. }
  239. // INIT_DEVICE_EXPORT(rt_usbd_init);
  240. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam)
  241. {
  242. rt_uint32_t ep_addr = (rt_uint32_t)callbackParam;
  243. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  244. udcd_t udcd = RT_NULL;
  245. uint8_t state;
  246. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  247. udcd = &_fsl_udc_0;
  248. if(message->isSetup)
  249. {
  250. rt_usbd_ep0_setup_handler(udcd, (struct urequest*)message->buffer);
  251. }
  252. else if(ep_addr == 0x00)
  253. {
  254. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  255. if(state == kUSB_DeviceStateAddressing)
  256. {
  257. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  258. {
  259. state = kUSB_DeviceStateAddress;
  260. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  261. }
  262. }
  263. rt_usbd_ep0_out_handler(udcd, message->length);
  264. }
  265. else if(ep_addr == 0x80)
  266. {
  267. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  268. if(state == kUSB_DeviceStateAddressing)
  269. {
  270. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  271. {
  272. state = kUSB_DeviceStateAddress;
  273. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  274. }
  275. }
  276. rt_usbd_ep0_in_handler(udcd);
  277. }
  278. else if(ep_addr & 0x80)
  279. {
  280. rt_usbd_ep_in_handler(udcd, ep_addr, message->length);
  281. }
  282. else
  283. {
  284. rt_usbd_ep_out_handler(udcd, ep_addr, message->length);
  285. }
  286. return kStatus_USB_Success;
  287. }
  288. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam)
  289. {
  290. usb_status_t error = kStatus_USB_Error;
  291. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  292. usb_device_endpoint_init_struct_t ep0_init =
  293. {
  294. 0x40,
  295. 0x00,
  296. USB_EP_ATTR_CONTROL,
  297. 0
  298. };
  299. usb_device_endpoint_callback_struct_t ep0_callback =
  300. {
  301. usb_device_endpoint_callback,
  302. 0,
  303. 0
  304. };
  305. udcd_t udcd = RT_NULL;
  306. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  307. udcd = &_fsl_udc_0;
  308. switch (callbackEvent)
  309. {
  310. case kUSB_DeviceEventBusReset:
  311. ep0_init.endpointAddress = 0x00;
  312. ep0_callback.callbackParam = (void *)0x00;
  313. USB_DeviceInitEndpoint(deviceHandle, &ep0_init, &ep0_callback);
  314. ep0_init.endpointAddress = 0x80;
  315. ep0_callback.callbackParam = (void *)0x80;
  316. USB_DeviceInitEndpoint(deviceHandle, &ep0_init, &ep0_callback);
  317. rt_usbd_reset_handler(udcd);
  318. break;
  319. case kUSB_DeviceEventAttach:
  320. rt_usbd_connect_handler(udcd);
  321. break;
  322. case kUSB_DeviceEventDetach:
  323. rt_usbd_disconnect_handler(udcd);
  324. break;
  325. }
  326. return error;
  327. }
  328. #endif
  329. /********************* end of file ************************/