Lines Matching +full:timestamp +full:- +full:prescaler
4 * SPDX-License-Identifier: Apache-2.0
24 #define RCAR_CAN_CTLR_BOM (3 << 11) /* Bus-Off Recovery Mode Bits */
25 #define RCAR_CAN_CTLR_BOM_ENT BIT(11) /* Automatic halt mode entry at bus-off entry */
136 #define RCAR_CAN_EIFR_BORIF BIT(4) /* Bus-Off Recovery Detect Flag */
137 #define RCAR_CAN_EIFR_BOEIF BIT(3) /* Bus-Off Entry Detect Flag */
148 * mailbox 60 - 63 - Rx FIFO mailboxes
149 * mailbox 56 - 59 - Tx FIFO mailboxes
150 * non-FIFO mailboxes are not used
157 #define RCAR_CAN_MB_TSH_OFFSET 0x14 /* Timestamp upper byte */
158 #define RCAR_CAN_MB_TSL_OFFSET 0x15 /* Timestamp lower byte */
211 return sys_read16(config->reg_addr + offs); in can_rcar_read16()
217 sys_write16(value, config->reg_addr + offs); in can_rcar_write16()
222 struct can_rcar_data *data = dev->data; in can_rcar_tx_done()
225 tx_cb = &data->tx_cb[data->tx_tail]; in can_rcar_tx_done()
226 data->tx_tail++; in can_rcar_tx_done()
227 if (data->tx_tail >= RCAR_CAN_FIFO_DEPTH) { in can_rcar_tx_done()
228 data->tx_tail = 0; in can_rcar_tx_done()
231 data->tx_unsent--; in can_rcar_tx_done()
232 tx_cb->cb(dev, err, tx_cb->cb_arg); in can_rcar_tx_done()
233 k_sem_give(&data->tx_sem); in can_rcar_tx_done()
239 err_cnt->tx_err_cnt = sys_read8(config->reg_addr + RCAR_CAN_TECR); in can_rcar_get_error_count()
240 err_cnt->rx_err_cnt = sys_read8(config->reg_addr + RCAR_CAN_RECR); in can_rcar_get_error_count()
245 const struct can_rcar_cfg *config = dev->config; in can_rcar_state_change()
246 struct can_rcar_data *data = dev->data; in can_rcar_state_change()
247 const can_state_change_callback_t cb = data->state_change_cb; in can_rcar_state_change()
248 void *state_change_cb_data = data->state_change_cb_data; in can_rcar_state_change()
251 if (data->state == newstate) { in can_rcar_state_change()
255 LOG_DBG("Can state change new: %u old:%u\n", newstate, data->state); in can_rcar_state_change()
257 data->state = newstate; in can_rcar_state_change()
268 const struct can_rcar_cfg *config = dev->config; in can_rcar_error()
271 eifr = sys_read8(config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
275 ecsr = sys_read8(config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
279 config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
284 config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
289 config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
294 config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
299 config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
304 config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
309 config->reg_addr + RCAR_CAN_ECSR); in can_rcar_error()
313 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
319 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
326 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
330 LOG_DBG("Bus-off recovery interrupt\n"); in can_rcar_error()
331 sys_write8(RCAR_CAN_IER_ERSIE, config->reg_addr + RCAR_CAN_IER); in can_rcar_error()
334 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
338 LOG_DBG("Bus-off entry interrupt\n"); in can_rcar_error()
339 sys_write8(RCAR_CAN_IER_ERSIE, config->reg_addr + RCAR_CAN_IER); in can_rcar_error()
342 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
349 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
354 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
359 config->reg_addr + RCAR_CAN_EIFR); in can_rcar_error()
371 if (data->rx_callback[i] == NULL) { in can_rcar_rx_filter_isr()
375 if (!can_frame_matches_filter(frame, &data->filter[i])) { in can_rcar_rx_filter_isr()
382 data->rx_callback[i](dev, &tmp_frame, data->rx_callback_arg[i]); in can_rcar_rx_filter_isr()
388 const struct can_rcar_cfg *config = dev->config; in can_rcar_rx_isr()
389 struct can_rcar_data *data = dev->data; in can_rcar_rx_isr()
394 val = sys_read32(config->reg_addr + RCAR_CAN_MB_60); in can_rcar_rx_isr()
402 frame.dlc = sys_read16(config->reg_addr + in can_rcar_rx_isr()
416 frame.data[i] = sys_read8(config->reg_addr + in can_rcar_rx_isr()
422 frame.timestamp = sys_read8(config->reg_addr + in can_rcar_rx_isr()
425 frame.timestamp |= sys_read8(config->reg_addr + in can_rcar_rx_isr()
429 sys_write8(0xff, config->reg_addr + RCAR_CAN_RFPCR); in can_rcar_rx_isr()
436 const struct can_rcar_cfg *config = dev->config; in can_rcar_isr()
437 struct can_rcar_data *data = dev->data; in can_rcar_isr()
440 isr = sys_read8(config->reg_addr + RCAR_CAN_ISR); in can_rcar_isr()
444 sys_write8(isr, config->reg_addr + RCAR_CAN_ISR); in can_rcar_isr()
450 unsent = sys_read8(config->reg_addr + RCAR_CAN_TFCR); in can_rcar_isr()
453 if (data->tx_unsent <= unsent) { in can_rcar_isr()
460 isr = sys_read8(config->reg_addr + RCAR_CAN_ISR); in can_rcar_isr()
462 sys_write8(isr, config->reg_addr + RCAR_CAN_ISR); in can_rcar_isr()
466 while (!(sys_read8(config->reg_addr + RCAR_CAN_RFCR) in can_rcar_isr()
471 isr = sys_read8(config->reg_addr + RCAR_CAN_ISR); in can_rcar_isr()
473 sys_write8(isr, config->reg_addr + RCAR_CAN_ISR); in can_rcar_isr()
491 return -EAGAIN; in can_rcar_leave_sleep_mode()
511 return -EAGAIN; in can_rcar_enter_reset_mode()
533 return -EAGAIN; in can_rcar_enter_halt_mode()
556 return -EAGAIN; in can_rcar_enter_operation_mode()
560 sys_write8(RCAR_CAN_RFCR_RFE, config->reg_addr + RCAR_CAN_RFCR); in can_rcar_enter_operation_mode()
561 sys_write8(RCAR_CAN_TFCR_TFE, config->reg_addr + RCAR_CAN_TFCR); in can_rcar_enter_operation_mode()
577 const struct can_rcar_cfg *config = dev->config; in can_rcar_start()
578 struct can_rcar_data *data = dev->data; in can_rcar_start()
581 if (data->started) { in can_rcar_start()
582 return -EALREADY; in can_rcar_start()
585 if (config->phy != NULL) { in can_rcar_start()
586 ret = can_transceiver_enable(config->phy); in can_rcar_start()
593 k_mutex_lock(&data->inst_mutex, K_FOREVER); in can_rcar_start()
601 if (config->phy != NULL) { in can_rcar_start()
603 (void)can_transceiver_disable(config->phy); in can_rcar_start()
606 data->started = true; in can_rcar_start()
609 k_mutex_unlock(&data->inst_mutex); in can_rcar_start()
616 const struct can_rcar_cfg *config = dev->config; in can_rcar_stop()
617 struct can_rcar_data *data = dev->data; in can_rcar_stop()
620 if (!data->started) { in can_rcar_stop()
621 return -EALREADY; in can_rcar_stop()
624 k_mutex_lock(&data->inst_mutex, K_FOREVER); in can_rcar_stop()
629 k_mutex_unlock(&data->inst_mutex); in can_rcar_stop()
633 data->started = false; in can_rcar_stop()
635 k_mutex_unlock(&data->inst_mutex); in can_rcar_stop()
637 if (config->phy != NULL) { in can_rcar_stop()
638 ret = can_transceiver_disable(config->phy); in can_rcar_stop()
646 sys_write8((uint8_t)~RCAR_CAN_TFCR_TFE, config->reg_addr + RCAR_CAN_TFCR); in can_rcar_stop()
647 sys_write8(RCAR_CAN_TFCR_TFE, config->reg_addr + RCAR_CAN_TFCR); in can_rcar_stop()
650 while (data->tx_unsent) { in can_rcar_stop()
651 can_rcar_tx_done(dev, -ENETDOWN); in can_rcar_stop()
659 const struct can_rcar_cfg *config = dev->config; in can_rcar_set_mode()
660 struct can_rcar_data *data = dev->data; in can_rcar_set_mode()
666 return -ENOTSUP; in can_rcar_set_mode()
669 if (data->started) { in can_rcar_set_mode()
670 return -EBUSY; in can_rcar_set_mode()
673 k_mutex_lock(&data->inst_mutex, K_FOREVER); in can_rcar_set_mode()
678 ret = -ENOTSUP; in can_rcar_set_mode()
684 /* Listen-only mode */ in can_rcar_set_mode()
691 sys_write8(tcr, config->reg_addr + RCAR_CAN_TCR); in can_rcar_set_mode()
694 k_mutex_unlock(&data->inst_mutex); in can_rcar_set_mode()
710 bcr = RCAR_CAN_BCR_TSEG1(timing->phase_seg1 + timing->prop_seg - 1) | in can_rcar_set_bittiming()
711 RCAR_CAN_BCR_BPR(timing->prescaler - 1) | in can_rcar_set_bittiming()
712 RCAR_CAN_BCR_SJW(timing->sjw - 1) | in can_rcar_set_bittiming()
713 RCAR_CAN_BCR_TSEG2(timing->phase_seg2 - 1); in can_rcar_set_bittiming()
715 /* Don't overwrite CLKR with 32-bit BCR access; CLKR has 8-bit access. in can_rcar_set_bittiming()
716 * All the registers are big-endian but they get byte-swapped on 32-bit in can_rcar_set_bittiming()
717 * read/write (but not on 8-bit, contrary to the manuals)... in can_rcar_set_bittiming()
720 config->reg_addr + RCAR_CAN_BCR); in can_rcar_set_bittiming()
726 const struct can_rcar_cfg *config = dev->config; in can_rcar_set_timing()
727 struct can_rcar_data *data = dev->data; in can_rcar_set_timing()
738 if (data->started) { in can_rcar_set_timing()
739 return -EBUSY; in can_rcar_set_timing()
742 k_mutex_lock(&data->inst_mutex, K_FOREVER); in can_rcar_set_timing()
750 regs[i].value = sys_read8(config->reg_addr + regs[i].address); in can_rcar_set_timing()
770 sys_write8(regs[i].value, config->reg_addr + regs[i].address); in can_rcar_set_timing()
774 k_mutex_unlock(&data->inst_mutex); in can_rcar_set_timing()
782 struct can_rcar_data *data = dev->data; in can_rcar_set_state_change_callback()
784 data->state_change_cb = cb; in can_rcar_set_state_change_callback()
785 data->state_change_cb_data = user_data; in can_rcar_set_state_change_callback()
791 const struct can_rcar_cfg *config = dev->config; in can_rcar_get_state()
792 struct can_rcar_data *data = dev->data; in can_rcar_get_state()
795 if (!data->started) { in can_rcar_get_state()
798 *state = data->state; in can_rcar_get_state()
812 const struct can_rcar_cfg *config = dev->config; in can_rcar_recover()
813 struct can_rcar_data *data = dev->data; in can_rcar_recover()
817 if (!data->started) { in can_rcar_recover()
818 return -ENETDOWN; in can_rcar_recover()
821 if (data->state != CAN_STATE_BUS_OFF) { in can_rcar_recover()
825 if (k_mutex_lock(&data->inst_mutex, K_FOREVER)) { in can_rcar_recover()
826 return -EAGAIN; in can_rcar_recover()
830 while (data->state == CAN_STATE_BUS_OFF) { in can_rcar_recover()
837 k_uptime_ticks() - start_time >= timeout.ticks) { in can_rcar_recover()
838 ret = -EAGAIN; in can_rcar_recover()
844 k_mutex_unlock(&data->inst_mutex); in can_rcar_recover()
853 const struct can_rcar_cfg *config = dev->config; in can_rcar_send()
854 struct can_rcar_data *data = dev->data; in can_rcar_send()
863 , frame->dlc, dev->name in can_rcar_send()
864 , frame->id in can_rcar_send()
865 , (frame->flags & CAN_FRAME_IDE) != 0 ? in can_rcar_send()
867 , (frame->flags & CAN_FRAME_RTR) != 0 ? "yes" : "no"); in can_rcar_send()
870 __ASSERT(frame->dlc == 0U || frame->data != NULL, "Dataptr is null"); in can_rcar_send()
872 if (frame->dlc > CAN_MAX_DLC) { in can_rcar_send()
874 frame->dlc, CAN_MAX_DLC); in can_rcar_send()
875 return -EINVAL; in can_rcar_send()
878 if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) { in can_rcar_send()
879 LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags); in can_rcar_send()
880 return -ENOTSUP; in can_rcar_send()
883 if (!data->started) { in can_rcar_send()
884 return -ENETDOWN; in can_rcar_send()
888 if (k_sem_take(&data->tx_sem, timeout) != 0) { in can_rcar_send()
889 return -EAGAIN; in can_rcar_send()
892 k_mutex_lock(&data->inst_mutex, K_FOREVER); in can_rcar_send()
893 tx_cb = &data->tx_cb[data->tx_head]; in can_rcar_send()
894 tx_cb->cb = callback; in can_rcar_send()
895 tx_cb->cb_arg = user_data; in can_rcar_send()
897 data->tx_head++; in can_rcar_send()
898 if (data->tx_head >= RCAR_CAN_FIFO_DEPTH) { in can_rcar_send()
899 data->tx_head = 0; in can_rcar_send()
902 if ((frame->flags & CAN_FRAME_IDE) != 0) { in can_rcar_send()
903 identifier = frame->id | RCAR_CAN_MB_IDE; in can_rcar_send()
905 identifier = frame->id << RCAR_CAN_MB_SID_SHIFT; in can_rcar_send()
908 if ((frame->flags & CAN_FRAME_RTR) != 0) { in can_rcar_send()
912 sys_write32(identifier, config->reg_addr + RCAR_CAN_MB_56); in can_rcar_send()
914 sys_write16(frame->dlc, config->reg_addr in can_rcar_send()
917 if ((frame->flags & CAN_FRAME_RTR) == 0) { in can_rcar_send()
918 for (i = 0; i < frame->dlc; i++) { in can_rcar_send()
919 sys_write8(frame->data[i], config->reg_addr in can_rcar_send()
925 data->tx_unsent++; in can_rcar_send()
926 /* Start Tx: increment the CPU-side pointer for the transmit FIFO in can_rcar_send()
929 sys_write8(0xff, config->reg_addr + RCAR_CAN_TFPCR); in can_rcar_send()
931 k_mutex_unlock(&data->inst_mutex); in can_rcar_send()
941 struct can_rcar_data *data = dev->data; in can_rcar_add_rx_filter_unlocked()
945 if (data->rx_callback[i] == NULL) { in can_rcar_add_rx_filter_unlocked()
946 data->rx_callback_arg[i] = cb_arg; in can_rcar_add_rx_filter_unlocked()
947 data->filter[i] = *filter; in can_rcar_add_rx_filter_unlocked()
949 data->rx_callback[i] = cb; in can_rcar_add_rx_filter_unlocked()
954 return -ENOSPC; in can_rcar_add_rx_filter_unlocked()
960 struct can_rcar_data *data = dev->data; in can_rcar_add_rx_filter()
963 if ((filter->flags & ~(CAN_FILTER_IDE | CAN_FILTER_DATA)) != 0) { in can_rcar_add_rx_filter()
964 LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags); in can_rcar_add_rx_filter()
965 return -ENOTSUP; in can_rcar_add_rx_filter()
968 k_mutex_lock(&data->rx_mutex, K_FOREVER); in can_rcar_add_rx_filter()
970 k_mutex_unlock(&data->rx_mutex); in can_rcar_add_rx_filter()
976 struct can_rcar_data *data = dev->data; in can_rcar_remove_rx_filter()
982 k_mutex_lock(&data->rx_mutex, K_FOREVER); in can_rcar_remove_rx_filter()
984 data->rx_callback[filter_id] = NULL; in can_rcar_remove_rx_filter()
985 k_mutex_unlock(&data->rx_mutex); in can_rcar_remove_rx_filter()
990 const struct can_rcar_cfg *config = dev->config; in can_rcar_init()
991 struct can_rcar_data *data = dev->data; in can_rcar_init()
996 k_mutex_init(&data->inst_mutex); in can_rcar_init()
997 k_mutex_init(&data->rx_mutex); in can_rcar_init()
998 k_sem_init(&data->tx_sem, RCAR_CAN_FIFO_DEPTH, RCAR_CAN_FIFO_DEPTH); in can_rcar_init()
1000 data->tx_head = 0; in can_rcar_init()
1001 data->tx_tail = 0; in can_rcar_init()
1002 data->tx_unsent = 0; in can_rcar_init()
1004 memset(data->rx_callback, 0, sizeof(data->rx_callback)); in can_rcar_init()
1005 data->state = CAN_STATE_ERROR_ACTIVE; in can_rcar_init()
1006 data->state_change_cb = NULL; in can_rcar_init()
1007 data->state_change_cb_data = NULL; in can_rcar_init()
1009 if (config->phy != NULL) { in can_rcar_init()
1010 if (!device_is_ready(config->phy)) { in can_rcar_init()
1012 return -ENODEV; in can_rcar_init()
1016 if (!device_is_ready(config->clock_dev)) { in can_rcar_init()
1018 return -ENODEV; in can_rcar_init()
1022 ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); in can_rcar_init()
1028 ret = clock_control_off(config->clock_dev, in can_rcar_init()
1029 (clock_control_subsys_t)&config->mod_clk); in can_rcar_init()
1034 ret = clock_control_on(config->clock_dev, in can_rcar_init()
1035 (clock_control_subsys_t)&config->mod_clk); in can_rcar_init()
1040 ret = clock_control_on(config->clock_dev, in can_rcar_init()
1041 (clock_control_subsys_t)&config->bus_clk); in can_rcar_init()
1058 if (config->sample_point) { in can_rcar_init()
1059 ret = can_calc_timing(dev, &timing, config->bus_speed, in can_rcar_init()
1060 config->sample_point); in can_rcar_init()
1061 if (ret == -EINVAL) { in can_rcar_init()
1063 return -EIO; in can_rcar_init()
1066 timing.prescaler, timing.phase_seg1, timing.phase_seg2); in can_rcar_init()
1067 LOG_DBG("Sample-point err : %d", ret); in can_rcar_init()
1069 timing.sjw = config->sjw; in can_rcar_init()
1070 timing.prop_seg = config->prop_seg; in can_rcar_init()
1071 timing.phase_seg1 = config->phase_seg1; in can_rcar_init()
1072 timing.phase_seg2 = config->phase_seg2; in can_rcar_init()
1073 ret = can_calc_prescaler(dev, &timing, config->bus_speed); in can_rcar_init()
1092 ctlr |= RCAR_CAN_CTLR_BOM_ENT; /* Entry to halt mode automatically at bus-off */ in can_rcar_init()
1100 sys_write32(0, config->reg_addr + RCAR_CAN_MKR8); in can_rcar_init()
1101 sys_write32(0, config->reg_addr + RCAR_CAN_MKR9); in can_rcar_init()
1103 sys_write32(0, config->reg_addr + RCAR_CAN_MKIVLR0); in can_rcar_init()
1104 sys_write32(0, config->reg_addr + RCAR_CAN_MKIVLR1); in can_rcar_init()
1108 sys_write32(0, config->reg_addr + RCAR_CAN_FIDCR0); in can_rcar_init()
1110 config->reg_addr + RCAR_CAN_FIDCR1); in can_rcar_init()
1114 config->reg_addr + RCAR_CAN_MIER1); in can_rcar_init()
1117 config->reg_addr + RCAR_CAN_IER); in can_rcar_init()
1120 sys_write8(RCAR_CAN_ECSR_EDPM, config->reg_addr + RCAR_CAN_ECSR); in can_rcar_init()
1123 sys_write8(0xFF, config->reg_addr + RCAR_CAN_EIER); in can_rcar_init()
1125 config->init_func(dev); in can_rcar_init()
1132 const struct can_rcar_cfg *config = dev->config; in can_rcar_get_core_clock()
1134 *rate = config->bus_clk.rate; in can_rcar_get_core_clock()
1147 const struct can_rcar_cfg *config = dev->config; in can_rcar_get_max_bitrate()
1149 *max_bitrate = config->max_bitrate; in can_rcar_get_max_bitrate()
1176 .prescaler = 0x01
1183 .prescaler = 0x400