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