Skip to content

Commit 7bf390c

Browse files
drivers: can: add NXP LPC MCAN front-end for the Bosch MCAN driver
Add a NXP LPC MCAN-specific front-end for the generic Bosch MCAN driver. Signed-off-by: Henrik Brix Andersen <[email protected]>
1 parent 30b6e4b commit 7bf390c

File tree

6 files changed

+319
-7
lines changed

6 files changed

+319
-7
lines changed

drivers/can/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
zephyr_library()
4+
zephyr_sources_ifdef(CONFIG_CAN_MCUX_MCAN can_mcux_mcan.c)
45

56
zephyr_library_sources_ifdef(CONFIG_CAN can_common.c)
67
zephyr_library_sources_ifdef(CONFIG_CAN_LOOPBACK can_loopback.c)

drivers/can/Kconfig.mcux

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ config CAN_MAX_FILTER
1818
range 1 64 if SOC_SERIES_IMX_RT
1919
help
2020
Defines maximum number of concurrent active RX filters
21+
22+
config CAN_MCUX_MCAN
23+
bool "MCUX MCAN driver"
24+
depends on HAS_MCUX_MCAN && CLOCK_CONTROL
25+
select CAN_MCAN
26+
help
27+
Enable support for mcux mcan driver.

drivers/can/can_mcan.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,24 @@ int can_mcan_init(const struct device *dev, const struct can_mcan_config *cfg,
297297
(can->crel & CAN_MCAN_CREL_DAY) >> CAN_MCAN_CREL_DAY_POS);
298298

