Lines Matching +full:timing +full:- +full:config +full:- +full:mask
5 * SPDX-License-Identifier: Apache-2.0
27 if ((src->flags & CAN_FRAME_IDE) != 0) { in mcp251xfd_canframe_to_txobj()
28 dst->id = FIELD_PREP(MCP251XFD_OBJ_ID_SID_MASK, src->id >> 18); in mcp251xfd_canframe_to_txobj()
29 dst->id |= FIELD_PREP(MCP251XFD_OBJ_ID_EID_MASK, src->id); in mcp251xfd_canframe_to_txobj()
31 dst->flags |= MCP251XFD_OBJ_FLAGS_IDE; in mcp251xfd_canframe_to_txobj()
33 dst->id = FIELD_PREP(MCP251XFD_OBJ_ID_SID_MASK, src->id); in mcp251xfd_canframe_to_txobj()
36 if ((src->flags & CAN_FRAME_BRS) != 0) { in mcp251xfd_canframe_to_txobj()
37 dst->flags |= MCP251XFD_OBJ_FLAGS_BRS; in mcp251xfd_canframe_to_txobj()
40 dst->flags |= FIELD_PREP(MCP251XFD_OBJ_FLAGS_DLC_MASK, src->dlc); in mcp251xfd_canframe_to_txobj()
42 if ((src->flags & CAN_FRAME_FDF) != 0) { in mcp251xfd_canframe_to_txobj()
43 dst->flags |= MCP251XFD_OBJ_FLAGS_FDF; in mcp251xfd_canframe_to_txobj()
46 dst->flags |= FIELD_PREP(MCP251XFD_OBJ_FLAGS_SEQ_MASK, mailbox_idx); in mcp251xfd_canframe_to_txobj()
48 dst->id = sys_cpu_to_le32(dst->id); in mcp251xfd_canframe_to_txobj()
49 dst->flags = sys_cpu_to_le32(dst->flags); in mcp251xfd_canframe_to_txobj()
51 if ((src->flags & CAN_FRAME_RTR) != 0) { in mcp251xfd_canframe_to_txobj()
52 dst->flags |= MCP251XFD_OBJ_FLAGS_RTR; in mcp251xfd_canframe_to_txobj()
54 memcpy(dst->data, src->data, MIN(can_dlc_to_bytes(src->dlc), CAN_MAX_DLEN)); in mcp251xfd_canframe_to_txobj()
60 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_read_reg()
61 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_read_reg()
62 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_read_reg()
67 memcpy(&spi_data->header[1], &spi_cmd, sizeof(spi_cmd)); in mcp251xfd_read_reg()
69 struct spi_buf tx_buf = {.buf = &spi_data->header[1], .len = MCP251XFD_SPI_CMD_LEN + len}; in mcp251xfd_read_reg()
70 struct spi_buf rx_buf = {.buf = &spi_data->header[1], .len = MCP251XFD_SPI_CMD_LEN + len}; in mcp251xfd_read_reg()
75 ret = spi_transceive_dt(&dev_cfg->bus, &tx, &rx); in mcp251xfd_read_reg()
80 return &spi_data->buf[0]; in mcp251xfd_read_reg()
85 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_read_crc()
86 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_read_crc()
87 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_read_crc()
91 while (num_retries-- > 0) { in mcp251xfd_read_crc()
94 struct spi_buf tx_buf = {.buf = &spi_data->header[0], in mcp251xfd_read_crc()
99 struct spi_buf rx_buf = {.buf = &spi_data->header[0], in mcp251xfd_read_crc()
108 memcpy(&spi_data->header[0], &spi_cmd, sizeof(spi_cmd)); in mcp251xfd_read_crc()
109 spi_data->header[2] = len; in mcp251xfd_read_crc()
116 (uint8_t *)(&spi_data->header[0]), in mcp251xfd_read_crc()
119 ret = spi_transceive_dt(&dev_cfg->bus, &tx, &rx); in mcp251xfd_read_crc()
125 crc = crc16(MCP251XFD_CRC_POLY, crc_in, &spi_data->buf[0], in mcp251xfd_read_crc()
128 return &spi_data->buf[0]; in mcp251xfd_read_crc()
137 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_get_spi_buf_ptr()
138 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_get_spi_buf_ptr()
140 return &spi_data->buf[0]; in mcp251xfd_get_spi_buf_ptr()
145 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_write()
146 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_write()
147 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_write()
150 struct spi_buf tx_buf = {.buf = &spi_data->header[1], .len = MCP251XFD_SPI_CMD_LEN + len}; in mcp251xfd_write()
154 memcpy(&spi_data->header[1], &spi_cmd, sizeof(spi_cmd)); in mcp251xfd_write()
156 return spi_write_dt(&dev_cfg->bus, &tx); in mcp251xfd_write()
173 return -EINVAL; in mcp251xfd_fifo_write()
178 return -ENOMEM; in mcp251xfd_fifo_write()
187 if ((msg->flags & CAN_FRAME_RTR) == 0) { in mcp251xfd_fifo_write()
188 tx_len += ROUND_UP(can_dlc_to_bytes(msg->dlc), MCP251XFD_RAM_ALIGNMENT); in mcp251xfd_fifo_write()
207 src->id = sys_le32_to_cpu(src->id); in mcp251xfd_rxobj_to_canframe()
208 src->flags = sys_le32_to_cpu(src->flags); in mcp251xfd_rxobj_to_canframe()
210 if ((src->flags & MCP251XFD_OBJ_FLAGS_IDE) != 0) { in mcp251xfd_rxobj_to_canframe()
211 dst->id = FIELD_GET(MCP251XFD_OBJ_ID_EID_MASK, src->id); in mcp251xfd_rxobj_to_canframe()
212 dst->id |= FIELD_GET(MCP251XFD_OBJ_ID_SID_MASK, src->id) << 18; in mcp251xfd_rxobj_to_canframe()
213 dst->flags |= CAN_FRAME_IDE; in mcp251xfd_rxobj_to_canframe()
215 dst->id = FIELD_GET(MCP251XFD_OBJ_ID_SID_MASK, src->id); in mcp251xfd_rxobj_to_canframe()
218 if ((src->flags & MCP251XFD_OBJ_FLAGS_BRS) != 0) { in mcp251xfd_rxobj_to_canframe()
219 dst->flags |= CAN_FRAME_BRS; in mcp251xfd_rxobj_to_canframe()
223 if ((src->flags & MCP251XFD_OBJ_FLAGS_FDF) != 0) { in mcp251xfd_rxobj_to_canframe()
224 dst->flags |= CAN_FRAME_FDF; in mcp251xfd_rxobj_to_canframe()
228 dst->dlc = FIELD_GET(MCP251XFD_OBJ_FLAGS_DLC_MASK, src->flags); in mcp251xfd_rxobj_to_canframe()
231 dst->timestamp = sys_le32_to_cpu(src->timestamp); in mcp251xfd_rxobj_to_canframe()
234 if ((src->flags & MCP251XFD_OBJ_FLAGS_RTR) != 0) { in mcp251xfd_rxobj_to_canframe()
235 dst->flags |= CAN_FRAME_RTR; in mcp251xfd_rxobj_to_canframe()
237 memcpy(dst->data, src->data, MIN(can_dlc_to_bytes(dst->dlc), CAN_MAX_DLEN)); in mcp251xfd_rxobj_to_canframe()
244 uint32_t mask = MCP251XFD_UINT32_FLAG_TO_BYTE_MASK(MCP251XFD_REG_CON_OPMOD_MASK); in mcp251xfd_get_mode_internal() local
248 return -EINVAL; in mcp251xfd_get_mode_internal()
251 *mode = FIELD_GET(mask, *reg_byte); in mcp251xfd_get_mode_internal()
257 uint32_t value, uint32_t mask, in mcp251xfd_reg_check_value_wtimeout() argument
266 return -EINVAL; in mcp251xfd_reg_check_value_wtimeout()
271 if ((*reg & mask) == value) { in mcp251xfd_reg_check_value_wtimeout()
275 if (--retries < 0) { in mcp251xfd_reg_check_value_wtimeout()
277 return -EIO; in mcp251xfd_reg_check_value_wtimeout()
293 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_tdc()
299 tmp |= FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, dev_data->tdco); in mcp251xfd_set_tdc()
311 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_mode_internal()
316 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_set_mode_internal()
320 ret = -EINVAL; in mcp251xfd_set_mode_internal()
332 if (dev_data->current_mcp251xfd_mode == MCP251XFD_REG_CON_MODE_CONFIG) { in mcp251xfd_set_mode_internal()
363 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_set_mode_internal()
369 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_mode()
371 if (dev_data->common.started) { in mcp251xfd_set_mode()
372 return -EBUSY; in mcp251xfd_set_mode()
377 return -ENOTSUP; in mcp251xfd_set_mode()
381 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_CAN2_0; in mcp251xfd_set_mode()
386 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_MIXED; in mcp251xfd_set_mode()
388 return -ENOTSUP; in mcp251xfd_set_mode()
393 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_LISTENONLY; in mcp251xfd_set_mode()
397 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_EXT_LOOPBACK; in mcp251xfd_set_mode()
400 dev_data->common.mode = mode; in mcp251xfd_set_mode()
405 static int mcp251xfd_set_timing(const struct device *dev, const struct can_timing *timing) in mcp251xfd_set_timing() argument
407 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_timing()
412 if (!timing) { in mcp251xfd_set_timing()
413 return -EINVAL; in mcp251xfd_set_timing()
416 if (dev_data->common.started) { in mcp251xfd_set_timing()
417 return -EBUSY; in mcp251xfd_set_timing()
420 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_set_timing()
423 tmp = FIELD_PREP(MCP251XFD_REG_NBTCFG_BRP_MASK, timing->prescaler - 1); in mcp251xfd_set_timing()
425 timing->prop_seg + timing->phase_seg1 - 1); in mcp251xfd_set_timing()
426 tmp |= FIELD_PREP(MCP251XFD_REG_NBTCFG_TSEG2_MASK, timing->phase_seg2 - 1); in mcp251xfd_set_timing()
427 tmp |= FIELD_PREP(MCP251XFD_REG_NBTCFG_SJW_MASK, timing->sjw - 1); in mcp251xfd_set_timing()
435 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_set_timing()
442 static int mcp251xfd_set_timing_data(const struct device *dev, const struct can_timing *timing) in mcp251xfd_set_timing_data() argument
444 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_timing_data()
449 if (!timing) { in mcp251xfd_set_timing_data()
450 return -EINVAL; in mcp251xfd_set_timing_data()
453 if (dev_data->common.started) { in mcp251xfd_set_timing_data()
454 return -EBUSY; in mcp251xfd_set_timing_data()
457 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_set_timing_data()
461 tmp = FIELD_PREP(MCP251XFD_REG_DBTCFG_BRP_MASK, timing->prescaler - 1); in mcp251xfd_set_timing_data()
463 timing->prop_seg + timing->phase_seg1 - 1); in mcp251xfd_set_timing_data()
464 tmp |= FIELD_PREP(MCP251XFD_REG_DBTCFG_TSEG2_MASK, timing->phase_seg2 - 1); in mcp251xfd_set_timing_data()
465 tmp |= FIELD_PREP(MCP251XFD_REG_DBTCFG_SJW_MASK, timing->sjw - 1); in mcp251xfd_set_timing_data()
469 /* actual TDCO minimum is -64 but driver implementation only sets >= 0 values */ in mcp251xfd_set_timing_data()
470 dev_data->tdco = CAN_CALC_TDCO(timing, 0U, MCP251XFD_REG_TDC_TDCO_MAX); in mcp251xfd_set_timing_data()
477 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_set_timing_data()
486 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_send()
490 LOG_DBG("Sending %d bytes. Id: 0x%x, ID type: %s %s %s %s", can_dlc_to_bytes(msg->dlc), in mcp251xfd_send()
491 msg->id, msg->flags & CAN_FRAME_IDE ? "extended" : "standard", in mcp251xfd_send()
492 msg->flags & CAN_FRAME_RTR ? "RTR" : "", in mcp251xfd_send()
493 msg->flags & CAN_FRAME_FDF ? "FD frame" : "", in mcp251xfd_send()
494 msg->flags & CAN_FRAME_BRS ? "BRS" : ""); in mcp251xfd_send()
496 if (!dev_data->common.started) { in mcp251xfd_send()
497 return -ENETDOWN; in mcp251xfd_send()
500 if (dev_data->state == CAN_STATE_BUS_OFF) { in mcp251xfd_send()
501 return -ENETUNREACH; in mcp251xfd_send()
504 if ((msg->flags & CAN_FRAME_FDF) == 0 && msg->dlc > CAN_MAX_DLC) { in mcp251xfd_send()
505 LOG_ERR("DLC of %d without fd flag set.", msg->dlc); in mcp251xfd_send()
506 return -EINVAL; in mcp251xfd_send()
509 if ((msg->flags & CAN_FRAME_FDF) && !(dev_data->common.mode & CAN_MODE_FD)) { in mcp251xfd_send()
510 return -ENOTSUP; in mcp251xfd_send()
513 if (k_sem_take(&dev_data->tx_sem, timeout) != 0) { in mcp251xfd_send()
514 return -EAGAIN; in mcp251xfd_send()
517 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_send()
519 if ((BIT(mailbox_idx) & dev_data->mailbox_usage) == 0) { in mcp251xfd_send()
520 dev_data->mailbox_usage |= BIT(mailbox_idx); in mcp251xfd_send()
526 k_sem_give(&dev_data->tx_sem); in mcp251xfd_send()
527 ret = -EIO; in mcp251xfd_send()
531 dev_data->mailbox[mailbox_idx].cb = callback; in mcp251xfd_send()
532 dev_data->mailbox[mailbox_idx].cb_arg = callback_arg; in mcp251xfd_send()
537 dev_data->mailbox_usage &= ~BIT(mailbox_idx); in mcp251xfd_send()
538 dev_data->mailbox[mailbox_idx].cb = NULL; in mcp251xfd_send()
539 k_sem_give(&dev_data->tx_sem); in mcp251xfd_send()
543 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_send()
550 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_add_rx_filter()
557 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_add_rx_filter()
560 if ((BIT(filter_idx) & dev_data->filter_usage) == 0) { in mcp251xfd_add_rx_filter()
566 filter_idx = -ENOSPC; in mcp251xfd_add_rx_filter()
572 if ((filter->flags & CAN_FILTER_IDE) != 0) { in mcp251xfd_add_rx_filter()
573 tmp = FIELD_PREP(MCP251XFD_REG_FLTOBJ_SID_MASK, filter->id >> 18); in mcp251xfd_add_rx_filter()
574 tmp |= FIELD_PREP(MCP251XFD_REG_FLTOBJ_EID_MASK, filter->id); in mcp251xfd_add_rx_filter()
577 tmp = FIELD_PREP(MCP251XFD_REG_FLTOBJ_SID_MASK, filter->id); in mcp251xfd_add_rx_filter()
588 if ((filter->flags & CAN_FILTER_IDE) != 0) { in mcp251xfd_add_rx_filter()
589 tmp = FIELD_PREP(MCP251XFD_REG_MASK_MSID_MASK, filter->mask >> 18); in mcp251xfd_add_rx_filter()
590 tmp |= FIELD_PREP(MCP251XFD_REG_MASK_MEID_MASK, filter->mask); in mcp251xfd_add_rx_filter()
592 tmp = FIELD_PREP(MCP251XFD_REG_MASK_MSID_MASK, filter->mask); in mcp251xfd_add_rx_filter()
614 dev_data->filter_usage |= BIT(filter_idx); in mcp251xfd_add_rx_filter()
615 dev_data->filter[filter_idx] = *filter; in mcp251xfd_add_rx_filter()
616 dev_data->rx_cb[filter_idx] = rx_cb; in mcp251xfd_add_rx_filter()
617 dev_data->cb_arg[filter_idx] = cb_arg; in mcp251xfd_add_rx_filter()
620 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_add_rx_filter()
627 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_remove_rx_filter()
637 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_remove_rx_filter()
648 dev_data->filter_usage &= ~BIT(filter_idx); in mcp251xfd_remove_rx_filter()
659 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_remove_rx_filter()
665 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_state_change_callback()
667 dev_data->common.state_change_cb = cb; in mcp251xfd_set_state_change_callback()
668 dev_data->common.state_change_cb_user_data = user_data; in mcp251xfd_set_state_change_callback()
674 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_get_state()
679 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_get_state()
683 ret = -EINVAL; in mcp251xfd_get_state()
690 err_cnt->tx_err_cnt = FIELD_GET(MCP251XFD_REG_TREC_TEC_MASK, tmp); in mcp251xfd_get_state()
691 err_cnt->rx_err_cnt = FIELD_GET(MCP251XFD_REG_TREC_REC_MASK, tmp); in mcp251xfd_get_state()
698 if (!dev_data->common.started) { in mcp251xfd_get_state()
718 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_get_state()
724 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_get_core_clock()
726 *rate = dev_cfg->osc_freq; in mcp251xfd_get_core_clock()
741 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_fifo_read()
751 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_fifo_read()
754 regs = mcp251xfd_read_crc(dev, MCP251XFD_REG_FIFOCON_TO_STA(fifo->reg_fifocon_addr), in mcp251xfd_handle_fifo_read()
757 ret = -EINVAL; in mcp251xfd_handle_fifo_read()
769 fifo_tail_index = (fifo_tail_addr - fifo->ram_start_addr) / fifo->item_size; in mcp251xfd_handle_fifo_read()
778 fifo_head_index = fifo->capacity - 1; in mcp251xfd_handle_fifo_read()
780 fifo_head_index -= 1; in mcp251xfd_handle_fifo_read()
785 fetch_total = fifo->capacity - 1 - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
788 fetch_total = fifo_head_index - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
795 ret = -EINVAL; in mcp251xfd_handle_fifo_read()
804 len = fifo->capacity - 1 - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
806 len = fifo_head_index - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
809 memory_addr = MCP251XFD_RAM_START_ADDR + fifo->ram_start_addr + in mcp251xfd_handle_fifo_read()
810 fifo_tail_index * fifo->item_size; in mcp251xfd_handle_fifo_read()
812 data = mcp251xfd_read_reg(dev, memory_addr, len * fifo->item_size); in mcp251xfd_handle_fifo_read()
815 ret = -EINVAL; in mcp251xfd_handle_fifo_read()
820 fifo->msg_handler(dev, (void *)(&data[i * fifo->item_size])); in mcp251xfd_handle_fifo_read()
823 fifo_tail_index = (fifo_tail_index + len) % fifo->capacity; in mcp251xfd_handle_fifo_read()
824 fetch_total -= len; in mcp251xfd_handle_fifo_read()
832 ret = mcp251xfd_write(dev, fifo->reg_fifocon_addr + 1, 1); in mcp251xfd_handle_fifo_read()
840 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_fifo_read()
846 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_reset_tx_fifos()
849 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_reset_tx_fifos()
853 if (!(dev_data->mailbox_usage & BIT(i))) { in mcp251xfd_reset_tx_fifos()
857 callback = dev_data->mailbox[i].cb; in mcp251xfd_reset_tx_fifos()
859 callback(dev, status, dev_data->mailbox[i].cb_arg); in mcp251xfd_reset_tx_fifos()
862 dev_data->mailbox_usage &= ~BIT(i); in mcp251xfd_reset_tx_fifos()
863 dev_data->mailbox[i].cb = NULL; in mcp251xfd_reset_tx_fifos()
864 k_sem_give(&dev_data->tx_sem); in mcp251xfd_reset_tx_fifos()
866 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_reset_tx_fifos()
883 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_cerrif()
887 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_cerrif()
894 if (new_state == dev_data->state) { in mcp251xfd_handle_cerrif()
898 LOG_INF("State %d -> %d (tx: %d, rx: %d)", dev_data->state, new_state, err_cnt.tx_err_cnt, in mcp251xfd_handle_cerrif()
901 /* Upon entering bus-off, all the fifos are reset. */ in mcp251xfd_handle_cerrif()
902 dev_data->state = new_state; in mcp251xfd_handle_cerrif()
904 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_handle_cerrif()
907 if (dev_data->common.state_change_cb) { in mcp251xfd_handle_cerrif()
908 dev_data->common.state_change_cb(dev, new_state, err_cnt, in mcp251xfd_handle_cerrif()
909 dev_data->common.state_change_cb_user_data); in mcp251xfd_handle_cerrif()
913 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_cerrif()
919 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_modif()
923 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_modif()
930 dev_data->current_mcp251xfd_mode = mode; in mcp251xfd_handle_modif()
934 if (mode == dev_data->next_mcp251xfd_mode) { in mcp251xfd_handle_modif()
940 if (dev_data->common.started) { in mcp251xfd_handle_modif()
941 LOG_INF("Switching back into mode %d", dev_data->next_mcp251xfd_mode); in mcp251xfd_handle_modif()
942 ret = mcp251xfd_set_mode_internal(dev, dev_data->next_mcp251xfd_mode); in mcp251xfd_handle_modif()
946 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_modif()
953 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_ivmif()
957 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_ivmif()
961 ret = -EINVAL; in mcp251xfd_handle_ivmif()
968 LOG_INF("ivmif bus-off error"); in mcp251xfd_handle_ivmif()
969 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_handle_ivmif()
1006 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_ivmif()
1014 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_rxovif()
1017 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_rxovif()
1030 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_rxovif()
1037 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_handle_interrupts()
1038 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_interrupts()
1045 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_interrupts()
1049 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_interrupts()
1070 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_interrupts()
1073 ret = mcp251xfd_handle_fifo_read(dev, &dev_cfg->rx_fifo, in mcp251xfd_handle_interrupts()
1081 ret = mcp251xfd_handle_fifo_read(dev, &dev_cfg->tef_fifo, in mcp251xfd_handle_interrupts()
1105 * TX ERROR_WARNING -> TX ERROR_ACTIVE. in mcp251xfd_handle_interrupts()
1108 dev_data->state > CAN_STATE_ERROR_ACTIVE) { in mcp251xfd_handle_interrupts()
1126 ret = gpio_pin_get_dt(&dev_cfg->int_gpio_dt); in mcp251xfd_handle_interrupts()
1143 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_int_thread()
1144 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_int_thread()
1149 k_sem_take(&dev_data->int_sem, K_FOREVER); in mcp251xfd_int_thread()
1152 /* Re-enable pin interrupts */ in mcp251xfd_int_thread()
1153 ret = gpio_pin_interrupt_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INT_LEVEL_ACTIVE); in mcp251xfd_int_thread()
1166 const struct device *dev = dev_data->dev; in mcp251xfd_int_gpio_callback()
1167 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_int_gpio_callback()
1171 ret = gpio_pin_interrupt_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INT_DISABLE); in mcp251xfd_int_gpio_callback()
1177 k_sem_give(&dev_data->int_sem); in mcp251xfd_int_gpio_callback()
1195 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_start()
1196 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_start()
1199 if (dev_data->common.started) { in mcp251xfd_start()
1200 return -EALREADY; in mcp251xfd_start()
1204 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_start()
1206 if (dev_cfg->common.phy != NULL) { in mcp251xfd_start()
1207 ret = can_transceiver_enable(dev_cfg->common.phy, dev_data->common.mode); in mcp251xfd_start()
1214 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_start()
1218 ret = mcp251xfd_set_mode_internal(dev, dev_data->next_mcp251xfd_mode); in mcp251xfd_start()
1221 if (dev_cfg->common.phy != NULL) { in mcp251xfd_start()
1223 (void)can_transceiver_disable(dev_cfg->common.phy); in mcp251xfd_start()
1226 dev_data->common.started = true; in mcp251xfd_start()
1229 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_start()
1236 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_stop()
1237 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_stop()
1241 if (!dev_data->common.started) { in mcp251xfd_stop()
1242 return -EALREADY; in mcp251xfd_stop()
1245 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_stop()
1253 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_stop()
1267 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_stop()
1271 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_stop()
1275 dev_data->common.started = false; in mcp251xfd_stop()
1276 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_stop()
1278 if (dev_cfg->common.phy != NULL) { in mcp251xfd_stop()
1279 ret = can_transceiver_disable(dev_cfg->common.phy); in mcp251xfd_stop()
1292 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_rx_fifo_handler()
1304 filhit = FIELD_GET(MCP251XFD_OBJ_FILHIT_MASK, rxobj->flags); in mcp251xfd_rx_fifo_handler()
1305 if ((dev_data->filter_usage & BIT(filhit)) != 0) { in mcp251xfd_rx_fifo_handler()
1307 dev_data->rx_cb[filhit](dev, &dst, dev_data->cb_arg[filhit]); in mcp251xfd_rx_fifo_handler()
1313 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_tef_fifo_handler()
1318 mailbox_idx = FIELD_GET(MCP251XFD_OBJ_FLAGS_SEQ_MASK, tefobj->flags); in mcp251xfd_tef_fifo_handler()
1320 mcp251xfd_reset_tx_fifos(dev, -EIO); in mcp251xfd_tef_fifo_handler()
1325 callback = dev_data->mailbox[mailbox_idx].cb; in mcp251xfd_tef_fifo_handler()
1327 callback(dev, 0, dev_data->mailbox[mailbox_idx].cb_arg); in mcp251xfd_tef_fifo_handler()
1330 dev_data->mailbox_usage &= ~BIT(mailbox_idx); in mcp251xfd_tef_fifo_handler()
1331 dev_data->mailbox[mailbox_idx].cb = NULL; in mcp251xfd_tef_fifo_handler()
1332 k_sem_give(&dev_data->tx_sem); in mcp251xfd_tef_fifo_handler()
1353 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init_osc_reg()
1358 tmp = FIELD_PREP(MCP251XFD_REG_OSC_CLKODIV_MASK, dev_cfg->clko_div); in mcp251xfd_init_osc_reg()
1359 if (dev_cfg->pll_enable) { in mcp251xfd_init_osc_reg()
1378 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init_iocon_reg()
1393 if (dev_cfg->sof_on_clko) { in mcp251xfd_init_iocon_reg()
1424 tmp |= FIELD_PREP(MCP251XFD_REG_TEFCON_FSIZE_MASK, MCP251XFD_TX_QUEUE_ITEMS - 1); in mcp251xfd_init_tef_fifo()
1438 tmp |= FIELD_PREP(MCP251XFD_REG_TXQCON_FSIZE_MASK, MCP251XFD_TX_QUEUE_ITEMS - 1); in mcp251xfd_init_tx_queue()
1440 can_bytes_to_dlc(MCP251XFD_PAYLOAD_SIZE) - 8); in mcp251xfd_init_tx_queue()
1456 tmp |= FIELD_PREP(MCP251XFD_REG_FIFOCON_FSIZE_MASK, MCP251XFD_RX_FIFO_ITEMS - 1); in mcp251xfd_init_rx_fifo()
1458 can_bytes_to_dlc(MCP251XFD_PAYLOAD_SIZE) - 8); in mcp251xfd_init_rx_fifo()
1473 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init_tscon()
1478 dev_cfg->timestamp_prescaler - 1); in mcp251xfd_init_tscon()
1488 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_reset()
1500 ret = spi_write_dt(&dev_cfg->bus, &tx); in mcp251xfd_reset()
1508 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init()
1509 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_init()
1513 struct can_timing timing = { 0 }; in mcp251xfd_init() local
1518 dev_data->dev = dev; in mcp251xfd_init()
1520 if (dev_cfg->clk_dev != NULL) { in mcp251xfd_init()
1521 uint32_t clk_id = dev_cfg->clk_id; in mcp251xfd_init()
1523 if (!device_is_ready(dev_cfg->clk_dev)) { in mcp251xfd_init()
1525 return -ENODEV; in mcp251xfd_init()
1528 ret = clock_control_on(dev_cfg->clk_dev, (clock_control_subsys_t)clk_id); in mcp251xfd_init()
1535 k_sem_init(&dev_data->int_sem, 0, 1); in mcp251xfd_init()
1536 k_sem_init(&dev_data->tx_sem, MCP251XFD_TX_QUEUE_ITEMS, MCP251XFD_TX_QUEUE_ITEMS); in mcp251xfd_init()
1538 k_mutex_init(&dev_data->mutex); in mcp251xfd_init()
1540 if (!spi_is_ready_dt(&dev_cfg->bus)) { in mcp251xfd_init()
1541 LOG_ERR("SPI bus %s not ready", dev_cfg->bus.bus->name); in mcp251xfd_init()
1542 return -ENODEV; in mcp251xfd_init()
1545 if (!gpio_is_ready_dt(&dev_cfg->int_gpio_dt)) { in mcp251xfd_init()
1547 return -ENODEV; in mcp251xfd_init()
1550 if (gpio_pin_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INPUT) < 0) { in mcp251xfd_init()
1552 return -EINVAL; in mcp251xfd_init()
1555 gpio_init_callback(&dev_data->int_gpio_cb, mcp251xfd_int_gpio_callback, in mcp251xfd_init()
1556 BIT(dev_cfg->int_gpio_dt.pin)); in mcp251xfd_init()
1558 if (gpio_add_callback_dt(&dev_cfg->int_gpio_dt, &dev_data->int_gpio_cb) < 0) { in mcp251xfd_init()
1559 return -EINVAL; in mcp251xfd_init()
1562 if (gpio_pin_interrupt_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INT_LEVEL_ACTIVE) < 0) { in mcp251xfd_init()
1563 return -EINVAL; in mcp251xfd_init()
1566 k_thread_create(&dev_data->int_thread, dev_data->int_thread_stack, in mcp251xfd_init()
1571 (void)k_thread_name_set(&dev_data->int_thread, "MCP251XFD interrupt thread"); in mcp251xfd_init()
1579 ret = can_calc_timing(dev, &timing, dev_cfg->common.bitrate, in mcp251xfd_init()
1580 dev_cfg->common.sample_point); in mcp251xfd_init()
1582 LOG_ERR("Can't find timing for given param"); in mcp251xfd_init()
1586 LOG_DBG("Presc: %d, BS1: %d, BS2: %d", timing.prescaler, timing.phase_seg1, in mcp251xfd_init()
1587 timing.phase_seg2); in mcp251xfd_init()
1588 LOG_DBG("Sample-point err : %d", ret); in mcp251xfd_init()
1591 ret = can_calc_timing_data(dev, &timing_data, dev_cfg->common.bitrate_data, in mcp251xfd_init()
1592 dev_cfg->common.sample_point_data); in mcp251xfd_init()
1594 LOG_ERR("Can't find data timing for given param"); in mcp251xfd_init()
1600 LOG_DBG("Data phase Sample-point err : %d", ret); in mcp251xfd_init()
1605 return -EIO; in mcp251xfd_init()
1614 return -EIO; in mcp251xfd_init()
1617 dev_data->current_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_CONFIG; in mcp251xfd_init()
1673 ret = can_set_timing(dev, &timing); in mcp251xfd_init()