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