1 /*
2  * Copyright (c) 2018 Alexander Wachter
3  * Copyright (c) 2022 Martin Jäger <martin@libre.solar>
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /* Include soc.h prior to Zephyr CAN headers to pull in HAL fixups */
9 #include <soc.h>
10 #include <zephyr/drivers/can.h>
11 #include <zephyr/drivers/can/transceiver.h>
12 
13 #include <zephyr/drivers/clock_control.h>
14 #include <zephyr/drivers/clock_control/stm32_clock_control.h>
15 #include <zephyr/drivers/pinctrl.h>
16 #include <zephyr/irq.h>
17 #include <zephyr/kernel.h>
18 #include <zephyr/logging/log.h>
19 #include <zephyr/sys/util.h>
20 
21 LOG_MODULE_REGISTER(can_stm32, CONFIG_CAN_LOG_LEVEL);
22 
23 #define CAN_INIT_TIMEOUT  (10 * (sys_clock_hw_cycles_per_sec() / MSEC_PER_SEC))
24 
25 #define DT_DRV_COMPAT st_stm32_bxcan
26 
27 #define CAN_STM32_NUM_FILTER_BANKS (14)
28 #define CAN_STM32_MAX_FILTER_ID \
29 	(CONFIG_CAN_MAX_EXT_ID_FILTER + CONFIG_CAN_MAX_STD_ID_FILTER * 2)
30 
31 #define CAN_STM32_FIRX_STD_IDE_POS   (3U)
32 #define CAN_STM32_FIRX_STD_RTR_POS   (4U)
33 #define CAN_STM32_FIRX_STD_ID_POS    (5U)
34 
35 #define CAN_STM32_FIRX_EXT_IDE_POS    (2U)
36 #define CAN_STM32_FIRX_EXT_RTR_POS    (1U)
37 #define CAN_STM32_FIRX_EXT_STD_ID_POS (21U)
38 #define CAN_STM32_FIRX_EXT_EXT_ID_POS (3U)
39 
40 #if (CONFIG_CAN_MAX_STD_ID_FILTER + CONFIG_CAN_MAX_EXT_ID_FILTER * 2) > \
41 						(CAN_STM32_NUM_FILTER_BANKS * 2)
42 #error Number of configured filters exceeds available filter bank slots.
43 #endif
44 
45 struct can_stm32_mailbox {
46 	can_tx_callback_t tx_callback;
47 	void *callback_arg;
48 };
49 
50 struct can_stm32_data {
51 	struct can_driver_data common;
52 	struct k_mutex inst_mutex;
53 	struct k_sem tx_int_sem;
54 	struct can_stm32_mailbox mb0;
55 	struct can_stm32_mailbox mb1;
56 	struct can_stm32_mailbox mb2;
57 	can_rx_callback_t rx_cb_std[CONFIG_CAN_MAX_STD_ID_FILTER];
58 	can_rx_callback_t rx_cb_ext[CONFIG_CAN_MAX_EXT_ID_FILTER];
59 	void *cb_arg_std[CONFIG_CAN_MAX_STD_ID_FILTER];
60 	void *cb_arg_ext[CONFIG_CAN_MAX_EXT_ID_FILTER];
61 	enum can_state state;
62 };
63 
64 struct can_stm32_config {
65 	const struct can_driver_config common;
66 	CAN_TypeDef *can;   /*!< CAN Registers*/
67 	CAN_TypeDef *master_can;   /*!< CAN Registers for shared filter */
68 	struct stm32_pclken pclken;
69 	void (*config_irq)(CAN_TypeDef *can);
70 	const struct pinctrl_dev_config *pcfg;
71 };
72 
73 /*
74  * Mutex to prevent simultaneous access to filter registers shared between CAN1
75  * and CAN2.
76  */
77 static struct k_mutex filter_mutex;
78 
can_stm32_signal_tx_complete(const struct device * dev,struct can_stm32_mailbox * mb,int status)79 static void can_stm32_signal_tx_complete(const struct device *dev, struct can_stm32_mailbox *mb,
80 					 int status)
81 {
82 	can_tx_callback_t callback = mb->tx_callback;
83 
84 	if (callback != NULL) {
85 		callback(dev, status, mb->callback_arg);
86 		mb->tx_callback = NULL;
87 	}
88 }
89 
can_stm32_rx_fifo_pop(CAN_FIFOMailBox_TypeDef * mbox,struct can_frame * frame)90 static void can_stm32_rx_fifo_pop(CAN_FIFOMailBox_TypeDef *mbox, struct can_frame *frame)
91 {
92 	memset(frame, 0, sizeof(*frame));
93 
94 	if (mbox->RIR & CAN_RI0R_IDE) {
95 		frame->id = mbox->RIR >> CAN_RI0R_EXID_Pos;
96 		frame->flags |= CAN_FRAME_IDE;
97 	} else {
98 		frame->id = mbox->RIR >> CAN_RI0R_STID_Pos;
99 	}
100 
101 	if ((mbox->RIR & CAN_RI0R_RTR) != 0) {
102 		frame->flags |= CAN_FRAME_RTR;
103 	} else {
104 		frame->data_32[0] = mbox->RDLR;
105 		frame->data_32[1] = mbox->RDHR;
106 	}
107 
108 	frame->dlc = mbox->RDTR & (CAN_RDT0R_DLC >> CAN_RDT0R_DLC_Pos);
109 #ifdef CONFIG_CAN_RX_TIMESTAMP
110 	frame->timestamp = ((mbox->RDTR & CAN_RDT0R_TIME) >> CAN_RDT0R_TIME_Pos);
111 #endif
112 }
113 
can_stm32_rx_isr_handler(const struct device * dev)114 static inline void can_stm32_rx_isr_handler(const struct device *dev)
115 {
116 	struct can_stm32_data *data = dev->data;
117 	const struct can_stm32_config *cfg = dev->config;
118 	CAN_TypeDef *can = cfg->can;
119 	CAN_FIFOMailBox_TypeDef *mbox;
120 	int filter_id, index;
121 	struct can_frame frame;
122 	can_rx_callback_t callback = NULL;
123 	void *cb_arg;
124 
125 	while (can->RF0R & CAN_RF0R_FMP0) {
126 		mbox = &can->sFIFOMailBox[0];
127 		filter_id = ((mbox->RDTR & CAN_RDT0R_FMI) >> CAN_RDT0R_FMI_Pos);
128 
129 		LOG_DBG("Message on filter_id %d", filter_id);
130 
131 		can_stm32_rx_fifo_pop(mbox, &frame);
132 
133 		if (filter_id < CONFIG_CAN_MAX_EXT_ID_FILTER) {
134 			callback = data->rx_cb_ext[filter_id];
135 			cb_arg = data->cb_arg_ext[filter_id];
136 		} else if (filter_id < CAN_STM32_MAX_FILTER_ID) {
137 			index = filter_id - CONFIG_CAN_MAX_EXT_ID_FILTER;
138 			callback = data->rx_cb_std[index];
139 			cb_arg = data->cb_arg_std[index];
140 		}
141 
142 		if (callback) {
143 			callback(dev, &frame, cb_arg);
144 		}
145 
146 		/* Release message */
147 		can->RF0R |= CAN_RF0R_RFOM0;
148 	}
149 
150 	if (can->RF0R & CAN_RF0R_FOVR0) {
151 		LOG_ERR("RX FIFO Overflow");
152 		CAN_STATS_RX_OVERRUN_INC(dev);
153 	}
154 }
155 
can_stm32_get_state(const struct device * dev,enum can_state * state,struct can_bus_err_cnt * err_cnt)156 static int can_stm32_get_state(const struct device *dev, enum can_state *state,
157 			       struct can_bus_err_cnt *err_cnt)
158 {
159 	const struct can_stm32_config *cfg = dev->config;
160 	struct can_stm32_data *data = dev->data;
161 	CAN_TypeDef *can = cfg->can;
162 
163 	if (state != NULL) {
164 		if (!data->common.started) {
165 			*state = CAN_STATE_STOPPED;
166 		} else if (can->ESR & CAN_ESR_BOFF) {
167 			*state = CAN_STATE_BUS_OFF;
168 		} else if (can->ESR & CAN_ESR_EPVF) {
169 			*state = CAN_STATE_ERROR_PASSIVE;
170 		} else if (can->ESR & CAN_ESR_EWGF) {
171 			*state = CAN_STATE_ERROR_WARNING;
172 		} else {
173 			*state = CAN_STATE_ERROR_ACTIVE;
174 		}
175 	}
176 
177 	if (err_cnt != NULL) {
178 		err_cnt->tx_err_cnt =
179 			((can->ESR & CAN_ESR_TEC) >> CAN_ESR_TEC_Pos);
180 		err_cnt->rx_err_cnt =
181 			((can->ESR & CAN_ESR_REC) >> CAN_ESR_REC_Pos);
182 	}
183 
184 	return 0;
185 }
186 
can_stm32_bus_state_change_isr(const struct device * dev)187 static inline void can_stm32_bus_state_change_isr(const struct device *dev)
188 {
189 	struct can_stm32_data *data = dev->data;
190 	struct can_bus_err_cnt err_cnt;
191 	enum can_state state;
192 	const can_state_change_callback_t cb = data->common.state_change_cb;
193 	void *state_change_cb_data = data->common.state_change_cb_user_data;
194 
195 #ifdef CONFIG_CAN_STATS
196 	const struct can_stm32_config *cfg = dev->config;
197 	CAN_TypeDef *can = cfg->can;
198 
199 	switch (can->ESR & CAN_ESR_LEC) {
200 	case (CAN_ESR_LEC_0):
201 		CAN_STATS_STUFF_ERROR_INC(dev);
202 		break;
203 	case (CAN_ESR_LEC_1):
204 		CAN_STATS_FORM_ERROR_INC(dev);
205 		break;
206 	case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
207 		CAN_STATS_ACK_ERROR_INC(dev);
208 		break;
209 	case (CAN_ESR_LEC_2):
210 		CAN_STATS_BIT1_ERROR_INC(dev);
211 		break;
212 	case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
213 		CAN_STATS_BIT0_ERROR_INC(dev);
214 		break;
215 	case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
216 		CAN_STATS_CRC_ERROR_INC(dev);
217 		break;
218 	default:
219 		break;
220 	}
221 
222 	/* Clear last error code flag */
223 	can->ESR |= CAN_ESR_LEC;
224 #endif /* CONFIG_CAN_STATS */
225 
226 	(void)can_stm32_get_state(dev, &state, &err_cnt);
227 
228 	if (state != data->state) {
229 		data->state = state;
230 
231 		if (cb != NULL) {
232 			cb(dev, state, err_cnt, state_change_cb_data);
233 		}
234 	}
235 }
236 
can_stm32_tx_isr_handler(const struct device * dev)237 static inline void can_stm32_tx_isr_handler(const struct device *dev)
238 {
239 	struct can_stm32_data *data = dev->data;
240 	const struct can_stm32_config *cfg = dev->config;
241 	CAN_TypeDef *can = cfg->can;
242 	uint32_t bus_off;
243 	int status;
244 
245 	bus_off = can->ESR & CAN_ESR_BOFF;
246 
247 	if ((can->TSR & CAN_TSR_RQCP0) | bus_off) {
248 		status = can->TSR & CAN_TSR_TXOK0 ? 0  :
249 			 can->TSR & CAN_TSR_TERR0 ? -EIO :
250 			 can->TSR & CAN_TSR_ALST0 ? -EBUSY :
251 					  bus_off ? -ENETUNREACH :
252 						    -EIO;
253 		/* clear the request. */
254 		can->TSR |= CAN_TSR_RQCP0;
255 		can_stm32_signal_tx_complete(dev, &data->mb0, status);
256 	}
257 
258 	if ((can->TSR & CAN_TSR_RQCP1) | bus_off) {
259 		status = can->TSR & CAN_TSR_TXOK1 ? 0  :
260 			 can->TSR & CAN_TSR_TERR1 ? -EIO :
261 			 can->TSR & CAN_TSR_ALST1 ? -EBUSY :
262 			 bus_off                  ? -ENETUNREACH :
263 						    -EIO;
264 		/* clear the request. */
265 		can->TSR |= CAN_TSR_RQCP1;
266 		can_stm32_signal_tx_complete(dev, &data->mb1, status);
267 	}
268 
269 	if ((can->TSR & CAN_TSR_RQCP2) | bus_off) {
270 		status = can->TSR & CAN_TSR_TXOK2 ? 0  :
271 			 can->TSR & CAN_TSR_TERR2 ? -EIO :
272 			 can->TSR & CAN_TSR_ALST2 ? -EBUSY :
273 			 bus_off                  ? -ENETUNREACH :
274 						    -EIO;
275 		/* clear the request. */
276 		can->TSR |= CAN_TSR_RQCP2;
277 		can_stm32_signal_tx_complete(dev, &data->mb2, status);
278 	}
279 
280 	if (can->TSR & CAN_TSR_TME) {
281 		k_sem_give(&data->tx_int_sem);
282 	}
283 }
284 
285 #ifdef CONFIG_SOC_SERIES_STM32F0X
286 
can_stm32_isr(const struct device * dev)287 static void can_stm32_isr(const struct device *dev)
288 {
289 	const struct can_stm32_config *cfg = dev->config;
290 	CAN_TypeDef *can = cfg->can;
291 
292 	can_stm32_tx_isr_handler(dev);
293 	can_stm32_rx_isr_handler(dev);
294 
295 	if (can->MSR & CAN_MSR_ERRI) {
296 		can_stm32_bus_state_change_isr(dev);
297 		can->MSR |= CAN_MSR_ERRI;
298 	}
299 }
300 
301 #else
302 
can_stm32_rx_isr(const struct device * dev)303 static void can_stm32_rx_isr(const struct device *dev)
304 {
305 	can_stm32_rx_isr_handler(dev);
306 }
307 
can_stm32_tx_isr(const struct device * dev)308 static void can_stm32_tx_isr(const struct device *dev)
309 {
310 	can_stm32_tx_isr_handler(dev);
311 }
312 
can_stm32_state_change_isr(const struct device * dev)313 static void can_stm32_state_change_isr(const struct device *dev)
314 {
315 	const struct can_stm32_config *cfg = dev->config;
316 	CAN_TypeDef *can = cfg->can;
317 
318 	/* Signal bus-off to waiting tx */
319 	if (can->MSR & CAN_MSR_ERRI) {
320 		can_stm32_tx_isr_handler(dev);
321 		can_stm32_bus_state_change_isr(dev);
322 		can->MSR |= CAN_MSR_ERRI;
323 	}
324 }
325 
326 #endif
327 
can_stm32_enter_init_mode(CAN_TypeDef * can)328 static int can_stm32_enter_init_mode(CAN_TypeDef *can)
329 {
330 	uint32_t start_time;
331 
332 	can->MCR |= CAN_MCR_INRQ;
333 	start_time = k_cycle_get_32();
334 
335 	while ((can->MSR & CAN_MSR_INAK) == 0U) {
336 		if (k_cycle_get_32() - start_time > CAN_INIT_TIMEOUT) {
337 			can->MCR &= ~CAN_MCR_INRQ;
338 			return -EAGAIN;
339 		}
340 	}
341 
342 	return 0;
343 }
344 
can_stm32_leave_init_mode(CAN_TypeDef * can)345 static int can_stm32_leave_init_mode(CAN_TypeDef *can)
346 {
347 	uint32_t start_time;
348 
349 	can->MCR &= ~CAN_MCR_INRQ;
350 	start_time = k_cycle_get_32();
351 
352 	while ((can->MSR & CAN_MSR_INAK) != 0U) {
353 		if (k_cycle_get_32() - start_time > CAN_INIT_TIMEOUT) {
354 			return -EAGAIN;
355 		}
356 	}
357 
358 	return 0;
359 }
360 
can_stm32_leave_sleep_mode(CAN_TypeDef * can)361 static int can_stm32_leave_sleep_mode(CAN_TypeDef *can)
362 {
363 	uint32_t start_time;
364 
365 	can->MCR &= ~CAN_MCR_SLEEP;
366 	start_time = k_cycle_get_32();
367 
368 	while ((can->MSR & CAN_MSR_SLAK) != 0) {
369 		if (k_cycle_get_32() - start_time > CAN_INIT_TIMEOUT) {
370 			return -EAGAIN;
371 		}
372 	}
373 
374 	return 0;
375 }
376 
can_stm32_get_capabilities(const struct device * dev,can_mode_t * cap)377 static int can_stm32_get_capabilities(const struct device *dev, can_mode_t *cap)
378 {
379 	ARG_UNUSED(dev);
380 
381 	*cap = CAN_MODE_NORMAL | CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY | CAN_MODE_ONE_SHOT;
382 
383 	if (IS_ENABLED(CONFIG_CAN_MANUAL_RECOVERY_MODE)) {
384 		*cap |= CAN_MODE_MANUAL_RECOVERY;
385 	}
386 
387 	return 0;
388 }
389 
can_stm32_start(const struct device * dev)390 static int can_stm32_start(const struct device *dev)
391 {
392 	const struct can_stm32_config *cfg = dev->config;
393 	struct can_stm32_data *data = dev->data;
394 	CAN_TypeDef *can = cfg->can;
395 	int ret = 0;
396 
397 	k_mutex_lock(&data->inst_mutex, K_FOREVER);
398 
399 	if (data->common.started) {
400 		ret = -EALREADY;
401 		goto unlock;
402 	}
403 
404 	if (cfg->common.phy != NULL) {
405 		ret = can_transceiver_enable(cfg->common.phy, data->common.mode);
406 		if (ret != 0) {
407 			LOG_ERR("failed to enable CAN transceiver (err %d)", ret);
408 			goto unlock;
409 		}
410 	}
411 
412 	CAN_STATS_RESET(dev);
413 
414 	ret = can_stm32_leave_init_mode(can);
415 	if (ret < 0) {
416 		LOG_ERR("Failed to leave init mode");
417 
418 		if (cfg->common.phy != NULL) {
419 			/* Attempt to disable the CAN transceiver in case of error */
420 			(void)can_transceiver_disable(cfg->common.phy);
421 		}
422 
423 		ret = -EIO;
424 		goto unlock;
425 	}
426 
427 	data->common.started = true;
428 
429 unlock:
430 	k_mutex_unlock(&data->inst_mutex);
431 
432 	return ret;
433 }
434 
can_stm32_stop(const struct device * dev)435 static int can_stm32_stop(const struct device *dev)
436 {
437 	const struct can_stm32_config *cfg = dev->config;
438 	struct can_stm32_data *data = dev->data;
439 	CAN_TypeDef *can = cfg->can;
440 	int ret = 0;
441 
442 	k_mutex_lock(&data->inst_mutex, K_FOREVER);
443 
444 	if (!data->common.started) {
445 		ret = -EALREADY;
446 		goto unlock;
447 	}
448 
449 	ret = can_stm32_enter_init_mode(can);
450 	if (ret < 0) {
451 		LOG_ERR("Failed to enter init mode");
452 		ret = -EIO;
453 		goto unlock;
454 	}
455 
456 	/* Abort any pending transmissions */
457 	can_stm32_signal_tx_complete(dev, &data->mb0, -ENETDOWN);
458 	can_stm32_signal_tx_complete(dev, &data->mb1, -ENETDOWN);
459 	can_stm32_signal_tx_complete(dev, &data->mb2, -ENETDOWN);
460 	can->TSR |= CAN_TSR_ABRQ2 | CAN_TSR_ABRQ1 | CAN_TSR_ABRQ0;
461 
462 	if (cfg->common.phy != NULL) {
463 		ret = can_transceiver_disable(cfg->common.phy);
464 		if (ret != 0) {
465 			LOG_ERR("failed to enable CAN transceiver (err %d)", ret);
466 			goto unlock;
467 		}
468 	}
469 
470 	data->common.started = false;
471 
472 unlock:
473 	k_mutex_unlock(&data->inst_mutex);
474 
475 	return ret;
476 }
477 
can_stm32_set_mode(const struct device * dev,can_mode_t mode)478 static int can_stm32_set_mode(const struct device *dev, can_mode_t mode)
479 {
480 	can_mode_t supported = CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY | CAN_MODE_ONE_SHOT;
481 	const struct can_stm32_config *cfg = dev->config;
482 	CAN_TypeDef *can = cfg->can;
483 	struct can_stm32_data *data = dev->data;
484 
485 	LOG_DBG("Set mode %d", mode);
486 
487 	if (IS_ENABLED(CONFIG_CAN_MANUAL_RECOVERY_MODE)) {
488 		supported |= CAN_MODE_MANUAL_RECOVERY;
489 	}
490 
491 	if ((mode & ~(supported)) != 0) {
492 		LOG_ERR("unsupported mode: 0x%08x", mode);
493 		return -ENOTSUP;
494 	}
495 
496 	if (data->common.started) {
497 		return -EBUSY;
498 	}
499 
500 	k_mutex_lock(&data->inst_mutex, K_FOREVER);
501 
502 	if ((mode & CAN_MODE_LOOPBACK) != 0) {
503 		/* Loopback mode */
504 		can->BTR |= CAN_BTR_LBKM;
505 	} else {
506 		can->BTR &= ~CAN_BTR_LBKM;
507 	}
508 
509 	if ((mode & CAN_MODE_LISTENONLY) != 0) {
510 		/* Silent mode */
511 		can->BTR |= CAN_BTR_SILM;
512 	} else {
513 		can->BTR &= ~CAN_BTR_SILM;
514 	}
515 
516 	if ((mode & CAN_MODE_ONE_SHOT) != 0) {
517 		/* No automatic retransmission */
518 		can->MCR |= CAN_MCR_NART;
519 	} else {
520 		can->MCR &= ~CAN_MCR_NART;
521 	}
522 
523 	if (IS_ENABLED(CONFIG_CAN_MANUAL_RECOVERY_MODE)) {
524 		if ((mode & CAN_MODE_MANUAL_RECOVERY) != 0) {
525 			/* No automatic recovery from bus-off */
526 			can->MCR &= ~CAN_MCR_ABOM;
527 		} else {
528 			can->MCR |= CAN_MCR_ABOM;
529 		}
530 	}
531 
532 	data->common.mode = mode;
533 
534 	k_mutex_unlock(&data->inst_mutex);
535 
536 	return 0;
537 }
538 
can_stm32_set_timing(const struct device * dev,const struct can_timing * timing)539 static int can_stm32_set_timing(const struct device *dev,
540 				const struct can_timing *timing)
541 {
542 	const struct can_stm32_config *cfg = dev->config;
543 	CAN_TypeDef *can = cfg->can;
544 	struct can_stm32_data *data = dev->data;
545 
546 	k_mutex_lock(&data->inst_mutex, K_FOREVER);
547 
548 	if (data->common.started) {
549 		k_mutex_unlock(&data->inst_mutex);
550 		return -EBUSY;
551 	}
552 
553 	can->BTR = (can->BTR & ~(CAN_BTR_SJW_Msk | CAN_BTR_BRP_Msk |
554 				 CAN_BTR_TS1_Msk | CAN_BTR_TS2_Msk)) |
555 	     (((timing->sjw        - 1) << CAN_BTR_SJW_Pos) & CAN_BTR_SJW_Msk) |
556 	     (((timing->phase_seg1 - 1) << CAN_BTR_TS1_Pos) & CAN_BTR_TS1_Msk) |
557 	     (((timing->phase_seg2 - 1) << CAN_BTR_TS2_Pos) & CAN_BTR_TS2_Msk) |
558 	     (((timing->prescaler  - 1) << CAN_BTR_BRP_Pos) & CAN_BTR_BRP_Msk);
559 
560 	k_mutex_unlock(&data->inst_mutex);
561 
562 	return 0;
563 }
564 
can_stm32_get_core_clock(const struct device * dev,uint32_t * rate)565 static int can_stm32_get_core_clock(const struct device *dev, uint32_t *rate)
566 {
567 	const struct can_stm32_config *cfg = dev->config;
568 	const struct device *clock;
569 	int ret;
570 
571 	clock = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
572 
573 	ret = clock_control_get_rate(clock,
574 				     (clock_control_subsys_t) &cfg->pclken,
575 				     rate);
576 	if (ret != 0) {
577 		LOG_ERR("Failed call clock_control_get_rate: return [%d]", ret);
578 		return -EIO;
579 	}
580 
581 	return 0;
582 }
583 
can_stm32_get_max_filters(const struct device * dev,bool ide)584 static int can_stm32_get_max_filters(const struct device *dev, bool ide)
585 {
586 	ARG_UNUSED(dev);
587 
588 	if (ide) {
589 		return CONFIG_CAN_MAX_EXT_ID_FILTER;
590 	} else {
591 		return CONFIG_CAN_MAX_STD_ID_FILTER;
592 	}
593 }
594 
can_stm32_init(const struct device * dev)595 static int can_stm32_init(const struct device *dev)
596 {
597 	const struct can_stm32_config *cfg = dev->config;
598 	struct can_stm32_data *data = dev->data;
599 	CAN_TypeDef *can = cfg->can;
600 	struct can_timing timing = { 0 };
601 	const struct device *clock;
602 	uint32_t bank_offset;
603 	int ret;
604 
605 	k_mutex_init(&filter_mutex);
606 	k_mutex_init(&data->inst_mutex);
607 	k_sem_init(&data->tx_int_sem, 0, 1);
608 
609 	if (cfg->common.phy != NULL) {
610 		if (!device_is_ready(cfg->common.phy)) {
611 			LOG_ERR("CAN transceiver not ready");
612 			return -ENODEV;
613 		}
614 	}
615 
616 	clock = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
617 	if (!device_is_ready(clock)) {
618 		LOG_ERR("clock control device not ready");
619 		return -ENODEV;
620 	}
621 
622 	ret = clock_control_on(clock, (clock_control_subsys_t) &cfg->pclken);
623 	if (ret != 0) {
624 		LOG_ERR("HAL_CAN_Init clock control on failed: %d", ret);
625 		return -EIO;
626 	}
627 
628 	/* Configure dt provided device signals when available */
629 	ret = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
630 	if (ret < 0) {
631 		LOG_ERR("CAN pinctrl setup failed (%d)", ret);
632 		return ret;
633 	}
634 
635 	ret = can_stm32_enter_init_mode(can);
636 	if (ret) {
637 		LOG_ERR("Failed to enter init mode");
638 		return ret;
639 	}
640 
641 	ret = can_stm32_leave_sleep_mode(can);
642 	if (ret) {
643 		LOG_ERR("Failed to exit sleep mode");
644 		return ret;
645 	}
646 
647 	/* configure scale of filter banks < CONFIG_CAN_MAX_EXT_ID_FILTER for ext ids */
648 	bank_offset = (cfg->can == cfg->master_can) ? 0 : CAN_STM32_NUM_FILTER_BANKS;
649 	cfg->master_can->FMR |= CAN_FMR_FINIT;
650 	cfg->master_can->FS1R |= ((1U << CONFIG_CAN_MAX_EXT_ID_FILTER) - 1) << bank_offset;
651 	cfg->master_can->FMR &= ~CAN_FMR_FINIT;
652 
653 	can->MCR &= ~CAN_MCR_TTCM & ~CAN_MCR_ABOM & ~CAN_MCR_AWUM &
654 		    ~CAN_MCR_NART & ~CAN_MCR_RFLM & ~CAN_MCR_TXFP;
655 #ifdef CONFIG_CAN_RX_TIMESTAMP
656 	can->MCR |= CAN_MCR_TTCM;
657 #endif
658 
659 	/* Enable automatic bus-off recovery */
660 	can->MCR |= CAN_MCR_ABOM;
661 
662 	ret = can_calc_timing(dev, &timing, cfg->common.bitrate,
663 			      cfg->common.sample_point);
664 	if (ret == -EINVAL) {
665 		LOG_ERR("Can't find timing for given param");
666 		return -EIO;
667 	}
668 	LOG_DBG("Presc: %d, TS1: %d, TS2: %d",
669 		timing.prescaler, timing.phase_seg1, timing.phase_seg2);
670 	LOG_DBG("Sample-point err : %d", ret);
671 
672 	ret = can_set_timing(dev, &timing);
673 	if (ret) {
674 		return ret;
675 	}
676 
677 	ret = can_stm32_set_mode(dev, CAN_MODE_NORMAL);
678 	if (ret) {
679 		return ret;
680 	}
681 
682 	(void)can_stm32_get_state(dev, &data->state, NULL);
683 
684 	cfg->config_irq(can);
685 	can->IER |= CAN_IER_TMEIE;
686 
687 	return 0;
688 }
689 
can_stm32_set_state_change_callback(const struct device * dev,can_state_change_callback_t cb,void * user_data)690 static void can_stm32_set_state_change_callback(const struct device *dev,
691 						can_state_change_callback_t cb,
692 						void *user_data)
693 {
694 	struct can_stm32_data *data = dev->data;
695 	const struct can_stm32_config *cfg = dev->config;
696 	CAN_TypeDef *can = cfg->can;
697 
698 	data->common.state_change_cb = cb;
699 	data->common.state_change_cb_user_data = user_data;
700 
701 	if (cb == NULL) {
702 		can->IER &= ~(CAN_IER_BOFIE | CAN_IER_EPVIE | CAN_IER_EWGIE);
703 	} else {
704 		can->IER |= CAN_IER_BOFIE | CAN_IER_EPVIE | CAN_IER_EWGIE;
705 	}
706 }
707 
708 #ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
can_stm32_recover(const struct device * dev,k_timeout_t timeout)709 static int can_stm32_recover(const struct device *dev, k_timeout_t timeout)
710 {
711 	const struct can_stm32_config *cfg = dev->config;
712 	struct can_stm32_data *data = dev->data;
713 	CAN_TypeDef *can = cfg->can;
714 	int ret = -EAGAIN;
715 	int64_t start_time;
716 
717 	if (!data->common.started) {
718 		return -ENETDOWN;
719 	}
720 
721 	if ((data->common.mode & CAN_MODE_MANUAL_RECOVERY) == 0U) {
722 		return -ENOTSUP;
723 	}
724 
725 	if (!(can->ESR & CAN_ESR_BOFF)) {
726 		return 0;
727 	}
728 
729 	if (k_mutex_lock(&data->inst_mutex, K_FOREVER)) {
730 		return -EAGAIN;
731 	}
732 
733 	ret = can_stm32_enter_init_mode(can);
734 	if (ret) {
735 		goto done;
736 	}
737 
738 	can_stm32_leave_init_mode(can);
739 
740 	start_time = k_uptime_ticks();
741 
742 	while (can->ESR & CAN_ESR_BOFF) {
743 		if (!K_TIMEOUT_EQ(timeout, K_FOREVER) &&
744 		    k_uptime_ticks() - start_time >= timeout.ticks) {
745 			goto done;
746 		}
747 	}
748 
749 	ret = 0;
750 
751 done:
752 	k_mutex_unlock(&data->inst_mutex);
753 	return ret;
754 }
755 #endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
756 
can_stm32_send(const struct device * dev,const struct can_frame * frame,k_timeout_t timeout,can_tx_callback_t callback,void * user_data)757 static int can_stm32_send(const struct device *dev, const struct can_frame *frame,
758 			  k_timeout_t timeout, can_tx_callback_t callback,
759 			  void *user_data)
760 {
761 	const struct can_stm32_config *cfg = dev->config;
762 	struct can_stm32_data *data = dev->data;
763 	CAN_TypeDef *can = cfg->can;
764 	uint32_t transmit_status_register = 0;
765 	CAN_TxMailBox_TypeDef *mailbox = NULL;
766 	struct can_stm32_mailbox *mb = NULL;
767 
768 	LOG_DBG("Sending %d bytes on %s. "
769 		    "Id: 0x%x, "
770 		    "ID type: %s, "
771 		    "Remote Frame: %s"
772 		    , frame->dlc, dev->name
773 		    , frame->id
774 		    , (frame->flags & CAN_FRAME_IDE) != 0 ? "extended" : "standard"
775 		    , (frame->flags & CAN_FRAME_RTR) != 0 ? "yes" : "no");
776 
777 	if (frame->dlc > CAN_MAX_DLC) {
778 		LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
779 		return -EINVAL;
780 	}
781 
782 	if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) {
783 		LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
784 		return -ENOTSUP;
785 	}
786 
787 	if (!data->common.started) {
788 		return -ENETDOWN;
789 	}
790 
791 	if (can->ESR & CAN_ESR_BOFF) {
792 		return -ENETUNREACH;
793 	}
794 
795 	k_mutex_lock(&data->inst_mutex, K_FOREVER);
796 	transmit_status_register = can->TSR;
797 	while (!(transmit_status_register & CAN_TSR_TME)) {
798 		k_mutex_unlock(&data->inst_mutex);
799 		LOG_DBG("Transmit buffer full");
800 		if (k_sem_take(&data->tx_int_sem, timeout)) {
801 			return -EAGAIN;
802 		}
803 
804 		k_mutex_lock(&data->inst_mutex, K_FOREVER);
805 		transmit_status_register = can->TSR;
806 	}
807 
808 	if (transmit_status_register & CAN_TSR_TME0) {
809 		LOG_DBG("Using TX mailbox 0");
810 		mailbox = &can->sTxMailBox[0];
811 		mb = &(data->mb0);
812 	} else if (transmit_status_register & CAN_TSR_TME1) {
813 		LOG_DBG("Using TX mailbox 1");
814 		mailbox = &can->sTxMailBox[1];
815 		mb = &data->mb1;
816 	} else if (transmit_status_register & CAN_TSR_TME2) {
817 		LOG_DBG("Using TX mailbox 2");
818 		mailbox = &can->sTxMailBox[2];
819 		mb = &data->mb2;
820 	}
821 
822 	mb->tx_callback = callback;
823 	mb->callback_arg = user_data;
824 
825 	/* mailbox identifier register setup */
826 	mailbox->TIR &= CAN_TI0R_TXRQ;
827 
828 	if ((frame->flags & CAN_FRAME_IDE) != 0) {
829 		mailbox->TIR |= (frame->id << CAN_TI0R_EXID_Pos)
830 				| CAN_TI0R_IDE;
831 	} else {
832 		mailbox->TIR |= (frame->id << CAN_TI0R_STID_Pos);
833 	}
834 
835 	if ((frame->flags & CAN_FRAME_RTR) != 0) {
836 		mailbox->TIR |= CAN_TI1R_RTR;
837 	} else {
838 		mailbox->TDLR = frame->data_32[0];
839 		mailbox->TDHR = frame->data_32[1];
840 	}
841 
842 	mailbox->TDTR = (mailbox->TDTR & ~CAN_TDT1R_DLC) |
843 			((frame->dlc & 0xF) << CAN_TDT1R_DLC_Pos);
844 
845 	mailbox->TIR |= CAN_TI0R_TXRQ;
846 	k_mutex_unlock(&data->inst_mutex);
847 
848 	return 0;
849 }
850 
can_stm32_set_filter_bank(int filter_id,CAN_FilterRegister_TypeDef * filter_reg,bool ide,uint32_t id,uint32_t mask)851 static void can_stm32_set_filter_bank(int filter_id, CAN_FilterRegister_TypeDef *filter_reg,
852 				      bool ide, uint32_t id, uint32_t mask)
853 {
854 	if (ide) {
855 		filter_reg->FR1 = id;
856 		filter_reg->FR2 = mask;
857 	} else {
858 		if ((filter_id - CONFIG_CAN_MAX_EXT_ID_FILTER) % 2 == 0) {
859 			/* even std filter id: first 1/2 bank */
860 			filter_reg->FR1 = id | (mask << 16);
861 		} else {
862 			/* uneven std filter id: first 1/2 bank */
863 			filter_reg->FR2 = id | (mask << 16);
864 		}
865 	}
866 }
867 
can_stm32_filter_to_std_mask(const struct can_filter * filter)868 static inline uint32_t can_stm32_filter_to_std_mask(const struct can_filter *filter)
869 {
870 	uint32_t rtr_mask = !IS_ENABLED(CONFIG_CAN_ACCEPT_RTR);
871 
872 	return  (filter->mask << CAN_STM32_FIRX_STD_ID_POS) |
873 		(rtr_mask << CAN_STM32_FIRX_STD_RTR_POS) |
874 		(1U << CAN_STM32_FIRX_STD_IDE_POS);
875 }
876 
can_stm32_filter_to_ext_mask(const struct can_filter * filter)877 static inline uint32_t can_stm32_filter_to_ext_mask(const struct can_filter *filter)
878 {
879 	uint32_t rtr_mask = !IS_ENABLED(CONFIG_CAN_ACCEPT_RTR);
880 
881 	return  (filter->mask << CAN_STM32_FIRX_EXT_EXT_ID_POS) |
882 		(rtr_mask << CAN_STM32_FIRX_EXT_RTR_POS) |
883 		(1U << CAN_STM32_FIRX_EXT_IDE_POS);
884 }
885 
can_stm32_filter_to_std_id(const struct can_filter * filter)886 static inline uint32_t can_stm32_filter_to_std_id(const struct can_filter *filter)
887 {
888 	return  (filter->id  << CAN_STM32_FIRX_STD_ID_POS);
889 }
890 
can_stm32_filter_to_ext_id(const struct can_filter * filter)891 static inline uint32_t can_stm32_filter_to_ext_id(const struct can_filter *filter)
892 {
893 	return  (filter->id << CAN_STM32_FIRX_EXT_EXT_ID_POS) |
894 		(1U << CAN_STM32_FIRX_EXT_IDE_POS);
895 }
896 
can_stm32_set_filter(const struct device * dev,const struct can_filter * filter)897 static inline int can_stm32_set_filter(const struct device *dev, const struct can_filter *filter)
898 {
899 	const struct can_stm32_config *cfg = dev->config;
900 	struct can_stm32_data *data = dev->data;
901 	CAN_TypeDef *can = cfg->master_can;
902 	uint32_t mask = 0U;
903 	uint32_t id = 0U;
904 	int filter_id = -ENOSPC;
905 	int bank_offset = 0;
906 	int bank_num;
907 
908 	if (cfg->can != cfg->master_can) {
909 		/* CAN slave instance: start with offset */
910 		bank_offset = CAN_STM32_NUM_FILTER_BANKS;
911 	}
912 
913 	if ((filter->flags & CAN_FILTER_IDE) != 0) {
914 		for (int i = 0; i < CONFIG_CAN_MAX_EXT_ID_FILTER; i++) {
915 			if (data->rx_cb_ext[i] == NULL) {
916 				id = can_stm32_filter_to_ext_id(filter);
917 				mask = can_stm32_filter_to_ext_mask(filter);
918 				filter_id = i;
919 				bank_num = bank_offset + i;
920 				break;
921 			}
922 		}
923 	} else {
924 		for (int i = 0; i < CONFIG_CAN_MAX_STD_ID_FILTER; i++) {
925 			if (data->rx_cb_std[i] == NULL) {
926 				id = can_stm32_filter_to_std_id(filter);
927 				mask = can_stm32_filter_to_std_mask(filter);
928 				filter_id = CONFIG_CAN_MAX_EXT_ID_FILTER + i;
929 				bank_num = bank_offset + CONFIG_CAN_MAX_EXT_ID_FILTER + i / 2;
930 				break;
931 			}
932 		}
933 	}
934 
935 	if (filter_id != -ENOSPC) {
936 		LOG_DBG("Adding filter_id %d, CAN ID: 0x%x, mask: 0x%x",
937 			filter_id, filter->id, filter->mask);
938 
939 		/* set the filter init mode */
940 		can->FMR |= CAN_FMR_FINIT;
941 
942 		can_stm32_set_filter_bank(filter_id, &can->sFilterRegister[bank_num],
943 					  (filter->flags & CAN_FILTER_IDE) != 0,
944 					  id, mask);
945 
946 		can->FA1R |= 1U << bank_num;
947 		can->FMR &= ~(CAN_FMR_FINIT);
948 	} else {
949 		LOG_WRN("No free filter left");
950 	}
951 
952 	return filter_id;
953 }
954 
955 
956 /*
957  * This driver uses masked mode for all filters (CAN_FM1R left at reset value
958  * 0x00) in order to simplify mapping between filter match index from the FIFOs
959  * and array index for the callbacks. All ext ID filters are stored in the
960  * banks below CONFIG_CAN_MAX_EXT_ID_FILTER, followed by the std ID filters,
961  * which consume only 1/2 bank per filter.
962  *
963  * The more complicated list mode must be implemented if someone requires more
964  * than 28 std ID or 14 ext ID filters.
965  *
966  * Currently, all filter banks are assigned to FIFO 0 and FIFO 1 is not used.
967  */
can_stm32_add_rx_filter(const struct device * dev,can_rx_callback_t cb,void * cb_arg,const struct can_filter * filter)968 static int can_stm32_add_rx_filter(const struct device *dev, can_rx_callback_t cb,
969 				   void *cb_arg, const struct can_filter *filter)
970 {
971 	struct can_stm32_data *data = dev->data;
972 	int filter_id;
973 
974 	if ((filter->flags & ~(CAN_FILTER_IDE)) != 0) {
975 		LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags);
976 		return -ENOTSUP;
977 	}
978 
979 	k_mutex_lock(&filter_mutex, K_FOREVER);
980 	k_mutex_lock(&data->inst_mutex, K_FOREVER);
981 
982 	filter_id = can_stm32_set_filter(dev, filter);
983 	if (filter_id >= 0) {
984 		if ((filter->flags & CAN_FILTER_IDE) != 0) {
985 			data->rx_cb_ext[filter_id] = cb;
986 			data->cb_arg_ext[filter_id] = cb_arg;
987 		} else {
988 			data->rx_cb_std[filter_id - CONFIG_CAN_MAX_EXT_ID_FILTER] = cb;
989 			data->cb_arg_std[filter_id - CONFIG_CAN_MAX_EXT_ID_FILTER] = cb_arg;
990 		}
991 	}
992 
993 	k_mutex_unlock(&data->inst_mutex);
994 	k_mutex_unlock(&filter_mutex);
995 
996 	return filter_id;
997 }
998 
can_stm32_remove_rx_filter(const struct device * dev,int filter_id)999 static void can_stm32_remove_rx_filter(const struct device *dev, int filter_id)
1000 {
1001 	const struct can_stm32_config *cfg = dev->config;
1002 	struct can_stm32_data *data = dev->data;
1003 	CAN_TypeDef *can = cfg->master_can;
1004 	bool ide;
1005 	int bank_offset = 0;
1006 	int bank_num;
1007 	bool bank_unused;
1008 
1009 	if (filter_id < 0 || filter_id >= CAN_STM32_MAX_FILTER_ID) {
1010 		LOG_ERR("filter ID %d out of bounds", filter_id);
1011 		return;
1012 	}
1013 
1014 	k_mutex_lock(&filter_mutex, K_FOREVER);
1015 	k_mutex_lock(&data->inst_mutex, K_FOREVER);
1016 
1017 	if (cfg->can != cfg->master_can) {
1018 		bank_offset = CAN_STM32_NUM_FILTER_BANKS;
1019 	}
1020 
1021 	if (filter_id < CONFIG_CAN_MAX_EXT_ID_FILTER) {
1022 		ide = true;
1023 		bank_num = bank_offset + filter_id;
1024 
1025 		data->rx_cb_ext[filter_id] = NULL;
1026 		data->cb_arg_ext[filter_id] = NULL;
1027 
1028 		bank_unused = true;
1029 	} else {
1030 		int filter_index = filter_id - CONFIG_CAN_MAX_EXT_ID_FILTER;
1031 
1032 		ide = false;
1033 		bank_num = bank_offset + CONFIG_CAN_MAX_EXT_ID_FILTER +
1034 			  (filter_id - CONFIG_CAN_MAX_EXT_ID_FILTER) / 2;
1035 
1036 		data->rx_cb_std[filter_index] = NULL;
1037 		data->cb_arg_std[filter_index] = NULL;
1038 
1039 		if (filter_index % 2 == 1) {
1040 			bank_unused = data->rx_cb_std[filter_index - 1] == NULL;
1041 		} else if (filter_index + 1 < CONFIG_CAN_MAX_STD_ID_FILTER) {
1042 			bank_unused = data->rx_cb_std[filter_index + 1] == NULL;
1043 		} else {
1044 			bank_unused = true;
1045 		}
1046 	}
1047 
1048 	LOG_DBG("Removing filter_id %d, ide %d", filter_id, ide);
1049 
1050 	can->FMR |= CAN_FMR_FINIT;
1051 
1052 	can_stm32_set_filter_bank(filter_id, &can->sFilterRegister[bank_num],
1053 				  ide, 0, 0xFFFFFFFF);
1054 
1055 	if (bank_unused) {
1056 		can->FA1R &= ~(1U << bank_num);
1057 		LOG_DBG("Filter bank %d is unused -> deactivate", bank_num);
1058 	}
1059 
1060 	can->FMR &= ~(CAN_FMR_FINIT);
1061 
1062 	k_mutex_unlock(&data->inst_mutex);
1063 	k_mutex_unlock(&filter_mutex);
1064 }
1065 
1066 static DEVICE_API(can, can_api_funcs) = {
1067 	.get_capabilities = can_stm32_get_capabilities,
1068 	.start = can_stm32_start,
1069 	.stop = can_stm32_stop,
1070 	.set_mode = can_stm32_set_mode,
1071 	.set_timing = can_stm32_set_timing,
1072 	.send = can_stm32_send,
1073 	.add_rx_filter = can_stm32_add_rx_filter,
1074 	.remove_rx_filter = can_stm32_remove_rx_filter,
1075 	.get_state = can_stm32_get_state,
1076 #ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
1077 	.recover = can_stm32_recover,
1078 #endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
1079 	.set_state_change_callback = can_stm32_set_state_change_callback,
1080 	.get_core_clock = can_stm32_get_core_clock,
1081 	.get_max_filters = can_stm32_get_max_filters,
1082 	.timing_min = {
1083 		.sjw = 0x1,
1084 		.prop_seg = 0x00,
1085 		.phase_seg1 = 0x01,
1086 		.phase_seg2 = 0x01,
1087 		.prescaler = 0x01
1088 	},
1089 	.timing_max = {
1090 		.sjw = 0x04,
1091 		.prop_seg = 0x00,
1092 		.phase_seg1 = 0x10,
1093 		.phase_seg2 = 0x08,
1094 		.prescaler = 0x400
1095 	}
1096 };
1097 
1098 #ifdef CONFIG_SOC_SERIES_STM32F0X
1099 #define CAN_STM32_IRQ_INST(inst)                                     \
1100 static void config_can_##inst##_irq(CAN_TypeDef *can)                \
1101 {                                                                    \
1102 	IRQ_CONNECT(DT_INST_IRQN(inst),                              \
1103 		    DT_INST_IRQ(inst, priority),                     \
1104 		    can_stm32_isr, DEVICE_DT_INST_GET(inst), 0);     \
1105 	irq_enable(DT_INST_IRQN(inst));                              \
1106 	can->IER |= CAN_IER_TMEIE | CAN_IER_ERRIE | CAN_IER_FMPIE0 | \
1107 		    CAN_IER_FMPIE1 | CAN_IER_BOFIE;                  \
1108 	if (IS_ENABLED(CONFIG_CAN_STATS)) {                          \
1109 		can->IER |= CAN_IER_LECIE;                           \
1110 	}                                                            \
1111 }
1112 #else
1113 #define CAN_STM32_IRQ_INST(inst)                                     \
1114 static void config_can_##inst##_irq(CAN_TypeDef *can)                \
1115 {                                                                    \
1116 	IRQ_CONNECT(DT_INST_IRQ_BY_NAME(inst, rx0, irq),             \
1117 		    DT_INST_IRQ_BY_NAME(inst, rx0, priority),        \
1118 		    can_stm32_rx_isr, DEVICE_DT_INST_GET(inst), 0);  \
1119 	irq_enable(DT_INST_IRQ_BY_NAME(inst, rx0, irq));             \
1120 	IRQ_CONNECT(DT_INST_IRQ_BY_NAME(inst, tx, irq),              \
1121 		    DT_INST_IRQ_BY_NAME(inst, tx, priority),         \
1122 		    can_stm32_tx_isr, DEVICE_DT_INST_GET(inst), 0);  \
1123 	irq_enable(DT_INST_IRQ_BY_NAME(inst, tx, irq));              \
1124 	IRQ_CONNECT(DT_INST_IRQ_BY_NAME(inst, sce, irq),             \
1125 		    DT_INST_IRQ_BY_NAME(inst, sce, priority),        \
1126 		    can_stm32_state_change_isr,                      \
1127 		    DEVICE_DT_INST_GET(inst), 0);                    \
1128 	irq_enable(DT_INST_IRQ_BY_NAME(inst, sce, irq));             \
1129 	can->IER |= CAN_IER_TMEIE | CAN_IER_ERRIE | CAN_IER_FMPIE0 | \
1130 		    CAN_IER_FMPIE1 | CAN_IER_BOFIE;                  \
1131 	if (IS_ENABLED(CONFIG_CAN_STATS)) {                          \
1132 		can->IER |= CAN_IER_LECIE;                           \
1133 	}                                                            \
1134 }
1135 #endif /* CONFIG_SOC_SERIES_STM32F0X */
1136 
1137 #define CAN_STM32_CONFIG_INST(inst)                                      \
1138 PINCTRL_DT_INST_DEFINE(inst);                                            \
1139 static const struct can_stm32_config can_stm32_cfg_##inst = {            \
1140 	.common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 1000000),       \
1141 	.can = (CAN_TypeDef *)DT_INST_REG_ADDR(inst),                    \
1142 	.master_can = (CAN_TypeDef *)DT_INST_PROP_OR(inst,               \
1143 		master_can_reg, DT_INST_REG_ADDR(inst)),                 \
1144 	.pclken = {                                                      \
1145 		.enr = DT_INST_CLOCKS_CELL(inst, bits),                  \
1146 		.bus = DT_INST_CLOCKS_CELL(inst, bus),                   \
1147 	},                                                               \
1148 	.config_irq = config_can_##inst##_irq,                           \
1149 	.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst),	                 \
1150 };
1151 
1152 #define CAN_STM32_DATA_INST(inst) \
1153 static struct can_stm32_data can_stm32_dev_data_##inst;
1154 
1155 #define CAN_STM32_DEFINE_INST(inst)                                      \
1156 CAN_DEVICE_DT_INST_DEFINE(inst, can_stm32_init, NULL,                    \
1157 			  &can_stm32_dev_data_##inst, &can_stm32_cfg_##inst, \
1158 			  POST_KERNEL, CONFIG_CAN_INIT_PRIORITY,         \
1159 			  &can_api_funcs);
1160 
1161 #define CAN_STM32_INST(inst)      \
1162 CAN_STM32_IRQ_INST(inst)          \
1163 CAN_STM32_CONFIG_INST(inst)       \
1164 CAN_STM32_DATA_INST(inst)         \
1165 CAN_STM32_DEFINE_INST(inst)
1166 
1167 DT_INST_FOREACH_STATUS_OKAY(CAN_STM32_INST)
1168