drv_usbd.c 11 KB

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