Lines Matching +full:timestamp +full:- +full:prescaler

5  * SPDX-License-Identifier: Apache-2.0
35 if ((src->flags & CAN_FRAME_IDE) != 0) { in mcp251xfd_canframe_to_txobj()
36 dst->id = FIELD_PREP(MCP251XFD_OBJ_ID_SID_MASK, src->id >> 18); in mcp251xfd_canframe_to_txobj()
37 dst->id |= FIELD_PREP(MCP251XFD_OBJ_ID_EID_MASK, src->id); in mcp251xfd_canframe_to_txobj()
39 dst->flags |= MCP251XFD_OBJ_FLAGS_IDE; in mcp251xfd_canframe_to_txobj()
41 dst->id = FIELD_PREP(MCP251XFD_OBJ_ID_SID_MASK, src->id); in mcp251xfd_canframe_to_txobj()
44 if ((src->flags & CAN_FRAME_BRS) != 0) { in mcp251xfd_canframe_to_txobj()
45 dst->flags |= MCP251XFD_OBJ_FLAGS_BRS; in mcp251xfd_canframe_to_txobj()
48 dst->flags |= FIELD_PREP(MCP251XFD_OBJ_FLAGS_DLC_MASK, src->dlc); in mcp251xfd_canframe_to_txobj()
50 if ((src->flags & CAN_FRAME_FDF) != 0) { in mcp251xfd_canframe_to_txobj()
51 dst->flags |= MCP251XFD_OBJ_FLAGS_FDF; in mcp251xfd_canframe_to_txobj()
54 dst->flags |= FIELD_PREP(MCP251XFD_OBJ_FLAGS_SEQ_MASK, mailbox_idx); in mcp251xfd_canframe_to_txobj()
56 dst->id = sys_cpu_to_le32(dst->id); in mcp251xfd_canframe_to_txobj()
57 dst->flags = sys_cpu_to_le32(dst->flags); in mcp251xfd_canframe_to_txobj()
59 if ((src->flags & CAN_FRAME_RTR) != 0) { in mcp251xfd_canframe_to_txobj()
60 dst->flags |= MCP251XFD_OBJ_FLAGS_RTR; in mcp251xfd_canframe_to_txobj()
62 memcpy(dst->data, src->data, MIN(can_dlc_to_bytes(src->dlc), CAN_MAX_DLEN)); in mcp251xfd_canframe_to_txobj()
68 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_read_reg()
69 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_read_reg()
70 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_read_reg()
75 memcpy(&spi_data->header[1], &spi_cmd, sizeof(spi_cmd)); in mcp251xfd_read_reg()
77 struct spi_buf tx_buf = {.buf = &spi_data->header[1], .len = MCP251XFD_SPI_CMD_LEN + len}; in mcp251xfd_read_reg()
78 struct spi_buf rx_buf = {.buf = &spi_data->header[1], .len = MCP251XFD_SPI_CMD_LEN + len}; in mcp251xfd_read_reg()
83 ret = spi_transceive_dt(&dev_cfg->bus, &tx, &rx); in mcp251xfd_read_reg()
88 return &spi_data->buf[0]; in mcp251xfd_read_reg()
93 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_read_crc()
94 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_read_crc()
95 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_read_crc()
99 while (num_retries-- > 0) { in mcp251xfd_read_crc()
102 struct spi_buf tx_buf = {.buf = &spi_data->header[0], in mcp251xfd_read_crc()
107 struct spi_buf rx_buf = {.buf = &spi_data->header[0], in mcp251xfd_read_crc()
116 memcpy(&spi_data->header[0], &spi_cmd, sizeof(spi_cmd)); in mcp251xfd_read_crc()
117 spi_data->header[2] = len; in mcp251xfd_read_crc()
124 (uint8_t *)(&spi_data->header[0]), in mcp251xfd_read_crc()
127 ret = spi_transceive_dt(&dev_cfg->bus, &tx, &rx); in mcp251xfd_read_crc()
133 crc = crc16(MCP251XFD_CRC_POLY, crc_in, &spi_data->buf[0], in mcp251xfd_read_crc()
136 return &spi_data->buf[0]; in mcp251xfd_read_crc()
145 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_get_spi_buf_ptr()
146 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_get_spi_buf_ptr()
148 return &spi_data->buf[0]; in mcp251xfd_get_spi_buf_ptr()
153 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_write()
154 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_write()
155 struct mcp251xfd_spi_data *spi_data = &dev_data->spi_data; in mcp251xfd_write()
158 struct spi_buf tx_buf = {.buf = &spi_data->header[1], .len = MCP251XFD_SPI_CMD_LEN + len}; in mcp251xfd_write()
162 memcpy(&spi_data->header[1], &spi_cmd, sizeof(spi_cmd)); in mcp251xfd_write()
164 return spi_write_dt(&dev_cfg->bus, &tx); in mcp251xfd_write()
181 return -EINVAL; in mcp251xfd_fifo_write()
186 return -ENOMEM; in mcp251xfd_fifo_write()
195 ROUND_UP(can_dlc_to_bytes(msg->dlc), MCP251XFD_RAM_ALIGNMENT); in mcp251xfd_fifo_write()
213 src->id = sys_le32_to_cpu(src->id); in mcp251xfd_rxobj_to_canframe()
214 src->flags = sys_le32_to_cpu(src->flags); in mcp251xfd_rxobj_to_canframe()
216 if ((src->flags & MCP251XFD_OBJ_FLAGS_IDE) != 0) { in mcp251xfd_rxobj_to_canframe()
217 dst->id = FIELD_GET(MCP251XFD_OBJ_ID_EID_MASK, src->id); in mcp251xfd_rxobj_to_canframe()
218 dst->id |= FIELD_GET(MCP251XFD_OBJ_ID_SID_MASK, src->id) << 18; in mcp251xfd_rxobj_to_canframe()
219 dst->flags |= CAN_FRAME_IDE; in mcp251xfd_rxobj_to_canframe()
221 dst->id = FIELD_GET(MCP251XFD_OBJ_ID_SID_MASK, src->id); in mcp251xfd_rxobj_to_canframe()
224 if ((src->flags & MCP251XFD_OBJ_FLAGS_BRS) != 0) { in mcp251xfd_rxobj_to_canframe()
225 dst->flags |= CAN_FRAME_BRS; in mcp251xfd_rxobj_to_canframe()
229 if ((src->flags & MCP251XFD_OBJ_FLAGS_FDF) != 0) { in mcp251xfd_rxobj_to_canframe()
230 dst->flags |= CAN_FRAME_FDF; in mcp251xfd_rxobj_to_canframe()
234 dst->dlc = FIELD_GET(MCP251XFD_OBJ_FLAGS_DLC_MASK, src->flags); in mcp251xfd_rxobj_to_canframe()
237 dst->timestamp = sys_le32_to_cpu(src->timestamp); in mcp251xfd_rxobj_to_canframe()
240 if ((src->flags & MCP251XFD_OBJ_FLAGS_RTR) != 0) { in mcp251xfd_rxobj_to_canframe()
241 dst->flags |= CAN_FRAME_RTR; in mcp251xfd_rxobj_to_canframe()
243 memcpy(dst->data, src->data, MIN(can_dlc_to_bytes(dst->dlc), CAN_MAX_DLEN)); in mcp251xfd_rxobj_to_canframe()
254 return -EINVAL; in mcp251xfd_get_mode_internal()
272 return -EINVAL; in mcp251xfd_reg_check_value_wtimeout()
281 if (--retries < 0) { in mcp251xfd_reg_check_value_wtimeout()
283 return -EIO; in mcp251xfd_reg_check_value_wtimeout()
301 return -EINVAL; in mcp251xfd_set_tdc()
320 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_mode_internal()
325 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_set_mode_internal()
329 ret = -EINVAL; in mcp251xfd_set_mode_internal()
341 if (dev_data->current_mcp251xfd_mode == MCP251XFD_REG_CON_MODE_CONFIG) { in mcp251xfd_set_mode_internal()
347 ret = mcp251xfd_set_tdc(dev, true, dev_data->tdco); in mcp251xfd_set_mode_internal()
372 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_set_mode_internal()
378 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_mode()
380 if (dev_data->started) { in mcp251xfd_set_mode()
381 return -EBUSY; in mcp251xfd_set_mode()
386 return -ENOTSUP; in mcp251xfd_set_mode()
390 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_CAN2_0; in mcp251xfd_set_mode()
395 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_MIXED; in mcp251xfd_set_mode()
397 return -ENOTSUP; in mcp251xfd_set_mode()
402 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_LISTENONLY; in mcp251xfd_set_mode()
406 dev_data->next_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_EXT_LOOPBACK; in mcp251xfd_set_mode()
409 dev_data->mode = mode; in mcp251xfd_set_mode()
416 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_timing()
421 return -EINVAL; in mcp251xfd_set_timing()
424 if (dev_data->started) { in mcp251xfd_set_timing()
425 return -EBUSY; in mcp251xfd_set_timing()
428 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_set_timing()
431 *reg = FIELD_PREP(MCP251XFD_REG_NBTCFG_BRP_MASK, timing->prescaler - 1); in mcp251xfd_set_timing()
433 timing->prop_seg + timing->phase_seg1 - 1); in mcp251xfd_set_timing()
434 *reg |= FIELD_PREP(MCP251XFD_REG_NBTCFG_TSEG2_MASK, timing->phase_seg2 - 1); in mcp251xfd_set_timing()
435 *reg |= FIELD_PREP(MCP251XFD_REG_NBTCFG_SJW_MASK, timing->sjw - 1); in mcp251xfd_set_timing()
442 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_set_timing()
451 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_timing_data()
456 return -EINVAL; in mcp251xfd_set_timing_data()
459 if (dev_data->started) { in mcp251xfd_set_timing_data()
460 return -EBUSY; in mcp251xfd_set_timing_data()
463 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_set_timing_data()
467 *reg = FIELD_PREP(MCP251XFD_REG_DBTCFG_BRP_MASK, timing->prescaler - 1); in mcp251xfd_set_timing_data()
469 timing->prop_seg + timing->phase_seg1 - 1); in mcp251xfd_set_timing_data()
470 *reg |= FIELD_PREP(MCP251XFD_REG_DBTCFG_TSEG2_MASK, timing->phase_seg2 - 1); in mcp251xfd_set_timing_data()
471 *reg |= FIELD_PREP(MCP251XFD_REG_DBTCFG_SJW_MASK, timing->sjw - 1); in mcp251xfd_set_timing_data()
475 dev_data->tdco = timing->prescaler * (timing->prop_seg + timing->phase_seg1); in mcp251xfd_set_timing_data()
482 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_set_timing_data()
491 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_send()
495 LOG_DBG("Sending %d bytes. Id: 0x%x, ID type: %s %s %s %s", can_dlc_to_bytes(msg->dlc), in mcp251xfd_send()
496 msg->id, msg->flags & CAN_FRAME_IDE ? "extended" : "standard", in mcp251xfd_send()
497 msg->flags & CAN_FRAME_RTR ? "RTR" : "", in mcp251xfd_send()
498 msg->flags & CAN_FRAME_FDF ? "FD frame" : "", in mcp251xfd_send()
499 msg->flags & CAN_FRAME_BRS ? "BRS" : ""); in mcp251xfd_send()
503 if (!dev_data->started) { in mcp251xfd_send()
504 return -ENETDOWN; in mcp251xfd_send()
507 if (dev_data->state == CAN_STATE_BUS_OFF) { in mcp251xfd_send()
508 return -ENETUNREACH; in mcp251xfd_send()
511 if ((msg->flags & CAN_FRAME_FDF) == 0 && msg->dlc > CAN_MAX_DLC) { in mcp251xfd_send()
512 LOG_ERR("DLC of %d without fd flag set.", msg->dlc); in mcp251xfd_send()
513 return -EINVAL; in mcp251xfd_send()
516 if ((msg->flags & CAN_FRAME_FDF) && !(dev_data->mode & CAN_MODE_FD)) { in mcp251xfd_send()
517 return -ENOTSUP; in mcp251xfd_send()
520 if (k_sem_take(&dev_data->tx_sem, timeout) != 0) { in mcp251xfd_send()
521 return -EAGAIN; in mcp251xfd_send()
524 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_send()
526 if ((BIT(mailbox_idx) & dev_data->mailbox_usage) == 0) { in mcp251xfd_send()
527 dev_data->mailbox_usage |= BIT(mailbox_idx); in mcp251xfd_send()
533 k_sem_give(&dev_data->tx_sem); in mcp251xfd_send()
534 ret = -EIO; in mcp251xfd_send()
538 dev_data->mailbox[mailbox_idx].cb = callback; in mcp251xfd_send()
539 dev_data->mailbox[mailbox_idx].cb_arg = callback_arg; in mcp251xfd_send()
544 dev_data->mailbox_usage &= ~BIT(mailbox_idx); in mcp251xfd_send()
545 dev_data->mailbox[mailbox_idx].cb = NULL; in mcp251xfd_send()
546 k_sem_give(&dev_data->tx_sem); in mcp251xfd_send()
550 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_send()
557 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_add_rx_filter()
564 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_add_rx_filter()
567 if ((BIT(filter_idx) & dev_data->filter_usage) == 0) { in mcp251xfd_add_rx_filter()
573 filter_idx = -ENOSPC; in mcp251xfd_add_rx_filter()
577 if ((filter->flags & CAN_FILTER_RTR) != 0) { in mcp251xfd_add_rx_filter()
578 filter_idx = -ENOTSUP; in mcp251xfd_add_rx_filter()
584 if ((filter->flags & CAN_FILTER_IDE) != 0) { in mcp251xfd_add_rx_filter()
585 *reg = FIELD_PREP(MCP251XFD_REG_FLTOBJ_SID_MASK, filter->id >> 18); in mcp251xfd_add_rx_filter()
586 *reg |= FIELD_PREP(MCP251XFD_REG_FLTOBJ_EID_MASK, filter->id); in mcp251xfd_add_rx_filter()
589 *reg = FIELD_PREP(MCP251XFD_REG_FLTOBJ_SID_MASK, filter->id); in mcp251xfd_add_rx_filter()
600 if ((filter->flags & CAN_FILTER_IDE) != 0) { in mcp251xfd_add_rx_filter()
601 *reg = FIELD_PREP(MCP251XFD_REG_MASK_MSID_MASK, filter->mask >> 18); in mcp251xfd_add_rx_filter()
602 *reg |= FIELD_PREP(MCP251XFD_REG_MASK_MEID_MASK, filter->mask); in mcp251xfd_add_rx_filter()
604 *reg = FIELD_PREP(MCP251XFD_REG_MASK_MSID_MASK, filter->mask); in mcp251xfd_add_rx_filter()
626 dev_data->filter_usage |= BIT(filter_idx); in mcp251xfd_add_rx_filter()
627 dev_data->filter[filter_idx] = *filter; in mcp251xfd_add_rx_filter()
628 dev_data->rx_cb[filter_idx] = rx_cb; in mcp251xfd_add_rx_filter()
629 dev_data->cb_arg[filter_idx] = cb_arg; in mcp251xfd_add_rx_filter()
632 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_add_rx_filter()
639 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_remove_rx_filter()
649 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_remove_rx_filter()
660 dev_data->filter_usage &= ~BIT(filter_idx); in mcp251xfd_remove_rx_filter()
671 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_remove_rx_filter()
677 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_set_state_change_callback()
679 dev_data->state_change_cb = cb; in mcp251xfd_set_state_change_callback()
680 dev_data->state_change_cb_data = user_data; in mcp251xfd_set_state_change_callback()
686 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_get_state()
690 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_get_state()
694 ret = -EINVAL; in mcp251xfd_get_state()
701 err_cnt->tx_err_cnt = FIELD_GET(MCP251XFD_REG_TREC_TEC_MASK, *reg); in mcp251xfd_get_state()
702 err_cnt->rx_err_cnt = FIELD_GET(MCP251XFD_REG_TREC_REC_MASK, *reg); in mcp251xfd_get_state()
709 if (!dev_data->started) { in mcp251xfd_get_state()
729 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_get_state()
735 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_get_core_clock()
737 *rate = dev_cfg->osc_freq; in mcp251xfd_get_core_clock()
750 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_get_max_bitrate()
752 *max_bitrate = dev_cfg->max_bitrate; in mcp251xfd_get_max_bitrate()
760 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_recover()
764 if (!dev_data->started) { in mcp251xfd_recover()
765 return -ENETDOWN; in mcp251xfd_recover()
768 return -ENOTSUP; in mcp251xfd_recover()
776 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_fifo_read()
786 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_fifo_read()
789 regs = mcp251xfd_read_crc(dev, MCP251XFD_REG_FIFOCON_TO_STA(fifo->reg_fifocon_addr), in mcp251xfd_handle_fifo_read()
792 ret = -EINVAL; in mcp251xfd_handle_fifo_read()
804 fifo_tail_index = (fifo_tail_addr - fifo->ram_start_addr) / fifo->item_size; in mcp251xfd_handle_fifo_read()
813 fifo_head_index = fifo->capacity - 1; in mcp251xfd_handle_fifo_read()
815 fifo_head_index -= 1; in mcp251xfd_handle_fifo_read()
820 fetch_total = fifo->capacity - 1 - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
823 fetch_total = fifo_head_index - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
830 ret = -EINVAL; in mcp251xfd_handle_fifo_read()
839 len = fifo->capacity - 1 - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
841 len = fifo_head_index - fifo_tail_index + 1; in mcp251xfd_handle_fifo_read()
844 memory_addr = MCP251XFD_RAM_START_ADDR + fifo->ram_start_addr + in mcp251xfd_handle_fifo_read()
845 fifo_tail_index * fifo->item_size; in mcp251xfd_handle_fifo_read()
847 data = mcp251xfd_read_reg(dev, memory_addr, len * fifo->item_size); in mcp251xfd_handle_fifo_read()
850 ret = -EINVAL; in mcp251xfd_handle_fifo_read()
855 fifo->msg_handler(dev, (void *)(&data[i * fifo->item_size])); in mcp251xfd_handle_fifo_read()
858 fifo_tail_index = (fifo_tail_index + len) % fifo->capacity; in mcp251xfd_handle_fifo_read()
859 fetch_total -= len; in mcp251xfd_handle_fifo_read()
867 ret = mcp251xfd_write(dev, fifo->reg_fifocon_addr + 1, 1); in mcp251xfd_handle_fifo_read()
875 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_fifo_read()
881 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_reset_tx_fifos()
884 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_reset_tx_fifos()
888 if (!(dev_data->mailbox_usage & BIT(i))) { in mcp251xfd_reset_tx_fifos()
892 callback = dev_data->mailbox[i].cb; in mcp251xfd_reset_tx_fifos()
894 callback(dev, status, dev_data->mailbox[i].cb_arg); in mcp251xfd_reset_tx_fifos()
897 dev_data->mailbox_usage &= ~BIT(i); in mcp251xfd_reset_tx_fifos()
898 dev_data->mailbox[i].cb = NULL; in mcp251xfd_reset_tx_fifos()
899 k_sem_give(&dev_data->tx_sem); in mcp251xfd_reset_tx_fifos()
901 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_reset_tx_fifos()
918 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_cerrif()
922 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_cerrif()
929 if (new_state == dev_data->state) { in mcp251xfd_handle_cerrif()
933 LOG_INF("State %d -> %d (tx: %d, rx: %d)", dev_data->state, new_state, err_cnt.tx_err_cnt, in mcp251xfd_handle_cerrif()
936 /* Upon entering bus-off, all the fifos are reset. */ in mcp251xfd_handle_cerrif()
937 dev_data->state = new_state; in mcp251xfd_handle_cerrif()
939 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_handle_cerrif()
942 if (dev_data->state_change_cb) { in mcp251xfd_handle_cerrif()
943 dev_data->state_change_cb(dev, new_state, err_cnt, dev_data->state_change_cb_data); in mcp251xfd_handle_cerrif()
947 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_cerrif()
953 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_modif()
957 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_modif()
964 dev_data->current_mcp251xfd_mode = mode; in mcp251xfd_handle_modif()
968 if (mode == dev_data->next_mcp251xfd_mode) { in mcp251xfd_handle_modif()
974 if (dev_data->started) { in mcp251xfd_handle_modif()
975 LOG_INF("Switching back into mode %d", dev_data->next_mcp251xfd_mode); in mcp251xfd_handle_modif()
976 ret = mcp251xfd_set_mode_internal(dev, dev_data->next_mcp251xfd_mode); in mcp251xfd_handle_modif()
980 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_modif()
987 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_ivmif()
990 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_ivmif()
994 ret = -EINVAL; in mcp251xfd_handle_ivmif()
1001 LOG_INF("ivmif bus-off error"); in mcp251xfd_handle_ivmif()
1002 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_handle_ivmif()
1011 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_ivmif()
1017 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_handle_interrupts()
1018 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_handle_interrupts()
1025 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_handle_interrupts()
1029 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_interrupts()
1050 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_handle_interrupts()
1053 ret = mcp251xfd_handle_fifo_read(dev, &dev_cfg->rx_fifo, in mcp251xfd_handle_interrupts()
1061 ret = mcp251xfd_handle_fifo_read(dev, &dev_cfg->tef_fifo, in mcp251xfd_handle_interrupts()
1085 * TX ERROR_WARNING -> TX ERROR_ACTIVE. in mcp251xfd_handle_interrupts()
1088 dev_data->state > CAN_STATE_ERROR_ACTIVE) { in mcp251xfd_handle_interrupts()
1097 ret = gpio_pin_get_dt(&dev_cfg->int_gpio_dt); in mcp251xfd_handle_interrupts()
1114 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_int_thread()
1115 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_int_thread()
1120 k_sem_take(&dev_data->int_sem, K_FOREVER); in mcp251xfd_int_thread()
1123 /* Re-enable pin interrupts */ in mcp251xfd_int_thread()
1124 ret = gpio_pin_interrupt_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INT_LEVEL_ACTIVE); in mcp251xfd_int_thread()
1137 const struct device *dev = dev_data->dev; in mcp251xfd_int_gpio_callback()
1138 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_int_gpio_callback()
1142 ret = gpio_pin_interrupt_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INT_DISABLE); in mcp251xfd_int_gpio_callback()
1148 k_sem_give(&dev_data->int_sem); in mcp251xfd_int_gpio_callback()
1166 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_start()
1167 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_start()
1170 if (dev_data->started) { in mcp251xfd_start()
1171 return -EALREADY; in mcp251xfd_start()
1175 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_start()
1177 if (dev_cfg->phy != NULL) { in mcp251xfd_start()
1178 ret = can_transceiver_enable(dev_cfg->phy); in mcp251xfd_start()
1185 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_start()
1187 ret = mcp251xfd_set_mode_internal(dev, dev_data->next_mcp251xfd_mode); in mcp251xfd_start()
1190 if (dev_cfg->phy != NULL) { in mcp251xfd_start()
1192 (void)can_transceiver_disable(dev_cfg->phy); in mcp251xfd_start()
1195 dev_data->started = true; in mcp251xfd_start()
1198 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_start()
1205 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_stop()
1206 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_stop()
1210 if (!dev_data->started) { in mcp251xfd_stop()
1211 return -EALREADY; in mcp251xfd_stop()
1214 k_mutex_lock(&dev_data->mutex, K_FOREVER); in mcp251xfd_stop()
1222 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_stop()
1236 mcp251xfd_reset_tx_fifos(dev, -ENETDOWN); in mcp251xfd_stop()
1240 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_stop()
1244 dev_data->started = false; in mcp251xfd_stop()
1245 k_mutex_unlock(&dev_data->mutex); in mcp251xfd_stop()
1247 if (dev_cfg->phy != NULL) { in mcp251xfd_stop()
1248 ret = can_transceiver_disable(dev_cfg->phy); in mcp251xfd_stop()
1261 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_rx_fifo_handler()
1267 filhit = FIELD_GET(MCP251XFD_OBJ_FILHIT_MASK, rxobj->flags); in mcp251xfd_rx_fifo_handler()
1268 if ((dev_data->filter_usage & BIT(filhit)) != 0) { in mcp251xfd_rx_fifo_handler()
1270 dev_data->rx_cb[filhit](dev, &dst, dev_data->cb_arg[filhit]); in mcp251xfd_rx_fifo_handler()
1276 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_tef_fifo_handler()
1281 mailbox_idx = FIELD_GET(MCP251XFD_OBJ_FLAGS_SEQ_MASK, tefobj->flags); in mcp251xfd_tef_fifo_handler()
1283 mcp251xfd_reset_tx_fifos(dev, -EIO); in mcp251xfd_tef_fifo_handler()
1288 callback = dev_data->mailbox[mailbox_idx].cb; in mcp251xfd_tef_fifo_handler()
1290 callback(dev, 0, dev_data->mailbox[mailbox_idx].cb_arg); in mcp251xfd_tef_fifo_handler()
1293 dev_data->mailbox_usage &= ~BIT(mailbox_idx); in mcp251xfd_tef_fifo_handler()
1294 dev_data->mailbox[mailbox_idx].cb = NULL; in mcp251xfd_tef_fifo_handler()
1295 k_sem_give(&dev_data->tx_sem); in mcp251xfd_tef_fifo_handler()
1305 if (USE_SP_ALGO && timing_params->sample_point > 0) { in mcp251xfd_init_timing_struct()
1307 ret = can_calc_timing(dev, timing, timing_params->bus_speed, in mcp251xfd_init_timing_struct()
1308 timing_params->sample_point); in mcp251xfd_init_timing_struct()
1310 ret = can_calc_timing_data(dev, timing, timing_params->bus_speed, in mcp251xfd_init_timing_struct()
1311 timing_params->sample_point); in mcp251xfd_init_timing_struct()
1316 LOG_DBG("Presc: %d, BS1: %d, BS2: %d", timing->prescaler, timing->phase_seg1, in mcp251xfd_init_timing_struct()
1317 timing->phase_seg2); in mcp251xfd_init_timing_struct()
1318 LOG_DBG("Sample-point err : %d", ret); in mcp251xfd_init_timing_struct()
1320 timing->sjw = timing_params->sjw; in mcp251xfd_init_timing_struct()
1321 timing->prop_seg = timing_params->prop_seg; in mcp251xfd_init_timing_struct()
1322 timing->phase_seg1 = timing_params->phase_seg1; in mcp251xfd_init_timing_struct()
1323 timing->phase_seg2 = timing_params->phase_seg2; in mcp251xfd_init_timing_struct()
1324 ret = can_calc_prescaler(dev, timing, timing_params->bus_speed); in mcp251xfd_init_timing_struct()
1349 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init_osc_reg()
1353 *reg = FIELD_PREP(MCP251XFD_REG_OSC_CLKODIV_MASK, dev_cfg->clko_div); in mcp251xfd_init_osc_reg()
1354 if (dev_cfg->pll_enable) { in mcp251xfd_init_osc_reg()
1373 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init_iocon_reg()
1387 if (dev_cfg->sof_on_clko) { in mcp251xfd_init_iocon_reg()
1413 *reg |= FIELD_PREP(MCP251XFD_REG_TEFCON_FSIZE_MASK, MCP251XFD_TX_QUEUE_ITEMS - 1); in mcp251xfd_init_tef_fifo()
1426 *reg |= FIELD_PREP(MCP251XFD_REG_TXQCON_FSIZE_MASK, MCP251XFD_TX_QUEUE_ITEMS - 1); in mcp251xfd_init_tx_queue()
1428 can_bytes_to_dlc(MCP251XFD_PAYLOAD_SIZE) - 8); in mcp251xfd_init_tx_queue()
1440 *reg |= FIELD_PREP(MCP251XFD_REG_FIFOCON_FSIZE_MASK, MCP251XFD_RX_FIFO_ITEMS - 1); in mcp251xfd_init_rx_fifo()
1442 can_bytes_to_dlc(MCP251XFD_PAYLOAD_SIZE) - 8); in mcp251xfd_init_rx_fifo()
1457 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init_tscon()
1461 dev_cfg->timestamp_prescaler - 1); in mcp251xfd_init_tscon()
1471 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_reset()
1483 return spi_write_dt(&dev_cfg->bus, &tx); in mcp251xfd_reset()
1488 const struct mcp251xfd_config *dev_cfg = dev->config; in mcp251xfd_init()
1489 struct mcp251xfd_data *dev_data = dev->data; in mcp251xfd_init()
1498 dev_data->dev = dev; in mcp251xfd_init()
1500 if (dev_cfg->clk_dev != NULL) { in mcp251xfd_init()
1501 uint32_t clk_id = dev_cfg->clk_id; in mcp251xfd_init()
1503 if (!device_is_ready(dev_cfg->clk_dev)) { in mcp251xfd_init()
1505 return -ENODEV; in mcp251xfd_init()
1508 ret = clock_control_on(dev_cfg->clk_dev, (clock_control_subsys_t)clk_id); in mcp251xfd_init()
1515 k_sem_init(&dev_data->int_sem, 0, 1); in mcp251xfd_init()
1516 k_sem_init(&dev_data->tx_sem, MCP251XFD_TX_QUEUE_ITEMS, MCP251XFD_TX_QUEUE_ITEMS); in mcp251xfd_init()
1518 k_mutex_init(&dev_data->mutex); in mcp251xfd_init()
1520 if (!spi_is_ready_dt(&dev_cfg->bus)) { in mcp251xfd_init()
1521 LOG_ERR("SPI bus %s not ready", dev_cfg->bus.bus->name); in mcp251xfd_init()
1522 return -ENODEV; in mcp251xfd_init()
1525 if (!gpio_is_ready_dt(&dev_cfg->int_gpio_dt)) { in mcp251xfd_init()
1527 return -ENODEV; in mcp251xfd_init()
1530 if (gpio_pin_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INPUT) < 0) { in mcp251xfd_init()
1532 return -EINVAL; in mcp251xfd_init()
1535 gpio_init_callback(&dev_data->int_gpio_cb, mcp251xfd_int_gpio_callback, in mcp251xfd_init()
1536 BIT(dev_cfg->int_gpio_dt.pin)); in mcp251xfd_init()
1538 if (gpio_add_callback_dt(&dev_cfg->int_gpio_dt, &dev_data->int_gpio_cb) < 0) { in mcp251xfd_init()
1539 return -EINVAL; in mcp251xfd_init()
1542 if (gpio_pin_interrupt_configure_dt(&dev_cfg->int_gpio_dt, GPIO_INT_LEVEL_ACTIVE) < 0) { in mcp251xfd_init()
1543 return -EINVAL; in mcp251xfd_init()
1546 k_thread_create(&dev_data->int_thread, dev_data->int_thread_stack, in mcp251xfd_init()
1551 (void)k_thread_name_set(&dev_data->int_thread, "MCP251XFD interrupt thread"); in mcp251xfd_init()
1559 ret = mcp251xfd_init_timing_struct(&timing, dev, &dev_cfg->timing_params, true); in mcp251xfd_init()
1566 ret = mcp251xfd_init_timing_struct(&timing_data, dev, &dev_cfg->timing_params_data, false); in mcp251xfd_init()
1575 ret = -EINVAL; in mcp251xfd_init()
1585 ret = -EIO; in mcp251xfd_init()
1589 dev_data->current_mcp251xfd_mode = MCP251XFD_REG_CON_MODE_CONFIG; in mcp251xfd_init()
1685 .prescaler = 1,
1692 .prescaler = 256,
1700 .prescaler = 1,
1707 .prescaler = 256,