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