1 /*
2  * Copyright (c) 2022 Henrik Brix Andersen <henrik@brixandersen.dk>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/can/can_sja1000.h>
8 #include "can_sja1000_priv.h"
9 
10 #include <zephyr/drivers/can.h>
11 #include <zephyr/drivers/can/transceiver.h>
12 #include <zephyr/logging/log.h>
13 
14 LOG_MODULE_REGISTER(can_sja1000, CONFIG_CAN_LOG_LEVEL);
15 
16 /* Timeout for entering/leaving reset mode */
17 #define CAN_SJA1000_RESET_MODE_TIMEOUT_USEC 1000
18 #define CAN_SJA1000_RESET_MODE_RETRIES      100
19 #define CAN_SJA1000_RESET_MODE_DELAY	    \
20 	K_USEC(CAN_SJA1000_RESET_MODE_TIMEOUT_USEC / CAN_SJA1000_RESET_MODE_RETRIES)
21 
can_sja1000_write_reg(const struct device * dev,uint8_t reg,uint8_t val)22 static inline void can_sja1000_write_reg(const struct device *dev, uint8_t reg, uint8_t val)
23 {
24 	const struct can_sja1000_config *config = dev->config;
25 
26 	config->write_reg(dev, reg, val);
27 }
28 
can_sja1000_read_reg(const struct device * dev,uint8_t reg)29 static inline uint8_t can_sja1000_read_reg(const struct device *dev, uint8_t reg)
30 {
31 	const struct can_sja1000_config *config = dev->config;
32 
33 	return config->read_reg(dev, reg);
34 }
35 
can_sja1000_enter_reset_mode(const struct device * dev)36 static inline int can_sja1000_enter_reset_mode(const struct device *dev)
37 {
38 	int retries = CAN_SJA1000_RESET_MODE_RETRIES;
39 	uint8_t mod;
40 
41 	mod = can_sja1000_read_reg(dev, CAN_SJA1000_MOD);
42 
43 	while ((mod & CAN_SJA1000_MOD_RM) == 0) {
44 		if (--retries < 0) {
45 			return -EIO;
46 		}
47 
48 		can_sja1000_write_reg(dev, CAN_SJA1000_MOD, mod | CAN_SJA1000_MOD_RM);
49 		k_sleep(CAN_SJA1000_RESET_MODE_DELAY);
50 		mod = can_sja1000_read_reg(dev, CAN_SJA1000_MOD);
51 	};
52 
53 	return 0;
54 }
55 
can_sja1000_leave_reset_mode_nowait(const struct device * dev)56 static inline void can_sja1000_leave_reset_mode_nowait(const struct device *dev)
57 {
58 	uint8_t mod;
59 
60 	mod = can_sja1000_read_reg(dev, CAN_SJA1000_MOD);
61 	can_sja1000_write_reg(dev, CAN_SJA1000_MOD, mod & ~(CAN_SJA1000_MOD_RM));
62 }
63 
can_sja1000_leave_reset_mode(const struct device * dev)64 static inline int can_sja1000_leave_reset_mode(const struct device *dev)
65 {
66 	int retries = CAN_SJA1000_RESET_MODE_RETRIES;
67 	uint8_t mod;
68 
69 	mod = can_sja1000_read_reg(dev, CAN_SJA1000_MOD);
70 
71 	while ((mod & CAN_SJA1000_MOD_RM) == 1) {
72 		if (--retries < 0) {
73 			return -EIO;
74 		}
75 
76 		can_sja1000_write_reg(dev, CAN_SJA1000_MOD, mod & ~(CAN_SJA1000_MOD_RM));
77 		k_sleep(CAN_SJA1000_RESET_MODE_DELAY);
78 		mod = can_sja1000_read_reg(dev, CAN_SJA1000_MOD);
79 	};
80 
81 	return 0;
82 }
83 
can_sja1000_clear_errors(const struct device * dev)84 static inline void can_sja1000_clear_errors(const struct device *dev)
85 {
86 	/* Clear error counters */
87 	can_sja1000_write_reg(dev, CAN_SJA1000_RXERR, 0);
88 	can_sja1000_write_reg(dev, CAN_SJA1000_TXERR, 0);
89 
90 	/* Clear error capture */
91 	(void)can_sja1000_read_reg(dev, CAN_SJA1000_ECC);
92 }
93 
can_sja1000_tx_done(const struct device * dev,int status)94 static void can_sja1000_tx_done(const struct device *dev, int status)
95 {
96 	struct can_sja1000_data *data = dev->data;
97 	can_tx_callback_t callback = data->tx_callback;
98 	void *user_data = data->tx_user_data;
99 
100 	if (callback != NULL) {
101 		data->tx_callback = NULL;
102 		callback(dev, status, user_data);
103 	}
104 
105 	k_sem_give(&data->tx_idle);
106 }
107 
can_sja1000_set_timing(const struct device * dev,const struct can_timing * timing)108 int can_sja1000_set_timing(const struct device *dev, const struct can_timing *timing)
109 {
110 	struct can_sja1000_data *data = dev->data;
111 	uint8_t btr0;
112 	uint8_t btr1;
113 
114 	if (data->common.started) {
115 		return -EBUSY;
116 	}
117 
118 	k_mutex_lock(&data->mod_lock, K_FOREVER);
119 
120 	btr0 = CAN_SJA1000_BTR0_BRP_PREP(timing->prescaler - 1) |
121 	       CAN_SJA1000_BTR0_SJW_PREP(timing->sjw - 1);
122 	btr1 = CAN_SJA1000_BTR1_TSEG1_PREP(timing->phase_seg1 - 1) |
123 	       CAN_SJA1000_BTR1_TSEG2_PREP(timing->phase_seg2 - 1);
124 
125 	if ((data->common.mode & CAN_MODE_3_SAMPLES) != 0) {
126 		btr1 |= CAN_SJA1000_BTR1_SAM;
127 	}
128 
129 	can_sja1000_write_reg(dev, CAN_SJA1000_BTR0, btr0);
130 	can_sja1000_write_reg(dev, CAN_SJA1000_BTR1, btr1);
131 
132 	k_mutex_unlock(&data->mod_lock);
133 
134 	return 0;
135 }
136 
can_sja1000_get_capabilities(const struct device * dev,can_mode_t * cap)137 int can_sja1000_get_capabilities(const struct device *dev, can_mode_t *cap)
138 {
139 	ARG_UNUSED(dev);
140 
141 	*cap = CAN_MODE_NORMAL | CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY |
142 	       CAN_MODE_ONE_SHOT | CAN_MODE_3_SAMPLES;
143 
144 	if (IS_ENABLED(CONFIG_CAN_MANUAL_RECOVERY_MODE)) {
145 		*cap |= CAN_MODE_MANUAL_RECOVERY;
146 	}
147 
148 	return 0;
149 }
150 
can_sja1000_start(const struct device * dev)151 int can_sja1000_start(const struct device *dev)
152 {
153 	const struct can_sja1000_config *config = dev->config;
154 	struct can_sja1000_data *data = dev->data;
155 	int err;
156 
157 	if (data->common.started) {
158 		return -EALREADY;
159 	}
160 
161 	if (config->common.phy != NULL) {
162 		err = can_transceiver_enable(config->common.phy, data->common.mode);
163 		if (err != 0) {
164 			LOG_ERR("failed to enable CAN transceiver (err %d)", err);
165 			return err;
166 		}
167 	}
168 
169 	can_sja1000_clear_errors(dev);
170 	CAN_STATS_RESET(dev);
171 
172 	err = can_sja1000_leave_reset_mode(dev);
173 	if (err != 0) {
174 		if (config->common.phy != NULL) {
175 			/* Attempt to disable the CAN transceiver in case of error */
176 			(void)can_transceiver_disable(config->common.phy);
177 		}
178 
179 		return err;
180 	}
181 
182 	data->common.started = true;
183 
184 	return 0;
185 }
186 
can_sja1000_stop(const struct device * dev)187 int can_sja1000_stop(const struct device *dev)
188 {
189 	const struct can_sja1000_config *config = dev->config;
190 	struct can_sja1000_data *data = dev->data;
191 	int err;
192 
193 	if (!data->common.started) {
194 		return -EALREADY;
195 	}
196 
197 	/* Entering reset mode aborts current transmission, if any */
198 	err = can_sja1000_enter_reset_mode(dev);
199 	if (err != 0) {
200 		return err;
201 	}
202 
203 	if (config->common.phy != NULL) {
204 		err = can_transceiver_disable(config->common.phy);
205 		if (err != 0) {
206 			LOG_ERR("failed to disable CAN transceiver (err %d)", err);
207 			return err;
208 		}
209 	}
210 
211 	data->common.started = false;
212 
213 	can_sja1000_tx_done(dev, -ENETDOWN);
214 
215 	return 0;
216 }
217 
can_sja1000_set_mode(const struct device * dev,can_mode_t mode)218 int can_sja1000_set_mode(const struct device *dev, can_mode_t mode)
219 {
220 	can_mode_t supported = CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY | CAN_MODE_ONE_SHOT |
221 		CAN_MODE_3_SAMPLES;
222 	struct can_sja1000_data *data = dev->data;
223 	uint8_t btr1;
224 	uint8_t mod;
225 
226 	if (IS_ENABLED(CONFIG_CAN_MANUAL_RECOVERY_MODE)) {
227 		supported |= CAN_MODE_MANUAL_RECOVERY;
228 	}
229 
230 	if ((mode & ~(supported)) != 0) {
231 		LOG_ERR("unsupported mode: 0x%08x", mode);
232 		return -ENOTSUP;
233 	}
234 
235 	if (data->common.started) {
236 		return -EBUSY;
237 	}
238 
239 	k_mutex_lock(&data->mod_lock, K_FOREVER);
240 
241 	mod = can_sja1000_read_reg(dev, CAN_SJA1000_MOD);
242 	mod |= CAN_SJA1000_MOD_AFM;
243 
244 	if ((mode & CAN_MODE_LOOPBACK) != 0) {
245 		/* (Local) self test mode */
246 		mod |= CAN_SJA1000_MOD_STM;
247 	} else {
248 		mod &= ~(CAN_SJA1000_MOD_STM);
249 	}
250 
251 	if ((mode & CAN_MODE_LISTENONLY) != 0) {
252 		mod |= CAN_SJA1000_MOD_LOM;
253 	} else {
254 		mod &= ~(CAN_SJA1000_MOD_LOM);
255 	}
256 
257 	btr1 = can_sja1000_read_reg(dev, CAN_SJA1000_BTR1);
258 	if ((mode & CAN_MODE_3_SAMPLES) != 0) {
259 		btr1 |= CAN_SJA1000_BTR1_SAM;
260 	} else {
261 		btr1 &= ~(CAN_SJA1000_BTR1_SAM);
262 	}
263 
264 	can_sja1000_write_reg(dev, CAN_SJA1000_MOD, mod);
265 	can_sja1000_write_reg(dev, CAN_SJA1000_BTR1, btr1);
266 
267 	data->common.mode = mode;
268 
269 	k_mutex_unlock(&data->mod_lock);
270 
271 	return 0;
272 }
273 
can_sja1000_read_frame(const struct device * dev,struct can_frame * frame)274 static void can_sja1000_read_frame(const struct device *dev, struct can_frame *frame)
275 {
276 	uint32_t id;
277 	uint8_t info;
278 	int i;
279 
280 	memset(frame, 0, sizeof(*frame));
281 
282 	info = can_sja1000_read_reg(dev, CAN_SJA1000_FRAME_INFO);
283 
284 	if ((info & CAN_SJA1000_FRAME_INFO_RTR) != 0) {
285 		frame->flags |= CAN_FRAME_RTR;
286 	}
287 
288 	frame->dlc = CAN_SJA1000_FRAME_INFO_DLC_GET(info);
289 	if (frame->dlc > CAN_MAX_DLC) {
290 		LOG_ERR("RX frame DLC %u exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
291 		return;
292 	}
293 
294 	if ((info & CAN_SJA1000_FRAME_INFO_FF) != 0) {
295 		frame->flags |= CAN_FRAME_IDE;
296 
297 		id = FIELD_PREP(GENMASK(28, 21),
298 				can_sja1000_read_reg(dev, CAN_SJA1000_XFF_ID1));
299 		id |= FIELD_PREP(GENMASK(20, 13),
300 				 can_sja1000_read_reg(dev, CAN_SJA1000_XFF_ID2));
301 		id |= FIELD_PREP(GENMASK(12, 5),
302 				 can_sja1000_read_reg(dev, CAN_SJA1000_EFF_ID3));
303 		id |= FIELD_PREP(GENMASK(4, 0),
304 				 can_sja1000_read_reg(dev, CAN_SJA1000_EFF_ID4) >> 3);
305 		frame->id = id;
306 
307 		if ((frame->flags & CAN_FRAME_RTR) == 0U) {
308 			for (i = 0; i < frame->dlc; i++) {
309 				frame->data[i] = can_sja1000_read_reg(dev, CAN_SJA1000_EFF_DATA +
310 								      i);
311 			}
312 		}
313 	} else {
314 		id = FIELD_PREP(GENMASK(10, 3),
315 				can_sja1000_read_reg(dev, CAN_SJA1000_XFF_ID1));
316 		id |= FIELD_PREP(GENMASK(2, 0),
317 				 can_sja1000_read_reg(dev, CAN_SJA1000_XFF_ID2) >> 5);
318 		frame->id = id;
319 
320 		if ((frame->flags & CAN_FRAME_RTR) == 0U) {
321 			for (i = 0; i < frame->dlc; i++) {
322 				frame->data[i] = can_sja1000_read_reg(dev, CAN_SJA1000_SFF_DATA +
323 								      i);
324 			}
325 		}
326 	}
327 }
328 
can_sja1000_write_frame(const struct device * dev,const struct can_frame * frame)329 void can_sja1000_write_frame(const struct device *dev, const struct can_frame *frame)
330 {
331 	uint32_t id;
332 	uint8_t info;
333 	int i;
334 
335 	info = CAN_SJA1000_FRAME_INFO_DLC_PREP(frame->dlc);
336 
337 	if ((frame->flags & CAN_FRAME_RTR) != 0) {
338 		info |= CAN_SJA1000_FRAME_INFO_RTR;
339 	}
340 
341 	if ((frame->flags & CAN_FRAME_IDE) != 0) {
342 		info |= CAN_SJA1000_FRAME_INFO_FF;
343 	}
344 
345 	can_sja1000_write_reg(dev, CAN_SJA1000_FRAME_INFO, info);
346 
347 	if ((frame->flags & CAN_FRAME_IDE) != 0) {
348 		id = frame->id;
349 		can_sja1000_write_reg(dev, CAN_SJA1000_XFF_ID1,
350 				      FIELD_GET(GENMASK(28, 21), id));
351 		can_sja1000_write_reg(dev, CAN_SJA1000_XFF_ID2,
352 				      FIELD_GET(GENMASK(20, 13), id));
353 		can_sja1000_write_reg(dev, CAN_SJA1000_EFF_ID3,
354 				      FIELD_GET(GENMASK(12, 5), id));
355 		can_sja1000_write_reg(dev, CAN_SJA1000_EFF_ID4,
356 				      FIELD_GET(GENMASK(4, 0), id) << 3);
357 
358 		if ((frame->flags & CAN_FRAME_RTR) == 0U) {
359 			for (i = 0; i < frame->dlc; i++) {
360 				can_sja1000_write_reg(dev, CAN_SJA1000_EFF_DATA + i,
361 						      frame->data[i]);
362 			}
363 		}
364 	} else {
365 		id = frame->id;
366 		can_sja1000_write_reg(dev, CAN_SJA1000_XFF_ID1,
367 				      FIELD_GET(GENMASK(10, 3), id));
368 		can_sja1000_write_reg(dev, CAN_SJA1000_XFF_ID2,
369 				      FIELD_GET(GENMASK(2, 0), id) << 5);
370 
371 		if ((frame->flags & CAN_FRAME_RTR) == 0U) {
372 			for (i = 0; i < frame->dlc; i++) {
373 				can_sja1000_write_reg(dev, CAN_SJA1000_SFF_DATA + i,
374 						      frame->data[i]);
375 			}
376 		}
377 	}
378 }
379 
can_sja1000_send(const struct device * dev,const struct can_frame * frame,k_timeout_t timeout,can_tx_callback_t callback,void * user_data)380 int can_sja1000_send(const struct device *dev, const struct can_frame *frame, k_timeout_t timeout,
381 		     can_tx_callback_t callback, void *user_data)
382 {
383 	struct can_sja1000_data *data = dev->data;
384 	uint8_t cmr;
385 	uint8_t sr;
386 
387 	if (frame->dlc > CAN_MAX_DLC) {
388 		LOG_ERR("TX frame DLC %u exceeds maximum (%d)", frame->dlc, CAN_MAX_DLC);
389 		return -EINVAL;
390 	}
391 
392 	if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) {
393 		LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
394 		return -ENOTSUP;
395 	}
396 
397 	if (!data->common.started) {
398 		return -ENETDOWN;
399 	}
400 
401 	if (data->state == CAN_STATE_BUS_OFF) {
402 		LOG_DBG("transmit failed, bus-off");
403 		return -ENETUNREACH;
404 	}
405 
406 	if (k_sem_take(&data->tx_idle, timeout) != 0) {
407 		return -EAGAIN;
408 	}
409 
410 	sr = can_sja1000_read_reg(dev, CAN_SJA1000_SR);
411 	if ((sr & CAN_SJA1000_SR_TBS) == 0) {
412 		LOG_ERR("transmit buffer locked, sr = 0x%02x", sr);
413 		return -EIO;
414 	}
415 
416 	data->tx_callback = callback;
417 	data->tx_user_data = user_data;
418 
419 	can_sja1000_write_frame(dev, frame);
420 
421 	if ((data->common.mode & CAN_MODE_LOOPBACK) != 0) {
422 		cmr = CAN_SJA1000_CMR_SRR;
423 	} else {
424 		cmr = CAN_SJA1000_CMR_TR;
425 	}
426 
427 	if ((data->common.mode & CAN_MODE_ONE_SHOT) != 0) {
428 		cmr |= CAN_SJA1000_CMR_AT;
429 	}
430 
431 	can_sja1000_write_reg(dev, CAN_SJA1000_CMR, cmr);
432 
433 	return 0;
434 }
435 
can_sja1000_add_rx_filter(const struct device * dev,can_rx_callback_t callback,void * user_data,const struct can_filter * filter)436 int can_sja1000_add_rx_filter(const struct device *dev, can_rx_callback_t callback, void *user_data,
437 			      const struct can_filter *filter)
438 {
439 	struct can_sja1000_data *data = dev->data;
440 	int filter_id = -ENOSPC;
441 	int i;
442 
443 	if ((filter->flags & ~(CAN_FILTER_IDE)) != 0) {
444 		LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags);
445 		return -ENOTSUP;
446 	}
447 
448 	for (i = 0; i < ARRAY_SIZE(data->filters); i++) {
449 		if (!atomic_test_and_set_bit(data->rx_allocs, i)) {
450 			filter_id = i;
451 			break;
452 		}
453 	}
454 
455 	if (filter_id >= 0) {
456 		data->filters[filter_id].filter = *filter;
457 		data->filters[filter_id].user_data = user_data;
458 		data->filters[filter_id].callback = callback;
459 	}
460 
461 	return filter_id;
462 }
463 
can_sja1000_remove_rx_filter(const struct device * dev,int filter_id)464 void can_sja1000_remove_rx_filter(const struct device *dev, int filter_id)
465 {
466 	struct can_sja1000_data *data = dev->data;
467 
468 	if (filter_id < 0 || filter_id >= ARRAY_SIZE(data->filters)) {
469 		LOG_ERR("filter ID %d out of bounds", filter_id);
470 		return;
471 	}
472 
473 	if (atomic_test_and_clear_bit(data->rx_allocs, filter_id)) {
474 		data->filters[filter_id].callback = NULL;
475 		data->filters[filter_id].user_data = NULL;
476 		data->filters[filter_id].filter = (struct can_filter){0};
477 	}
478 }
479 
480 #ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
can_sja1000_recover(const struct device * dev,k_timeout_t timeout)481 int can_sja1000_recover(const struct device *dev, k_timeout_t timeout)
482 {
483 	struct can_sja1000_data *data = dev->data;
484 	int64_t start_ticks;
485 	uint8_t sr;
486 	int err;
487 
488 	if (!data->common.started) {
489 		return -ENETDOWN;
490 	}
491 
492 	if ((data->common.mode & CAN_MODE_MANUAL_RECOVERY) == 0U) {
493 		return -ENOTSUP;
494 	}
495 
496 	sr = can_sja1000_read_reg(dev, CAN_SJA1000_SR);
497 	if ((sr & CAN_SJA1000_SR_BS) == 0) {
498 		return 0;
499 	}
500 
501 	start_ticks = k_uptime_ticks();
502 
503 	err = k_mutex_lock(&data->mod_lock, timeout);
504 	if (err != 0) {
505 		LOG_WRN("failed to acquire MOD lock");
506 		return err;
507 	}
508 
509 	err = can_sja1000_leave_reset_mode(dev);
510 	if (err != 0) {
511 		LOG_ERR("failed to initiate bus recovery");
512 		k_mutex_unlock(&data->mod_lock);
513 		return err;
514 	}
515 
516 	k_mutex_unlock(&data->mod_lock);
517 
518 	while ((sr & CAN_SJA1000_SR_BS) != 0) {
519 		if (k_uptime_ticks() - start_ticks > timeout.ticks) {
520 			LOG_WRN("bus recovery timed out");
521 			return -EAGAIN;
522 		}
523 
524 		sr = can_sja1000_read_reg(dev, CAN_SJA1000_SR);
525 	}
526 
527 	return 0;
528 }
529 #endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
530 
can_sja1000_get_state(const struct device * dev,enum can_state * state,struct can_bus_err_cnt * err_cnt)531 int can_sja1000_get_state(const struct device *dev, enum can_state *state,
532 			  struct can_bus_err_cnt *err_cnt)
533 {
534 	struct can_sja1000_data *data = dev->data;
535 
536 	if (state != NULL) {
537 		if (!data->common.started) {
538 			*state = CAN_STATE_STOPPED;
539 		} else {
540 			*state = data->state;
541 		}
542 	}
543 
544 	if (err_cnt != NULL) {
545 		err_cnt->rx_err_cnt = can_sja1000_read_reg(dev, CAN_SJA1000_RXERR);
546 		err_cnt->tx_err_cnt = can_sja1000_read_reg(dev, CAN_SJA1000_TXERR);
547 	}
548 
549 	return 0;
550 }
551 
can_sja1000_set_state_change_callback(const struct device * dev,can_state_change_callback_t callback,void * user_data)552 void can_sja1000_set_state_change_callback(const struct device *dev,
553 					   can_state_change_callback_t callback, void *user_data)
554 {
555 	struct can_sja1000_data *data = dev->data;
556 
557 	data->common.state_change_cb = callback;
558 	data->common.state_change_cb_user_data = user_data;
559 }
560 
can_sja1000_get_max_filters(const struct device * dev,bool ide)561 int can_sja1000_get_max_filters(const struct device *dev, bool ide)
562 {
563 	ARG_UNUSED(dev);
564 	ARG_UNUSED(ide);
565 
566 	return CONFIG_CAN_MAX_FILTER;
567 }
568 
can_sja1000_handle_receive_irq(const struct device * dev)569 static void can_sja1000_handle_receive_irq(const struct device *dev)
570 {
571 	struct can_sja1000_data *data = dev->data;
572 	struct can_frame frame;
573 	can_rx_callback_t callback;
574 	uint8_t sr;
575 	int i;
576 
577 	do {
578 		can_sja1000_read_frame(dev, &frame);
579 
580 #ifndef CONFIG_CAN_ACCEPT_RTR
581 		if ((frame.flags & CAN_FRAME_RTR) == 0U) {
582 #endif /* !CONFIG_CAN_ACCEPT_RTR */
583 			for (i = 0; i < ARRAY_SIZE(data->filters); i++) {
584 				if (!atomic_test_bit(data->rx_allocs, i)) {
585 					continue;
586 				}
587 
588 				if (can_frame_matches_filter(&frame, &data->filters[i].filter)) {
589 					callback = data->filters[i].callback;
590 					if (callback != NULL) {
591 						callback(dev, &frame, data->filters[i].user_data);
592 					}
593 				}
594 			}
595 #ifndef CONFIG_CAN_ACCEPT_RTR
596 		}
597 #endif /* !CONFIG_CAN_ACCEPT_RTR */
598 
599 		can_sja1000_write_reg(dev, CAN_SJA1000_CMR, CAN_SJA1000_CMR_RRB);
600 		sr = can_sja1000_read_reg(dev, CAN_SJA1000_SR);
601 	} while ((sr & CAN_SJA1000_SR_RBS) != 0);
602 }
603 
can_sja1000_handle_transmit_irq(const struct device * dev)604 static void can_sja1000_handle_transmit_irq(const struct device *dev)
605 {
606 	int status = 0;
607 	uint8_t sr;
608 
609 	sr = can_sja1000_read_reg(dev, CAN_SJA1000_SR);
610 	if ((sr & CAN_SJA1000_SR_TCS) == 0) {
611 		status = -EIO;
612 	}
613 
614 	can_sja1000_tx_done(dev, status);
615 }
616 
617 #ifdef CONFIG_CAN_STATS
can_sja1000_handle_data_overrun_irq(const struct device * dev)618 static void can_sja1000_handle_data_overrun_irq(const struct device *dev)
619 {
620 	/* See NXP SJA1000 Application Note AN97076 (figure 18) for data overrun details */
621 
622 	CAN_STATS_RX_OVERRUN_INC(dev);
623 
624 	can_sja1000_write_reg(dev, CAN_SJA1000_CMR, CAN_SJA1000_CMR_CDO);
625 }
626 
can_sja1000_handle_bus_error_irq(const struct device * dev)627 static void can_sja1000_handle_bus_error_irq(const struct device *dev)
628 {
629 	/* See NXP SJA1000 Application Note AN97076 (tables 6 and 7) for ECC details */
630 	uint8_t ecc;
631 
632 	/* Read the Error Code Capture register to re-activate it */
633 	ecc = can_sja1000_read_reg(dev, CAN_SJA1000_ECC);
634 
635 	if (ecc == (CAN_SJA1000_ECC_ERRC_OTHER_ERROR | CAN_SJA1000_ECC_DIR_TX |
636 		CAN_SJA1000_ECC_SEG_ACK_SLOT)) {
637 		/* Missing ACK is reported as a TX "other" error in the ACK slot */
638 		CAN_STATS_ACK_ERROR_INC(dev);
639 		return;
640 	}
641 
642 	if (ecc == (CAN_SJA1000_ECC_ERRC_FORM_ERROR | CAN_SJA1000_ECC_DIR_RX |
643 		CAN_SJA1000_ECC_SEG_ACK_DELIM)) {
644 		/* CRC error is reported as a RX "form" error in the ACK delimiter */
645 		CAN_STATS_CRC_ERROR_INC(dev);
646 		return;
647 	}
648 
649 	switch (ecc & CAN_SJA1000_ECC_ERRC_MASK) {
650 	case CAN_SJA1000_ECC_ERRC_BIT_ERROR:
651 		CAN_STATS_BIT_ERROR_INC(dev);
652 		break;
653 
654 	case CAN_SJA1000_ECC_ERRC_FORM_ERROR:
655 		CAN_STATS_FORM_ERROR_INC(dev);
656 		break;
657 	case CAN_SJA1000_ECC_ERRC_STUFF_ERROR:
658 		CAN_STATS_STUFF_ERROR_INC(dev);
659 		break;
660 
661 	case CAN_SJA1000_ECC_ERRC_OTHER_ERROR:
662 		__fallthrough;
663 	default:
664 		/* Other error not currently reported in CAN statistics */
665 		break;
666 	}
667 }
668 #endif /* CONFIG_CAN_STATS */
669 
can_sja1000_handle_error_warning_irq(const struct device * dev)670 static void can_sja1000_handle_error_warning_irq(const struct device *dev)
671 {
672 	struct can_sja1000_data *data = dev->data;
673 	uint8_t sr;
674 
675 	sr = can_sja1000_read_reg(dev, CAN_SJA1000_SR);
676 	if ((sr & CAN_SJA1000_SR_BS) != 0) {
677 		data->state = CAN_STATE_BUS_OFF;
678 		can_sja1000_tx_done(dev, -ENETUNREACH);
679 
680 		if (data->common.started &&
681 			(data->common.mode & CAN_MODE_MANUAL_RECOVERY) == 0U) {
682 			can_sja1000_leave_reset_mode_nowait(dev);
683 		}
684 	} else if ((sr & CAN_SJA1000_SR_ES) != 0) {
685 		data->state = CAN_STATE_ERROR_WARNING;
686 	} else {
687 		data->state = CAN_STATE_ERROR_ACTIVE;
688 	}
689 }
690 
can_sja1000_handle_error_passive_irq(const struct device * dev)691 static void can_sja1000_handle_error_passive_irq(const struct device *dev)
692 {
693 	struct can_sja1000_data *data = dev->data;
694 
695 	if (data->state == CAN_STATE_ERROR_PASSIVE) {
696 		data->state = CAN_STATE_ERROR_WARNING;
697 	} else {
698 		data->state = CAN_STATE_ERROR_PASSIVE;
699 	}
700 }
701 
can_sja1000_isr(const struct device * dev)702 void can_sja1000_isr(const struct device *dev)
703 {
704 	struct can_sja1000_data *data = dev->data;
705 	const can_state_change_callback_t cb = data->common.state_change_cb;
706 	void *cb_data = data->common.state_change_cb_user_data;
707 	enum can_state prev_state = data->state;
708 	struct can_bus_err_cnt err_cnt;
709 	uint8_t ir;
710 
711 	ir = can_sja1000_read_reg(dev, CAN_SJA1000_IR);
712 
713 	if ((ir & CAN_SJA1000_IR_TI) != 0) {
714 		can_sja1000_handle_transmit_irq(dev);
715 	}
716 
717 	if ((ir & CAN_SJA1000_IR_RI) != 0) {
718 		can_sja1000_handle_receive_irq(dev);
719 	}
720 
721 #ifdef CONFIG_CAN_STATS
722 	if ((ir & CAN_SJA1000_IR_DOI) != 0) {
723 		can_sja1000_handle_data_overrun_irq(dev);
724 	}
725 
726 	if ((ir & CAN_SJA1000_IR_BEI) != 0) {
727 		can_sja1000_handle_bus_error_irq(dev);
728 	}
729 #endif /* CONFIG_CAN_STATS */
730 
731 	if ((ir & CAN_SJA1000_IR_EI) != 0) {
732 		can_sja1000_handle_error_warning_irq(dev);
733 	}
734 
735 	if ((ir & CAN_SJA1000_IR_EPI) != 0) {
736 		can_sja1000_handle_error_passive_irq(dev);
737 	}
738 
739 	if (prev_state != data->state && cb != NULL) {
740 		err_cnt.rx_err_cnt = can_sja1000_read_reg(dev, CAN_SJA1000_RXERR);
741 		err_cnt.tx_err_cnt = can_sja1000_read_reg(dev, CAN_SJA1000_TXERR);
742 		cb(dev, data->state, err_cnt, cb_data);
743 	}
744 }
745 
can_sja1000_init(const struct device * dev)746 int can_sja1000_init(const struct device *dev)
747 {
748 	const struct can_sja1000_config *config = dev->config;
749 	struct can_sja1000_data *data = dev->data;
750 	struct can_timing timing = { 0 };
751 	int err;
752 
753 	__ASSERT_NO_MSG(config->read_reg != NULL);
754 	__ASSERT_NO_MSG(config->write_reg != NULL);
755 
756 	if (config->common.phy != NULL) {
757 		if (!device_is_ready(config->common.phy)) {
758 			LOG_ERR("CAN transceiver not ready");
759 			return -ENODEV;
760 		}
761 	}
762 
763 	k_mutex_init(&data->mod_lock);
764 	k_sem_init(&data->tx_idle, 1, 1);
765 
766 	data->state = CAN_STATE_ERROR_ACTIVE;
767 
768 	/* See NXP SJA1000 Application Note AN97076 (figure 12) for initialization sequence */
769 
770 	/* Enter reset mode */
771 	err = can_sja1000_enter_reset_mode(dev);
772 	if (err != 0) {
773 		return err;
774 	}
775 
776 	/* Set PeliCAN mode */
777 	can_sja1000_write_reg(dev, CAN_SJA1000_CDR, config->cdr | CAN_SJA1000_CDR_CAN_MODE);
778 
779 	/* Set up acceptance code and mask to match any frame (software filtering) */
780 	can_sja1000_write_reg(dev, CAN_SJA1000_ACR0, 0x00);
781 	can_sja1000_write_reg(dev, CAN_SJA1000_ACR1, 0x00);
782 	can_sja1000_write_reg(dev, CAN_SJA1000_ACR2, 0x00);
783 	can_sja1000_write_reg(dev, CAN_SJA1000_ACR3, 0x00);
784 
785 	can_sja1000_write_reg(dev, CAN_SJA1000_AMR0, 0xFF);
786 	can_sja1000_write_reg(dev, CAN_SJA1000_AMR1, 0xFF);
787 	can_sja1000_write_reg(dev, CAN_SJA1000_AMR2, 0xFF);
788 	can_sja1000_write_reg(dev, CAN_SJA1000_AMR3, 0xFF);
789 
790 	err = can_calc_timing(dev, &timing, config->common.bitrate,
791 			      config->common.sample_point);
792 	if (err == -EINVAL) {
793 		LOG_ERR("bitrate/sample point cannot be met (err %d)", err);
794 		return err;
795 	}
796 
797 	LOG_DBG("initial sample point error: %d", err);
798 
799 	/* Configure timing */
800 	err = can_set_timing(dev, &timing);
801 	if (err != 0) {
802 		LOG_ERR("timing parameters cannot be met (err %d)", err);
803 		return err;
804 	}
805 
806 	/* Set output control */
807 	can_sja1000_write_reg(dev, CAN_SJA1000_OCR, config->ocr);
808 
809 	/* Clear error counters and error capture */
810 	can_sja1000_clear_errors(dev);
811 
812 	/* Set error warning limit */
813 	can_sja1000_write_reg(dev, CAN_SJA1000_EWLR, 96);
814 
815 	/* Set normal mode */
816 	data->common.mode = CAN_MODE_NORMAL;
817 	err = can_sja1000_set_mode(dev, CAN_MODE_NORMAL);
818 	if (err != 0) {
819 		return err;
820 	}
821 
822 	/* Enable interrupts */
823 	can_sja1000_write_reg(dev, CAN_SJA1000_IER,
824 #ifdef CONFIG_CAN_STATS
825 			      CAN_SJA1000_IER_BEIE | CAN_SJA1000_IER_DOIE |
826 #endif /* CONFIG_CAN_STATS */
827 			      CAN_SJA1000_IER_RIE | CAN_SJA1000_IER_TIE |
828 			      CAN_SJA1000_IER_EIE | CAN_SJA1000_IER_EPIE);
829 
830 	return 0;
831 }
832