299299
#ifndef CONFIG_CAN_STM32FD
300+
#ifdef CONFIG_CAN_MCUX_MCAN
301+
uint32_t mrba = (uint32_t)msg_ram & CAN_MCAN_MRBA_BA_MSK;
302+
303+
can->mrba = mrba;
304+
can->sidfc = (((uint32_t)msg_ram->std_filt - mrba) & CAN_MCAN_SIDFC_FLSSA_MSK) |
305+
(ARRAY_SIZE(msg_ram->std_filt) << CAN_MCAN_SIDFC_LSS_POS);
306+
can->xidfc = (((uint32_t)msg_ram->ext_filt - mrba) & CAN_MCAN_XIDFC_FLESA_MSK) |
307+
(ARRAY_SIZE(msg_ram->ext_filt) << CAN_MCAN_XIDFC_LSS_POS);
308+
can->rxf0c = (((uint32_t)msg_ram->rx_fifo0 - mrba) & CAN_MCAN_RXF0C_F0SA) |
309+
(ARRAY_SIZE(msg_ram->rx_fifo0) << CAN_MCAN_RXF0C_F0S_POS);
310+
can->rxf1c = (((uint32_t)msg_ram->rx_fifo1 - mrba) & CAN_MCAN_RXF1C_F1SA) |
311+
(ARRAY_SIZE(msg_ram->rx_fifo1) << CAN_MCAN_RXF1C_F1S_POS);
312+
can->rxbc = (((uint32_t)msg_ram->rx_buffer - mrba) & CAN_MCAN_RXBC_RBSA);
313+
can->txefc = (((uint32_t)msg_ram->tx_event_fifo - mrba) & CAN_MCAN_TXEFC_EFSA_MSK) |
314+
(ARRAY_SIZE(msg_ram->tx_event_fifo) << CAN_MCAN_TXEFC_EFS_POS);
315+
can->txbc = (((uint32_t)msg_ram->tx_buffer - mrba) & CAN_MCAN_TXBC_TBSA_MSK) |
316+
(ARRAY_SIZE(msg_ram->tx_buffer) << CAN_MCAN_TXBC_TFQS_POS);
317+
#else /* CONFIG_CAN_MCUX_MCAN */
300318
can->sidfc = ((uint32_t)msg_ram->std_filt & CAN_MCAN_SIDFC_FLSSA_MSK) |
301319
(ARRAY_SIZE(msg_ram->std_filt) << CAN_MCAN_SIDFC_LSS_POS);
302320
can->xidfc = ((uint32_t)msg_ram->ext_filt & CAN_MCAN_XIDFC_FLESA_MSK) |
@@ -311,6 +329,8 @@ int can_mcan_init(const struct device *dev, const struct can_mcan_config *cfg,
311329
CAN_MCAN_TXEFC_EFS_POS);
312330
can->txbc = ((uint32_t)msg_ram->tx_buffer & CAN_MCAN_TXBC_TBSA) |
313331
(ARRAY_SIZE(msg_ram->tx_buffer) << CAN_MCAN_TXBC_TFQS_POS);
332+
#endif /* !CONFIG_CAN_MCUX_MCAN */
333+
314334
if (sizeof(msg_ram->tx_buffer[0].data) <= 24) {
315335
can->txesc = (sizeof(msg_ram->tx_buffer[0].data) - 8) / 4;
316336
} else {

drivers/can/can_mcan.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
#ifndef ZEPHYR_DRIVERS_CAN_MCAN_H_
99
#define ZEPHYR_DRIVERS_CAN_MCAN_H_
1010

11-
#define NUM_STD_FILTER_ELEMENTS DT_PROP(DT_PATH(soc, can), std_filter_elements)
12-
#define NUM_EXT_FILTER_ELEMENTS DT_PROP(DT_PATH(soc, can), ext_filter_elements)
13-
#define NUM_RX_FIFO0_ELEMENTS DT_PROP(DT_PATH(soc, can), rx_fifo0_elements)
14-
#define NUM_RX_FIFO1_ELEMENTS DT_PROP(DT_PATH(soc, can), rx_fifo0_elements)
15-
#define NUM_RX_BUF_ELEMENTS DT_PROP(DT_PATH(soc, can), rx_buffer_elements)
11+
#ifdef CONFIG_CAN_MCUX_MCAN
12+
#define MCAN_DT_PATH DT_NODELABEL(can0)
13+
#else
14+
#define MCAN_DT_PATH DT_PATH(soc, can)
15+
#endif
16+
17+
#define NUM_STD_FILTER_ELEMENTS DT_PROP(MCAN_DT_PATH, std_filter_elements)
18+
#define NUM_EXT_FILTER_ELEMENTS DT_PROP(MCAN_DT_PATH, ext_filter_elements)
19+
#define NUM_RX_FIFO0_ELEMENTS DT_PROP(MCAN_DT_PATH, rx_fifo0_elements)
20+
#define NUM_RX_FIFO1_ELEMENTS DT_PROP(MCAN_DT_PATH, rx_fifo0_elements)
21+
#define NUM_RX_BUF_ELEMENTS DT_PROP(MCAN_DT_PATH, rx_buffer_elements)
1622
#define NUM_TX_EVENT_FIFO_ELEMENTS \
17-
DT_PROP(DT_PATH(soc, can), tx_event_fifo_elements)
18-
#define NUM_TX_BUF_ELEMENTS DT_PROP(DT_PATH(soc, can), tx_buffer_elements)
23+
DT_PROP(MCAN_DT_PATH, tx_event_fifo_elements)
24+
#define NUM_TX_BUF_ELEMENTS DT_PROP(MCAN_DT_PATH, tx_buffer_elements)
1925

2026

2127
#ifdef CONFIG_CAN_STM32FD

drivers/can/can_mcan_int.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,14 @@
14531453
#define CAN_MCAN_TXEFA_EFAI CAN_MCAN_TXEFA_EFAI_MSK
14541454
#endif /* CONFIG_CAN_STM32FD */
14551455

1456+
/*************** Bit definition for CAN_MCAN_MRBA register *****************/
1457+
#ifdef CONFIG_CAN_MCUX_MCAN
1458+
/* Event FIFO Acknowledge Index */
1459+
#define CAN_MCAN_MRBA_BA_POS (16U)
1460+
#define CAN_MCAN_MRBA_BA_MSK (0xFFFFUL << CAN_MCAN_MRBA_BA_POS)
1461+
#define CAN_MCAN_MRBA_BA CAN_MCAN_MRBA_BA_MSK
1462+
#endif /* CONFIG_CAN_MCUX_MCAN */
1463+
14561464
#ifdef CONFIG_CAN_STM32FD
14571465
struct can_mcan_reg {
14581466
volatile uint32_t crel; /* Core Release Register */
@@ -1553,6 +1561,10 @@ struct can_mcan_reg {
15531561
volatile uint32_t txefc; /* Tx Event FIFO Configuration */
15541562
volatile uint32_t txefs; /* Tx Event FIFO Status */
15551563
volatile uint32_t txefa; /* Tx Event FIFO Acknowledge */
1564+
#ifdef CONFIG_CAN_MCUX_MCAN
1565+
volatile uint32_t res6[65]; /* Reserved (65) */
1566+
volatile uint32_t mrba; /* Message RAM Base Address */
1567+
#endif /* CONFIG_CAN_MCUX_MCAN */
15561568
};
15571569

15581570
#endif /* CONFIG_CAN_STM32FD */

drivers/can/can_mcux_mcan.c

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
/*
2+
* Copyright (c) 2021 Henrik Brix Andersen <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <device.h>
8+
#include <drivers/can.h>
9+
#include <drivers/clock_control.h>
10+
#include <logging/log.h>
11+
12+
#include "can_mcan.h"
13+
14+
LOG_MODULE_REGISTER(mcux_mcan, CONFIG_CAN_LOG_LEVEL);
15+
16+
#define DT_DRV_COMPAT nxp_lpc_mcan
17+
18+
struct mcux_mcan_config {
19+
struct can_mcan_config mcan;
20+
const struct device *clock_dev;
21+
clock_control_subsys_t clock_subsys;
22+
void (*irq_config_func)(const struct device *dev);
23+
};
24+
25+
struct mcux_mcan_data {
26+
struct can_mcan_data mcan;
27+
struct can_mcan_msg_sram msg_ram __nocache;
28+
};
29+
30+
static int mcux_mcan_set_mode(const struct device *dev, enum can_mode mode)
31+
{
32+
const struct mcux_mcan_config *config = dev->config;
33+
34+
return can_mcan_set_mode(&config->mcan, mode);
35+
}
36+
37+
static int mcux_mcan_set_timing(const struct device *dev,
38+
const struct can_timing *timing,
39+
const struct can_timing *timing_data)
40+
{
41+
const struct mcux_mcan_config *config = dev->config;
42+
43+
return can_mcan_set_timing(&config->mcan, timing, timing_data);
44+
}
45+
46+
static int mcux_mcan_send(const struct device *dev, const struct zcan_frame *msg,
47+
k_timeout_t timeout, can_tx_callback_t callback,
48+
void *user_data)
49+
{
50+
const struct mcux_mcan_config *config = dev->config;
51+
struct mcux_mcan_data *data = dev->data;
52+
53+
return can_mcan_send(&config->mcan, &data->mcan, &data->msg_ram,
54+
msg, timeout, callback, user_data);
55+
}
56+
57+
static int mcux_mcan_add_rx_filter(const struct device *dev,
58+
can_rx_callback_t cb,
59+
void *user_data,
60+
const struct zcan_filter *filter)
61+
{
62+
struct mcux_mcan_data *data = dev->data;
63+
64+
return can_mcan_add_rx_filter(&data->mcan, &data->msg_ram,
65+
cb, user_data, filter);
66+
}
67+
68+
static void mcux_mcan_remove_rx_filter(const struct device *dev, int filter_id)
69+
{
70+
struct mcux_mcan_data *data = dev->data;
71+
72+
can_mcan_remove_rx_filter(&data->mcan, &data->msg_ram, filter_id);
73+
}
74+
75+
static enum can_state mcux_mcan_get_state(const struct device *dev,
76+
struct can_bus_err_cnt *err_cnt)
77+
{
78+
const struct mcux_mcan_config *config = dev->config;
79+
80+
return can_mcan_get_state(&config->mcan, err_cnt);
81+
}
82+
83+
static void mcux_mcan_set_state_change_callback(const struct device *dev,
84+
can_state_change_callback_t cb)
85+
{
86+
struct mcux_mcan_data *data = dev->data;
87+
88+
data->mcan.state_change_cb = cb;
89+
}
90+
91+
static int mcux_mcan_get_core_clock(const struct device *dev, uint32_t *rate)
92+
{
93+
const struct mcux_mcan_config *config = dev->config;
94+
95+
return clock_control_get_rate(config->clock_dev, config->clock_subsys,
96+
rate);
97+
}
98+
99+
static void mcux_mcan_line_0_isr(const struct device *dev)
100+
{
101+
const struct mcux_mcan_config *config = dev->config;
102+
struct mcux_mcan_data *data = dev->data;
103+
104+
can_mcan_line_0_isr(&config->mcan, &data->msg_ram, &data->mcan);
105+
}
106+
107+
static void mcux_mcan_line_1_isr(const struct device *dev)
108+
{
109+
const struct mcux_mcan_config *config = dev->config;
110+
struct mcux_mcan_data *data = dev->data;
111+
112+
can_mcan_line_1_isr(&config->mcan, &data->msg_ram, &data->mcan);
113+
}
114+
115+
static int mcux_mcan_init(const struct device *dev)
116+
{
117+
const struct mcux_mcan_config *config = dev->config;
118+
struct mcux_mcan_data *data = dev->data;
119+
int err;
120+
121+
err = clock_control_on(config->clock_dev, config->clock_subsys);
122+
if (err) {
123+
LOG_ERR("failed to enable clock (err %d)", err);
124+
return -EINVAL;
125+
}
126+
127+
err = can_mcan_init(dev, &config->mcan, &data->msg_ram, &data->mcan);
128+
if (err) {
129+
LOG_ERR("failed to initialize mcan (err %d)", err);
130+
return err;
131+
}
132+
133+
config->irq_config_func(dev);
134+
135+
return 0;
136+
}
137+
138+
static const struct can_driver_api mcux_mcan_driver_api = {
139+
.set_mode = mcux_mcan_set_mode,
140+
.set_timing = mcux_mcan_set_timing,
141+
.send = mcux_mcan_send,
142+
.add_rx_filter = mcux_mcan_add_rx_filter,
143+
.remove_rx_filter = mcux_mcan_remove_rx_filter,
144+
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
145+
.recover = can_mcan_recover,
146+
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
147+
.get_state = mcux_mcan_get_state,
148+
.set_state_change_callback = mcux_mcan_set_state_change_callback,
149+
.get_core_clock = mcux_mcan_get_core_clock,
150+
/*
151+
* MCUX MCAN timing limits are specified in the "Nominal bit timing and
152+
* prescaler register (NBTP)" table in the SoC reference manual.
153+
*
154+
* Note that the values here are the "physical" timing limits, whereas
155+
* the register field limits are physical values minus 1 (which is
156+
* handled by the register assignments in the common MCAN driver code).
157+
*/
158+
.timing_min = {
159+
.sjw = 1,
160+
.prop_seg = 0,
161+
.phase_seg1 = 1,
162+
.phase_seg2 = 1,
163+
.prescaler = 1
164+
},
165+
.timing_max = {
166+
.sjw = 128,
167+
.prop_seg = 0,
168+
.phase_seg1 = 256,
169+
.phase_seg2 = 128,
170+
.prescaler = 512,
171+
},
172+
#ifdef CONFIG_CAN_FD_MODE
173+
/*
174+
* MCUX MCAN data timing limits are specified in the "Data bit timing
175+
* and prescaler register (DBTP)" table in the SoC reference manual.
176+
*
177+
* Note that the values here are the "physical" timing limits, whereas
178+
* the register field limits are physical values minus 1 (which is
179+
* handled by the register assignments in the common MCAN driver code).
180+
*/
181+
.timing_min_data = {
182+
.sjw = 1,
183+
.prop_seg = 0,
184+
.phase_seg1 = 1,
185+
.phase_seg2 = 1,
186+
.prescaler = 1,
187+
},
188+
.timing_max_data = {
189+
.sjw = 16,
190+
.prop_seg = 0,
191+
.phase_seg1 = 16,
192+
.phase_seg2 = 16,
193+
.prescaler = 32,
194+
}
195+
#endif /* CONFIG_CAN_FD_MODE */
196+
};
197+
198+
#ifdef CONFIG_CAN_FD_MODE
199+
#define MCUX_MCAN_MCAN_INIT(n) \
200+
{ \
201+
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \
202+
.bus_speed = DT_INST_PROP(n, bus_speed), \
203+
.sjw = DT_INST_PROP(n, sjw), \
204+
.sample_point = DT_INST_PROP_OR(n, sample_point, 0), \
205+
.prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \
206+
DT_INST_PROP_OR(n, phase_seg1, 0), \
207+
.ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \
208+
.bus_speed_data = DT_INST_PROP(n, bus_speed_data), \
209+
.sjw_data = DT_INST_PROP(n, sjw_data), \
210+
.sample_point_data = \
211+
DT_INST_PROP_OR(n, sample_point_data, 0), \
212+
.prop_ts1_data = DT_INST_PROP_OR(n, prop_seg_data, 0) + \
213+
DT_INST_PROP_OR(n, phase_seg1_data, 0), \
214+
.ts2_data = DT_INST_PROP_OR(n, phase_seg2_data, 0), \
215+
.tx_delay_comp_offset = \
216+
DT_INST_PROP(n, tx_delay_comp_offset) \
217+
}
218+
#else /* CONFIG_CAN_FD_MODE */
219+
#define MCUX_MCAN_MCAN_INIT(n) \
220+
{ \
221+
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \
222+
.bus_speed = DT_INST_PROP(n, bus_speed), \
223+
.sjw = DT_INST_PROP(n, sjw), \
224+
.sample_point = DT_INST_PROP_OR(n, sample_point, 0), \
225+
.prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \
226+
DT_INST_PROP_OR(n, phase_seg1, 0), \
227+
.ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \
228+
}
229+
#endif /* !CONFIG_CAN_FD_MODE */
230+
231+
#define MCUX_MCAN_INIT(n) \
232+
static void mcux_mcan_irq_config_##n(const struct device *dev); \
233+
\
234+
static const struct mcux_mcan_config mcux_mcan_config_##n = { \
235+
.mcan = MCUX_MCAN_MCAN_INIT(n), \
236+
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
237+
.clock_subsys = (clock_control_subsys_t) \
238+
DT_INST_CLOCKS_CELL(n, name), \
239+
.irq_config_func = mcux_mcan_irq_config_##n, \
240+
}; \
241+
\
242+
static struct mcux_mcan_data mcux_mcan_data_##n; \
243+
\
244+
DEVICE_DT_INST_DEFINE(n, &mcux_mcan_init, NULL, \
245+
&mcux_mcan_data_##n, \
246+
&mcux_mcan_config_##n, \
247+
POST_KERNEL, \
248+
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
249+
&mcux_mcan_driver_api); \
250+
\
251+
static void mcux_mcan_irq_config_##n(const struct device *dev) \
252+
{ \
253+
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, 0, irq), \
254+
DT_INST_IRQ_BY_IDX(n, 0, priority), \
255+
mcux_mcan_line_0_isr, \
256+
DEVICE_DT_INST_GET(n), 0); \
257+
irq_enable(DT_INST_IRQ_BY_IDX(n, 0, irq)); \
258+
\
259+
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, 1, irq), \
260+
DT_INST_IRQ_BY_IDX(n, 1, priority), \
261+
mcux_mcan_line_1_isr, \
262+
DEVICE_DT_INST_GET(n), 0); \
263+
irq_enable(DT_INST_IRQ_BY_IDX(n, 1, irq)); \
264+
}
265+
266+
DT_INST_FOREACH_STATUS_OKAY(MCUX_MCAN_INIT)

0 commit comments

Comments
 (0)