forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan_renesas_ra.c
1158 lines (970 loc) · 40.3 KB
/
can_renesas_ra.c
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2024 Renesas Electronics Corporation
* SPDX-License-Identifier: Apache-2.0
*/
#include <soc.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/clock_control/renesas_ra_cgc.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/can.h>
#include <zephyr/drivers/can/transceiver.h>
#include "r_can_api.h"
#include "r_canfd.h"
LOG_MODULE_REGISTER(can_renesas_ra, CONFIG_CAN_LOG_LEVEL);
#define DT_DRV_COMPAT renesas_ra_canfd
#define CAN_RENESAS_RA_TIMING_MAX \
{ \
.sjw = 128, \
.prop_seg = 1, \
.phase_seg1 = 255, \
.phase_seg2 = 128, \
.prescaler = 1024, \
}
#define CAN_RENESAS_RA_TIMING_MIN \
{ \
.sjw = 1, \
.prop_seg = 1, \
.phase_seg1 = 2, \
.phase_seg2 = 2, \
.prescaler = 1, \
}
#ifdef CONFIG_CAN_FD_MODE
#define CAN_RENESAS_RA_TIMING_DATA_MAX \
{ \
.sjw = 16, \
.prop_seg = 1, \
.phase_seg1 = 31, \
.phase_seg2 = 16, \
.prescaler = 128, \
}
#define CAN_RENESAS_RA_TIMING_DATA_MIN \
{ \
.sjw = 1, \
.prop_seg = 1, \
.phase_seg1 = 2, \
.phase_seg2 = 2, \
.prescaler = 1, \
}
#endif
/* This frame ID will be reserved. Any filter using this ID may cause undefined behavior. */
#define CAN_RENESAS_RA_RESERVED_ID (CAN_EXT_ID_MASK)
/*
* Common FIFO configuration: refer to '34.2.28 CFDCFCC : Common FIFO Configuration/Control
* Register' - RA8M1 MCU group HWM
*/
#define CANFD_CFG_COMMONFIFO0 (0U << R_CANFD_CFDCFCC_CFE_Pos) /* Common FIFO Disable */
#define CANFD_CFG_COMMONFIFO {CANFD_CFG_COMMONFIFO0}
/*
* RX FIFO configuration: refer to '34.2.25 CFDRFCCa : RX FIFO Configuration/Control Registers' -
* RA8M1 MCU group HWM
*/
#define CANFD_CFG_RX_FIFO0 \
((1U << R_CANFD_CFDRFCC_RFE_Pos) | /* RX FIFO Enable */ \
(1U << R_CANFD_CFDRFCC_RFIE_Pos) | /* RX FIFO Interrupt Enable */ \
(7U << R_CANFD_CFDRFCC_RFPLS_Pos) | /* RX FIFO Payload Data Size: 64 */ \
(3U << R_CANFD_CFDRFCC_RFDC_Pos) | /* RX FIFO Depth: 16 messages */ \
(1U << R_CANFD_CFDRFCC_RFIM_Pos)) /* Interrupt generated at every received message \
* storage \
*/
#define CANFD_CFG_RX_FIFO1 (0U << R_CANFD_CFDRFCC_RFE_Pos) /* RX FIFO Disable */
#define CANFD_CFG_RXFIFO {CANFD_CFG_RX_FIFO0, CANFD_CFG_RX_FIFO1}
/*
* Global Configuration: refer to '34.2.11 CFDGCFG : Global Configuration Register' - RA8M1 MCU
* group HWM
*/
#define CANFD_CFG_GLOBAL \
((0U << R_CANFD_CFDGCFG_TPRI_Pos) | /* Transmission Priority: ID priority */ \
(0U << R_CANFD_CFDGCFG_DCE_Pos) | /* DLC check disabled */ \
(1U << R_CANFD_CFDGCFG_DCS_Pos)) /* DLL Clock Select: CANFDCLK */
/*
* TX Message Buffer Interrupt Enable Configuration: refer to '34.2.43 CFDTMIEC : TX Message Buffer
* Interrupt Enable Configuration Register' - RA8M1 MCU group HWM
*/
#define CANFD_CFG_TXMB_TXI_ENABLE (BIT(0)) /* Enable TXMB0 interrupt */
/*
* Number and size of RX Message Buffers: refer to '34.2.23 CFDRMNB : RX Message Buffer Number
* Register' - RA8M1 MCU group HWM
*/
#define CANFD_CFG_RXMB (0U << R_CANFD_CFDRMNB_NRXMB_Pos) /* Number of RX Message Buffers: 0 */
/*
* Channel Error IRQ configurations: refer to '34.2.3 CFDC0CTR : Control Register' - RA8M1 MCU group
* HWM
*/
#define CANFD_CFG_ERR_IRQ \
(BIT(R_CANFD_CFDC_CTR_EWIE_Pos) | /* Error Warning Interrupt Enable */ \
BIT(R_CANFD_CFDC_CTR_EPIE_Pos) | /* Error Passive Interrupt Enable */ \
BIT(R_CANFD_CFDC_CTR_BOEIE_Pos) | /* Bus-Off Entry Interrupt Enable */ \
BIT(R_CANFD_CFDC_CTR_BORIE_Pos) | /* Bus-Off Recovery Interrupt Enable */ \
BIT(R_CANFD_CFDC_CTR_OLIE_Pos)) /* Overload Interrupt Enable */
/*
* Global Error IRQ configurations: refer to '34.2.12 CFDGCTR : Global Control Register' - RA8M1 MCU
* group HWM
*/
#define CANFD_CFG_GLERR_IRQ \
((3UL << R_CANFD_CFDGCTR_GMDC_Pos) | /* Global Mode Control: Keep current value */ \
(0UL << R_CANFD_CFDGCTR_DEIE_Pos) | /* DLC check interrupt disabled */ \
(0UL << R_CANFD_CFDGCTR_MEIE_Pos) | /* Message lost error interrupt disabled */ \
(0UL << R_CANFD_CFDGCTR_THLEIE_Pos) | /* TX history list entry lost interrupt disabled */ \
(0UL << R_CANFD_CFDGCTR_CMPOFIE_Pos)) /* CANFD message payload overflow flag interrupt \
* disabled \
*/
/* Keycode to enable/disable accessing to AFL entry */
#define CFDGAFLIGNCTR_KEY_CODE (0xC4UL)
/* Default Dataphase bitrate configuration in case classic mode is enabled */
static const can_bit_timing_cfg_t classic_can_data_timing_default = {
.baud_rate_prescaler = 1,
.time_segment_1 = 3,
.time_segment_2 = 2,
.synchronization_jump_width = 1,
};
struct can_renesas_ra_global_cfg {
const struct device *op_clk;
const struct device *ram_clk;
const struct clock_control_ra_subsys_cfg op_subsys;
const struct clock_control_ra_subsys_cfg ram_subsys;
};
struct can_renesas_ra_filter {
bool set;
struct can_filter filter;
can_rx_callback_t rx_cb;
void *rx_usr_data;
};
struct can_renesas_ra_cfg {
struct can_driver_config common;
const struct pinctrl_dev_config *pcfg;
const struct device *global_dev;
const struct device *dll_clk;
const struct clock_control_ra_subsys_cfg dll_subsys;
const uint32_t rx_filter_num;
};
struct can_renesas_ra_data {
struct can_driver_data common;
struct k_mutex inst_mutex;
const struct device *dev;
can_instance_t fsp_can;
can_tx_callback_t tx_cb;
struct k_sem tx_sem;
void *tx_usr_data;
struct can_renesas_ra_filter *rx_filter;
can_bit_timing_cfg_t data_timing;
};
extern void canfd_error_isr(void);
extern void canfd_rx_fifo_isr(void);
extern void canfd_common_fifo_rx_isr(void);
extern void canfd_channel_tx_isr(void);
static const can_api_t *can_api = &g_canfd_on_canfd;
static inline int set_hw_timing_configuration(const struct device *dev,
can_bit_timing_cfg_t *f_timing,
const struct can_timing *z_timing)
{
struct can_renesas_ra_data *data = dev->data;
if (f_timing == NULL || z_timing == NULL) {
return -EINVAL;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
*f_timing = (can_bit_timing_cfg_t){
.baud_rate_prescaler = z_timing->prescaler,
.time_segment_1 = z_timing->prop_seg + z_timing->phase_seg1,
.time_segment_2 = z_timing->phase_seg2,
.synchronization_jump_width = z_timing->sjw,
};
k_mutex_unlock(&data->inst_mutex);
return 0;
}
static inline int get_free_filter_id(const struct device *dev)
{
struct can_renesas_ra_data *data = dev->data;
const struct can_renesas_ra_cfg *cfg = dev->config;
for (uint32_t filter_id = 0; filter_id < cfg->rx_filter_num; filter_id++) {
if (!data->rx_filter[filter_id].set) {
return filter_id;
}
}
return -ENOSPC;
}
static void set_afl_rule(const struct device *dev, const struct can_filter *filter,
uint32_t afl_offset)
{
struct can_renesas_ra_data *data = dev->data;
canfd_extended_cfg_t const *p_extend = data->fsp_can.p_cfg->p_extend;
canfd_afl_entry_t *afl = (canfd_afl_entry_t *)&p_extend->p_afl[afl_offset];
*afl = (canfd_afl_entry_t){.id = {.id = filter->id,
#ifndef CONFIG_CAN_ACCEPT_RTR
.frame_type = CAN_FRAME_TYPE_DATA,
#endif
.id_mode = ((filter->flags & CAN_FILTER_IDE) != 0)
? CAN_ID_MODE_EXTENDED
: CAN_ID_MODE_STANDARD},
.mask = {.mask_id = filter->mask,
#ifdef CONFIG_CAN_ACCEPT_RTR
/* Accept all types of frames */
.mask_frame_type = 0,
#else
/* Only accept frames with the configured frametype */
.mask_frame_type = 1,
#endif
.mask_id_mode = ((filter->flags & CAN_FILTER_IDE) != 0)
? CAN_ID_MODE_EXTENDED
: CAN_ID_MODE_STANDARD},
.destination = {.fifo_select_flags = CANFD_RX_FIFO_0}};
if (data->common.started) {
/* These steps help update AFL rules while CAN module running */
canfd_instance_ctrl_t *p_ctrl = (canfd_instance_ctrl_t *)data->fsp_can.p_ctrl;
R_CANFD_Type *reg = p_ctrl->p_reg;
/* Ignore AFL entry which will be changed */
reg->CFDGAFLIGNENT_b.IRN = afl_offset;
reg->CFDGAFLIGNCTR = ((CFDGAFLIGNCTR_KEY_CODE << R_CANFD_CFDGAFLIGNCTR_KEY_Pos) |
BIT(R_CANFD_CFDGAFLIGNCTR_IREN_Pos));
reg->CFDGAFLECTR_b.AFLDAE = 1;
/* Write AFL configuration */
reg->CFDGAFL[afl_offset] = *(R_CANFD_CFDGAFL_Type *)afl;
/* Lock AFL entry access */
reg->CFDGAFLECTR_b.AFLDAE = 0;
reg->CFDGAFLIGNCTR = ((CFDGAFLIGNCTR_KEY_CODE << R_CANFD_CFDGAFLIGNCTR_KEY_Pos));
}
}
static void remove_afl_rule(const struct device *dev, uint32_t afl_offset)
{
struct can_renesas_ra_data *data = dev->data;
canfd_extended_cfg_t const *p_extend = data->fsp_can.p_cfg->p_extend;
canfd_afl_entry_t *afl = (canfd_afl_entry_t *)&p_extend->p_afl[afl_offset];
/* Set the AFL ID to reserved ID */
*afl = (canfd_afl_entry_t){
.id = {.id = CAN_RENESAS_RA_RESERVED_ID, .id_mode = CAN_ID_MODE_EXTENDED},
.mask = {.mask_id = CAN_RENESAS_RA_RESERVED_ID,
.mask_id_mode = CAN_ID_MODE_EXTENDED}};
if (data->common.started) {
/* These steps help update AFL rules while CAN module running */
canfd_instance_ctrl_t *p_ctrl = (canfd_instance_ctrl_t *)data->fsp_can.p_ctrl;
R_CANFD_Type *reg = p_ctrl->p_reg;
/* Ignore AFL entry which will be changed */
reg->CFDGAFLIGNENT_b.IRN = afl_offset;
reg->CFDGAFLIGNCTR = ((CFDGAFLIGNCTR_KEY_CODE << R_CANFD_CFDGAFLIGNCTR_KEY_Pos) |
BIT(R_CANFD_CFDGAFLIGNCTR_IREN_Pos));
reg->CFDGAFLECTR_b.AFLDAE = 1;
/* Change configurations for AFL entry */
reg->CFDGAFL[afl_offset] = *(R_CANFD_CFDGAFL_Type *)(afl);
/* Lock AFL entry access */
reg->CFDGAFLECTR_b.AFLDAE = 0;
reg->CFDGAFLIGNCTR = ((CFDGAFLIGNCTR_KEY_CODE << R_CANFD_CFDGAFLIGNCTR_KEY_Pos));
}
}
#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
static int recover_bus(const struct device *dev, k_timeout_t timeout)
{
struct can_renesas_ra_data *data = dev->data;
canfd_instance_ctrl_t *p_ctrl = data->fsp_can.p_ctrl;
R_CANFD_Type *reg = p_ctrl->p_reg;
uint32_t cfdcnctr = reg->CFDC->CTR;
int ret = 0;
if (reg->CFDC->ERFL_b.BOEF == 0) {
/* Channel bus-off entry not detected */
goto end;
}
reg->CFDC->CTR_b.BOM = 0x00; /* Switch to Normal Bus-Off mode (comply with ISO 11898-1) */
reg->CFDC->CTR_b.RTBO = 1; /* Force channel state to return from bus-off */
int64_t start_ticks = k_uptime_ticks();
while (reg->CFDC->ERFL_b.BORF == 0) {
if ((k_uptime_ticks() - start_ticks) > timeout.ticks) {
ret = -EAGAIN;
goto end;
}
}
end:
reg->CFDC->CTR = cfdcnctr; /* Restore channel configuration */
return ret;
}
#endif
static inline void can_renesas_ra_call_tx_cb(const struct device *dev, int err)
{
struct can_renesas_ra_data *data = dev->data;
can_tx_callback_t cb = data->tx_cb;
if (cb != NULL) {
data->tx_cb = NULL;
cb(dev, err, data->tx_usr_data);
k_sem_give(&data->tx_sem);
}
}
static inline void can_renesas_ra_call_rx_cb(const struct device *dev, can_callback_args_t *p_args)
{
struct can_renesas_ra_data *data = dev->data;
struct can_renesas_ra_filter *rx_filter = data->rx_filter;
const struct can_renesas_ra_cfg *cfg = dev->config;
struct can_frame frame = {
.dlc = can_bytes_to_dlc(p_args->frame.data_length_code),
.id = p_args->frame.id,
.flags = (((p_args->frame.id_mode == CAN_ID_MODE_EXTENDED) ? CAN_FRAME_IDE : 0UL) |
((p_args->frame.type == CAN_FRAME_TYPE_REMOTE) ? CAN_FRAME_RTR : 0UL) |
((p_args->frame.options & CANFD_FRAME_OPTION_FD) != 0 ? CAN_FRAME_FDF
: 0UL) |
((p_args->frame.options & CANFD_FRAME_OPTION_ERROR) != 0 ? CAN_FRAME_ESI
: 0UL) |
((p_args->frame.options & CANFD_FRAME_OPTION_BRS) != 0 ? CAN_FRAME_BRS
: 0UL)),
};
memcpy(frame.data, p_args->frame.data, p_args->frame.data_length_code);
for (uint32_t i = 0; i < cfg->rx_filter_num; i++) {
can_rx_callback_t cb = rx_filter->rx_cb;
void *usr_data = rx_filter->rx_usr_data;
if (cb == NULL) {
rx_filter++;
continue; /* this filter has not set yet */
}
if (!can_frame_matches_filter(&frame, &rx_filter->filter)) {
rx_filter++;
continue; /* filter did not match */
}
cb(dev, &frame, usr_data);
break;
}
}
static inline void can_renesas_ra_call_state_change_cb(const struct device *dev,
enum can_state state)
{
struct can_renesas_ra_data *data = dev->data;
can_info_t can_info;
if (FSP_SUCCESS != can_api->infoGet(data->fsp_can.p_ctrl, &can_info)) {
LOG_DBG("get state info failed");
return;
}
struct can_bus_err_cnt err_cnt = {
.rx_err_cnt = can_info.error_count_receive,
.tx_err_cnt = can_info.error_count_transmit,
};
data->common.state_change_cb(dev, state, err_cnt, data->common.state_change_cb_user_data);
}
static int can_renesas_ra_get_capabilities(const struct device *dev, can_mode_t *cap)
{
ARG_UNUSED(dev);
*cap = CAN_MODE_NORMAL | CAN_MODE_LOOPBACK;
#ifdef CONFIG_CAN_FD_MODE
*cap |= CAN_MODE_FD;
#endif
#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
*cap |= CAN_MODE_MANUAL_RECOVERY;
#endif
return 0;
}
static int can_renesas_ra_start(const struct device *dev)
{
struct can_renesas_ra_data *data = dev->data;
const struct device *transceiver_dev = can_get_transceiver(dev);
int ret = 0;
if (!device_is_ready(dev)) {
return -EIO;
}
if (data->common.started) {
return -EALREADY;
}
if (((transceiver_dev != NULL) &&
can_transceiver_enable(transceiver_dev, data->common.mode))) {
LOG_DBG("CAN transceiver enable failed");
return -EIO;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
canfd_extended_cfg_t *p_extend = (canfd_extended_cfg_t *)data->fsp_can.p_cfg->p_extend;
p_extend->p_data_timing =
(can_bit_timing_cfg_t *)((data->common.mode & CAN_MODE_FD) != 0
? &data->data_timing
: &classic_can_data_timing_default);
if (FSP_SUCCESS != can_api->close(data->fsp_can.p_ctrl)) {
LOG_DBG("CAN close failed");
ret = -EIO;
goto end;
}
if (FSP_SUCCESS != can_api->open(data->fsp_can.p_ctrl, data->fsp_can.p_cfg)) {
LOG_DBG("CAN open failed");
ret = -EIO;
goto end;
}
if ((data->common.mode & CAN_MODE_LOOPBACK) != 0) {
if (FSP_SUCCESS != can_api->modeTransition(data->fsp_can.p_ctrl,
CAN_OPERATION_MODE_NORMAL,
CAN_TEST_MODE_LOOPBACK_INTERNAL)) {
LOG_DBG("CAN mode change failed");
ret = -EIO;
goto end;
}
}
data->common.started = true;
end:
k_mutex_unlock(&data->inst_mutex);
return ret;
}
static int can_renesas_ra_stop(const struct device *dev)
{
struct can_renesas_ra_data *data = dev->data;
const struct device *transceiver_dev = can_get_transceiver(dev);
int ret = 0;
if (!data->common.started) {
return -EALREADY;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
if (FSP_SUCCESS != can_api->modeTransition(data->fsp_can.p_ctrl, CAN_OPERATION_MODE_HALT,
CAN_TEST_MODE_DISABLED)) {
LOG_DBG("CAN stop failed");
ret = -EIO;
goto end;
}
if (((transceiver_dev != NULL) && can_transceiver_disable(transceiver_dev))) {
LOG_DBG("CAN transceiver disable failed");
ret = -EIO;
goto end;
}
if (data->tx_cb != NULL) {
data->tx_cb = NULL;
k_sem_give(&data->tx_sem);
}
data->common.started = false;
end:
k_mutex_unlock(&data->inst_mutex);
return ret;
}
static int can_renesas_ra_set_mode(const struct device *dev, can_mode_t mode)
{
struct can_renesas_ra_data *data = dev->data;
int ret = 0;
if (data->common.started) {
/* CAN controller should be in stopped state */
return -EBUSY;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
can_mode_t caps = 0;
ret = can_renesas_ra_get_capabilities(dev, &caps);
if (ret != 0) {
goto end;
}
if ((mode & ~caps) != 0) {
ret = -ENOTSUP;
goto end;
}
data->common.mode = mode;
end:
k_mutex_unlock(&data->inst_mutex);
return ret;
}
static int can_renesas_ra_set_timing(const struct device *dev, const struct can_timing *timing)
{
struct can_renesas_ra_data *data = dev->data;
if (data->common.started) {
/* Device is not in stopped state */
return -EBUSY;
}
return set_hw_timing_configuration(dev, data->fsp_can.p_cfg->p_bit_timing, timing);
}
static int can_renesas_ra_send(const struct device *dev, const struct can_frame *frame,
k_timeout_t timeout, can_tx_callback_t callback, void *user_data)
{
struct can_renesas_ra_data *data = dev->data;
if (!data->common.started) {
return -ENETDOWN;
}
#ifdef CONFIG_CAN_FD_MODE
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR | CAN_FRAME_FDF | CAN_FRAME_BRS)) !=
0) {
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
return -ENOTSUP;
}
if ((data->common.mode & CAN_MODE_FD) == 0U &&
((frame->flags & (CAN_FRAME_FDF | CAN_FRAME_BRS)) != 0U)) {
LOG_ERR("CAN FD format not supported in non-FD mode");
return -ENOTSUP;
}
#else /* CONFIG_CAN_FD_MODE */
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0U) {
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
return -ENOTSUP;
}
#endif /* !CONFIG_CAN_FD_MODE */
if ((frame->flags & CAN_FRAME_FDF) != 0U) {
if (frame->dlc > CANFD_MAX_DLC) {
LOG_ERR("DLC of %d for CAN FD format frame", frame->dlc);
return -EINVAL;
}
} else {
if (frame->dlc > CAN_MAX_DLC) {
LOG_ERR("DLC of %d for non-FD format frame", frame->dlc);
return -EINVAL;
}
}
if (k_sem_take(&data->tx_sem, timeout) != 0) {
return -EAGAIN;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
int ret = 0;
data->tx_cb = callback;
data->tx_usr_data = user_data;
can_frame_t fsp_frame = {
.id = frame->id,
.id_mode = ((frame->flags & CAN_FRAME_IDE) != 0) ? CAN_ID_MODE_EXTENDED
: CAN_ID_MODE_STANDARD,
.type = ((frame->flags & CAN_FRAME_RTR) != 0) ? CAN_FRAME_TYPE_REMOTE
: CAN_FRAME_TYPE_DATA,
.data_length_code = can_dlc_to_bytes(frame->dlc),
.options =
((((frame->flags & CAN_FRAME_FDF) != 0) ? CANFD_FRAME_OPTION_FD : 0UL) |
(((frame->flags & CAN_FRAME_BRS) != 0) ? CANFD_FRAME_OPTION_BRS : 0UL) |
(((frame->flags & CAN_FRAME_ESI) != 0) ? CANFD_FRAME_OPTION_ERROR : 0UL)),
};
memcpy(fsp_frame.data, frame->data, fsp_frame.data_length_code);
if (FSP_SUCCESS != can_api->write(data->fsp_can.p_ctrl, CANFD_TX_BUFFER_0, &fsp_frame)) {
LOG_DBG("CAN transmit failed");
data->tx_cb = NULL;
data->tx_usr_data = NULL;
k_sem_give(&data->tx_sem);
ret = -EIO;
goto end;
}
end:
k_mutex_unlock(&data->inst_mutex);
return ret;
}
static int can_renesas_ra_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
void *user_data, const struct can_filter *filter)
{
struct can_renesas_ra_data *data = dev->data;
int filter_id = -ENOSPC;
k_mutex_lock(&data->inst_mutex, K_FOREVER);
filter_id = get_free_filter_id(dev);
if (filter_id == -ENOSPC) {
goto end;
}
set_afl_rule(dev, filter, filter_id);
memcpy(&data->rx_filter[filter_id].filter, filter, sizeof(struct can_filter));
data->rx_filter[filter_id].rx_cb = callback;
data->rx_filter[filter_id].rx_usr_data = user_data;
data->rx_filter[filter_id].set = true;
end:
k_mutex_unlock(&data->inst_mutex);
return filter_id;
}
static void can_renesas_ra_remove_rx_filter(const struct device *dev, int filter_id)
{
struct can_renesas_ra_data *data = dev->data;
const struct can_renesas_ra_cfg *cfg = dev->config;
if ((filter_id < 0) || (filter_id >= cfg->rx_filter_num)) {
return;
}
k_mutex_lock(&data->inst_mutex, K_FOREVER);
remove_afl_rule(dev, filter_id);
data->rx_filter[filter_id].rx_cb = NULL;
data->rx_filter[filter_id].rx_usr_data = NULL;
data->rx_filter[filter_id].set = false;
k_mutex_unlock(&data->inst_mutex);
}
#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
static int can_renesas_ra_recover(const struct device *dev, k_timeout_t timeout)
{
struct can_renesas_ra_data *data = dev->data;
if (!data->common.started) {
return -ENETDOWN;
}
if ((data->common.mode & CAN_MODE_MANUAL_RECOVERY) == 0) {
return -ENOTSUP;
}
return recover_bus(dev, timeout);
}
#endif
static int can_renesas_ra_get_state(const struct device *dev, enum can_state *state,
struct can_bus_err_cnt *err_cnt)
{
struct can_renesas_ra_data *data = dev->data;
can_info_t fsp_info = {0};
if (state == NULL && err_cnt == NULL) {
return 0;
}
if (FSP_SUCCESS != can_api->infoGet(data->fsp_can.p_ctrl, &fsp_info)) {
LOG_DBG("CAN get state info failed");
return -EIO;
}
if (state != NULL) {
if (!data->common.started) {
*state = CAN_STATE_STOPPED;
} else {
if ((fsp_info.error_code & R_CANFD_CFDC_ERFL_BOEF_Msk) != 0) {
*state = CAN_STATE_BUS_OFF;
} else if ((fsp_info.error_code & R_CANFD_CFDC_ERFL_EPF_Msk) != 0) {
*state = CAN_STATE_ERROR_PASSIVE;
} else if ((fsp_info.error_code & R_CANFD_CFDC_ERFL_EWF_Msk) != 0) {
*state = CAN_STATE_ERROR_WARNING;
} else if ((fsp_info.error_code & R_CANFD_CFDC_ERFL_BEF_Msk) != 0) {
*state = CAN_STATE_ERROR_ACTIVE;
}
}
}
if (err_cnt != NULL) {
err_cnt->tx_err_cnt = fsp_info.error_count_transmit;
err_cnt->rx_err_cnt = fsp_info.error_count_receive;
}
return 0;
}
static void can_renesas_ra_set_state_change_callback(const struct device *dev,
can_state_change_callback_t callback,
void *user_data)
{
struct can_renesas_ra_data *data = dev->data;
canfd_instance_ctrl_t *p_ctrl = data->fsp_can.p_ctrl;
int key = irq_lock();
k_mutex_lock(&data->inst_mutex, K_FOREVER);
if (callback != NULL) {
/* Enable state change interrupt */
p_ctrl->p_reg->CFDC->CTR |= (uint32_t)CANFD_CFG_ERR_IRQ;
} else {
/* Disable state change interrupt */
p_ctrl->p_reg->CFDC->CTR &= (uint32_t)~CANFD_CFG_ERR_IRQ;
/* Clear state change interrupt flags */
p_ctrl->p_reg->CFDC->ERFL &=
~(BIT(R_CANFD_CFDC_ERFL_BOEF_Pos) | BIT(R_CANFD_CFDC_ERFL_EWF_Pos) |
BIT(R_CANFD_CFDC_ERFL_EPF_Pos) | BIT(R_CANFD_CFDC_ERFL_BOEF_Pos));
}
data->common.state_change_cb = callback;
data->common.state_change_cb_user_data = user_data;
k_mutex_unlock(&data->inst_mutex);
irq_unlock(key);
}
static int can_renesas_ra_get_core_clock(const struct device *dev, uint32_t *rate)
{
const struct can_renesas_ra_cfg *cfg = dev->config;
clock_control_subsys_t dll_subsys = (clock_control_subsys_t)&cfg->dll_subsys;
return clock_control_get_rate(cfg->dll_clk, dll_subsys, rate);
}
static int can_renesas_ra_get_max_filters(const struct device *dev, bool ide)
{
ARG_UNUSED(ide);
const struct can_renesas_ra_cfg *cfg = dev->config;
return cfg->rx_filter_num;
}
#ifdef CONFIG_CAN_FD_MODE
static int can_renesas_ra_set_timing_data(const struct device *dev,
const struct can_timing *timing_data)
{
struct can_renesas_ra_data *data = dev->data;
if (data->common.started) {
return -EBUSY;
}
return set_hw_timing_configuration(dev, &data->data_timing, timing_data);
}
#endif /* CONFIG_CAN_FD_MODE */
void can_renesas_ra_fsp_cb(can_callback_args_t *p_args)
{
const struct device *dev = p_args->p_context;
switch (p_args->event) {
case CAN_EVENT_RX_COMPLETE: {
can_renesas_ra_call_rx_cb(dev, p_args);
break;
}
case CAN_EVENT_TX_COMPLETE: {
can_renesas_ra_call_tx_cb(dev, 0);
break;
}
case CAN_EVENT_ERR_CHANNEL: {
if ((p_args->error & R_CANFD_CFDC_ERFL_BEF_Msk) != 0) {
can_renesas_ra_call_state_change_cb(dev, CAN_STATE_ERROR_ACTIVE);
}
if ((p_args->error & R_CANFD_CFDC_ERFL_EWF_Msk) != 0) {
can_renesas_ra_call_state_change_cb(dev, CAN_STATE_ERROR_WARNING);
}
if ((p_args->error & R_CANFD_CFDC_ERFL_EPF_Msk) != 0) {
can_renesas_ra_call_state_change_cb(dev, CAN_STATE_ERROR_PASSIVE);
}
if ((p_args->error & R_CANFD_CFDC_ERFL_BOEF_Msk) != 0) {
can_renesas_ra_call_state_change_cb(dev, CAN_STATE_BUS_OFF);
}
if ((p_args->error & R_CANFD_CFDC_ERFL_ALF_Msk) != 0) { /* Arbitration Lost Error */
can_renesas_ra_call_tx_cb(dev, -EBUSY);
}
if ((p_args->error & (R_CANFD_CFDC_ERFL_AERR_Msk | /* ACK Error */
R_CANFD_CFDC_ERFL_ADERR_Msk | /* ACK Delimiter Error */
R_CANFD_CFDC_ERFL_B1ERR_Msk | /* Bit 1 Error */
R_CANFD_CFDC_ERFL_B0ERR_Msk)) /* Bit 0 Error */
!= 0) {
can_renesas_ra_call_tx_cb(dev, -EIO);
}
}
default:
break;
}
}
static inline int can_renesas_ra_apply_default_config(const struct device *dev)
{
struct can_renesas_ra_cfg *cfg = (struct can_renesas_ra_cfg *)dev->config;
int ret;
struct can_timing timing = {0};
/* Calculate bit_timing parameter */
ret = can_calc_timing(dev, &timing, cfg->common.bitrate, cfg->common.sample_point);
if (ret < 0) {
return ret;
}
ret = can_renesas_ra_set_timing(dev, &timing);
if (ret != 0) {
return ret;
}
#ifdef CONFIG_CAN_FD_MODE
can_calc_timing_data(dev, &timing, cfg->common.bitrate_data, cfg->common.sample_point_data);
ret = can_renesas_ra_set_timing_data(dev, &timing);
if (ret < 0) {
return ret;
}
#endif
for (uint32_t filter_id = 0; filter_id < cfg->rx_filter_num; filter_id++) {
remove_afl_rule(dev, filter_id);
}
return 0;
}
static inline int can_renesas_module_clock_init(const struct device *dev)
{
const struct can_renesas_ra_cfg *cfg = dev->config;
const struct can_renesas_ra_global_cfg *global_cfg = cfg->global_dev->config;
int ret;
ret = clock_control_on(cfg->dll_clk, (clock_control_subsys_t)&cfg->dll_subsys);
if (ret < 0) {
return -EIO;
}
uint32_t op_rate;
uint32_t ram_rate;
uint32_t dll_rate;
ret = clock_control_get_rate(global_cfg->op_clk,
(clock_control_subsys_t)&global_cfg->op_subsys, &op_rate);
if (ret < 0) {
return ret;
}
ret = clock_control_get_rate(global_cfg->ram_clk,
(clock_control_subsys_t)&global_cfg->ram_subsys, &ram_rate);
if (ret < 0) {
return ret;
}
ret = clock_control_get_rate(cfg->dll_clk, (clock_control_subsys_t)&cfg->dll_subsys,
&dll_rate);
if (ret < 0) {
return ret;
}
/* Clock constraint: refer to '34.1.2 Clock restriction' - RA8M1 MCU group HWM */
/*
* Operation clock rate must be at least 40Mhz in case CANFD mode.
* Otherwise, it must be at least 32MHz.
*/
if (IS_ENABLED(CONFIG_CAN_FD_MODE) ? op_rate < 40000000 : op_rate < 32000000) {
return -ENOTSUP;
}
/*
* (RAM clock rate / 2) >= DLL rate
* (CANFD operation clock rate) >= DLL rate
*/
if ((ram_rate / 2) < dll_rate || op_rate < dll_rate) {
return -ENOTSUP;
}
return 0;
}
static int can_renesas_ra_init(const struct device *dev)
{
const struct can_renesas_ra_cfg *cfg = dev->config;
struct can_renesas_ra_data *data = dev->data;
int ret = 0;
k_mutex_init(&data->inst_mutex);
k_sem_init(&data->tx_sem, 1, 1);
data->common.started = false;
ret = can_renesas_module_clock_init(dev);
if (ret < 0) {
LOG_DBG("clock initialize failed");
return ret;
}
/* Configure dt provided device signals when available */
ret = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
if (ret < 0) {
LOG_DBG("pin function initial failed");
return ret;
}
/* Apply config and setting for CAN controller HW */
ret = can_renesas_ra_apply_default_config(dev);
if (ret < 0) {
LOG_DBG("invalid default configuration");
return ret;
}
ret = can_api->open(data->fsp_can.p_ctrl, data->fsp_can.p_cfg);
if (ret != FSP_SUCCESS) {
LOG_DBG("CAN bus initialize failed");
return -EIO;
}
/* Put CAN controller into stopped state */
ret = can_api->modeTransition(data->fsp_can.p_ctrl, CAN_OPERATION_MODE_HALT,
CAN_TEST_MODE_DISABLED);
if (ret != FSP_SUCCESS) {
can_api->close(data->fsp_can.p_ctrl);
LOG_DBG("CAN bus initialize failed");
return -EIO;
}
return 0;
}
static DEVICE_API(can, can_renesas_ra_driver_api) = {
.get_capabilities = can_renesas_ra_get_capabilities,
.start = can_renesas_ra_start,
.stop = can_renesas_ra_stop,
.set_mode = can_renesas_ra_set_mode,
.set_timing = can_renesas_ra_set_timing,
.send = can_renesas_ra_send,
.add_rx_filter = can_renesas_ra_add_rx_filter,
.remove_rx_filter = can_renesas_ra_remove_rx_filter,
#if defined(CONFIG_CAN_MANUAL_RECOVERY_MODE)
.recover = can_renesas_ra_recover,
#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
.get_state = can_renesas_ra_get_state,
.set_state_change_callback = can_renesas_ra_set_state_change_callback,
.get_core_clock = can_renesas_ra_get_core_clock,
.get_max_filters = can_renesas_ra_get_max_filters,
.timing_min = CAN_RENESAS_RA_TIMING_MIN,
.timing_max = CAN_RENESAS_RA_TIMING_MAX,
#if defined(CONFIG_CAN_FD_MODE)
.set_timing_data = can_renesas_ra_set_timing_data,
.timing_data_min = CAN_RENESAS_RA_TIMING_DATA_MIN,
.timing_data_max = CAN_RENESAS_RA_TIMING_DATA_MAX,
#endif /* CONFIG_CAN_FD_MODE */
};
#define CAN_RENESAS_RA_GLOBAL_IRQ_INIT() \
R_ICU->IELSR_b[VECTOR_NUMBER_CAN_GLERR].IELS = ELC_EVENT_CAN_GLERR; \
R_ICU->IELSR_b[VECTOR_NUMBER_CAN_RXF].IELS = ELC_EVENT_CAN_RXF; \
IRQ_CONNECT(VECTOR_NUMBER_CAN_GLERR, \