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