forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuhc_renesas_ra.c
More file actions
827 lines (681 loc) · 22.6 KB
/
Copy pathuhc_renesas_ra.c
File metadata and controls
827 lines (681 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
* Copyright (c) 2024 Renesas Electronics Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT renesas_ra_uhc
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/dlist.h>
#include "uhc_common.h"
#include "r_usb_host.h"
LOG_MODULE_REGISTER(uhc_renesas_ra, CONFIG_UHC_DRIVER_LOG_LEVEL);
#define UHC_RENESA_RA_MAX_UDEV 5
enum uhc_renesas_ra_event_type {
/* Shim driver event to trigger next transfer */
UHC_RENESAS_RA_EVT_XFER,
/* Device speed check, typically performed after a RESET signal is issued */
UHC_RENESAS_RA_EVT_POLL_DEVICE_SPEED,
};
struct uhc_renesas_ra_evt {
enum uhc_renesas_ra_event_type type;
};
struct uhc_renesas_ra_data {
struct uhc_transfer *last_xfer;
struct k_thread thread_data;
struct k_msgq msgq;
struct st_usbh_instance_ctrl uhc_ctrl;
struct st_usb_cfg uhc_cfg;
sys_dlist_t xfers;
uint8_t devadd[UHC_RENESA_RA_MAX_UDEV];
uint32_t ep[UHC_RENESA_RA_MAX_UDEV][16][2];
};
struct uhc_renesas_ra_config {
const struct pinctrl_dev_config *pcfg;
k_thread_stack_t *drv_stack;
size_t drv_stack_size;
};
extern void r_usbh_isr(void);
static void uhc_renesas_ra_interrupt_handler(void *arg)
{
ARG_UNUSED(arg);
r_usbh_isr();
}
static void uhc_renesas_ra_xfer_request(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct uhc_renesas_ra_evt event = {.type = UHC_RENESAS_RA_EVT_XFER};
int ret;
ret = k_msgq_put(&priv->msgq, &event, K_NO_WAIT);
__ASSERT_NO_MSG(ret == 0);
}
static int uhc_renesas_ra_lock(const struct device *dev)
{
return uhc_lock_internal(dev, K_FOREVER);
}
static int uhc_renesas_ra_unlock(const struct device *dev)
{
return uhc_unlock_internal(dev);
}
static int uhc_renesas_ra_control_status_xfer(const struct device *dev,
struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
uint8_t inv_ep = (xfer->ep ^ USB_EP_DIR_MASK);
int err;
err = R_USBH_XferStart(&priv->uhc_ctrl, xfer->udev->addr, inv_ep, NULL, 0);
if (err != FSP_SUCCESS) {
return -EIO;
}
return 0;
}
static int uhc_renesas_ra_data_send(const struct device *dev, struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct net_buf *buf = xfer->buf;
int err;
err = R_USBH_XferStart(&priv->uhc_ctrl, xfer->udev->addr, xfer->ep, buf->data, buf->len);
if (err != FSP_SUCCESS) {
LOG_ERR("ep 0x%02x state data error", xfer->ep);
return -EIO;
}
return 0;
}
static int uhc_renesas_ra_data_receive(const struct device *dev, struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct net_buf *buf = xfer->buf;
size_t len = net_buf_tailroom(buf);
void *buffer_tail = net_buf_tail(buf);
int err;
err = R_USBH_XferStart(&priv->uhc_ctrl, xfer->udev->addr, xfer->ep, buffer_tail, len);
if (err != FSP_SUCCESS) {
net_buf_remove_mem(buf, len);
LOG_ERR("ep 0x%02x state status error", xfer->ep);
return -EIO;
}
return 0;
}
static int uhc_renesas_ra_data_xfer(const struct device *dev, struct uhc_transfer *const xfer)
{
if (USB_EP_DIR_IS_IN(xfer->ep)) {
return uhc_renesas_ra_data_receive(dev, xfer);
} else {
return uhc_renesas_ra_data_send(dev, xfer);
}
}
static int uhc_renesas_ra_edpt_open(const struct device *dev, struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
usb_desc_endpoint_t ep_desc = {
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = xfer->ep,
.bmAttributes = {.xfer = xfer->type, .sync = 0, .usage = 0},
.wMaxPacketSize = xfer->mps,
.bInterval = xfer->interval,
};
fsp_err_t err;
int ret;
err = R_USBH_EdptOpen(&priv->uhc_ctrl, xfer->udev->addr, &ep_desc);
if (err == FSP_SUCCESS) {
LOG_INF("ep 0x%02x has been opened", xfer->ep);
ret = 0;
} else if (err == FSP_ERR_USB_BUSY) {
LOG_ERR("No available pipe to configure for this EP");
ret = -EBUSY;
} else {
LOG_ERR("Open ep 0x%02x failed", xfer->ep);
ret = -EIO;
}
priv->ep[xfer->udev->addr][USB_EP_GET_IDX(xfer->ep)][!!USB_EP_GET_DIR(xfer->ep)] = 1;
return ret;
}
static int uhc_renesas_ra_control_xfer(const struct device *dev, struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
int ret = 0;
switch (xfer->stage) {
case UHC_CONTROL_STAGE_SETUP:
err = R_USBH_SetupSend(&priv->uhc_ctrl, xfer->udev->addr, xfer->setup_pkt);
if (err != FSP_SUCCESS) {
LOG_ERR("ep 0x%02x state setup error", xfer->ep);
ret = -EIO;
}
break;
case UHC_CONTROL_STAGE_DATA:
ret = uhc_renesas_ra_data_xfer(dev, xfer);
break;
case UHC_CONTROL_STAGE_STATUS:
ret = uhc_renesas_ra_control_status_xfer(dev, xfer);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
struct uhc_transfer *uhc_renesas_ra_xfer_get_next(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct uhc_transfer *xfer;
xfer = uhc_xfer_get_next(dev);
if (xfer == NULL) {
sys_dnode_t *node = sys_dlist_peek_head(&priv->xfers);
if (node != NULL) {
xfer = SYS_DLIST_CONTAINER(node, xfer, node);
}
}
return xfer;
}
static int uhc_renesas_ra_transfer_append(const struct device *dev, struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
int ret;
if (xfer->interval > 0) {
sys_dlist_append(&priv->xfers, &xfer->node);
} else {
ret = uhc_xfer_append(dev, xfer);
if (ret != 0) {
return ret;
}
}
return 0;
}
static int uhc_renesas_ra_schedule_xfer(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
if (priv->last_xfer == NULL) {
/* Allocate buffer to establish a new transaction */
priv->last_xfer = uhc_renesas_ra_xfer_get_next(dev);
if (priv->last_xfer == NULL) {
LOG_DBG("Cannot schedule transfer: no pending transfers");
return 0;
}
}
if (USB_EP_GET_IDX(priv->last_xfer->ep) == 0) {
/*
* If there is a control xfer, the driver should maintaince control xfer stage
* update properly to not conrrupt the control transfer seq
*/
return uhc_renesas_ra_control_xfer(dev, priv->last_xfer);
}
return uhc_renesas_ra_data_xfer(dev, priv->last_xfer);
}
static void uhc_control_stage_update(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct uhc_transfer *xfer = priv->last_xfer;
switch (xfer->stage) {
case UHC_CONTROL_STAGE_SETUP: {
if (xfer->buf != NULL) {
/* S-[in]-status or S-[out]-status */
xfer->stage = UHC_CONTROL_STAGE_DATA;
uhc_renesas_ra_xfer_request(dev);
} else {
/* S-[status] */
xfer->stage = UHC_CONTROL_STAGE_STATUS;
uhc_renesas_ra_control_status_xfer(dev, xfer);
}
break;
}
case UHC_CONTROL_STAGE_DATA:
/* S-in-[status] or S-out-[status] */
xfer->stage = UHC_CONTROL_STAGE_STATUS;
uhc_renesas_ra_control_status_xfer(dev, xfer);
break;
case UHC_CONTROL_STAGE_STATUS:
/* Transfer is completed */
uhc_xfer_return(dev, xfer, 0);
priv->last_xfer = NULL;
break;
default:
break;
}
}
static int uhc_renesas_ra_event_xfer_complete(const struct device *dev, usbh_event_t *hal_evt)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct uhc_transfer *const xfer = priv->last_xfer;
int ret = 0;
if (xfer == NULL) {
LOG_DBG("No transfer in progress");
return -EINVAL;
}
switch (hal_evt->complete.result) {
case USB_XFER_RESULT_STALLED:
uhc_xfer_return(dev, priv->last_xfer, -ENOTSUP);
ret = -EAGAIN;
break;
case USB_XFER_RESULT_TIMEOUT:
case USB_XFER_RESULT_FAILED:
uhc_xfer_return(dev, priv->last_xfer, -EPIPE);
ret = -EAGAIN;
break;
case USB_XFER_RESULT_SUCCESS: {
uint8_t ep = hal_evt->complete.ep_addr;
if (USB_EP_GET_IDX(ep) == 0) {
if (ep == USB_CONTROL_EP_IN &&
priv->last_xfer->stage == UHC_CONTROL_STAGE_DATA) {
net_buf_add(xfer->buf, hal_evt->complete.len);
}
uhc_control_stage_update(dev);
break;
}
if (USB_EP_DIR_IS_IN(ep)) {
if (hal_evt->complete.len > 0) {
net_buf_add(xfer->buf, hal_evt->complete.len);
}
}
uhc_xfer_return(dev, xfer, 0);
priv->last_xfer = NULL;
break;
}
default:
/* USB_XFER_RESULT_INVALID */
uhc_xfer_return(dev, xfer, -EINVAL);
ret = -EINVAL;
}
return ret;
}
static bool uhc_renesas_ra_chk_edpt_open(const struct device *dev, uint8_t addr, uint8_t ep)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
if (USB_EP_GET_IDX(ep) == 0) {
/* Control endpoint is always open after device address is assigned */
return true;
}
return priv->ep[addr][USB_EP_GET_IDX(ep)][!!USB_EP_GET_DIR(ep)] == 1;
}
/*
* Resolve the transaction translator for a device: the nearest high-speed hub on the path
* to the root port. For a Full-/Low-speed device attached behind a high-speed hub this hub
* address and port are programmed into DEVADD so the controller issues split transactions.
* Returns 0 in both outputs when the device is on the root port (no split required).
*/
static void uhc_renesas_ra_get_tt(struct usb_device *udev, uint8_t *hub_addr, uint8_t *hub_port)
{
struct usb_device *dev = udev;
*hub_addr = 0;
*hub_port = 0;
while (dev->hub != NULL) {
if (dev->hub->speed == USB_SPEED_SPEED_HS) {
*hub_addr = dev->hub->addr;
*hub_port = dev->hub_port;
return;
}
dev = dev->hub;
}
}
static int uhc_renesas_ra_ep_enqueue(const struct device *dev, struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
uint8_t mxps0 = xfer->udev->dev_desc.bMaxPacketSize0 > 0
? xfer->udev->dev_desc.bMaxPacketSize0
: 64;
uint8_t hub_addr;
uint8_t hub_port;
usb_speed_t speed;
fsp_err_t err;
int ret;
switch (xfer->udev->speed) {
case USB_SPEED_SPEED_LS:
speed = USB_SPEED_LS;
break;
case USB_SPEED_SPEED_FS:
speed = USB_SPEED_FS;
break;
case USB_SPEED_SPEED_HS:
speed = USB_SPEED_HS;
break;
default:
LOG_DBG("Device speed %d is not supported by controller", xfer->udev->speed);
return -ENOTSUP;
}
uhc_renesas_ra_get_tt(xfer->udev, &hub_addr, &hub_port);
err = R_USBH_PortOpen(&priv->uhc_ctrl, xfer->udev->addr, speed, mxps0, hub_addr, hub_port);
if (err != FSP_SUCCESS) {
return -EIO;
}
ret = uhc_renesas_ra_transfer_append(dev, xfer);
if (ret != 0) {
LOG_DBG("Failed to append transfer: %d", ret);
return ret;
}
if (!uhc_renesas_ra_chk_edpt_open(dev, xfer->udev->addr, xfer->ep)) {
ret = uhc_renesas_ra_edpt_open(dev, xfer);
if (ret != 0) {
uhc_xfer_return(dev, xfer, ret);
return ret;
}
}
uhc_renesas_ra_xfer_request(dev);
return 0;
}
static int uhc_renesas_ra_ep_dequeue(const struct device *dev, struct uhc_transfer *const xfer)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct uhc_transfer *const last_xfer = priv->last_xfer;
fsp_err_t err;
if (last_xfer != xfer) {
sys_dlist_remove(&xfer->node);
uhc_xfer_free(dev, xfer);
}
if (last_xfer == NULL) {
/* The previous transfer has already been fired */
return -EALREADY;
}
err = R_USBH_XferAbort(&priv->uhc_ctrl, last_xfer->udev->addr, last_xfer->ep);
if (err != FSP_SUCCESS) {
return -EIO;
}
return 0;
}
static int uhc_renesas_ra_device_attach(const struct device *dev, usbh_event_t *event)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
struct uhc_renesas_ra_evt evt = {.type = UHC_RENESAS_RA_EVT_POLL_DEVICE_SPEED};
fsp_err_t err;
err = R_USBH_PortReset(&priv->uhc_ctrl);
if (err != FSP_SUCCESS) {
return -EIO;
}
return k_msgq_put(&priv->msgq, &evt, K_NO_WAIT);
}
static int uhc_renesas_ra_poll_device_speed(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
usb_speed_t speed = USB_SPEED_INVALID;
fsp_err_t err;
for (size_t i = 0; i < CONFIG_UHC_RENESAS_RA_OSC_WAIT_RETRIES; i++) {
err = R_USBH_GetDeviceSpeed(&priv->uhc_ctrl, &speed);
if (err != FSP_SUCCESS) {
return -EIO;
}
if (speed != USB_SPEED_INVALID) {
break;
}
k_msleep(5);
}
switch (speed) {
case USB_SPEED_LS:
uhc_submit_event(dev, UHC_EVT_DEV_CONNECTED_LS, 0);
break;
case USB_SPEED_FS:
uhc_submit_event(dev, UHC_EVT_DEV_CONNECTED_FS, 0);
break;
case USB_SPEED_HS:
uhc_submit_event(dev, UHC_EVT_DEV_CONNECTED_HS, 0);
break;
default:
return -EINVAL;
}
/* Root port device: no upstream hub, so no split transaction is required. */
err = R_USBH_PortOpen(&priv->uhc_ctrl, 0, speed, 64, 0, 0);
if (err != FSP_SUCCESS) {
return -EIO;
}
LOG_INF("Device speed detected: speed type %d", speed);
return 0;
}
static int uhc_renesas_ra_port_release(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
err = R_USBH_DeviceRelease(&priv->uhc_ctrl, 0x01);
if (err != FSP_SUCCESS) {
return -EIO;
}
for (size_t i = 0; i < UHC_RENESA_RA_MAX_UDEV; i++) {
priv->devadd[i] = 0;
for (size_t j = 0; j < 16; j++) {
priv->ep[i][j][0] = 0;
priv->ep[i][j][1] = 0;
}
}
priv->last_xfer = NULL;
uhc_submit_event(dev, UHC_EVT_DEV_REMOVED, 0);
return 0;
}
static void uhc_renesas_ra_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p2);
ARG_UNUSED(p3);
const struct device *dev = p1;
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
LOG_DBG("UHC_RENESAS_RA thread started");
while (true) {
struct uhc_renesas_ra_evt event;
int ret;
k_msgq_get(&priv->msgq, &event, K_FOREVER);
switch (event.type) {
case UHC_RENESAS_RA_EVT_XFER:
ret = uhc_renesas_ra_schedule_xfer(dev);
if (unlikely(ret)) {
LOG_WRN("Schedule xfer failed with error %d", ret);
}
break;
case UHC_RENESAS_RA_EVT_POLL_DEVICE_SPEED:
ret = uhc_renesas_ra_poll_device_speed(dev);
if (unlikely(ret)) {
LOG_WRN("Poll device speed failed with error %d", ret);
}
break;
default:
break;
}
}
}
/* Enable SOF generator */
static int uhc_renesas_ra_sof_enable(const struct device *dev)
{
/* Already enabled by a uhc_enable() call */
ARG_UNUSED(dev);
return 0;
}
static int uhc_renesas_ra_bus_suspend(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
err = R_USBH_BusSuspend(&priv->uhc_ctrl);
if (err != FSP_SUCCESS) {
return -EIO;
}
uhc_submit_event(dev, UHC_EVT_SUSPENDED, 0);
return 0;
}
static int uhc_renesas_ra_bus_reset(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
err = R_USBH_PortReset(&priv->uhc_ctrl);
if (err != FSP_SUCCESS) {
return -EIO;
}
uhc_submit_event(dev, UHC_EVT_RESETED, 0);
return 0;
}
static int uhc_renesas_ra_bus_resume(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
err = R_USBH_BusResume(&priv->uhc_ctrl);
if (err != FSP_SUCCESS) {
return -EIO;
}
uhc_submit_event(dev, UHC_EVT_RESUMED, 0);
return 0;
}
static int uhc_renesas_ra_init(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
err = R_USBH_Open(&priv->uhc_ctrl, &priv->uhc_cfg);
if (err != FSP_SUCCESS) {
return -EIO;
}
LOG_INF("Initialized");
for (int i = 0; i < UHC_RENESA_RA_MAX_UDEV; i++) {
for (int j = 0; j < 16; j++) {
priv->ep[i][j][0] = 0;
priv->ep[i][j][1] = 0;
}
}
if (priv->uhc_cfg.hs_irq != FSP_INVALID_VECTOR) {
irq_enable(priv->uhc_cfg.hs_irq);
}
if (priv->uhc_cfg.irq != FSP_INVALID_VECTOR) {
irq_enable(priv->uhc_cfg.irq);
}
if (priv->uhc_cfg.irq_r != FSP_INVALID_VECTOR) {
irq_enable(priv->uhc_cfg.irq_r);
}
return 0;
}
static int uhc_renesas_ra_enable(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
err = R_USBH_Enable(&priv->uhc_ctrl);
if (err != FSP_SUCCESS) {
return -EIO;
}
return 0;
}
static int uhc_renesas_ra_disable(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
fsp_err_t err;
err = R_USBH_Disable(&priv->uhc_ctrl);
if (err != FSP_SUCCESS) {
return -EIO;
}
return 0;
}
static int uhc_renesas_ra_shutdown(const struct device *dev)
{
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
if (priv->last_xfer != NULL) {
uhc_xfer_free(dev, priv->last_xfer);
priv->last_xfer = NULL;
}
if (R_USBH_Close(&priv->uhc_ctrl) != FSP_SUCCESS) {
return -EIO;
}
return 0;
}
static const struct uhc_api uhc_renesas_ra_api = {
.lock = uhc_renesas_ra_lock,
.unlock = uhc_renesas_ra_unlock,
.init = uhc_renesas_ra_init,
.enable = uhc_renesas_ra_enable,
.disable = uhc_renesas_ra_disable,
.shutdown = uhc_renesas_ra_shutdown,
.bus_reset = uhc_renesas_ra_bus_reset,
.sof_enable = uhc_renesas_ra_sof_enable,
.bus_suspend = uhc_renesas_ra_bus_suspend,
.bus_resume = uhc_renesas_ra_bus_resume,
.ep_enqueue = uhc_renesas_ra_ep_enqueue,
.ep_dequeue = uhc_renesas_ra_ep_dequeue,
};
static void uhc_renesas_ra_callback(usbh_callback_arg_t *p_args)
{
const struct device *dev = p_args->p_context;
switch (p_args->event.event_id) {
case USBH_EVENT_XFER_COMPLETE:
uhc_renesas_ra_event_xfer_complete(dev, &p_args->event);
break;
case USBH_EVENT_DEVICE_ATTACH:
uhc_renesas_ra_device_attach(dev, &p_args->event);
break;
case USBH_EVENT_DEVICE_REMOVE:
uhc_renesas_ra_port_release(dev);
break;
default:
break;
}
}
static int uhc_ra_driver_preinit(const struct device *dev)
{
const struct uhc_renesas_ra_config *config = dev->config;
struct uhc_renesas_ra_data *priv = uhc_get_private(dev);
int ret;
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
if (ret < 0) {
LOG_ERR("USB pinctrl setup failed (%d)", ret);
}
if (priv->uhc_cfg.hs_irq != FSP_INVALID_VECTOR) {
R_ICU->IELSR[priv->uhc_cfg.hs_irq] = BSP_PRV_IELS_ENUM(EVENT_USBHS_USB_INT_RESUME);
BSP_ASSIGN_EVENT_TO_CURRENT_CORE(BSP_PRV_IELS_ENUM(EVENT_USBHS_USB_INT_RESUME));
}
if (priv->uhc_cfg.irq != FSP_INVALID_VECTOR) {
R_ICU->IELSR[priv->uhc_cfg.irq] = BSP_PRV_IELS_ENUM(EVENT_USBFS_INT);
BSP_ASSIGN_EVENT_TO_CURRENT_CORE(BSP_PRV_IELS_ENUM(EVENT_USBFS_INT));
}
if (priv->uhc_cfg.irq_r != FSP_INVALID_VECTOR) {
R_ICU->IELSR[priv->uhc_cfg.irq_r] = BSP_PRV_IELS_ENUM(EVENT_USBFS_RESUME);
BSP_ASSIGN_EVENT_TO_CURRENT_CORE(BSP_PRV_IELS_ENUM(EVENT_USBFS_RESUME));
}
sys_dlist_init(&priv->xfers);
k_msgq_alloc_init(&priv->msgq, sizeof(struct uhc_renesas_ra_evt),
CONFIG_UHC_RENESAS_RA_MAX_MSGQ);
k_thread_create(&priv->thread_data, config->drv_stack, config->drv_stack_size,
uhc_renesas_ra_thread, (void *)dev, NULL, NULL,
K_PRIO_COOP(CONFIG_UHC_RENESAS_RA_THREAD_PRIORITY), K_ESSENTIAL, K_NO_WAIT);
k_thread_name_set(&priv->thread_data, dev->name);
return ret;
}
#define IS_USB_HIGH_SPEED(n) \
DT_NODE_HAS_COMPAT(n, renesas_ra_usbhs) \
? DT_ENUM_IDX_OR(n, maximum_speed, 2) == 2 : false
#define USB_MODULE_NUMBER(n) ((DT_REG_ADDR(n)) == R_USB_FS0_BASE ? 0 : 1)
#define RENESAS_RA_USB_IRQ_CONNECT(idx, n) \
IRQ_CONNECT(DT_IRQ_BY_IDX(DT_INST_PARENT(n), idx, irq), \
DT_IRQ_BY_IDX(DT_INST_PARENT(n), idx, priority), \
uhc_renesas_ra_interrupt_handler, DEVICE_DT_INST_GET(n), 0)
#define RENESAS_RA_USB_IRQ_GET(id, name, cell) \
COND_CODE_1(DT_IRQ_HAS_NAME(id, name), (DT_IRQ_BY_NAME(id, name, cell)), \
((IRQn_Type) FSP_INVALID_VECTOR))
/* clang-format off */
#define UHC_RENESAS_RA_DEVICE_DEFINE(n) \
PINCTRL_DT_DEFINE(DT_INST_PARENT(n)); \
K_THREAD_STACK_DEFINE(uhc_renesas_ra_stack_##n, CONFIG_UHC_RENESAS_RA_STACK_SIZE); \
\
static const struct uhc_renesas_ra_config uhc_config_##n = { \
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(DT_INST_PARENT(n)), \
.drv_stack = uhc_renesas_ra_stack_##n, \
.drv_stack_size = K_THREAD_STACK_SIZEOF(uhc_renesas_ra_stack_##n), \
}; \
\
static struct uhc_renesas_ra_data uhc_priv_data_##n = { \
.uhc_cfg = { \
.irq = RENESAS_RA_USB_IRQ_GET(DT_INST_PARENT(n), usbfs_i, irq), \
.irq_r = RENESAS_RA_USB_IRQ_GET(DT_INST_PARENT(n), usbfs_r, irq), \
.hs_irq = RENESAS_RA_USB_IRQ_GET(DT_INST_PARENT(n), usbhs_ir, irq), \
.ipl = RENESAS_RA_USB_IRQ_GET(DT_INST_PARENT(n), usbfs_i, priority), \
.ipl_r = RENESAS_RA_USB_IRQ_GET(DT_INST_PARENT(n), usbfs_r, priority), \
.hsipl = RENESAS_RA_USB_IRQ_GET(DT_INST_PARENT(n), usbhs_ir, priority), \
.module_number = USB_MODULE_NUMBER(DT_INST_PARENT(n)), \
.high_speed = IS_USB_HIGH_SPEED(DT_INST_PARENT(n)), \
.p_callback = uhc_renesas_ra_callback, \
.p_context = DEVICE_DT_INST_GET(n), \
}, \
.devadd = {0}, \
}; \
\
static struct uhc_data uhc_data_##n = { \
.mutex = Z_MUTEX_INITIALIZER(uhc_data_##n.mutex), \
.priv = &uhc_priv_data_##n, \
}; \
\
static int uhc_ra_driver_init_##n(const struct device *dev) \
{ \
LISTIFY(DT_NUM_IRQS(DT_INST_PARENT(n)), RENESAS_RA_USB_IRQ_CONNECT, (;), n); \
return uhc_ra_driver_preinit(dev); \
} \
\
DEVICE_DT_INST_DEFINE(n, uhc_ra_driver_init_##n, NULL, &uhc_data_##n, &uhc_config_##n, \
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&uhc_renesas_ra_api);
/* clang-format on */
DT_INST_FOREACH_STATUS_OKAY(UHC_RENESAS_RA_DEVICE_DEFINE)